using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml; using Chernobyl.Collections.Generic.Event; using Chernobyl.Config; using Chernobyl.Dependency; using Chernobyl.IO; using Chernobyl.Reflection.Template.CodeDom; using NUnit.Framework; namespace Chernobyl.Reflection.Template.Xml { [TestFixture, Description("Tests the XmlInstance class.")] public class XmlInstanceTests : InstanceTests { public XmlInstanceTests() { // Search paths for these tests are slightly different than the default // ones since Chernobyl is started in a different location for these // tests. _core = new Core(new SearchPaths(".", "../../../")); _core.Services.Inject(this); } // TODO: // creates an instance that has an event // creates an instance that has custom attributes. [Test, Description("Tests to see if an instance can be created from " + "an element that is not empty (i.e., ).")] public void NonEmptyElementCreation() { IInstance instance = FromFile("creation/non_empty_element.fall"); Assert.AreEqual(typeof(string), instance.Type, "The instance did not get loaded."); } [Test, Description("Tests to see if an instance can be created from " + "an element that is empty (i.e., ).")] public void EmptyElementCreation() { IInstance instance = FromFile("creation/empty_element.fall"); Assert.AreEqual(typeof(string), instance.Type, "The instance did not get loaded."); } [Test, Description("Tests to see if an instance can constructed " + "by specifying string.")] public void ConstructionWithString() { IInstance instance = CreateInstanceWithConstructor(); IMember member = instance.ComponentChildren.OfType().First(); IEnumerable arguments = member.ComponentChildren.OfType(); Assert.AreEqual(1, arguments.Count(), "Only one instance should have loaded."); Assert.AreEqual("\"Hello world!\"", arguments.First().TheInstance, "The instance did not load the arguments properly."); } [Test, Description("Tests to see if an instance can constructed " + "by specifying a number.")] public void ConstructionWithNumber() { IInstance instance = FromFile("constructor/number_constructor.fall"); IMember member = instance.ComponentChildren.OfType().First(); IEnumerable arguments = member.ComponentChildren.OfType(); Assert.AreEqual(1, arguments.Count(), "Only one instance should have loaded."); Assert.AreEqual("3.14", arguments.OfType().First().TheInstance, "The instance did not load the arguments properly."); } [Test, Description("Tests to see if an instance can constructed " + "by specifying several types through a comma separated list.")] public void ConstructionWithCommaSeparatedList() { IInstance instance = FromFile("constructor/comma_seperated_constructor.fall"); IMember member = instance.ComponentChildren.OfType().First(); IEnumerable arguments = member.ComponentChildren.OfType(); Assert.AreEqual(2, arguments.Count(), "Only two instances should have loaded."); Assert.AreEqual("3.14f", arguments.OfType().First().TheInstance, "The first instance did not load the first argument properly."); Assert.AreEqual("\"Hello World!\"", arguments.ElementAt(1).TheInstance, "The second instance did not load the second argument properly."); } [Test, Description("Tests to see if an instance can constructed " + "by specifying the proper XML elements in the instance.")] public void ConstructionWithElements() { IInstance instance = FromFile("constructor/element_constructor.fall"); IMember member = instance.ComponentChildren.OfType().First(); IEnumerable arguments = member.ComponentChildren.OfType(); Assert.AreEqual(3, arguments.Count(), "Only three instances should have loaded."); // Note that we only check the type in this case because the file we loaded will // cause a constructor to be created for each argument. The constructor contains // the actual value of the instance but we don't check that value because that // use case is checked by other unit tests. Assert.AreEqual(typeof(Int32), arguments.First().Type, "The first instance did not load the first argument properly."); Assert.AreEqual(typeof(Int32), arguments.ElementAt(1).Type, "The second instance did not load the second argument properly."); Assert.AreEqual(typeof(Int32), arguments.ElementAt(2).Type, "The third instance did not load the second argument properly."); } [Test, Description("Tests to see if an instance can constructed " + "by specifying several instances on a type that takes an IEnumerable.")] public void ConstructionWithEnumerable() { IInstance instance = FromFile("constructor/enumerable_constructor.fall"); IMember member = instance.ComponentChildren.OfType().First(); IEnumerable arguments = member.ComponentChildren.OfType(); Assert.AreEqual(3, arguments.Count(), "Only three instances should have loaded."); // Note that we only check the type in this case because the file we loaded will // cause a constructor to be created for each argument. The constructor contains // the actual value of the instance but we don't check that value because that // use case is checked by other unit tests. Assert.AreEqual(typeof(String), arguments.First().Type, "The first instance did not load the first argument properly."); Assert.AreEqual(typeof(Int32), arguments.ElementAt(1).Type, "The second instance did not load the second argument properly."); Assert.AreEqual(typeof(Single), arguments.ElementAt(2).Type, "The third instance did not load the second argument properly."); } [Test, Description("Tests to see if an instance can constructed " + "by explicitly specifying it's constructor and invoking a member.")] public void ConstructionWithConstructorAndMember() { IInstance instance = FromFile("constructor/constructor_and_member.fall"); IMember constructor = instance.ComponentChildren.OfType().First(); IEnumerable constructorArguments = constructor.ComponentChildren.OfType(); Assert.AreEqual(2, constructorArguments.Count(), "Only two instances should have loaded."); Assert.AreEqual("3.14", constructorArguments.OfType().First().TheInstance, "The first constructor argument did not load properly."); Assert.AreEqual("\"Hello World!\"", constructorArguments.ElementAt(1).TheInstance, "The second constructor argument did not load properly."); IMember someProperty = instance.ComponentChildren.OfType().ElementAt(1); IEnumerable somePropertyArguments = someProperty.ComponentChildren.OfType(); Assert.AreEqual(1, somePropertyArguments.Count(), "Only one argument for SomeProperty should have loaded."); Assert.AreEqual("5", somePropertyArguments.OfType().First().TheInstance, "The first SomeProperty argument did not load properly."); } [Inject] public ISearchPaths SearchPaths { set { value.Add("../../../Chernobyl.Test/Reflection/Template/data/instance"); } } /// /// A helper method that creates an from a file. /// /// The instance that gives out services for use /// by this type and takes services from this type for use by other systems. /// The path to the file to load the /// from. /// The that was created from the file. /// public static IInstance FromFile(IEventCollection services, string filepath) { XmlInstance instance; using (Stream xmlStream = new FileSearchStream(services, filepath, FileMode.Open)) using (XmlReader xmlr = XmlReader.Create(xmlStream)) { xmlr.MoveToContent(); instance = new XmlInstance(services, xmlr); } return instance; } protected override IInstance CreateInstanceWithConstructor() { return FromFile("constructor/string_constructor.fall"); } protected override IInstance CreateInstanceWithMethod() { return FromFile("member/instance_with_method.fall"); } protected override IInstance CreateInstanceWithProperty() { return FromFile("member/instance_with_property.fall"); } protected override IInstance CreateInstanceWithField() { return FromFile("member/instance_with_field.fall"); } protected override IInstance CreateInstanceWithEvent() { return FromFile("member/instance_with_event.fall"); } protected override IInstance CreateLinkedInstance() { return FromFile("linked_instance.fall"); } protected override IInstance CreateLinkedToInstance() { return FromFile("constructor/comma_seperated_constructor.fall"); } protected override IInstance CreateTypedInstance() { return FromFile("typed_instance.fall"); } protected override INamed CreateNamed() { return FromFile("named_element.fall"); } /// /// A helper method that creates an from a file. /// /// The path to the file to load the /// from. /// The that was created from the file. IInstance FromFile(string filepath) { return FromFile(_core.Services, filepath); } Core _core; } }