Example #1
0
        public void Should_Build_NewObjects()
        {
            var builder = new ErrorBuilder();

            var error1 = builder.Build();
            var error2 = builder.Build();
            var error3 = builder.Build();

            error1.Should().NotBeSameAs(error2);
            error1.Should().NotBeSameAs(error3);
            error2.Should().NotBeSameAs(error3);
        }
Example #2
0
        private IEnumerable <IError> ParseErrors(JsonElement parent)
        {
            var length = parent.GetArrayLength();

            for (var i = 0; i < length; i++)
            {
                JsonElement  error   = parent[i];
                ErrorBuilder builder = ErrorBuilder.New();

                builder.SetMessage(error.GetProperty(_message).GetString());

                if (TryParseLocation(error, out IReadOnlyList <Location>?locations))
                {
                    builder.AddLocations(locations !);
                }

                if (TryParsePath(error, out IReadOnlyList <object>?path))
                {
                    builder.SetPath(path);
                }

                if (TryParseExtensions(error, out IReadOnlyDictionary <string, object?>?extensions))
                {
                    builder.SetExtensions(extensions !);
                }

                yield return(builder.Build());
            }
        }
Example #3
0
        public void Should_Build_WithArgs_When_AddedOnInit()
        {
            var args = new IArg[]
            {
                Arg.Text("name1", "value1"),
                Arg.Number("name2", 2)
            };

            var builder = new ErrorBuilder("key", args);

            builder.IsEmpty.Should().BeFalse();

            var error = builder.Build();

            error.Messages.Should().ContainSingle("key");
            error.Codes.Should().BeEmpty();

            error.Args.Should().BeSameAs(args);

            error.Args.ElementAt(0).Should().BeOfType <TextArg>();
            ((TextArg)error.Args.ElementAt(0)).Name.Should().Be("name1");
            ((TextArg)error.Args.ElementAt(0)).Value.Should().Be("value1");

            error.Args.ElementAt(1).Should().BeOfType <NumberArg <int> >();
            ((NumberArg <int>)error.Args.ElementAt(1)).Name.Should().Be("name2");
            ((NumberArg <int>)error.Args.ElementAt(1)).Value.Should().Be(2);
        }
Example #4
0
        public void Should_Build_WithArgs_When_AddedOnInit_And_CommandsOverride(object cmd, IError expectedError)
        {
            var args = new IArg[]
            {
                Arg.Text("name1", "value1"),
                Arg.Number("name2", 2)
            };

            var commands = (ICommand[])cmd;

            var builder = new ErrorBuilder("key", args);

            builder.IsEmpty.Should().BeFalse();

            foreach (var command in commands)
            {
                builder.TryAdd(command);
            }

            var error = builder.Build();

            var expectedErrorSettable = (Error)expectedError;

            expectedErrorSettable.Args = args;

            error.ShouldBeEqualTo(expectedErrorSettable);
        }
Example #5
0
            public IError OnError(IError error)
            {
                var errorBuilder = new ErrorBuilder();
                var lastError    = _log.Errors.Observable.FirstOrDefaultAsync()?.GetAwaiter().GetResult();

                errorBuilder.SetMessage(lastError?.Message ?? error.Exception?.Message ?? error.Message);
                error = errorBuilder.Build();

                return(error);
            }
Example #6
0
        public void Should_Build_WithMessage_When_AddedOnInit()
        {
            var builder = new ErrorBuilder("key");

            builder.IsEmpty.Should().BeFalse();

            var error = builder.Build();

            error.Messages.Should().ContainSingle("key");
            error.Codes.Should().BeEmpty();
        }
Example #7
0
            public void Should_TryAdd_Build_ManyCodes_When_WithCodeCommand_IsFollowedBy_WithExtraCodeCommands(object cmd, IError error)
            {
                var commands = (ICommand[])cmd;
                var builder  = new ErrorBuilder();

                foreach (var command in commands)
                {
                    builder.TryAdd(command);
                }

                builder.Build().ShouldBeEqualTo(error);
            }
Example #8
0
            public void Should_TryAdd_Build_SingleCode_FromLatestCommand_When_WithCodeCommandIsTheLastOne(object cmd, IError error)
            {
                var commands = (ICommand[])cmd;
                var builder  = new ErrorBuilder();

                foreach (var command in commands)
                {
                    builder.TryAdd(command);
                }

                builder.Build().ShouldBeEqualTo(error);
            }
Example #9
0
            public void Should_TryAdd_Build_SingleCode_When_SingleExtraCodeCommand(object cmd, IError error)
            {
                var commands = (ICommand[])cmd;
                var builder  = new ErrorBuilder();

                foreach (var command in commands)
                {
                    builder.TryAdd(command);
                }

                builder.Build().ShouldBeEqualTo(error);
            }
Example #10
0
        public void Should_TryAdd_Keep_EnabledCommands_And_DisabledCommands_Independent(object cmd, IError error)
        {
            var commands = (ICommand[])cmd;
            var builder  = new ErrorBuilder();

            foreach (var command in commands)
            {
                builder.TryAdd(command);
            }

            builder.Build().ShouldBeEqualTo(error);
        }
Example #11
0
        public void Should_Be_Empty_If_NothingAdded()
        {
            var builder = new ErrorBuilder();

            builder.IsEmpty.Should().BeTrue();
            builder.Mode.Should().Be(ErrorMode.Append);

            var error = builder.Build();

            error.Args.Should().BeEmpty();
            error.Messages.Should().BeEmpty();
            error.Codes.Should().BeEmpty();
        }
Example #12
0
        public void Should_Be_NotEmpty_If_SomethingAdded()
        {
            var builder = new ErrorBuilder();

            builder.TryAdd(new WithExtraCodeCommand("code"));

            builder.IsEmpty.Should().BeFalse();

            var error = builder.Build();

            error.Args.Should().BeEmpty();
            error.Messages.Should().BeEmpty();
            error.Codes.Should().NotBeEmpty();
        }
Example #13
0
            public void Should_BeIn_OverrideMode_If_AnyNonExtraCommand_Or_WithErrorClearedCommand(object cmd, IError error)
            {
                var commands = (ICommand[])cmd;
                var builder  = new ErrorBuilder();

                foreach (var command in commands)
                {
                    builder.TryAdd(command);
                }

                builder.Build().ShouldBeEqualTo(error);

                builder.Mode.Should().Be(ErrorMode.Override);
            }
Example #14
0
            public void Should_BeIn_AppendMode_If_OnlyExtraCommands(object cmd, IError error)
            {
                var commands = (ICommand[])cmd;
                var builder  = new ErrorBuilder();

                foreach (var command in commands)
                {
                    builder.TryAdd(command);
                }

                builder.Build().ShouldBeEqualTo(error);

                builder.Mode.Should().Be(ErrorMode.Append);
            }