public void TryFindExistingBuilderFor_WhenHaveExistingBuilder_ShouldUseIt() { //---------------Set up test pack------------------- //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = GenericBuilderLocator.TryFindExistingBuilderFor(typeof(SomeClassWithKnownBuilder)); //---------------Test Result ----------------------- Expect(result).To.Equal(typeof(SomeClassWithKnownBuilderBuilder)); }
public void TryFindExistingBuilderFor_GivenNullType_ShouldReturnNull() { //---------------Set up test pack------------------- //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = GenericBuilderLocator.TryFindExistingBuilderFor(null); //---------------Test Result ----------------------- Expect(result).To.Be.Null(); }
public void TryFindExistingBuilderFor_ShouldBeAbleToFindBuilderInAnotherAssembly() { //---------------Set up test pack------------------- var type = typeof(SomeEntityWithBuilder); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var builderType = GenericBuilderLocator.TryFindExistingBuilderFor(type); //---------------Test Result ----------------------- Expect(builderType).To.Equal(typeof(SomeEntityWithBuilderBuilder)); }
public void TryFindExistingBuilderFor_WhenHaveMultipleBuilders_ShouldReturnBuilderWithClosestNamespaceMatch() { //---------------Set up test pack------------------- var entityType = typeof(AnotherEntityWithBuilder); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = GenericBuilderLocator.TryFindExistingBuilderFor(entityType); //---------------Test Result ----------------------- Expect(typeof(GenericBuilderTestArtifactBuilders.Sub1.AnotherEntityWithBuilderBuilder)) .To.Equal(result); }
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 TryFindExistingBuilderFor_WhenCannotFindExistingBuilderInAnotherAssembly_ShouldReturnNull() { //---------------Set up test pack------------------- var type = typeof(SomeEntityWithoutBuilder); //---------------Assert Precondition---------------- var existingBuilders = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => t.IsBuilderFor(type)); Expect(existingBuilders).To.Be.Empty(); //---------------Execute Test ---------------------- var builderType = GenericBuilderLocator.TryFindExistingBuilderFor(type); //---------------Test Result ----------------------- Expect(builderType).To.Be.Null(); }