public void Test_Translate_Constructor()
        {
            FunctionModel a = GraphModel.CreateFunction("A", Vector2.zero);

            // Debug.Log(...)
            MethodInfo logMethod = typeof(Debug).GetMethod(nameof(Debug.Log), new[] { typeof(object) });

            Assume.That(logMethod, Is.Not.Null);
            FunctionCallNodeModel log = a.CreateStackedNode <FunctionCallNodeModel>("Log", 0, SpawnFlags.Default, n => n.MethodInfo = logMethod);

            // new Vector4(x, y)
            ConstructorInfo ctor = typeof(Vector4).GetConstructor(new[] { typeof(float), typeof(float) });

            Assume.That(ctor, Is.Not.Null);
            FunctionCallNodeModel newV4 = GraphModel.CreateNode <FunctionCallNodeModel>("New Vector4", Vector2.left * 200, SpawnFlags.Default, n => n.MethodInfo = ctor);

            GraphModel.CreateEdge(log.GetParameterPorts().First(), newV4.OutputPort);

            var b = new RoslynTranslator(Stencil);
            var c = b.Translate(GraphModel, CompilationOptions.Default);

            SyntaxNode      d    = c.GetRoot();
            StatementSyntax stmt = d.DescendantNodes().OfType <MethodDeclarationSyntax>().First(n => n.Identifier.ValueText == "A")
                                   .Body.Statements.First();

            ExpressionSyntax arg = ((InvocationExpressionSyntax)((ExpressionStatementSyntax)stmt).Expression).ArgumentList.Arguments.Single().Expression;

            Assert.That(arg.ToFullString(), Is.EqualTo("new UnityEngine.Vector4(0F, 0F)"));
        }
Exemplo n.º 2
0
        public void Test_EmbeddedConstantIsUsedWhenDisconnected([Values] TestingMode mode)
        {
            const float outerValue = 42f;
            const float innerValue = 347f;

            // make sure that we really test values that are not default
            Assume.That(outerValue, Is.Not.EqualTo(default(float)));
            Assume.That(innerValue, Is.Not.EqualTo(default(float)));

            var stencil = GraphModel.Stencil;

            FunctionModel a = GraphModel.CreateFunction("A", Vector2.zero);

            // Debug.Log(...)
            MethodInfo logMethod = typeof(Debug).GetMethod(nameof(Debug.Log), new[] { typeof(object) });

            Assume.That(logMethod, Is.Not.Null);
            FunctionCallNodeModel log = a.CreateStackedNode <FunctionCallNodeModel>("Log", 0, SpawnFlags.Default, n => n.MethodInfo = logMethod);
            var logParameterPort      = log.GetParameterPorts().Single();

            // Math.Abs(...)
            MethodInfo absMethod = typeof(Mathf).GetMethod(nameof(Mathf.Abs), new[] { typeof(float) });

            Assume.That(absMethod, Is.Not.Null);
            FunctionCallNodeModel abs = GraphModel.CreateNode <FunctionCallNodeModel>("Abs", Vector2.zero, SpawnFlags.Default, n => n.MethodInfo = absMethod);
            var absParameterPort      = abs.GetParameterPorts().Single();

            ((FloatConstantModel)abs.InputConstantsById[absParameterPort.UniqueId]).value = innerValue;

            GraphModel.CreateEdge(logParameterPort, abs.OutputPort);

            // float
            IConstantNodeModel outerFloat = GraphModel.CreateConstantNode("float42", typeof(float).GenerateTypeHandle(stencil), Vector2.zero);

            Assume.That(outerFloat, Is.Not.Null);
            ((FloatConstantModel)outerFloat).value = outerValue;

            string innerFloatString = SyntaxFactory.LiteralExpression(
                SyntaxKind.NumericLiteralExpression,
                SyntaxFactory.Literal(innerValue)).ToFullString();
            string outerFloatString = SyntaxFactory.LiteralExpression(
                SyntaxKind.NumericLiteralExpression,
                SyntaxFactory.Literal(outerValue)).ToFullString();

            TestPrereqActionPostreq(mode,
                                    () =>
            {
                // outer float disconnected, check that we use the inner value
                SyntaxNode astRoot = CompileCurrentGraphModel();
                LiteralExpressionSyntax literalInsideLogAbs = GetLiteralInsideLogAbs(astRoot);
                Assert.That(literalInsideLogAbs.ToFullString(), Is.EqualTo(innerFloatString));
                return(new CreateEdgeAction(absParameterPort, outerFloat.OutputPort));
            },
                                    () =>
            {
                // outer float connected, check that we use the outer value
                SyntaxNode astRoot = CompileCurrentGraphModel();
                LiteralExpressionSyntax literalInsideLogAbs = GetLiteralInsideLogAbs(astRoot);
                Assert.That(literalInsideLogAbs.ToFullString(), Is.EqualTo(outerFloatString));
            });
        }