public async Task Custom() { var configuration = new AugmenterConfiguration(); configuration.Configure <TestModel1>(c => { c.Custom((x, d) => { d["Opt1"] = Boxed.True; d["Opt2"] = Boxed.False; d["Opt3"] = "something"; d.Remove(nameof(TestModel1.Foo)); }); }); configuration.Build(); var augmenter = MocksHelper.Augmenter(configuration); var model = new TestModel1(); var result = (await augmenter.AugmentAsync(model)).Cast <AObject>(); result["Opt1"].Should().Be(Boxed.True); result["Opt2"].Should().Be(Boxed.False); result["Opt3"].Should().Be("something"); result.Should().NotContainKey(nameof(TestModel1.Foo)); }
public async Task CopiesGlobalState() { var configuration = CreateCommonConfiguration(); configuration.ConfigureGlobalState = (state, provider) => { var someService = provider.GetService <SomeService>(); state["Foo"] = someService.Foo; return(Task.FromResult(0)); }; var services = new ServiceCollection(); services.AddOptions(); services.AddSingleton(Options.Create(configuration)); services.AddSingleton <FakeAugmenterBase>(); services.AddSingleton <SomeService>(); var p = services.BuildServiceProvider(); var fixture = MocksHelper.For <FakeAugmenterBase>(p); await fixture.AugmentAsync(new TestModel1(), addState : state => { state["Some"] = "some"; }); var context = fixture.Contexts.Last(); context.State["Foo"].Should().Be("foo"); context.State["Some"].Should().Be("some"); await fixture.AugmentAsync(new TestModel1()); context = fixture.Contexts.Last(); context.State["Foo"].Should().Be("foo"); context.State.Should().NotContain("Some"); }
public void AugmenterConfigurationNotBuild_Throw() { var configration = new AugmenterConfiguration(); Assert.ThrowsAny <Exception>(() => { MocksHelper.AugmenterBase(configration); }); }
public async Task Null_ReturnsNull() { var fixture = MocksHelper.AugmenterBase(CreateBuiltConfiguration()); object model = null; var result = await fixture.AugmentAsync(model); result.Should().BeNull(); }
public async Task State_FallsThrough() { var fixture = MocksHelper.AugmenterBase(CreateCommonConfiguration()); var model = new TestModelC(); var someValue = "bars"; await fixture.AugmentAsync(model, addState : state => { state.Add("key", someValue); }); fixture.Contexts.First().State["key"].Should().Be(someValue); }
public async Task AlwaysAugmentsIfUsingAWrapper() { var configuration = new AugmenterConfiguration(); configuration.Build(); var fixture = MocksHelper.AugmenterBase(configuration); var model = new TestModel1(); var wrapper = new AugmenterWrapper <TestModel1>(model); var result = await fixture.AugmentAsync(wrapper); fixture.Contexts.Should().HaveCount(1); fixture.Contexts.First().TypeConfiguration.Augments.Should().HaveCount(0); fixture.Contexts.First().TypeConfiguration.Properties.Should().HaveCount(3); }
public async Task SupportsArrays() { var fixture = MocksHelper.AugmenterBase(CreateCommonConfiguration()); var arr = new TestModelC[] { new TestModelC(), new TestModelC() }; await fixture.AugmentAsync(arr); fixture.Contexts.Should().HaveCount(arr.Length); var context = fixture.Contexts.First(); context.Type.Should().Be(typeof(TestModelC)); context.TypeConfiguration.Type.Should().Be(typeof(TestModelC)); }
public async Task SupportsEnumerable() { var fixture = MocksHelper.AugmenterBase(CreateCommonConfiguration()); var list = new List <TestModelC> { new TestModelC(), new TestModelC() }; await fixture.AugmentAsync(list); fixture.Contexts.Should().HaveCount(list.Count); var context = fixture.Contexts.First(); context.Type.Should().Be(typeof(TestModelC)); context.TypeConfiguration.Type.Should().Be(typeof(TestModelC)); }
public async Task AlwaysAugmentsIfConfigureIsProvided() { var configuration = new AugmenterConfiguration(); configuration.Build(); var fixture = MocksHelper.AugmenterBase(configuration); var model = new TestModel1(); var result = await fixture.AugmentAsync(model, c => { c.Add("Bar", (x, state) => "bar"); }); fixture.Contexts.Should().HaveCount(1); fixture.Contexts.First().EphemeralTypeConfiguration.Augments.Should().HaveCount(1); fixture.Contexts.First().TypeConfiguration.Properties.Should().HaveCount(3); }
public async Task PicksUpWrapperState() { var fixture = MocksHelper.AugmenterBase(CreateCommonConfiguration()); var model = new TestModel1(); var wrapper = new AugmenterWrapper <TestModel1>(model); wrapper.SetAddState((x, s) => { s["Baz"] = x.Id; }); await fixture.AugmentAsync(wrapper); fixture.Contexts.Should().HaveCount(1); var context = fixture.Contexts.First(); context.State["Baz"].Cast <int>().Should().Be(model.Id); }
public NestedTest() { _configuration = new AugmenterConfiguration(); _configuration.Configure <TestModel1>(c => { }); _configuration.Configure <TestModelWithNested>(c => { c.Add("Foo", (_, __) => "42"); }); _configuration.Configure <TestModelNested>(c => { c.Add("Foo", (_, __) => "43"); }); _configuration.Configure <TestModelNestedNested>(c => { c.Add("Foo", (_, __) => "44"); }); _configuration.Build(); _fixture = MocksHelper.Augmenter(_configuration); }
public async Task PicksUpWrapperConfiguration() { var fixture = MocksHelper.AugmenterBase(CreateCommonConfiguration()); var model = new TestModel1(); var wrapper = new AugmenterWrapper <TestModel1>(model); wrapper.SetTypeConfiguration(c => { c.Add("Baz", (x, state) => x.Id); }); await fixture.AugmentAsync(wrapper); fixture.Contexts.Should().HaveCount(1); var context = fixture.Contexts.First(); context.Type.Should().Be(typeof(TestModel1)); context.TypeConfiguration.Type.Should().Be(typeof(TestModel1)); context.EphemeralTypeConfiguration.Type.Should().Be(typeof(TestModel1)); context.EphemeralTypeConfiguration.Augments.Should().HaveCount(1); }
public async Task Globally() { var model = new TestModel1(); _configuration = new AugmenterConfiguration(); _configuration.Configure <TestModel1>(c => { c.Add("Bar", (x, state) => { return(state["key"]); }); }); _configuration.Build(); _fixture = MocksHelper.Augmenter(_configuration); var result = await _fixture.AugmentAsync(model, addState : state => { state.Add("key", "bar"); }) as AObject; result["Bar"].Cast <string>().Should().Be("bar"); }
public async Task Nested() { var model = new TestModelWithNested(); _configuration = new AugmenterConfiguration(); _configuration.Configure <TestModelNested>(c => { c.Add("Foo", (x, state) => { return(state["key"]); }); }); _configuration.Build(); _fixture = MocksHelper.Augmenter(_configuration); var result = await _fixture.AugmentAsync(model, addState : state => { state.Add("key", "foo"); }) as AObject; result["Nested"].Cast <AObject>()["Foo"].Cast <string>().Should().Be("foo"); }
public AugmenterTest() { _configuration = CreateCommonConfiguration(); _fixture = MocksHelper.Augmenter(_configuration); }
public StateTest() { _configuration = CreateBuiltConfiguration(); _fixture = MocksHelper.Augmenter(_configuration); }
public NestedConfigureTest() { var configuration = new AugmenterConfiguration(); configuration.Configure <NestedConfigureModels.Model1>(c => { c.Add("Bar", (x, _) => $"({x.Id})"); c.Remove(nameof(NestedConfigureModels.Model1.Ex)); c.ConfigureNested(x => x.Nested1, nc => { nc.SetAddState((m, s1, s2) => { s2["ParentId"] = m.Id; }); nc.SetTypeConfiguration(ntc => { ntc.Add("Some", (x, state) => "some"); }); }); }); configuration.Configure <NestedConfigureModels.Model2>(c => { }); configuration.Configure <NestedConfigureModels.Model3>(c => { c.ConfigureNestedArray(x => x.Nested, nc => { nc.SetAddState((m, s1, s2) => { s2["ParentId"] = m.Id; }); nc.SetTypeConfiguration(ntc => { ntc.Add("Some", (x, state) => "some"); }); }); }); configuration.Configure <NestedConfigureModels.Model4>(c => { c.ConfigureNestedArray(x => x.Nested, nc => { nc.SetAddState((m, s1, s2) => { s2["ParentId"] = m.Id; }); nc.SetTypeConfiguration(ntc => { ntc.Add("Some", (x, state) => "some"); }); }); }); configuration.Configure <NestedConfigureModels.ModelNested>(c => { c.AddIf("ParentId", "ParentId", (m, s) => s["ParentId"]); }); configuration.Build(); _configuration = configuration; _fixture = MocksHelper.Augmenter(_configuration); }