public void FindOrGenerateDynamicBuilderFor_GivenNull_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = GenericBuilderLocator.FindOrGenerateDynamicBuilderFor(null);

            //---------------Test Result -----------------------
            Expect(result).To.Be.Null();
        }
        public void FindOrGenerateDynamicBuilderFor_GivenTypeWithNoBuilder_SecondCallShouldReturnSameBuilderType()
        {
            //---------------Set up test pack-------------------
            var type = typeof(SomeClassWithoutABuilder2);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result1 = GenericBuilderLocator.FindOrGenerateDynamicBuilderFor(type);
            var result2 = GenericBuilderLocator.FindOrGenerateDynamicBuilderFor(type);

            //---------------Test Result -----------------------
            Assert.AreEqual(result1, result2);
        }
Пример #3
0
        private TEntity BuildWithBuilder()
        {
            var entityType  = typeof(TEntity);
            var builderType = GenericBuilderLocator.TryFindExistingBuilderFor(entityType)
                              ?? GenericBuilderLocator.FindOrGenerateDynamicBuilderFor(entityType);

            Assert.IsNotNull(builderType, $"Can't find or create a builder for {entityType.Name}");
            var builder = Activator.CreateInstance(builderType) as IGenericBuilder;

            Assert.IsNotNull(builder, $"Located builder {builderType.Name} does not implement IGenericBuilder");
            var entity = builder.GenericWithRandomProps().GenericBuild() as TEntity;

            Assert.IsNotNull(entity, $"located builder {builderType.Name} for {entityType.Name} builds NULL or invalid entity");
            return(entity);
        }
        public void GetBuilderFor_WhenCannotFindExistingBuilderForType_ShouldGenerateIt()
        {
            //---------------Set up test pack-------------------
            var type = typeof(AnotherEntityWithoutBuilder);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var builder        = GenericBuilderLocator.GetBuilderFor(type);
            var dynamicBuilder = GenericBuilderLocator.FindOrGenerateDynamicBuilderFor(type);

            //---------------Test Result -----------------------
            Expect(builder).Not.To.Be.Null();
            Expect(dynamicBuilder).To.Equal(builder);
        }
        public void FindOrGenerateDynamicBuilderFor_GivenTypeWithNoBuilder_FirstCallShouldCreateBuilderType()
        {
            //---------------Set up test pack-------------------
            var type = typeof(SomeClassWithoutABuilder1);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = GenericBuilderLocator.FindOrGenerateDynamicBuilderFor(type);

            //---------------Test Result -----------------------
            Expect(result).Not.To.Be.Null();
            var genericBuilderType = typeof(GenericBuilder <,>);
            var baseType           = result.BaseType;

            Expect(genericBuilderType).To.Equal(baseType.GetGenericTypeDefinition());
            Expect(type).To.Equal(baseType.GetGenericArguments()[1]);
        }