public static TConfiguration Add <TConfiguration>(this ListConfiguration <TConfiguration, VirtualType> source, params Func <VirtualTypeBuilder, VirtualType>[] typeBuilders ) => source.Add( typeBuilders.Select(builder => builder(BuildRoutine.VirtualType()) ) );
public void Can_be_itself_and_object() { IType testing = BuildRoutine.VirtualType().FromBasic(); Assert.IsTrue(testing.CanBe(type.of <object>())); Assert.IsFalse(testing.CanBe(type.of <string>())); }
public void When_return_type_is_virtual__virtual_type_is_used_for_result_validation() { var vt = BuildRoutine.VirtualType().FromBasic() .Name.Set("VirtualType") .Namespace.Set("Virtual") .ToStringMethod.Set(o => o.Id) ; IMethod testing = new VirtualMethod(type.of <string>()) .Name.Set("VirtualMethod") .ReturnType.Set(vt) .Body.Set((target, _) => { if (Equals(target, "test")) { return(new VirtualObject((string)target, vt)); } return(target); }) ; var actual = testing.PerformOn("test"); Assert.AreEqual(new VirtualObject("test", vt), actual); Assert.Throws <InvalidCastException>(() => testing.PerformOn("dummy")); }
public void FullName_is_built_using_namespace_and_name() { IType testing = BuildRoutine.VirtualType().FromBasic() .Name.Set("Test") .Namespace.Set("Routine") ; Assert.AreEqual("Routine.Test", testing.FullName); }
public void Cannot_cast_to_a_real_type() { IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") ; Assert.Throws <InvalidCastException>(() => testing.Cast(testing.CreateInstance(), type.of <string>())); }
public void Methods_are_created_externally_and_added_to_type() { var mockMethod = new Mock <IMethod>(); IType testing = BuildRoutine.VirtualType().FromBasic() .Methods.Add(mockMethod.Object) ; Assert.AreEqual(1, testing.Methods.Count); Assert.AreSame(mockMethod.Object, testing.Methods[0]); }
public void Creates_virtual_object() { IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("default") ; var actual = testing.CreateInstance(); Assert.IsInstanceOf <VirtualObject>(actual); Assert.AreEqual("default", ((VirtualObject)actual).Id); }
public void By_default__to_string_returns_Id_and_virtual_type_name() { IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("default") .Namespace.Set("Namespace") .Name.Set("Name") ; var actual = testing.CreateInstance(); Assert.AreEqual("default (Namespace.Name)", actual.ToString()); }
public void Casts_a_virtual_object_to_object() { IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") ; var instance = testing.CreateInstance(); Assert.AreSame(instance, testing.Cast(instance, type.of <object>())); }
public void IsInterface_is_optional__default_is_false() { IType testing = BuildRoutine.VirtualType().FromBasic() .IsInterface.Set(true) ; Assert.IsTrue(testing.IsInterface); testing = BuildRoutine.VirtualType().FromBasic(); Assert.IsFalse(testing.IsInterface); }
public void Namespace_is_required() { IType testing = BuildRoutine.VirtualType().FromBasic() .Namespace.Set("Routine") ; Assert.AreEqual("Routine", testing.Namespace); testing = BuildRoutine.VirtualType().FromBasic(); Assert.Throws <ConfigurationException>(() => { var dummy = testing.Namespace; }); }
public void Cannot_cast_to_a_virtual_type_that_is_not_in_assignable_types() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") ; Assert.Throws <InvalidCastException>(() => testing.Cast(testing.CreateInstance(), virtualInterface)); }
public void Virtual_types_have_assignable_virtual_types() { IType testing = BuildRoutine.VirtualType().FromBasic() .Name.Set("Virtual") .Namespace.Set("Routine") .AssignableTypes.Add(vt => vt.FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true) ) ; Assert.AreEqual(1, testing.AssignableTypes.Count); Assert.AreEqual("IVirtual", testing.AssignableTypes[0].Name); }
public void Cannot_cast_a_real_object() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") .AssignableTypes.Add(virtualInterface) ; Assert.Throws <InvalidCastException>(() => testing.Cast("string", virtualInterface)); }
public void Can_be_one_of_its_assignable_types() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .Name.Set("Virtual") .Namespace.Set("Routine") .AssignableTypes.Add(virtualInterface) ; Assert.IsTrue(testing.CanBe(virtualInterface)); Assert.IsFalse(testing.CanBe(type.of <string>())); }
public void Virtual_types_support_formatting_and_equality_members() { IType testing = BuildRoutine.VirtualType().FromBasic() .Name.Set("Virtual") .Namespace.Set("Routine") ; Assert.AreEqual("Routine.Virtual", testing.ToString()); IType clone = BuildRoutine.VirtualType().FromBasic() .Name.Set("Virtual") .Namespace.Set("Routine") ; Assert.AreEqual(testing.GetHashCode(), clone.GetHashCode()); Assert.AreEqual(testing, clone); }
public void Casts_a_virtual_object_to_its_assignable_type() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") .AssignableTypes.Add(virtualInterface) ; var instance = testing.CreateInstance(); Assert.AreSame(instance, testing.Cast(instance, virtualInterface)); }
public void Not_supported_features() { IType testing = BuildRoutine.VirtualType().FromBasic(); Assert.IsNull(testing.ParentType); Assert.AreEqual(0, testing.GetCustomAttributes().Length); Assert.IsFalse(testing.IsAbstract); Assert.IsFalse(testing.IsEnum); Assert.IsFalse(testing.IsGenericType); Assert.IsFalse(testing.IsPrimitive); Assert.IsFalse(testing.IsValueType); Assert.AreEqual(type.of <object>(), testing.BaseType); Assert.AreEqual(0, testing.GetGenericArguments().Count); Assert.IsNull(testing.GetElementType()); Assert.IsNull(testing.GetParseMethod()); Assert.AreEqual(0, testing.GetEnumNames().Count); Assert.AreEqual(0, testing.GetEnumValues().Count); Assert.IsNull(testing.GetEnumUnderlyingType()); Assert.Throws <NotSupportedException>(() => testing.CreateListInstance(10)); Assert.AreEqual(0, testing.Constructors.Count); Assert.AreEqual(0, testing.Properties.Count); }
public void Virtual_types_are_public() { IType testing = BuildRoutine.VirtualType().FromBasic(); Assert.IsTrue(testing.IsPublic); }
public void Virtual_types_cannot_be_void() { IType testing = BuildRoutine.VirtualType().FromBasic(); Assert.IsFalse(testing.IsVoid); }
private VirtualType VirtualType(string name = "type", string @namespace = "namespace") => BuildRoutine.VirtualType().FromBasic().Name.Set(name).Namespace.Set(@namespace);
public static ConventionBasedCodingStyle AddTypes(this ConventionBasedCodingStyle source, params Func <VirtualTypeBuilder, VirtualType>[] typeBuilders ) => source.AddTypes(typeBuilders.Select(builder => builder(BuildRoutine.VirtualType())));