public void NamespaceFunction_WithoutModifiers_ShouldBePublic() { var tree = ExcessMock.Link(@" namespace SomeNamespace { function Function1() { } }", (compiler) => Functions.Apply(compiler)); var root = tree ?.GetRoot() ?.NormalizeWhitespace(); //should have created a public method var method = root .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single(); Assert.IsTrue(method .Modifiers .Where(modifier => modifier.IsKind(SyntaxKind.PublicKeyword)) .Any()); }
public void NamespaceFunction_ScopeKeyword_ShouldCreateANewContext() { var tree = ExcessMock.Link(@" namespace SomeNamespace { function Function1() { scope { var InjectedValue = ""Hello""; Function2(); } } function Function2() { inject { string InjectedValue; } Console.WriteLine(InjectedValue); } }", (compiler) => { Functions.Apply(compiler); DependencyInjection.Apply(compiler); }); var root = tree ?.GetRoot() ?.NormalizeWhitespace(); //the injected value must be read from the context Assert.IsTrue(root .DescendantNodes() .OfType <StatementSyntax>() .Where(statement => statement.ToString() == "string InjectedValue = __scope.get<string>(\"InjectedValue\");") .Any()); //as well as be saved to a different context Assert.IsTrue(root .DescendantNodes() .OfType <StatementSyntax>() .Where(statement => statement.ToString() == "__newScope.set(\"InjectedValue\", InjectedValue);") .Any()); }
public void NamespaceFunction_Injected_Requirements() { var tree = ExcessMock.Link(@" namespace SomeNamespace { interface SomeInterface { void SomeMethod(); } function SomeFunction() { inject { SomeInterface someInterface; } someInterface.SomeMethod(); } }", (compiler) => { Functions.Apply(compiler); DependencyInjection.Apply(compiler); }); var root = tree ?.GetRoot() ?.NormalizeWhitespace(); //the injection must transform in a variable assignment //essentially asking the context to resolve the requested instance Assert.IsTrue(root .DescendantNodes() .OfType <LocalDeclarationStatementSyntax>() .Single() .Declaration .Variables .Single() .Initializer .Value.ToString().StartsWith("__scope")); }
public void Debug() { var tree = ExcessMock.Link(@" using xs.server; using xs.concurrent; using demo_transpiler; namespace Home { public function Transpile(string text) { inject { ITranspiler _transpiler; } return _transpiler.Process(text); } public function TranspileGraph(string text) { inject { IGraphTranspiler _graphTranspiler; } return _graphTranspiler.Process(text); } }", (compiler) => { DependencyInjection.Apply(compiler); Functions.Apply(compiler); }); //the result must contain one class (testing the temp class is removed) Assert.AreNotEqual(null, tree); }
public void NamespaceFunction_Usage() { var tree = ExcessMock.Link(@" namespace SomeNamespace { function SomeFunction() { return 10; } }", (compiler) => Functions.Apply(compiler)); var root = tree ?.GetRoot() ?.NormalizeWhitespace(); //a class must have been created var @class = root .DescendantNodes() .OfType <ClassDeclarationSyntax>() .Single(); //named with the "Functions" convention Assert.AreEqual(@class.Identifier.ToString(), "Functions"); //the method must remain var method = root .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single(); //inside the class Assert.AreEqual(method.Parent, @class); //with its type calculated to int Assert.IsTrue(method .ReturnType.ToString().ToLower() .StartsWith("int")); }
public void NamespaceFunction_SeveralFunctions_ShouldBeInvokedWithContext() { var tree = ExcessMock.Link(@" namespace SomeNamespace { function Function1() { Function2(10); } function Function2(int value) { Console.WriteLine(value); } }", (compiler) => Functions.Apply(compiler)); var root = tree ?.GetRoot() ?.NormalizeWhitespace(); //two partial classes must have been created Assert.AreEqual(root .DescendantNodes() .OfType <ClassDeclarationSyntax>() .Count(), 2); //the call from Function1 to Function2 must be made with an internal scope Assert.AreEqual(root .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString() == "Function2") .Single() .ArgumentList .Arguments[1].ToString(), "__scope"); }