# Tuesday, April 27, 2004

NDoc is an extensible application: you can easily provide your own "documentation renderer", usually called documenter. I have used this feature to integrate NLiterate within NDoc and take advantage the built-in functions of the GUI. Here is the detailled procedure to create the NLiterate documenter.

1) Create the project:

Create an assembly project named NDoc.Documenter.*** where you replace *** with the name of your documenter. It is important to have your assembly named like this otherwise it will not be loaded by NDoc. Add the reference to NDoc.Core.dll.

2) Create the documenter config object:

The config class will be attached to the property grid control in the NDoc gui. This way we can easily define the different parameters of the documenter. The config class must implement IDocumenterConfig interface, however it is easier to inherit from BaseDocumenterConfig:

public class LiterateDocumenterConfig : BaseDocumenterConfig
{
    private string outputFile;
    public LiterateDocumenterConfig()
    :base("Literate")
    {
        this.OutputFile =String.Format(".{0}doc{0}compile-log.txt",Path.DirectorySeparatorChar);
    }

    public string OutputFile
    {
        get
        {
            return this.outputFile;
        }
        set
        {
            this.outputFile = value; 
            this.SetDirty();
        }
    }
}

Note that when OutputFile is modified, we notify NDoc using the protected method SetDirty.

3) Create the documenter class

The documenter class is the main class, it renders the documentation. This class must implement IDocumenter but again, it is easier to use the abstract base class BaseDocumenter:

public class LiterateDocumenter : BaseDocumenter
{ 
    public LiterateDocumenter()
    :base("Literate") // this is the name that will appear in the NDoc list box
    {
        this.Clear();
    }
    #region IDocumenter
    public override void Build(Project project)
    {
        // step one, load snippets
        loadSnippets(project);
        // step two, compile the snippets
        compileSnippets(project);
        // step three, create the report
        compileReport(project);
    }
    public override void Clear()
    {
        this.Config=new LiterateDocumenterConfig();
    }
    public override DocumenterDevelopmentStatus DevelopmentStatus 
    {  
        get
        {
            return DocumenterDevelopmentStatus.Alpha;
        }
    }
    public override string MainOutputFile
    {
        get
        {
            return ((LiterateDocumenterConfig)this.Config).OutputFile;
        }
    }
    #endregion
}

4) Adding progress notification

In the code above, we have not notified NDoc about the progress of the documentation rendering. This can be done through two functions: OnDocBuildingStep and OnDocBuildingProgress. For example:

    public override void Build(Project project)
    {
        // step one, load snippets
        OnDocBuildingStep(0, "Start loading snippets");
        loadSnippets(project);

        // step two, compile the snippets
        OnDocBuildingStep(40, "Start loading snippets");
        compileSnippets(project);
        // step three, create the report
        OnDocBuildingProgress(40); // increasing progress percentage of 40%
        compileReport(project);
    }

5) Copy the assembly

The last step is to copy your assembly into the NDoc bin folder. When launched, NDoc looks for assemblies named "NDoc.Documenter...." and loads by reflection that documenter they are containing. That's it.

posted on Tuesday, April 27, 2004 8:59:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]

I have added a new fixture, ProcessTestFixture, that implements the process testing presented by Marc Clifton in http://www.codeproject.com/csharp/autp3.asp.

A process is an ordered sequence of unit tests.  As long as one test passes, the next test is run. Due to the structure of MbUnit, this was straightforward to implement (I'll about this later in the blog). The syntax of the fixture is similar to Marc Clifton example but I have merge Test, Sequence(...) to TestSequence(...):

[ProcessTestFixture]
public MyProcess
{
    [TestSequence(1)]
    public void Hello()
    {...}
    [TestSequence(2)]
    public void World()
    {...}
    ...
}

The implementation of the fixture was done by the following steps:

  1. Create TestSequenceAttribute, a simple class inherited from TestPatternAttribute that stores the order,
    [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
    public sealed class TestSequenceAttribute : TestPatternAttribute
    {
        private int order;
      
        public TestSequenceAttribute(int order)
          {
            this.order = order;
        }
    
        public int Order
        {
            get
            {
                    return this.order;
            }
            set
            {
                this.order = value;
            }
        }
    }
  2. Create ProcessMethodRun, which implements IRun. This is where the "process" logic is created, more specifically in the Reflect method: since this method builds the tree of IRunInvoker instance, creating a process is just a matter of creating a sequence of MethodRunInvoker:
    public class ProcessMethodRun : IRun
    {
        ...
        public virtual void Reflect(
    			        RunInvokerTree tree, 
    			        RunInvokerVertex parent, 
            Type t
        )
        {
            // first gather all methods, with order.
            ArrayList methods = new ArrayList();
            foreach(MethodInfo mi in 
                    TypeHelper.GetAttributedMethods(t,this.AttributeType))
            {
                // get sequence attribute
                TestSequenceAttribute seq = 
                    (TestSequenceAttribute)TypeHelper.GetFirstCustomAttribute(mi,typeof(TestSequenceAttribute)); 
                methods.Add( new OrderedMethod(mi, seq.Order) ); 
            }
    
            // sort the methods
            QuickSorter sorter = new QuickSorter();
            sorter.Sort(methods);
    
            // populate execution tree.
            RunInvokerVertex child = parent;
            foreach(OrderedMethod om in methods)
            {
                IRunInvoker invoker = InstanceInvoker(om.Method);
                child = tree.AddChild(child,invoker);
            } 
        }
    
        protected IRunInvoker InstanceInvoker(MethodInfo mi)
        {
            IRunInvoker invoker = new MethodRunInvoker(this,mi);
            return DecoratorPatternAttribute.DecoreInvoker(mi,invoker);
        }
    
        #region OrderedMethod
        private class OrderedMethod : IComparable
        {
            private MethodInfo method;
            private int order;
       
            public  OrderedMethod(MethodInfo method, int order)
            {
                this.method = method;
                this.order = order;
            }
    
    
            public MethodInfo Method
            {
                get
                {
                    return this.method;
                }
            }
            public int Order
            {
                get
                {
                    return this.order; 
                }
            }
            public int CompareTo(OrderedMethod obj)
            {
                return this.order.CompareTo(obj.order);
            }
       
            int IComparable.CompareTo(Object obj)
            {
                return this.CompareTo((OrderedMethod)obj);
            }
        }
        #endregion
    	}
  3. Create ProcessTestFixtureAttribute, a class that inherits from TestFixturePatternAttribute and defines the fixture execution pipe:
    public class ProcessTestFixtureAttribute : TestFixturePatternAttribute 
    {
        public override IRun GetRun()
        {
            SequenceRun runs = new SequenceRun();
       
            OptionalMethodRun setup = new OptionalMethodRun(typeof(SetUpAttribute),false);
            runs.Runs.Add( setup );
       
            ProcessMethodRun test = new ProcessMethodRun(typeof(TestSequenceAttribute));
            runs.Runs.Add(test);
       
            OptionalMethodRun tearDown = new OptionalMethodRun(typeof(TearDownAttribute),false);
            runs.Runs.Add(tearDown);
       
            return runs;      
        }
    }

Et voilà!

posted on Tuesday, April 27, 2004 5:39:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [2]
# Sunday, April 25, 2004

It is very easy to compute the code coverage of the Unit tests using MbUnit and NCover.

  1. Install the latest version of NCover (from gotdotnet: http://www.gotdotnet.com/Community/Workspaces/Workspace.aspx?id=3122ee1a-46e7-48a5-857e-aad6739ef6b9 )
  2. Create a test assembly and compile it in debug mode. NCover needs the symbol files to work (.mdb)
  3. Launch NCover as follows:
    NCover.Console.exe /c "MbUnit.Cons.exe" "MyTestAssembly.dll" /a "TheAssemblyToComputeTheCoverageOn"

    where MyTestAssembly.dll is the unit test assembly, TheAssemblyToComputeTheCoverageOn is the name of the assembly to compute the code coverage on (without the .dll extension)

  4. That' it.
 

You can launch multiple test assembly by providing multiple assembly names. And you can monitor multiple assembly, by providing a ;-separated list of assemblies.

 

posted on Sunday, April 25, 2004 7:03:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [6]

I will be presenting MbUnit at the end of May on a BENUG meeting. BENUG stands for Belgian User Group, http://www.benug.be .

posted on Sunday, April 25, 2004 6:06:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [4]
# Thursday, April 22, 2004

NDoc is truly an amazing library.

You can easily define your own documentation renderer (Documenter) class by implementing the NDoc.Core.IDocumenter interface (inheriting from BaseDocumenter will help). Once this is done, put the assembly ( named NDoc.Documenter.YourRenderer.dll) into the directory where NDocGUI.exe is and it will be loaded by Reflection where the GUI opens.

Since NLiterate is one way of looking at documentation, it deserved it's own documenter. The task was surprinsgly easy... maybe this could be my 2cents for NDoc :)

posted on Thursday, April 22, 2004 2:31:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [1]

I have published a new article on CodeProject about NLiterate ( http://www.codeproject.com/useritems/nliterate.asp )... don't forget to vote :)

 

posted on Thursday, April 22, 2004 9:56:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]
# Wednesday, April 21, 2004

Literate programming ( http://www.literateprogramming.com  ) is a way of describing algorithms and programs invented by the the mythical D. Knuth (famous CWEB application). The nice thing about CWEB is that both code and documentation live in the same document. To compile the code, the tool reassembles the code snippets and compiles it. Therefore, you can verify that all the examples in your documentation are compilable. Sadly, the (excellent) C# documentation does not support literate programming... well now it does.

NLiterate is a small application that parses the XML documentation file, reassembles code section and compiles them. This gives you an automated way of testing all your documentation examples. I'll be posting the source soon.

posted on Wednesday, April 21, 2004 4:50:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]
# Tuesday, April 20, 2004
I have released an article on using XML scripts to pilot SQLpp. Unfortunately, SQLpp is written in C++ and uses template meta-programming which makes compilation time explode. Therefore, the XML script should make it more "user friendly". The full article is at http://www.codeproject.com/useritems/xmlsqlpp.asp
posted on Tuesday, April 20, 2004 11:21:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]

I have been wondering if IDataReader was suited for remoting. The answer is obviously no since it derives from IMarshalByRef and therefore, all calls to the interface create a round-trip to the server.

Check out this article for more details.

posted on Tuesday, April 20, 2004 8:47:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [1]
# Sunday, April 18, 2004
This little add-in makes Reflector spit out source code from an entire assembly. Scary and efficient.
posted on Sunday, April 18, 2004 12:00:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]