using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Chernobyl.Reflection.Template.Xml; using NUnit.Framework; namespace Chernobyl.Reflection.Template { [TestFixture, Description("A series of tests for testing the TypeParser type")] public class TypesParserTests : EnumerationTests { /// /// Initializes a new instance of the class. /// public TypesParserTests() : base(true) {} [Test, Description("A test that ensures the TypeParser is capable of " + "parsing one type.")] public void ParseOneTypeTest() { TypesParser parser = CreateParser("Int32"); Assert.AreEqual(1, parser.Count(), "The parser must " + "contain only one type."); Assert.AreEqual(typeof(Int32), parser.First(), "The parser must " + "contain one Int32 type."); } [Test, Description("A test that ensures the TypeParser is capable of " + "parsing two types.")] public void ParseTwoTypesTest() { TypesParser parser = CreateParser("Int32, Collections:ArrayList"); Assert.AreEqual(2, parser.Count(), "The parser must " + "contain two types."); Assert.AreEqual(typeof(Int32), parser.First(), "The parser must " + "contain one Int32 type for it's first element."); Assert.AreEqual(typeof(ArrayList), parser.ElementAt(1), "The parser must " + "contain one ArrayList type for it's second element."); } [Test, Description("A test that ensures the TypeParser is capable of " + "parsing one type containing one generic argument.")] public void ParseOneGenericTypeTest() { TypesParser parser = CreateParser("GenCollections:List{Int32}"); Assert.AreEqual(1, parser.Count(), "The parser must " + "contain only one type."); Assert.AreEqual(typeof(List), parser.First(), "The parser must " + "contain one List type."); } [Test, Description("A test that ensures the TypeParser is capable of " + "parsing one type with two generic arguments.")] public void ParseTwoGenericTypesTest() { TypesParser parser = CreateParser("GenCollections:KeyValuePair{String, GenCollections:List{Int32}}"); Assert.AreEqual(1, parser.Count(), "The parser must " + "contain only one type."); Assert.AreEqual(typeof(KeyValuePair>), parser.First(), "The parser must contain one " + "KeyValuePair> type."); } [Test, Description("A test that ensures the TypeParser is capable of " + "parsing a one dimensional array type.")] public void Parse1DArrayTest() { // TODO: add support for parsing one dimensional arrays Assert.Ignore("Parsing one dimensional arrays are not currently supported."); TypesParser parser = CreateParser("Int32[]"); Assert.AreEqual(1, parser.Count(), "The parser must " + "contain only one type."); Assert.AreEqual(typeof(Int32[]), parser.First(), "The parser must contain one " + "Int32[] type."); } [Test, Description("A test that ensures the TypeParser is capable of " + "parsing a two dimensional array type.")] public void Parse2DArrayTest() { // TODO: add support for parsing two dimensional arrays Assert.Ignore("Parsing two dimensional arrays are not currently supported."); TypesParser parser = CreateParser("Int32[,]"); Assert.AreEqual(1, parser.Count(), "The parser must " + "contain only one type."); Assert.AreEqual(typeof(Int32[,]), parser.First(), "The parser must contain one " + "Int32[,] type."); } [Test, Description("A test that ensures the TypeParser throws the " + "proper exception when it finds badly formed text.")] public void ExceptionOnBadText() { TypesParser parser = CreateParser("Int32zfda"); // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws(() => parser.Count()); // ReSharper restore ReturnValueOfPureMethodIsNotUsed } /// /// This method should return a new that can /// be iterated over exactly once. This method should create a new /// every time it is invoked and not reuse /// instances. /// /// /// The containing a single item. /// protected override IEnumerable CreateSingleItemEnumerable() { return CreateParser("String"); } /// /// This method should return a new that can /// be iterated over five or more times. The items will be compared to /// the items in but only /// if the property /// is set to true. This method should create a new /// every time it is invoked and not reuse /// instances. All instances returned through iteration of the /// should be unique. /// /// /// The containing many items. /// protected override IEnumerable CreateManyItemEnumerable() { return CreateParser("String, Collections:ArrayList, Int32, Single, " + "GenCollections:KeyValuePair{Int32, String}, Type, Int16"); } /// /// Creates the parser that is to be tested. This method should return /// a that parses the text passed /// to this method.This method should never return the same /// more than once. /// /// The that is to be /// tested. protected virtual TypesParser CreateParser(string text) { return new TypesParser(text, NamespaceAssemblyLookup); } protected override IEnumerable OrderedItems { get { if(_orderedItems == null) { _orderedItems = new[] { typeof(String), typeof(ArrayList), typeof(Int32), typeof(Single), typeof(KeyValuePair), typeof(Type), typeof(Int16) }; } return _orderedItems; } } /// /// Retrieves the that is referenced by /// the prefix. This method only handles the cases where prefix is empty /// or where the prefix is equal to "Collections". /// /// The prefix whose /// is to be found. /// If prefix is empty, returns the "System" namespace in the /// "mscorlib" assembly. If prefix is equal to "Collections", returns /// "System.Collections.Generic" in the "mscorlib" assembly. /// The prefix is not know, you must /// either extend this method or override it to add your own prefix /// lookup. protected virtual NamespaceAssembly NamespaceAssemblyLookup(string prefix) { NamespaceAssembly result; if(prefix == string.Empty) result = new NamespaceAssembly(typeof(Int32).Namespace, typeof(Int32).Assembly.GetName()); else if (prefix == "Collections") result = new NamespaceAssembly(typeof(ArrayList).Namespace, typeof(ArrayList).Assembly.GetName()); else if(prefix == "GenCollections") result = new NamespaceAssembly(typeof(List<>).Namespace, typeof(List<>).Assembly.GetName()); else { throw new ArgumentException("The NamespaceAssembly prefix \"" + prefix + "\" is unknown. Please extend this method or override it to " + "add the prefix."); } return result; } /// /// The backing field to . /// IEnumerable _orderedItems; } }