Exemplo n.º 1
0
        public static JSParserResult RunTest(string sourceName, string resultName)
        {
            var source = TestsHelper.GetEmbeddedText("JsParser.Test.Parser.Source." + sourceName);

            // Fix line endings in test files. All should be unix way, but on some GIT clients it is updated automatically.
            source = source.Replace("\r\n", "\n");

            var settings = new JavascriptParserSettings()
            {
                Filename = sourceName,
            };
            var actualResult = (new JavascriptParser(settings)).Parse(source);

            var outDir = "C:\\js_parser_units_output";

            Directory.CreateDirectory(outDir);

            // Save actual hierarchy xml
            var serialized = SimpleHierarchySerializer.Serialize(actualResult.Nodes);

            File.WriteAllText(outDir + "\\" + resultName, serialized);

            // Load test data
            var resName = "JsParser.Test.Parser.ExpectedResult." + resultName;

            var passed = false;

            if (TestsHelper.CheckEmbeddedRes(resName))
            {
                File.WriteAllText(outDir + "\\" + resultName, SimpleHierarchySerializer.Serialize(actualResult.Nodes));

                var expectedresultSerialized = TestsHelper.GetEmbeddedText(resName);
                var expectedresult           = SimpleHierarchySerializer.Deserialize <CodeNode>(expectedresultSerialized);

                // Save expected hierarchy serialized
                File.WriteAllText(outDir + "\\" + resultName + ".ex", expectedresultSerialized);

                if (HierarchyComparer.Compare(actualResult.Nodes, expectedresult, new CodeNodeAssertComparer()))
                {
                    passed = true;
                }
            }

            Assert.IsTrue(passed);

            return(actualResult);
        }
        public void SimpleHierarchySerializer_Test()
        {
            var testData = new Hierarchy <CodeNode>(new CodeNode("Node1", 0, 100, "Comment1"))
            {
                Children = new List <Hierarchy <CodeNode> >
                {
                    new Hierarchy <CodeNode>(new CodeNode("S1", 1, 2)),
                    new Hierarchy <CodeNode>(new CodeNode("S2", 2, 3))
                }
            };

            var serialized   = SimpleHierarchySerializer.Serialize(testData);
            var deserialised = SimpleHierarchySerializer.Deserialize <CodeNode>(serialized);
            var isEqual      = HierarchyComparer.Compare(testData, deserialised, CodeNode.GetDefaultComparer());

            Assert.IsTrue(isEqual);
        }
        public void SimpleHierarchySerializer_Test2()
        {
            var testData = new Hierarchy <CodeNode>(new CodeNode("Node1", 0, 100, "Comment1\r\nHere the second line"))
            {
                Children = new List <Hierarchy <CodeNode> >
                {
                    new Hierarchy <CodeNode>(new CodeNode("S1", 1, 20, "{}\\/+-_=!@#$%^&*;:'<>")),
                    new Hierarchy <CodeNode>(new CodeNode("S2", 20, 40, "Or some xml encoded &#A;&#D; &amp;&gt;"))
                    {
                        Children = new List <Hierarchy <CodeNode> >
                        {
                            new Hierarchy <CodeNode>(new CodeNode("D1", 20, 21)),
                            new Hierarchy <CodeNode>(new CodeNode("D2", 30, 31))
                        }
                    },
                    new Hierarchy <CodeNode>(new CodeNode("S3", 40, 60))
                    {
                        Children = new List <Hierarchy <CodeNode> >
                        {
                            new Hierarchy <CodeNode>(new CodeNode("D3", 40, 41)),
                            new Hierarchy <CodeNode>(new CodeNode("D4", 50, 55))
                            {
                                Children = new List <Hierarchy <CodeNode> >
                                {
                                    new Hierarchy <CodeNode>(new CodeNode("L1", 51, 52))
                                }
                            }
                        }
                    },
                    new Hierarchy <CodeNode>(new CodeNode("S4", 60, 70))
                }
            };

            var serialized   = SimpleHierarchySerializer.Serialize(testData);
            var deserialised = SimpleHierarchySerializer.Deserialize <CodeNode>(serialized);
            var isEqual      = HierarchyComparer.Compare(testData, deserialised, CodeNode.GetDefaultComparer());

            Assert.IsTrue(isEqual);
        }