public void GetShortTypeName()
        {
            var types = new[]
            {
                typeof(int),
                typeof(string),
                typeof(InnerClass),
                typeof(List <int>),
                typeof(List <string>),
                typeof(List <InnerClass>),
                typeof(int[]),
                typeof(string[]),
                typeof(InnerClass[]),
                typeof(Dictionary <List <InnerClass[]>, InnerClass>),
            };

            var results = types.Select(t => CsUtility.GetShortTypeName(t)).ToList();

            string expected =
                @"Int32
String
InnerClass
List`1<Int32>
List`1<String>
List`1<InnerClass>
Int32[]
String[]
InnerClass[]
Dictionary`2<List`1<InnerClass[]>, InnerClass>";

            TestUtility.AssertAreEqualByLine(expected, string.Join("\r\n", results));
        }
        public void RuntimeRegistrationsRegressionTest()
        {
            using (var scope = TestScope.Create())
            {
                var lifetimeScope = (ILifetimeScope)typeof(UnitOfWorkScope)
                                    .GetField("_lifetimeScope", BindingFlags.NonPublic | BindingFlags.Instance)
                                    .GetValue(scope);

                var registrations = lifetimeScope.ComponentRegistry.Registrations
                                    .Select(registration => registration.ToString())
                                    .OrderBy(text => text)
                                    // Removing repository class registrations. This test is focused on system components, not business features in test DSL scripts.
                                    .Where(text => !text.Contains("_Repository ("))
                                    // Removing implementations of generic interfaces. They are added dynamically at runtime as an internal Autofac caching mechanism.
                                    .Where(text => !_genericInterfaceRegistration.IsMatch(text))
                                    .ToList();

                foreach (string line in registrations)
                {
                    Console.WriteLine(line);
                }

                TestUtility.AssertAreEqualByLine(_expectedRegistrationsRuntime.Trim(), string.Join("\r\n", registrations));
            }
        }
Exemple #3
0
        public void ExpressionsParameters()
        {
            // Input format: "Expression / ArgumentTypes"
            string tests =
                @"
a => a.Length
(a, b) => a.Length
(a, b) => (a + b).Length / List<C> double
(string a, List<C> b) => (a + b).Length / List<C> double
(string a, List<C> b) => (a + b).Length
(string a, b) => (a + b).Length / int double
";
            string expected = // Format: MethodParameters / MethodBody / ResultLiteral(if available), or Exception.
                              @"
null-a
null-a, null-b
List<C>-a, double-b
string-a, List<C>-b
string-a, List<C>-b
int-a, double-b
";

            IConceptInfo testConcept = new TestConcept {
                Name = "Test"
            };

            var results = new List <string>();

            foreach (var test in tests.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var      testParts      = test.Split('/');
                string   expressionText = testParts[0].Trim();
                string[] argumentTypes  = testParts.Length > 1
                    ? testParts[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(argument => argument.Trim()).ToArray()
                    : null;

                try
                {
                    var parsedExpression = new ParsedExpression(expressionText, argumentTypes, testConcept);
                    results.Add(TestUtility.Dump(parsedExpression.ExpressionParameters, p => $"{p.Type ?? "null"}-{p.Name}"));
                }
                catch (Exception e)
                {
                    results.Add($"{e.GetType().Name}: {e.Message}");
                }
            }

            string report = string.Join("\r\n", results);

            Console.WriteLine(report.Replace("\"", "\"\""));

            TestUtility.AssertAreEqualByLine(expected.Trim(), report);
        }
Exemple #4
0
        public void CorrectRegistrationsServerRuntime()
        {
            var configuration = GetRuntimeConfiguration();

            using (var container = new RhetosRuntime(isHost: true).BuildContainer(new NLogProvider(), configuration, null))
            {
                var registrationsDump = DumpSortedRegistrations(container);
                System.Diagnostics.Trace.WriteLine(registrationsDump);
                TestUtility.AssertAreEqualByLine(_expectedRegistrationsServerRuntime, registrationsDump);

                TestAmbiguousRegistations(container,
                                          expectedMultiplePlugins: new[] { "Rhetos.Dsl.IDslModelIndex" },
                                          expectedOverridenRegistrations: new Dictionary <Type, string> {
                    { typeof(IUserInfo), "TestWebSecurityUserInfo" }
                });
            }
        }
Exemple #5
0
        public void CorrectRegistrationsRuntimeWithInitialization()
        {
            var configuration = GetRuntimeConfiguration();
            var deployment    = new ApplicationDeployment(configuration, new NLogProvider());

            using (var container = new RhetosRuntime(isHost: false).BuildContainer(new NLogProvider(), configuration, deployment.AddAppInitializationComponents))
            {
                var registrationsDump = DumpSortedRegistrations(container);
                System.Diagnostics.Trace.WriteLine(registrationsDump);
                TestUtility.AssertAreEqualByLine(_expectedRegistrationsRuntimeWithInitialization, registrationsDump);

                TestAmbiguousRegistations(container,
                                          expectedMultiplePlugins: new[] { "Rhetos.Dsl.IDslModelIndex" },
                                          expectedOverridenRegistrations: new Dictionary <Type, string> {
                    { typeof(IUserInfo), "ProcessUserInfo" }
                });
            }
        }
Exemple #6
0
        public void CorrectRegistrationsDbUpdate()
        {
            var configuration = GetRuntimeConfiguration();
            var deployment    = new ApplicationDeployment(configuration, new NLogProvider());
            var builder       = deployment.CreateDbUpdateComponentsContainer();

            using (var container = builder.Build())
            {
                var registrationsDump = DumpSortedRegistrations(container);
                System.Diagnostics.Trace.WriteLine(registrationsDump);
                TestUtility.AssertAreEqualByLine(_expectedRegistrationsDbUpdate, registrationsDump);

                TestAmbiguousRegistations(container,
                                          expectedOverridenRegistrations: new Dictionary <Type, string> {
                    { typeof(IUserInfo), "NullUserInfo" }
                });
            }
        }
Exemple #7
0
        public void CorrectRegistrationsBuildTime()
        {
            var configuration = GetBuildConfiguration();
            var build         = new ApplicationBuild(configuration, new NLogProvider(), PluginsFromThisAssembly(), new InstalledPackages());
            var builder       = build.CreateBuildComponentsContainer();

            using (var container = builder.Build())
            {
                var registrationsDump = DumpSortedRegistrations(container);
                System.Diagnostics.Trace.WriteLine(registrationsDump);
                TestUtility.AssertAreEqualByLine(_expectedRegistrationsBuild, registrationsDump);

                TestAmbiguousRegistations(container,
                                          expectedMultiplePlugins: new[] { "Rhetos.Dsl.IDslModelIndex", "Rhetos.Extensibility.IGenerator" },
                                          expectedOverridenRegistrations: new Dictionary <Type, string> {
                    { typeof(IUserInfo), "NullUserInfo" }
                });
            }
        }
        public void CorrectRegistrationsDbUpdate()
        {
            var deployment = new ApplicationDeploymentAccessor();

            var rhetosHostBuilder = new RhetosHostTestBuilder()
                                    .ConfigureConfiguration(RhetosHostTestBuilder.GetRuntimeConfiguration)
                                    .UseBuilderLogProvider(new NLogProvider())
                                    .OverrideContainerConfiguration(deployment.SetDbUpdateComponents);

            using (var rhetosHost = rhetosHostBuilder.Build())
            {
                var registrationsDump = DumpSortedRegistrations(rhetosHost.GetRootContainer());
                System.Diagnostics.Trace.WriteLine(registrationsDump);
                TestUtility.AssertAreEqualByLine(_expectedRegistrationsDbUpdate, registrationsDump);

                TestAmbiguousRegistations(rhetosHost.GetRootContainer(),
                                          expectedOverridenRegistrations: new Dictionary <Type, string> {
                    { typeof(IUserInfo), "NullUserInfo" }
                });
            }
        }
        public void CorrectRegistrationsRuntimeWithInitialization()
        {
            // we construct the object, but need only its 'almost' static .AddAppInitilizationComponents
            var deployment        = new ApplicationDeploymentAccessor();
            var rhetosHostBuilder = new RhetosHostTestBuilder()
                                    .ConfigureConfiguration(RhetosHostTestBuilder.GetRuntimeConfiguration)
                                    .ConfigureContainer(deployment.AddAppInitializationComponents);

            using (var rhetosHost = rhetosHostBuilder.Build())
            {
                var registrationsDump = DumpSortedRegistrations(rhetosHost.GetRootContainer());
                System.Diagnostics.Trace.WriteLine(registrationsDump);
                TestUtility.AssertAreEqualByLine(_expectedRegistrationsRuntimeWithInitialization, registrationsDump);

                TestAmbiguousRegistations(rhetosHost.GetRootContainer(),
                                          expectedMultiplePlugins: new[] { "Rhetos.Dsl.IDslModelIndex" },
                                          expectedOverridenRegistrations: new Dictionary <Type, string> {
                    { typeof(IUserInfo), "ProcessUserInfo" },
                    { typeof(ILogProvider), "NLogProvider" }
                });
            }
        }
Exemple #10
0
        public void Parsing()
        {
            // Input format: "Expression / ArgumentTypes"
            string tests =
                @"
a => /*1*/ a /*2*/ / int
a v => a.Length / 
(a, b) => (a + b).Length / int double
(string a, string b) => (a + b).Length / string string
(string a, string b) => (a + b).Length / string
(string a) => a.Length / string
(string a) => a.Length / int
(a) => a.Length / int
a => a.Length / int
delegate (string a) { return a.Length; } / string
(string x) => { /*1*/ x++; /*2*/ return /*3*/ x.Length; /*4*/ } / string
async () => await Task.Run(() => ""ad"") /
async () => ""ad""; // Good syntax, invalid semantics. /
a => a.Length / string
(List<Cus[]> a) => b.Length / x
/*asdf*/ a /*a*/ => /*fasd */ b.Length /*asdf */ / x
a => true / x
a => a / x
a => false / x
a => 123 / x
a => 123.0 / x
a => 123m / x
a => (int?)null / x
a => ""he\r\n\""llo"" / x
a => @""he""""llo"" / x
a => null / x
a => nulll / x
() => a / x
() => a /
{ print(a); } /
 /
a; b; /
";
            string expected = // Format: MethodParameters / MethodBody / ResultLiteral(if available), or Exception.
                              @"
(int a) { return /*1*/ a; }
DslSyntaxException: TestConcept Test: C# syntax error '(1,16): error CS1002: ; expected' in code snippet 'a v => a.Length'.
(int a, double b) { return (a + b).Length; }
(string a, string b) { return (a + b).Length; }
DslSyntaxException: TestConcept Test: The provided code snippet should have 1 parameters instead of 2. Code snippet: '(string a, string b) => (a + b).Length'. Expected parameter types: string.
(string a) { return a.Length; }
(string a) { return a.Length; }
(int a) { return a.Length; }
(int a) { return a.Length; }
DslSyntaxException: TestConcept Test: The provided code snippet should be formatted as a C# lambda expression. Code snippet 'delegate (string a) { return a.Length; }' is 'AnonymousMethodExpression' instead of 'SimpleLambdaExpression or ParenthesizedLambdaExpression'.
(string x) { /*1*/ x++; /*2*/ return /*3*/ x.Length; /*4*/ }
() { return await Task.Run(() => ""ad""); }
() { return ""ad""; }/""ad""
(string a) { return a.Length; }
(List<Cus[]> a) { return b.Length; }
(x a) { return /*fasd */ b.Length; }
(x a) { return true; }/true
(x a) { return a; }
(x a) { return false; }/false
(x a) { return 123; }/123
(x a) { return 123.0; }/123.0
(x a) { return 123m; }/123m
(x a) { return (int?)null; }
(x a) { return ""he\r\n\""llo""; }/""he\r\n\""llo""
(x a) { return @""he""""llo""; }/@""he""""llo""
(x a) { return null; }/null
(x a) { return nulll; }
DslSyntaxException: TestConcept Test: The provided code snippet should have 1 parameters instead of 0. Code snippet: '() => a'. Expected parameter types: x.
() { return a; }
DslSyntaxException: TestConcept Test: The provided code snippet should be formatted as a C# lambda expression. Code snippet '{ print(a); }' is 'Block' instead of 'ExpressionStatement'.
DslSyntaxException: TestConcept Test: The provided code snippet should be formatted as a C# lambda expression. Code snippet '' has no content. Expected content type is 'GlobalStatement'.
DslSyntaxException: TestConcept Test: The provided code snippet should be formatted as a C# lambda expression. Code snippet 'a; b;' contains multiple nodes while only one is expected. Expected child node type is 'GlobalStatement'. The provided snippet contains 2 child nodes: GlobalStatement, GlobalStatement.
";

            IConceptInfo testConcept = new TestConcept {
                Name = "Test"
            };

            var results = new List <string>();

            foreach (var test in tests.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
            {
                int      split          = test.LastIndexOf('/');
                string   expressionText = test.Substring(0, split).Trim();
                string[] argumentTypes  = test.Substring(split + 1).Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(part => part.Trim()).ToArray();

                try
                {
                    var parsedExpression = new ParsedExpression(expressionText, argumentTypes, testConcept);
                    results.Add($"{Simplify(parsedExpression.MethodParametersAndBody)}{(parsedExpression.ResultLiteral != null ? "/" + parsedExpression.ResultLiteral : "")}");
                }
                catch (Exception e)
                {
                    results.Add($"{e.GetType().Name}: {e.Message}");
                }
            }

            string report = string.Join("\r\n", results);

            Console.WriteLine(report.Replace("\"", "\"\""));

            TestUtility.AssertAreEqualByLine(expected.Trim(), report);
        }