예제 #1
0
        public void TestPrefixUnaryOperatorTypeConversions(string opText, string argumentText, string expectedResult)
        {
            var argument         = ExpressionTestUtility.GetValue(argumentText);
            var source           = $"{opText}{argument}";
            var syntaxTree       = SyntaxFactory.ParseExpression(source);
            var syntaxTreeSource = syntaxTree.Root.ToString();

            if (syntaxTreeSource != source)
            {
                Assert.Fail($"Source should have been {syntaxTreeSource} but is {source}");
            }

            var expression    = (PrefixUnaryExpressionSyntax)syntaxTree.Root;
            var compilation   = new HlslTools.Compilation.Compilation(syntaxTree);
            var semanticModel = compilation.GetSemanticModel();

            var operandType = ExpressionTestUtility.GetExpressionTypeString(semanticModel.GetExpressionType(expression.Operand));

            if (argumentText != operandType)
            {
                Assert.Fail($"Operand should be of type '{argumentText}' but has type '{operandType}'");
            }

            var diagnostic     = syntaxTree.GetDiagnostics().Concat(semanticModel.GetDiagnostics()).SingleOrDefault();
            var expressionType = semanticModel.GetExpressionType(expression);
            var result         = diagnostic == null
                ? ExpressionTestUtility.GetExpressionTypeString(expressionType)
                : ExpressionTestUtility.GetErrorString(diagnostic.DiagnosticId);

            Assert.AreEqual(expectedResult, result, $"Expression {source} should have evaluated to '{expectedResult}' but was '{result}'");
        }
        public void TestFunctionOverloadResolution1Arg(string type, string expectedMatchType)
        {
            var code             = $@"
struct MyStruct {{}};

int foo(int x)      {{ return 1; }}
int foo(float x)    {{ return 2; }}
int foo(double x)   {{ return 3; }}
int foo(int2 x)     {{ return 4; }}
int foo(float2 x)   {{ return 5; }}
int foo(double2 x)  {{ return 6; }}
int foo(float3x3 x) {{ return 7; }}

void main()
{{
    foo(({type}) 0);
}}";
            var syntaxTree       = SyntaxFactory.ParseSyntaxTree(SourceText.From(code));
            var syntaxTreeSource = syntaxTree.Root.ToFullString();

            Assert.AreEqual(code, syntaxTreeSource, $"Source should have been {code} but is {syntaxTreeSource}.");

            var expression = (FunctionInvocationExpressionSyntax)syntaxTree.Root.ChildNodes
                             .OfType <FunctionDefinitionSyntax>()
                             .Where(x => x.Name.GetName() == "main")
                             .Select(x => ((ExpressionStatementSyntax)x.Body.Statements[0]).Expression)
                             .First();

            var compilation         = new HlslTools.Compilation.Compilation(syntaxTree);
            var semanticModel       = compilation.GetSemanticModel();
            var combinedDiagnostics = syntaxTree.GetDiagnostics().Concat(semanticModel.GetDiagnostics()).ToList();

            foreach (var d in combinedDiagnostics)
            {
                Debug.WriteLine(d);
            }

            var invokedFunctionSymbol = (FunctionSymbol)semanticModel.GetSymbol(expression);
            var diagnostic            = combinedDiagnostics.SingleOrDefault(x => x.Severity == Diagnostics.DiagnosticSeverity.Error);
            var result = diagnostic == null
                ? ExpressionTestUtility.GetExpressionTypeString(invokedFunctionSymbol.Parameters[0].ValueType)
                : ExpressionTestUtility.GetErrorString(diagnostic.DiagnosticId);

            Assert.AreEqual(expectedMatchType, result, $"Expression should have matched the function overload '{expectedMatchType}' but it actually matched '{result}'.");
        }