Beispiel #1
0
        public void TemplateSerializationClean()
        {
            string dirPath  = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
            string filePath = Path.Combine(dirPath, "Resources", "TemplateTest.txt");
            string template = File.ReadAllText(filePath);

            var vars = new Dictionary <string, string>
            {
                { "CoolStuff", "I'm cool" },
                { "IsMyBuddy", "True" },
                { "BigNumber", "25" },
                { "Min1", "21" },
                { "Max1", "25" },
                { "Min2", "42" },
                { "Max2", "50" },
                { "MyCar", "Subaru" },
                { "DoraCar", "Kia" },
            };
            var arrays = new Dictionary <string, IEnumerable <string> >
            {
                { "Chocolate", new[] { "Dark", "White", "75%", "Milk", "Very Dark" } },
                { "Fruit", new[] { "Orange", "Lemon", "Pineapple", "Strawberry", "Kiwi" } },
                { "Person", new[] { "Billy", "Bob", "Julia" } }
            };

            string resFilePath    = Path.Combine(dirPath, "Resources", "TemplateTestResultClean.txt");
            string expectedResult = File.ReadAllText(resFilePath);

            Assert.AreEqual(expectedResult, XmTemplateSerializer.Serialize(XmTemplateDeserializer.Deserialize(template), vars, arrays));
        }
        public static void TestExtraction(IEnumerable <string> templates, IEnumerable <ExtractedVariable> expectedVars)
        {
            //Arrange
            var eVars = expectedVars.ToArray();

            foreach (var template in templates)
            {
                //Act
                var result = VariableExtractor.ExtractVariables(XmTemplateDeserializer.Deserialize(template));

                //Assert
                Assert.AreEqual(eVars.Count(), result.Count, template);
                Assert.AreEqual(false, result.Keys.Except(eVars.Select(x => x.Name)).Any(), Environment.NewLine, template);

                foreach (var xVar in eVars)
                {
                    var desc = "VAR: " + xVar.Name + Environment.NewLine + template;
                    var v    = result[xVar.Name];
                    Assert.AreEqual(xVar.Name, v.Name, desc);
                    Assert.AreEqual(xVar.GuessedType, v.GuessedType, desc);
                    Assert.AreEqual(xVar.IsArray, v.IsArray, desc);
                    Assert.AreEqual(xVar.Values.Count(), v.Values.Count(), desc);
                    Assert.AreEqual(false, xVar.Values.Except(v.Values).Any(), desc);
                }
            }
        }
Beispiel #3
0
        public void TemplateDeserialization()
        {
            string dirPath  = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
            string filePath = Path.Combine(dirPath, "Resources", "TemplateTest.txt");
            string template = File.ReadAllText(filePath);

            AbstractTemplateComparator.CompareTemplateElements(new TemplateTest(), XmTemplateDeserializer.Deserialize(template));
        }
Beispiel #4
0
        public static string ExecuteTemplate(string templateStr, StringWriter sw, IDictionary <string, string> variables = null, IDictionary <string, IEnumerable <string> > arrays = null)
        {
            var template = XmTemplateDeserializer.Deserialize(templateStr);

            //Act
            var arr    = arrays ?? new Dictionary <string, IEnumerable <string> >();
            var vars   = variables ?? new Dictionary <string, string>();
            var result = XmTemplateSerializer.Serialize(template, vars, arr);

            sw.WriteLine();
            sw.WriteLine(templateStr);
            foreach (var x in vars)
            {
                sw.WriteLine("{0} = {1}", x.Key, x.Value);
            }
            foreach (var array in arr)
            {
                sw.WriteLine("{0} = ({1})", array.Key, string.Join(", ", array.Value));
            }
            return(result);
        }