private static Thing ConfigureThing <T>(IServiceProvider provider) where T : Thing { var thing = provider.GetRequiredService <T>(); var option = provider.GetRequiredService <ThingOption>(); var optionsJson = new JsonSerializerOptions { WriteIndented = false, IgnoreNullValues = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var converter = new ConverterInterceptorFactory(thing, optionsJson); var properties = new PropertiesInterceptFactory(thing, option); var events = new EventInterceptFactory(thing, option); var actions = new ActionInterceptFactory(option); CodeGeneratorFactory.Generate(thing, new List <IInterceptorFactory>() { converter, properties, events, actions }); thing.ThingContext = new Context(converter.Create(), properties.Create(), events.Events, actions.Actions); return(thing); }
public void Valid() { var thing = new LampThing(); var eventFactory = new EventInterceptFactory(thing, _options); CodeGeneratorFactory.Generate(thing, new [] { eventFactory }); thing.ThingContext = new Context(Substitute.For <IThingConverter>(), Substitute.For <IProperties>(), eventFactory.Events, new Dictionary <string, ActionContext>()); var @int = _fixture.Create <int>(); thing.Emit(@int); var events = thing.ThingContext.Events[nameof(LampThing.Int)].ToArray(); events.Should().HaveCount(1); events[0].Data.Should().Be(@int); var @decimal = _fixture.Create <decimal>(); thing.Emit(@decimal); events = thing.ThingContext.Events[nameof(LampThing.Decimal)].ToArray(); events.Should().HaveCount(1); events[0].Data.Should().Be(@decimal); thing.Emit((decimal?)null); events = thing.ThingContext.Events[nameof(LampThing.Decimal)].ToArray(); events.Should().HaveCount(2); events[1].Data.Should().Be(null); var @dateTime = _fixture.Create <DateTime>(); thing.Emit(dateTime); events = thing.ThingContext.Events[nameof(LampThing.DateTime)].ToArray(); events.Should().HaveCount(1); events[0].Data.Should().Be(@dateTime); var @obj = _fixture.Create <object>(); thing.Emit(obj); events = thing.ThingContext.Events[nameof(LampThing.Any)].ToArray(); events.Should().HaveCount(1); events[0].Data.Should().Be(@obj); }
public void Ignore() { var thing = new LampThing(); var eventFactory = new EventInterceptFactory(thing, _options); CodeGeneratorFactory.Generate(thing, new [] { eventFactory }); thing.ThingContext = new Context(Substitute.For <IThingConverter>(), Substitute.For <IProperties>(), eventFactory.Events, new Dictionary <string, ActionContext>()); var @int = _fixture.Create <int>(); thing.EmitIgnore(@int); var events = thing.ThingContext.Events[nameof(LampThing.Int)].ToArray(); events.Should().BeEmpty(); }