public void Should_Build_RuleCommandScope_When_Rule(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestClass> expectedErrorSetup)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Predicate <TestClass> predicate = x => true;

                Specification <TestClass> specification = m => appendSetupCommands(m.Rule(predicate));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <RuleCommandScope <TestClass> >();

                var ruleCommandScope = (RuleCommandScope <TestClass>)scope.CommandScopes[0];

                ruleCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                ruleCommandScope.ExecutionCondition.Should().Be(expectedErrorSetup.ShouldExecute);
                ruleCommandScope.ErrorMode.Should().Be(ErrorMode.Append);
                ruleCommandScope.IsValid.Should().BeSameAs(predicate);

                ruleCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
            }
            public void Should_Build_AsMemberScope_When_AsMemberScope(string testId, Func <dynamic, ISpecificationOut <TestParent> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestParent> expectedErrorSetup)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> innerSpecification = m => m;

                Specification <TestParent> specification = m => appendSetupCommands(m.Member(m1 => m1.TestMember, innerSpecification));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <MemberCommandScope <TestParent, TestClass> >();

                var memberCommandScope = (MemberCommandScope <TestParent, TestClass>)scope.CommandScopes[0];

                memberCommandScope.Path.Should().Be(expectedErrorSetup.Path ?? "TestMember");
                memberCommandScope.ErrorMode.Should().Be(ErrorMode.Append);
                memberCommandScope.ErrorId.Should().BeNull();
                memberCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);

                context.Scopes.Keys.Should().Contain(memberCommandScope.ScopeId);

                context.Scopes[memberCommandScope.ScopeId].Should().BeOfType <SpecificationScope <TestClass> >();
            }
        public static bool Evaluate(IProcessorState processor, ref int bufferLength, ref int currentBufferPosition, out bool faulted)
        {
            ITokenTrie tokens = Instance.GetSymbols(processor);
            ScopeBuilder <Operators, TTokens> builder = processor.ScopeBuilder(tokens, Map, DereferenceInLiteralsSetting);
            bool       isFaulted = false;
            IEvaluable result    = builder.Build(ref bufferLength, ref currentBufferPosition, x => isFaulted = true);

            if (isFaulted)
            {
                faulted = true;
                return(false);
            }

            try
            {
                object evalResult = result.Evaluate();
                bool   r          = (bool)Convert.ChangeType(evalResult, typeof(bool));
                faulted = false;
                return(r);
            }
            catch
            {
                faulted = true;
                return(false);
            }
        }
            public void Should_Return_Required_WithCustomError_And_Rules(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendErrorCommands, ErrorContentApiHelper.ExpectedErrorContent expected)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => AppendRule(appendErrorCommands(m.Required()));

                var scope = builder.Build(specification, context);

                if (expected.ShouldBeEmpty(0))
                {
                    scope.RequiredErrorId.Should().Be(context.RequiredErrorId);
                }
                else
                {
                    scope.RequiredErrorId.Should().NotBe(context.RequiredErrorId);
                    context.Errors.Keys.Should().Contain(scope.RequiredErrorId);

                    expected.Match(context.Errors[scope.RequiredErrorId]);
                }

                dynamic AppendRule(dynamic api)
                {
                    if (api is IRuleIn <TestClass> ruleIn)
                    {
                        return(RuleExtension.Rule(ruleIn, m => false));
                    }

                    throw new InvalidOperationException("Dynamic api tests failed");
                }
            }
            public void Should_Build_AsNullableScope_When_AsNullable(string testId, Func <dynamic, ISpecificationOut <int?> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <int?> expectedErrorSetup)
            {
                _ = testId;

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <int> innerSpecification = m => m;

                Specification <int?> specification = m => appendSetupCommands(m.AsNullable(innerSpecification));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <NullableCommandScope <int> >();

                var nullableCommandScope = (NullableCommandScope <int>)scope.CommandScopes[0];

                nullableCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                nullableCommandScope.ErrorMode.Should().Be(ErrorMode.Append);
                nullableCommandScope.ErrorId.Should().BeNull();
                nullableCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);

                context.Scopes.Keys.Should().Contain(nullableCommandScope.ScopeId);

                context.Scopes[nullableCommandScope.ScopeId].Should().BeOfType <SpecificationScope <int> >();
            }
            public void Should_Return_Forbidden_WithCustomError(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendErrorCommands, ErrorContentApiHelper.ExpectedErrorContent expected)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => appendErrorCommands(m.Forbidden());

                var scope = builder.Build(specification, context);

                if (expected.ShouldBeEmpty(0))
                {
                    scope.ForbiddenErrorId.Should().Be(context.ForbiddenErrorId);
                }
                else
                {
                    scope.ForbiddenErrorId.Should().NotBe(context.ForbiddenErrorId);

                    context.Errors.Keys.Should().Contain(scope.ForbiddenErrorId);

                    expected.Match(context.Errors[scope.ForbiddenErrorId]);
                }
            }
        public void Should_ThrowException_When_NullContext()
        {
            var builder = new ScopeBuilder();

            Specification <TestClass> specification = m => m;

            Action action = () => builder.Build(specification, null);

            action.Should().ThrowExactly <ArgumentNullException>();
        }
        public void Should_ThrowException_When_NullSpecification()
        {
            var builder = new ScopeBuilder();

            var context = Substitute.For <IScopeBuilderContext>();

            Action action = () => builder.Build <TestClass>(null, context);

            action.Should().ThrowExactly <ArgumentNullException>();
        }
        public void Should_BeEmpty_When_NoCommand()
        {
            var context = new ScopeBuilderContext();

            var builder = new ScopeBuilder();

            Specification <TestClass> specification = m => m;

            var scope = builder.Build(specification, context);

            scope.CommandScopes.Should().BeEmpty();
        }
            public void Should_Return_Required_When_NoCommand()
            {
                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => m;

                var scope = builder.Build(specification, context);

                scope.Presence.Should().Be(Presence.Required);
                scope.RequiredErrorId.Should().Be(context.RequiredErrorId);
            }
            public void Should_Return_Optional_When_Optional()
            {
                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => m
                                                          .Optional();

                var scope = builder.Build(specification, context);

                scope.Presence.Should().Be(Presence.Optional);
            }
            public void Should_Return_Forbidden_When_Forbidden()
            {
                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => m
                                                          .Forbidden();

                var scope = builder.Build(specification, context);

                scope.Presence.Should().Be(Presence.Forbidden);
                scope.ForbiddenErrorId.Should().Be(context.ForbiddenErrorId);
            }
            public void Should_Build_RuleCommandScope_When_RuleTemplate_WithArgs_AndCustomError(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <TestClass> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestClass> expectedErrorSetup)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Predicate <TestClass> predicate = x => true;

                var args = new IArg[]
                {
                    Arg.Text("argName1", "argValue"),
                    Arg.Number("argName2", 2),
                };

                Specification <TestClass> specification = m => appendSetupCommands(appendContentCommands(m.RuleTemplate(predicate, "ruleKey", args)));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <RuleCommandScope <TestClass> >();

                var ruleCommandScope = (RuleCommandScope <TestClass>)scope.CommandScopes[0];

                ruleCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                ruleCommandScope.ExecutionCondition.Should().Be(expectedErrorSetup.ShouldExecute);
                ruleCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);
                ruleCommandScope.IsValid.Should().BeSameAs(predicate);

                ruleCommandScope.ErrorId.Should().BeGreaterOrEqualTo(0);

                if (expectedErrorContent.ShouldBeEmpty())
                {
                    ruleCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    ruleCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);

                    context.Errors.Keys.Should().Contain(ruleCommandScope.ErrorId);
                    var error = context.Errors[ruleCommandScope.ErrorId];

                    expectedErrorContent.Match(error);

                    error.Args.Should().BeSameAs(args);
                }
            }
            public void Should_Build_AsCollectionScope_When_AsCollection_WithCustomError(string testId, Func <dynamic, ISpecificationOut <TestParent> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <TestParent> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestParent> expectedErrorSetup)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> innerSpecification = m => m;

                Specification <TestParent> specification = m => appendContentCommands(appendSetupCommands(m.Member(m1 => m1.TestMember, innerSpecification)));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <MemberCommandScope <TestParent, TestClass> >();

                var memberCommandScope = (MemberCommandScope <TestParent, TestClass>)scope.CommandScopes[0];

                memberCommandScope.Path.Should().Be(expectedErrorSetup.Path ?? "TestMember");
                memberCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);
                memberCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);

                var testParent = new TestParent()
                {
                    TestMember = new TestClass()
                };

                memberCommandScope.GetMemberValue(testParent).Should().BeSameAs(testParent.TestMember);

                memberCommandScope.ErrorId.Should().HaveValue();
                context.Errors.Keys.Should().Contain(memberCommandScope.ErrorId.Value);

                if (expectedErrorContent.ShouldBeEmpty(0))
                {
                    memberCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    memberCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);
                    expectedErrorContent.Match(context.Errors[memberCommandScope.ErrorId.Value], 0);
                }

                context.Scopes.Keys.Should().Contain(memberCommandScope.ScopeId);

                context.Scopes[memberCommandScope.ScopeId].Should().BeOfType <SpecificationScope <TestClass> >();
            }
            public void Should_Build_AsNullableScope_When_AsNullable_WithCustomError(string testId, Func <dynamic, ISpecificationOut <int?> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <int?> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <int?> expectedErrorSetup)
            {
                _ = testId;

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <int> innerSpecification = m => m;

                Specification <int?> specification = m => appendContentCommands(appendSetupCommands(m.AsNullable(innerSpecification)));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <NullableCommandScope <int> >();

                var nullableCommandScope = (NullableCommandScope <int>)scope.CommandScopes[0];

                nullableCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                nullableCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);
                nullableCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);

                nullableCommandScope.ErrorId.Should().HaveValue();
                context.Errors.Keys.Should().Contain(nullableCommandScope.ErrorId.Value);

                if (expectedErrorContent.ShouldBeEmpty(0))
                {
                    nullableCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    nullableCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);
                    expectedErrorContent.Match(context.Errors[nullableCommandScope.ErrorId.Value], 0);
                }

                context.Scopes.Keys.Should().Contain(nullableCommandScope.ScopeId);

                context.Scopes[nullableCommandScope.ScopeId].Should().BeOfType <SpecificationScope <int> >();
            }
            public void Should_Build_RuleCommandScope_When_Rule_WithCustomError(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <TestClass> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestClass> expectedErrorSetup)
            {
                testId.Should().NotBeEmpty();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Predicate <TestClass> predicate = x => true;

                Specification <TestClass> specification = m => appendContentCommands(appendSetupCommands(m.Rule(predicate)));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <RuleCommandScope <TestClass> >();

                var ruleCommandScope = (RuleCommandScope <TestClass>)scope.CommandScopes[0];

                ruleCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                ruleCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);
                ruleCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);
                ruleCommandScope.IsValid.Should().BeSameAs(predicate);

                ruleCommandScope.ErrorId.Should().BeGreaterOrEqualTo(0);

                if (expectedErrorContent.ShouldBeEmpty(0))
                {
                    ruleCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    ruleCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);
                    context.Errors.Keys.Should().Contain(ruleCommandScope.ErrorId);
                    expectedErrorContent.Match(context.Errors[ruleCommandScope.ErrorId], initialMessagesAmount: 0);
                }
            }
        IServicesProvider ICoreApplication.Initialize(IServicesProvider servicesProvider)
        {
            var assembly           = GetType().Assembly;
            var builder            = new ScopeBuilder(servicesProvider);
            var elementTypeMapping = new ElementTypeMapping(assembly);
            var appValues          = new AppValues();
            var conversionService  = new ConversionService(assembly);
            var bindingService     = new BindingService(appValues, conversionService);

            LoadApplicationDefinition(appValues, bindingService, elementTypeMapping, servicesProvider.GetService <IObjectFactory>());

            builder
            .WithInstance(this).As <Application>().As(GetType())
            .WithInstance(elementTypeMapping).As <IElementTypeMapping>()
            .WithInstance(appValues).As <IAppValues>()
            .WithInstance(bindingService).As <IBindingService>()
            .WithType <TooltipService>().As <ITooltipService>().AsSingleton()
            .WithType <UIServices>().As <IUIServices>().AsSingleton()
            .WithType <XxFileParserImpl>().As <IXxFileParser>().AsSingleton();

            BeforeInitServices?.Invoke(servicesProvider, builder);
            InitServices(servicesProvider, builder);

            if (!builder.TryResolveInstance <IViewLocator>(out var _))
            {
                builder.WithType <DefaultViewLocator>().As <IViewLocator>().AsSingleton();
            }

            AfterInitServices?.Invoke(servicesProvider, builder);


            Services = builder.Build();

            LoadFonts(Services.GetService <IFontManager>());
            WindowsService = Services.GetService <IWindowsService>();
            ((WindowService)WindowsService).ObjectFactory = Services.GetService <IObjectFactory>();
            return(Services);
        }
            public void Should_Build_RuleCommandScope_When_RuleTemplate_WithoutArgs(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestClass> expectedErrorSetup)
            {
                _ = testId;

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Predicate <TestClass> predicate = x => true;

                Specification <TestClass> specification = m => appendSetupCommands(m.RuleTemplate(predicate, "ruleKey"));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <RuleCommandScope <TestClass> >();

                var ruleCommandScope = (RuleCommandScope <TestClass>)scope.CommandScopes[0];

                ruleCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                ruleCommandScope.ExecutionCondition.Should().Be(expectedErrorSetup.ShouldExecute);
                ruleCommandScope.ErrorMode.Should().Be(ErrorMode.Append);
                ruleCommandScope.IsValid.Should().BeSameAs(predicate);

                ruleCommandScope.ErrorId.Should().BeGreaterOrEqualTo(0);
                ruleCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);

                context.Errors.Keys.Should().Contain(ruleCommandScope.ErrorId);

                var error = context.Errors[ruleCommandScope.ErrorId];

                error.Messages.Count.Should().Be(1);
                error.Messages[0].Should().Be("ruleKey");
                error.Args.Should().BeEmpty();
                error.Codes.Should().BeEmpty();
            }
Exemple #19
0
 public IServiceProvider Build() => scopeBuilder.Build();
Exemple #20
0
        public void Run(ICoreApplication application, IServicesProvider servicesProvider = null)
        {
            FormsApplication.SetHighDpiMode(HighDpiMode.SystemAware);
            FormsApplication.SetCompatibleTextRenderingDefault(false);

            Sequence.CreateSequenceFunc = SequenceImpl.Create;
            Sequence.NextFrameSequence  = SequenceImpl.Create(0, 0, null, null);

            dispatcher.DispatcherThread = Thread.CurrentThread;

            using (var graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
            {
                UiUnit.PixelsPerUnit = graphics.DpiX / 96f;
            }

            var scopeBuilder = new ScopeBuilder(servicesProvider);

            scopeBuilder.WithSkia()
            .WithInstance(gamePads).As <IGamePads>()
            .WithInstance(keyboard).As <IKeyboard>()
            .WithType <UiInput>().As <IUiInput>().AsSingleton()
            .WithCrossTypes()
            .WithInstance(dispatcher).As <ISystemDispatcher>().As <IDispatcher>()
            .WithInstance(sequencer).As <ISequencer>()
            .WithType <SelectFileServiceWinForms>().As <ISelectFileService>().AsSingleton();


            var services = scopeBuilder.Build();

            application.AfterInitServices += (s, b) =>
            {
                b.WithType <WindowServiceWinForms>().AsSelf().As <IWindowsService>().AsSingleton()
                .WithType <AppColorTable>().AsSelf().AsSingleton();
            };

            services = application.Initialize(services);
            application.Load();

            var windowsService = services.GetService <WindowServiceWinForms>();

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            TimeSpan lastUpdateTimeSpan = stopwatch.Elapsed;

            while (windowsService.MainWindow != null)
            {
                TimeSpan currentTimeSpan = stopwatch.Elapsed;
                TimeSpan timeDelta       = currentTimeSpan - lastUpdateTimeSpan;
                lastUpdateTimeSpan = currentTimeSpan;

                gamePads.Update();
                keyboard.Update();
                dispatcher.Process();
                sequencer.Update(timeDelta);

                for (var idx = 0; idx < windowsService.Windows.Count; ++idx)
                {
                    var hostWindow = windowsService.Windows[idx];

                    hostWindow.Window.Update((float)timeDelta.TotalSeconds);
                    if (hostWindow.Window.IsDirty)
                    {
                        hostWindow.Redraw();
                    }
                }

                FormsApplication.DoEvents();
                Thread.Sleep(1);
            }

            foreach (var wnd in windowsService.Windows)
            {
                wnd.Window.Close();
            }
        }