public IValidationResult <T> Merge(params IErrorsCollection[] errorsCollections) { if (errorsCollections == null) { throw new ArgumentNullException(nameof(errorsCollections)); } if (errorsCollections.Contains(null)) { throw new ArgumentException("Null in collection", nameof(errorsCollections)); } var mergedErrorsCollection = new ErrorsCollection(); mergedErrorsCollection.Include(ErrorsCollection); foreach (var errorsCollection in errorsCollections) { mergedErrorsCollection.Include(errorsCollection); } return(new ValidationResult <T>(ValidationContextId, TranslationProxy, ExecutionOptions, Model, mergedErrorsCollection) { IsMergeResult = true }); }
public void Include_Should_Include_When_ContainsSingleLevel() { var errorsCollection = new ErrorsCollection(); errorsCollection.AddError("foo", new Error("test1")); errorsCollection.AddError("foo", new Error("test2")); errorsCollection.AddError("bar", new Error("test3")); errorsCollection.AddError("bar", new Error("test4")); var another = new ErrorsCollection(); another.AddError("foo", new Error("111")); another.AddError("foo", new Error("222")); another.AddError("bar", new Error("333")); another.AddError("bar", new Error("444")); another.AddError("X", new Error("555")); another.AddError("Y", new Error("666")); errorsCollection.Include(another); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "foo", "bar", "X", "Y" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"], new[] { "test1", "test2", "111", "222" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["bar"], new[] { "test3", "test4", "333", "444" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["X"], new[] { "555" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["Y"], new[] { "666" }); }
public void Include_Should_Include_When_ContainsMultipleNestedLevels() { var nested11 = new ErrorsCollection(); nested11.AddError("arg11", new Error("val111")); nested11.AddError("arg11", new Error("val112")); var nested12 = new ErrorsCollection(); nested12.AddError("arg12", new Error("val121")); nested12.AddError("arg12", new Error("val122")); var nested1 = new ErrorsCollection(); nested1.AddError("arg1", new Error("val11")); nested1.AddError("arg1", new Error("val12")); nested1.AddError("arg1", nested11); nested1.AddError("arg1", nested12); var errorsCollection = new ErrorsCollection(); errorsCollection.AddError("foo", nested1); var anotherNested12 = new ErrorsCollection(); anotherNested12.AddError("arg12", new Error("val122")); anotherNested12.AddError("arg12", new Error("val123")); var anotherNested13 = new ErrorsCollection(); anotherNested13.AddError("arg13", new Error("val131")); anotherNested13.AddError("arg13", new Error("val132")); var anotherNested1 = new ErrorsCollection(); anotherNested1.AddError("arg1", new Error("val12")); anotherNested1.AddError("arg1", new Error("val13")); anotherNested1.AddError("arg1", anotherNested12); anotherNested1.AddError("arg1", anotherNested13); var another = new ErrorsCollection(); another.AddError("foo", anotherNested1); errorsCollection.Include(another); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "foo" }); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["foo"], new[] { "arg1" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"], new[] { "val11", "val12", "val12", "val13" }); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["foo"].Members["arg1"], new[] { "arg11", "arg12", "arg13" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg11"], new[] { "val111", "val112" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg12"], new[] { "val121", "val122", "val122", "val123" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg13"], new[] { "val131", "val132" }); }
public void Include_Should_Include_When_BothSingleLevel() { var errorsCollection = new ErrorsCollection(); errorsCollection.AddError(new Error("test1")); errorsCollection.AddError(new Error("test2")); var another = new ErrorsCollection(); errorsCollection.AddError(new Error("foo")); errorsCollection.AddError(new Error("bar")); errorsCollection.Include(another); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection, new[] { "test1", "test2", "foo", "bar" }); }
public void Include_Should_ThrowException_When_NullArgument() { var errorsCollection = new ErrorsCollection(); Assert.Throws <ArgumentNullException>(() => { errorsCollection.Include(null); }); }
public void Include_Should_IncludeDifferentImplementation_When_ContainsMultipleNestedLevels() { var nested11 = new ErrorsCollectionImplementation { Members = new Dictionary <string, IErrorsCollection> { { "arg11", new ErrorsCollectionImplementation { Errors = new[] { new Error("val111"), new Error("val112") } } } } }; var nested12 = new ErrorsCollectionImplementation { Members = new Dictionary <string, IErrorsCollection> { { "arg12", new ErrorsCollectionImplementation { Errors = new[] { new Error("val121"), new Error("val122") } } } } }; var nested1 = new ErrorsCollection(); nested1.AddError("arg1", new Error("val11")); nested1.AddError("arg1", new Error("val12")); nested1.AddError("arg1", nested11); nested1.AddError("arg1", nested12); var errorsCollection = new ErrorsCollection(); errorsCollection.AddError("foo", nested1); var anotherNested12 = new ErrorsCollectionImplementation { Members = new Dictionary <string, IErrorsCollection> { { "arg12", new ErrorsCollectionImplementation { Errors = new[] { new Error("val122"), new Error("val123") } } } } }; var anotherNested13 = new ErrorsCollectionImplementation { Members = new Dictionary <string, IErrorsCollection> { { "arg13", new ErrorsCollectionImplementation { Errors = new[] { new Error("val131"), new Error("val132") } } } } }; var anotherNested1 = new ErrorsCollection(); anotherNested1.AddError("arg1", new Error("val12")); anotherNested1.AddError("arg1", new Error("val13")); anotherNested1.AddError("arg1", anotherNested12); anotherNested1.AddError("arg1", anotherNested13); var another = new ErrorsCollection(); another.AddError("foo", anotherNested1); errorsCollection.Include(another); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "foo" }); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["foo"], new[] { "arg1" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"], new[] { "val11", "val12", "val12", "val13" }); ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["foo"].Members["arg1"], new[] { "arg11", "arg12", "arg13" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg11"], new[] { "val111", "val112" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg12"], new[] { "val121", "val122", "val122", "val123" }); ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg13"], new[] { "val131", "val132" }); }