-
Notifications
You must be signed in to change notification settings - Fork 263
Tests as Documentation
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 asAs()
. -
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 asDomNode
,DomNodeType
, andDomNodeAdapter
. -
VectorMath
: Test vector math classes, such asBezierCurve
,Matrix3F
, andQuatF
. -
Wpf
: Test WPF models.
- Active collections (
TestActiveCollection.cs
) - Events (
TestEvent.cs
) - Selection (
TestSelection.cs
) - Trees (
TestTree.cs
)
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.
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.
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);
}
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);
}
- Home
- Getting Started
- Features & Benefits
- Requirements & Dependencies
- Gallery
- Technology & Samples
- Adoption
- News
- Release Notes
- ATF Community
- Searching Documentation
- Using Documentation
- Videos
- Tutorials
- How To
- Programmer's Guide
- Reference
- Code Samples
- Documentation Files
© 2014-2015, Sony Computer Entertainment America LLC