public void DapperParameters_Usage() { var tree = ExcessMock.Compile(@" class SomeClass { public void SomeMethod(IDBConnection connection) { sql(connection) { CREATE TABLE SomeTable ( Id varchar(36) not null ); } } }", builder: (compiler) => DapperExtension.Apply(compiler)); Assert.IsNotNull(tree); Assert.IsFalse(tree.GetDiagnostics().Any()); //must have added a typecast for parameter connection Assert.AreEqual(1, tree .GetRoot() .DescendantNodes() .OfType <CastExpressionSyntax>() .Count()); }
public void Contract_Usage() { var tree = ExcessMock.Compile(@" class SomeClass { void SomeMethod() { contract { x > 3; y != null; z.Validate() >> ArgumentException(""z""); } } }", builder: (compiler) => ContractExtension.Apply(compiler)); //must have added an if for each contract condition Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <IfStatementSyntax>() .Count() == 3); //must have added a throw statement for an ArgumentException var newExpr = tree.GetRoot() .DescendantNodes() .OfType <ObjectCreationExpressionSyntax>() .Where(creation => creation.Type.ToString() == "ArgumentException") .Single(); Assert.AreEqual(1, newExpr.ArgumentList.Arguments.Count); Assert.AreEqual("\"z\"", newExpr.ArgumentList.Arguments.Single().ToString()); }
public void R_Usage() { var tree = ExcessMock.Compile(@" void main() { R() { x <- c(10.4, 5.6, 3.1, 6.4, 21.7) y <- c(x, 0, x) z <- 2*x + y + 1 a <- x > 13 b <- x[!(is.na(x))] c <- x[-(1:5)] } }", builder: (compiler) => RExtension.Apply(compiler)); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <VariableDeclarationSyntax>() .Count() == 6); //must have created 5 variables (x, y, z, a, b) Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <BinaryExpressionSyntax>() .Count() == 0); //must have replaced all operators }
public void Match_Usage() { var tree = ExcessMock.Compile(@" class SomeClass { method SomeMethod() { match(x) { case > 10: break; case < 5: Console.WriteLine(""no breaks needed""); default: Console.WriteLine(""like a switch""); } } }", (compiler) => Match.Apply(compiler)); var root = tree ?.GetRoot() ?.NormalizeWhitespace(); //2 ifs (and one else) should have been created Assert.AreEqual(root .DescendantNodes() .OfType <IfStatementSyntax>() .Count(), 2); //no breaks Assert.IsFalse(root .DescendantNodes() .OfType <BreakStatementSyntax>() .Any()); }
public void Injection_Usage() { var tree = ExcessMock.Compile(@" class TestClass { inject { IInterface1 _1; IInterface2 _2; } }", (compiler) => DependencyInjection.Apply(compiler)); //the result must contain one class (testing the temp class is removed) Assert.AreEqual(1, tree .GetRoot() .DescendantNodes() .Count(node => node is ClassDeclarationSyntax)); //a constructor must be added var ctor = tree .GetRoot() .DescendantNodes() .OfType <ConstructorDeclarationSyntax>() .Single(); //one parameter per injection Assert.AreEqual(2, ctor.ParameterList.Parameters.Count); //a private field ought to be added per injection Assert.AreEqual(2, tree .GetRoot() .DescendantNodes() .Count(node => node is FieldDeclarationSyntax)); }
public void NInjector_Usage() { var tree = ExcessMock.Compile(@" namespace SomeNS { injector { ISomeType = ConcreteSomeType; } }", builder: (compiler) => NinjectExtension.Apply(compiler)); Assert.IsNotNull(tree); //must find the overriden Load call from NInject var loadCall = tree .GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Where(method => method.Identifier.ToString() == "Load") .Single(); //must find one bind call var bindCall = loadCall .DescendantNodes() .OfType <ExpressionStatementSyntax>() .Single(); //must have 2 generic parameter list (ISomeType and ConcreteSomeType) Assert.AreEqual(2, bindCall .DescendantNodes() .OfType <TypeArgumentListSyntax>() .Count()); }
public void DapperCommand_Usage() { var tree = ExcessMock.Compile(@" class SomeClass { void SomeMethod(int SomeInt) { sql { insert into SomeTable values(@SomeInt) } } }", builder: (compiler) => DapperExtension.Apply(compiler)); //must have a call to __connection.Execute var invocation = (tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single() .Body .Statements .Single() as ExpressionStatementSyntax) .Expression as InvocationExpressionSyntax; Assert.IsNotNull(invocation); Assert.IsTrue(invocation is InvocationExpressionSyntax); Assert.IsTrue((invocation as InvocationExpressionSyntax) .Expression .ToString() .StartsWith("__connection1.Execute")); //must have added a literal string @"...query..." var literal = tree.GetRoot() .DescendantNodes() .OfType <LiteralExpressionSyntax>() .Single() .ToString(); Assert.IsTrue(literal.Contains("@\"")); Assert.IsTrue(literal.Contains("values(@SomeInt)")); //must have added parameters as an anonymous object var parameters = tree.GetRoot() .DescendantNodes() .OfType <AnonymousObjectCreationExpressionSyntax>() .Single(); Assert.IsTrue(parameters .Initializers .Any(init => init .NameEquals .Name .ToString() == "SomeInt")); }
public void R_Statement_Usage() { var tree = ExcessMock.Compile(@" void main() { R() { x <- 1 y <- 2 z <- NA if (x == 1) 3 -> z if (y == 1) { 3 -> z } else { z1 <- 4 z <- z1 } while(z < 10) c(a, z) -> a for(i in z) { a <- c(a, i); } repeat { b <- a a <- c(b, b); if (length(a) > 10) break; } } }", builder: (compiler) => RExtension.Apply(compiler)); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <WhileStatementSyntax>() .Count() == 2); //must have replaced a while and a repeat Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <StatementSyntax>() .Where(ss => !(ss is ExpressionStatementSyntax || ss is LocalDeclarationStatementSyntax || ss is BlockSyntax)) .Count() == 7); //3 if, 2 whiles, a foreach, a break }
public void Model_Usage() { var tree = ExcessMock.Compile(@" namespace SomeNS { model SomeModel { int Property1; string Property2 = ""SomeValue""; } }", builder: (compiler) => ModelExtension.Apply(compiler)); Assert.IsNotNull(tree); //an struct should have been added var @class = tree .GetRoot() .DescendantNodes() .OfType <ClassDeclarationSyntax>() .Single(); //with one constructor var constructor = @class .DescendantNodes() .OfType <ConstructorDeclarationSyntax>() .Single(); //must have 2 parameters and 2 assignments Assert.AreEqual(2, constructor.ParameterList.Parameters.Count); Assert.AreEqual(2, constructor .DescendantNodes() .OfType <AssignmentExpressionSyntax>() .Count()); //must have 2 properties with private sets Assert.AreEqual(2, @class .DescendantNodes() .OfType <PropertyDeclarationSyntax>() .Where(property => property .AccessorList .Accessors .Any(accessor => accessor.Keyword.IsKind(SyntaxKind.SetKeyword) && accessor .Modifiers .Any(modifier => modifier.IsKind(SyntaxKind.PrivateKeyword)))) .Count()); }
public void TypeCodeExtensions_KeywordIdentifierParameters() { var tree = ExcessMock.Compile(@" SomeExtension SomeIdentifier(int SomeParam) { SomeCall(); }", (compiler) => compiler.extension( "SomeExtension", TypeCodeExtensions_Transform( expectsIdentifier: true, expectsParameters: true))); Assert.IsNotNull(tree); TypeCodeExtensions_Assertions(tree, expectedArguments: 1); }
public void TypeCodeExtensions_Keyword() { var tree = ExcessMock.Compile(@" SomeExtension { SomeCall(); }", (compiler) => compiler.extension( "SomeExtension", TypeCodeExtensions_Transform( expectsIdentifier: false, expectsParameters: false))); Assert.IsNotNull(tree); TypeCodeExtensions_Assertions(tree); }
public void TypeExtensions_KeywordParameters() { var tree = ExcessMock.Compile(@" SomeExtension(int SomeParameter) { void SomeMethod() { } }", (compiler) => compiler.extension( "SomeExtension", TypeExtensions_Transform( expectsIdentifier: false, expectsParameters: true))); Assert.IsNotNull(tree); TypeExtensions_Assertions(tree, expectedArguments: 1); }
public void Contract_WhenUsingInvocationThenException_ShouldSucceed() { var tree = ExcessMock.Compile(@" class SomeClass { void SomeMethod() { contract { SomeObject.SomeOtherMethod() >> SomeException(); } } }", builder: (compiler) => ContractExtension.Apply(compiler)); Assert.IsNotNull(tree); Assert.IsFalse(tree.GetDiagnostics().Any()); }
public void MemberExtensions_KeywordParameters() { var tree = ExcessMock.Compile(@" class SomeClass { SomeExtension(int SomeParam) { SomeCall(); } }", (compiler) => compiler.extension( "SomeExtension", MemberExtensions_Transform( expectsIdentifier: false, expectsParameters: true))); Assert.IsNotNull(tree); MemberExtensions_Assertions(tree, expectedArguments: 1); }
public void Settings_Usage() { var tree = ExcessMock.Compile(@" namespace SomeNS { settings { [SomeSection] SomeIntValue = 12 SomeStringValue = ""Hello"" [SomeOtherSection] SomeOtherValue = ""World"" } }", builder: (compiler) => SettingExtension.Apply(compiler)); Assert.IsNotNull(tree); //must have created a class var @class = tree .GetRoot() .DescendantNodes() .OfType <ClassDeclarationSyntax>() .Single(); //with an "__init" method var method = @class .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single(); Assert.AreEqual("__init", method.Identifier.ToString()); //with 3 statements Assert.AreEqual(3, method.Body.Statements.Count); //all containing a call to ConfigurationManager Assert.AreEqual(3, method .Body .DescendantNodes() .OfType <MemberAccessExpressionSyntax>() .Where(inv => inv.Expression.ToString() == "ConfigurationManager") .Count()); }
public void FreeFormExtensions_KeywordParameters() { var tree = ExcessMock.Compile(@" class SomeClass { void SomeMethod() { SomeExtension(int SomeIdentifier) { SomeCall(); } } }", (compiler) => compiler.Lexical().extension( "SomeExtension", ExtensionKind.Code, FreeFormExtensions__Transform)); Assert.IsNotNull(tree); FreeFormExtensions_Validation(tree, true); }
public void MemberTypeExtensions_KeywordIdentifier() { var tree = ExcessMock.Compile(@" class SomeClass { SomeExtension SomeIdentifier { void SomeMethod() { } } }", (compiler) => compiler.extension( "SomeExtension", MemberTypeExtensions_Transform( expectsIdentifier: true, expectsParameters: false))); Assert.IsNotNull(tree); MemberTypeExtensions_Assertions(tree); }
public void Model_WithMultiTokenType_ShouldSucceed() { var tree = ExcessMock.Compile(@" namespace SomeNamespace { public model SomeModel { IEnumerable<int> SomeGeneric; Guid[] SomeArray; } }", builder: (compiler) => ModelExtension.Apply(compiler)); Assert.IsNotNull(tree); //must have transform both fields into properties Assert.AreEqual(2, tree .GetRoot() .DescendantNodes() .OfType <PropertyDeclarationSyntax>() .Count()); }
public void TypeExtensions_Keyword() { var tree = ExcessMock.Compile(@" namespace SomeNamespace { SomeExtension { void SomeMethod() { } } }", (compiler) => compiler.extension( "SomeExtension", TypeExtensions_Transform( expectsIdentifier: false, expectsParameters: false))); Assert.IsNotNull(tree); TypeExtensions_Assertions(tree); }
public void R_Sequence_Usage() { var tree = ExcessMock.Compile(@" void main() { R() { x <- 1:30 y <- 2*1:15 seq(-5, 5, by=.2) -> s3 s4 <- seq(length=51, from=-5, by=.2) s5 <- rep(x, times=5) } }", builder: (compiler) => RExtension.Apply(compiler)); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString().Contains("RR")) .Count() == 6); //must have replaced all operators }
public void CodeExtensions_KeywordIdentifierParameters() { var tree = ExcessMock.Compile(@" class SomeClass { void SomeMethod() { SomeExtension SomeIdentifier(int SomeParam, SomeArgument) { SomeCall(); } } }", (compiler) => compiler.extension( "SomeExtension", CodeExtensions_Transform( expectsIdentifier: true, expectsParameters: true))); Assert.IsNotNull(tree); CodeExtensions_Assertions(tree, expectedArguments: 4); }
public void FreeFormExtensions_KeywordIdentifierParameters() { var tree = ExcessMock.Compile(@" class SomeClass { void SomeMethod() { SomeExtension SomeIdentifier(int SomeParam) { SomeCall(); } } }", (compiler) => compiler.Lexical().extension( "SomeExtension", ExtensionKind.None, FreeFormExtensions__Transform)); Assert.IsNotNull(tree); FreeFormExtensions_Validation(tree, false); //must have added 2 arguments var arguments = tree .GetRoot() .DescendantNodes() .OfType <ArgumentListSyntax>() .Where(args => args.Arguments.Count == 2) .Single(); //with 2 arguments, one for SomeIdentifier one for SomeParam var SomeIdentifier = arguments.Arguments.First(); Assert.AreEqual("SomeIdentifier", SomeIdentifier.ToString()); var SomeParam = arguments.Arguments.Last(); Assert.AreEqual("SomeParam", SomeParam.ToString()); }
public void Debug() { var tree = ExcessMock.Compile(@" using xs.server; using xs.concurrent; using demo_transpiler; namespace Home { function Transpile(string text) { return _transpiler.Process(text); } public function TranspileGraph(string text) { return _graphTranspiler.Process(text); } }", (compiler) => DependencyInjection.Apply(compiler)); //this is a placeholder to do some debuggin' Assert.AreNotEqual(null, tree); }
public void DapperQuery_Usage() { var tree = ExcessMock.Compile(@" function SomeFunction(int SomeInt, int SomeId) { IEnumerable<SomeModel> result = sql { select * from SomeTable where SomeColumn > @SomeInt } SomeModel anotherResult = sql { select * from SomeTable where IdColumn > @SomeId } }", builder: compiler => { Functions.Apply(compiler); DapperExtension.Apply(compiler); }); //must have a call to __connection1.Query var invocation = (tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single() .Body .Statements .Skip(2) //connection declarations .First() as LocalDeclarationStatementSyntax) .Declaration .Variables .Single() .Initializer .Value as InvocationExpressionSyntax; Assert.IsNotNull(invocation); Assert.IsTrue(invocation is InvocationExpressionSyntax); Assert.IsTrue((invocation as InvocationExpressionSyntax) .Expression .ToString() .StartsWith("__connection1.Query")); //must have added a literal string @"...query..." var literal = tree.GetRoot() .DescendantNodes() .OfType <LiteralExpressionSyntax>() .First() .ToString(); Assert.IsTrue(literal.Contains("@\"")); Assert.IsTrue(literal.Contains("where SomeColumn > @SomeInt")); //must have added parameters as an anonymous object var parameters = tree.GetRoot() .DescendantNodes() .OfType <AnonymousObjectCreationExpressionSyntax>() .First(); Assert.IsTrue(parameters .Initializers .Any(init => init .NameEquals .Name .ToString() == "SomeInt")); //must have added a second call invocation = (tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single() .Body .Statements .Last() as LocalDeclarationStatementSyntax) .Declaration .Variables .Single() .Initializer .Value as InvocationExpressionSyntax; //containing a Single() call Assert.AreEqual(1, invocation .DescendantTokens() .Count(token => token.ToString() == "Single")); }
public void IndentedGrammar_Usage() { var tree = ExcessMock.Compile(@" class TestClass { void TestMethod() { var TestVar = ""World""; var TestArray = new [] {""Hello"", ""World""}; someExtension() { [Header1] Value1 = 10 Value2 = ""SomeValue"" [Header2] Value3 = ""Hello "" + TestVar [ContactHeader] Call Someone at 1-800-WAT-EVER Or else at 1-800-456-7890 Less likely at (877) 789-1234 [Location] Now at 9th and Hennepin, Minneapolis //some code for i in TestArray Console.Write(i); } } }", (compiler) => MockIndentGrammar.Apply(compiler)); Assert.IsNotNull(tree); //must have add 3 calls in the form SetHeaderValue("[Header]", "Value", Value); Assert.AreEqual(3, tree .GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation .Expression .ToString() .StartsWith("SetHeaderValue")) .Count()); //must have added a call in the form SetContact("[Header]", "Name", "Telephone"); Assert.AreEqual(1, tree .GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation .Expression .ToString() .StartsWith("SetContact")) .Count()); //must refer to the supplied number Assert.AreEqual(1, tree .GetRoot() .DescendantNodes() .OfType <LiteralExpressionSyntax>() .Where(literal => literal .ToString() .Equals("\"1-800-WAT-EVER\"")) .Count()); //must have added extra numbers in the form AddContactNumber("Header", "Name", AreaCode, First 3 numbers, Last 4 numbers) Assert.AreEqual(2, tree .GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation .Expression .ToString() .Equals("AddContactNumber")) .Count()); //must have parsed correctly and include all secondary telephones var numbers = new[] { 800, 456, 7890, 877, 789, 1234 }; Assert.AreEqual(numbers.Length, tree .GetRoot() .DescendantNodes() .OfType <LiteralExpressionSyntax>() .Where(number => number.IsKind(SyntaxKind.NumericLiteralExpression) && numbers.Contains(int.Parse(number.ToString()))) .Count()); //must have added a location call Assert.AreEqual(1, tree .GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString() == "SetLocation") .Count()); //must have added a foreach statement Assert.AreEqual(1, tree .GetRoot() .DescendantNodes() .OfType <ForEachStatementSyntax>() .Count()); }