Skip to content

Tests as Documentation

Gary edited this page Mar 10, 2015 · 4 revisions

Table of Contents

Unit Tests

ATF has test suites for its code and samples. In particular, the unit tests are a set of C# files that test various parts of the ATF API. As such, this code demonstrates how to use classes and call various functions properly, so it serves as another form of API documentation besides the API Reference. All test code is part of the Everything.sln Visual Studio solution, and the unit tests are in the UnitTests.vs2010 project.

ATF uses NUnit for its test framework. For a description of how ATF tests work, see ATF Test Code. In particular, the C Sharp Unit Tests describes how unit tests are put together.

Unit tests reside in Test\UnitTests\Sce.Atf. This folder holds files that test a variety of areas. In addition, this folder contains test files that are grouped by folders whose names describe the area tested:

  • Adaptation: Test adapters and adaptation functions, such as As().
  • Applications: Basic application function tests, such as testing commands.
  • Controls: Test controls, such as property editing controls.
  • Dom: Test various ATF DOM related classes, such as DomNode, DomNodeType, and DomNodeAdapter.
  • VectorMath: Test vector math classes, such as BezierCurve, Matrix3F, and QuatF.
  • Wpf: Test WPF models.
The ungrouped test files cover such areas as:
  • Active collections (TestActiveCollection.cs)
  • Events (TestEvent.cs)
  • Selection (TestSelection.cs)
  • Trees (TestTree.cs)
Test file names prefix "Test" to the item tested, so TestDomNode.cs tests DomNode.

If you want to see how a certain class or API function is used, search the test code source for it.

Tests are a work in progress and currently cover only the most fundamental ATF areas.

Unit Test Examples

DomNode

The file TestDomNode.cs in the Test\UnitTests\Sce.Atf\Dom folder tests the DomNode class, one of the most important in the ATF DOM. The following tests the class's constructor and so shows how to construct a DomNode instance:

[Test]
public void TestConstructor()
{
    DomNodeType type = new DomNodeType("child");
    ChildInfo info = new ChildInfo("test", type);
    DomNode test = new DomNode(type, info);
    Assert.AreSame(test.Type, type);
    Assert.AreSame(test.ChildInfo, info);
}

The [Test] attribute is used by NUnit, as described in ATF Test Code.

This test demonstrates adding a child to a DomNode:

[Test]
public void TestChildParent()
{
    DomNodeType type = new DomNodeType("type");
    ChildInfo childInfo = new ChildInfo("child", type, true);
    type.Define(childInfo);

    DomNode child = new DomNode(type);
    DomNode parent = new DomNode(type);
    parent.GetChildList(childInfo).Add(child);
    Assert.AreSame(child.Parent, parent);
}

And this one shows how to remove a child:

[Test]
public void TestRemoveFromParentList()
{
    DomNodeType type = new DomNodeType("type");
    ChildInfo childInfo = new ChildInfo("child", type, true);
    type.Define(childInfo);
    DomNode parent = new DomNode(type);
    DomNode child = new DomNode(type);
    parent.GetChildList(childInfo).Add(child);
    child.RemoveFromParent();
    CollectionAssert.IsEmpty(parent.Children);
    Assert.Null(child.Parent);
}

These tests go on to perform more complicated operations, such as testing that DomNode events work for changing attributes and child insertion or removal. In the process, they show how to set up DomNode events.

Adaptation

TestAdapter.cs in Test\UnitTests\Sce.Atf\Adaptation tests adaptation basics and so demonstrates them. This test uses the SimpleAdapter class, defined in SimpleAdapter.cs in this folder, to show setting the adaptee:

[Test]
public void TestAdaptee()
{
    SimpleAdapter test = new SimpleAdapter();
    test.Adaptee = this;
    Assert.AreSame(test.Adaptee, this);
    Assert.True(test.OnAdapteeChangedCalled);
    test.Adaptee = null;
    Assert.Null(test.Adaptee);
}

This more involved examples tests the adaptation function AsAll():

[Test]
public void TestAsAll()
{
    SimpleAdapter test;

    test = new SimpleAdapter();
    Utilities.TestSequenceEqual(test.GetDecorators(typeof(ISimpleInterface)), test);
    Assert.True(test.AdaptCalled);

    test = new SimpleAdapter();
    CollectionAssert.IsEmpty(test.GetDecorators(typeof(TestAdapter)));
    Assert.True(test.AdaptCalled);

    SimpleAdapter adaptee = new SimpleAdapter();
    test = new SimpleAdapter(adaptee); // wrap itself
    Utilities.TestSequenceEqual(test.GetDecorators(typeof(SimpleAdapter)), test, adaptee);
    Assert.True(test.AdaptCalled);

    // If the adaptee can be adapted to the adapter, we want to make sure that
    //  GetDecorators() doesn't return two identical decorators.
    var decoratableAdaptee = new DecoratableAdaptee();
    var decoratingAdapter = new DecoratingAdapter(decoratableAdaptee);
    var adapters = new List<DecoratingAdapter>(decoratingAdapter.AsAll<DecoratingAdapter>());
    Utilities.TestSequenceContainSameItems(adapters, decoratingAdapter);
}

ActiveCollection

The file TestActiveCollection.cs in the Test\UnitTests\Sce.Atf folder tests the ActiveCollection class. This test attempts to enumerate objects in a collection, with the help of the Utilities.TestSequenceEqual() function:

[Test]
public void TestIEnumerable()
{
    ActiveCollection<object> test = new ActiveCollection<object>();
    CollectionAssert.IsEmpty(test);
    test.Add("a");
    Utilities.TestSequenceEqual(test, "a");
    test.Add("b");
    Utilities.TestSequenceEqual(test, "a", "b");
}

And this tests the Count property:

public void TestCount()
{
    ActiveCollection<object> test = new ActiveCollection<object>();
    Assert.True(test.Count == 0);
    test.Add("a");
    Assert.True(test.Count == 1);
    test.Add("b");
    Assert.True(test.Count == 2);
}
Clone this wiki locally