Пример #1
0
        protected void RunTest(string folderName, string fileName)
        {
            var filePath = Path.Combine(
                this.rootDirectory.FullName,
                "TestFiles",
                folderName,
                fileName + ".cst"
                );
            var fileReaderResult =
                FileReader.ReadFile(filePath, new FileSystem(), CancellationToken.None).Result;

            var formatter = new CodeFormatter();
            var result    = formatter.Format(
                fileReaderResult.FileContents,
                new PrinterOptions()
            {
                Width = PrinterOptions.WidthUsedByTests
            }
                );

            var actualFilePath = filePath.Replace(".cst", ".actual.cst");

            File.WriteAllText(actualFilePath, result.Code, fileReaderResult.Encoding);

            var filePathToChange = filePath;
            var expectedFilePath = actualFilePath.Replace(".actual.", ".expected.");

            var expectedCode = fileReaderResult.FileContents;

            if (File.Exists(expectedFilePath))
            {
                expectedCode     = File.ReadAllText(expectedFilePath, Encoding.UTF8);
                filePathToChange = expectedFilePath;
            }

            if (Environment.GetEnvironmentVariable("NormalizeLineEndings") != null)
            {
                expectedCode = expectedCode.Replace("\r\n", "\n");
                result.Code  = result.Code.Replace("\r\n", "\n");
            }

            var comparer = new SyntaxNodeComparer(
                expectedCode,
                result.Code,
                CancellationToken.None
                );

            result.Errors.Should().BeEmpty();
            result.FailureMessage.Should().BeEmpty();

            if (result.Code != expectedCode && !BuildServerDetector.Detected)
            {
                DiffRunner.Launch(filePathToChange, actualFilePath);
            }
            result.Code.Should().Be(expectedCode);

            var compareResult = comparer.CompareSource();

            compareResult.Should().BeNullOrEmpty();
        }
        private string AreEqual(string left, string right)
        {
            var result = new SyntaxNodeComparer(
                left,
                right,
                CancellationToken.None
                ).CompareSource();

            if (Environment.GetEnvironmentVariable("NormalizeLineEndings") != null)
            {
                result = result.Replace("\r\n", "\n");
            }

            return(result);
        }
Пример #3
0
        protected void RunTest(string folderName, string fileName)
        {
            var filePath = Path.Combine(
                this.rootDirectory.FullName,
                "TestFiles",
                folderName,
                fileName + ".cst"
                );
            var code = File.ReadAllText(filePath);

            var formatter = new CodeFormatter();
            var result    = formatter.Format(code, new Options());

            var actualFilePath = filePath.Replace(".cst", ".actual.cst");

            File.WriteAllText(actualFilePath, result.Code, Encoding.UTF8);

            var filePathToChange = filePath;
            var expectedFilePath = actualFilePath.Replace(
                ".actual.",
                ".expected."
                );

            if (File.Exists(expectedFilePath))
            {
                code             = File.ReadAllText(expectedFilePath, Encoding.UTF8);
                filePathToChange = expectedFilePath;
            }

            var comparer = new SyntaxNodeComparer(
                code,
                result.Code,
                CancellationToken.None
                );

            if (result.Code != code && !BuildServerDetector.Detected)
            {
                DiffRunner.Launch(filePathToChange, actualFilePath);
            }
            result.Code.Should().Be(code);

            var compareResult = comparer.CompareSource();

            compareResult.Should().BeNullOrEmpty();
        }
Пример #4
0
        public void Default_SyntaxNodeComparer()
        {
            var syntaxNodeComparer = new SyntaxNodeComparer(code, code, CancellationToken.None);

            syntaxNodeComparer.CompareSource();
        }
Пример #5
0
        /// <summary>
        /// Learning a set of `program`s, i.e. tree transformers, that are consistent with the specification.
        /// </summary>
        /// <param name="spec">Specification of the form: input -> new tree.</param>
        /// <returns>Consistent programs (if exist) or nothing.</returns>
        private Optional <ProgramSet> LearnProgram(PremSpec <TInput, SyntaxNode> spec)
        {
            // Preparation: compute sources.
            foreach (var input in spec.Keys)
            {
                errNodes[input] = input.errNode as Leaf;
            }

            foreach (var key in spec.Keys.Select(i => i.Keys).Intersect())
            {
                var varNodes = spec.MapOutputs((i, o) => i.inputTree.Leaves()
                                               .Where(l => l.code == i[key] && i[key] != i.errNode.code).ArgMin(l => l.depth));
                if (varNodes.Forall((i, v) => v != null))
                {
                    varNodeDict[key] = varNodes;
                }
            }

            // Preparation: Before we synthesize `target`, we have to first perform a diff.
            var diffResults = spec.MapOutputs((i, o) => SyntaxNodeComparer.Diff(i.inputTree, o));
// #if DEBUG
//             var printer = new IndentPrinter();
//             foreach (var p in diffResults)
//             {
//                 Log.Fine("Diff:");
//                 p.Value.Value.Item1.PrintTo(printer);
//                 printer.PrintLine("<->");
//                 p.Value.Value.Item2.PrintTo(printer);
//             }
// #endif
            // Preparation: Before we synthesize `newTree`,
            // we have to perform matching between the old tree and the new node.
            var treeSpec = diffResults.MapOutputs((i, o) => o.Value.Item2);

            foreach (var p in treeSpec)
            {
                var matcher  = new SyntaxNodeMatcher();
                var matching = matcher.GetMatching(p.Value, p.Key.inputTree);
                foreach (var match in matching)
                {
                    match.Key.matches = new List <SyntaxNode>(match.Value);
                }
            }

            // Start synthesis.
            if (diffResults.Forall((i, o) => o.HasValue))
            {
                // 1. Synthesize param `target`.
                var        targetSpec = diffResults.MapOutputs((i, o) => o.Value.Item1);
                ProgramSet targetSpace;
#if DEBUG
                Log.Tree("target |- {0}", targetSpec);
                Log.IncIndent();
#endif
                // Special case: error node = expected output, use `Err`.
                if (targetSpec.Forall((i, o) => i.errNode.Equals(o)))
                {
#if DEBUG
                    Log.Tree("Err(old)");
#endif
                    targetSpace = ProgramSet.List(Symbol(nameof(Semantics.Err)), Err());
                }
                // General case: try `ref`.
                else
                {
                    targetSpace = LearnRef(targetSpec);
                }
#if DEBUG
                Log.DecIndent();
#endif
                if (targetSpace.IsEmpty)
                {
                    return(Optional <ProgramSet> .Nothing);
                }

                // 2. Synthesize param `tree` as the `newTree`, constructed by `New(tree)`.
#if DEBUG
                Log.Tree("tree |- {0}", treeSpec);
                Log.IncIndent();
#endif
                var treeSpace = LearnTree(treeSpec);
#if DEBUG
                Log.DecIndent();
#endif
                if (treeSpace.IsEmpty)
                {
                    return(Optional <ProgramSet> .Nothing);
                }

                // All done, return program set.
                return(ProgramSet.Join(Op(nameof(Semantics.Transform)), targetSpace,
                                       ProgramSet.Join(Op(nameof(Semantics.New)), treeSpace)).Some());
            }

            return(Optional <ProgramSet> .Nothing);
        }