예제 #1
0
        public void IndirectVariableAccessTest()
        {
            string program         = @"
                fun kju (param : Int) : Unit {
                    var x : Int;
                    fun f (par1 : Int, par2 : Int) : Int {
                        x;
                        return par1;
                    };
                    fun g () : Unit {
                        f(1, 2);
                    };
                    g();
                }";
            var    diagnosticsMock = new Mock <IDiagnostics>();
            var    diagnostics     = diagnosticsMock.Object;
            var    ast             = KjuCompilerUtils.MakeAstWithLinkedNames(program, diagnostics);
            var    identifiersMap  = this.CreateIdDictionary(ast);
            var    functions       = identifiersMap
                                     .Where(p => p.Value is FunctionDeclaration)
                                     .ToDictionary(p => p.Key, p => p.Value as FunctionDeclaration);
            var variables = identifiersMap
                            .Where(p => p.Value is VariableDeclaration)
                            .ToDictionary(p => p.Key, p => p.Value as VariableDeclaration);
            var mockCallGraph = new Mock <ICallGraphGenerator>();
            var callGraphDict = new Dictionary <FunctionDeclaration, IReadOnlyCollection <FunctionDeclaration> >
            {
                [functions["kju"]] = new HashSet <FunctionDeclaration>()
                {
                    functions["f"]
                },
                [functions["f"]] = new HashSet <FunctionDeclaration>(),
                [functions["g"]] = new HashSet <FunctionDeclaration>()
                {
                    functions["f"]
                },
            };

            mockCallGraph.Setup(foo => foo.BuildCallGraph(It.IsAny <Node>())).Returns(callGraphDict);

            var nodeInfoExtractors = new Dictionary <VariableInfo, INodeInfoExtractor>
            {
                [VariableInfo.Access]        = new AccessInfoExtractor(),
                [VariableInfo.Modifications] = new ModifyInfoExtractor(),
            };

            VariableAccessGraphGenerator tempQualifier =
                new VariableAccessGraphGenerator(mockCallGraph.Object, nodeInfoExtractors);
            INodeInfoExtractor infoExtractor = new AccessInfoExtractor();
            var graph = VariableAccessGraphGenerator.TransitiveCallClosure(mockCallGraph.Object, ast, infoExtractor);

            Assert.IsTrue(graph[functions["kju"]].Contains(variables["x"]));
            Assert.IsTrue(graph[functions["kju"]].Contains(variables["par1"]));
            Assert.IsTrue(graph[functions["g"]].Contains(variables["x"]));
            Assert.IsTrue(graph[functions["g"]].Contains(variables["par1"]));
            MockDiagnostics.Verify(diagnosticsMock);
        }