Пример #1
0
 public static TConfiguration Add <TConfiguration>(this ListConfiguration <TConfiguration, VirtualType> source,
                                                   params Func <VirtualTypeBuilder, VirtualType>[] typeBuilders
                                                   ) => source.Add(
     typeBuilders.Select(builder =>
                         builder(BuildRoutine.VirtualType())
                         )
     );
Пример #2
0
        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>()));
        }
Пример #3
0
        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"));
        }
Пример #4
0
        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);
        }
Пример #5
0
        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>()));
        }
Пример #6
0
        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]);
        }
Пример #7
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);
        }
Пример #8
0
        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());
        }
Пример #9
0
        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>()));
        }
Пример #10
0
        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);
        }
Пример #11
0
        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; });
        }
Пример #12
0
        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));
        }
Пример #13
0
        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);
        }
Пример #14
0
        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));
        }
Пример #15
0
        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>()));
        }
Пример #16
0
        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);
        }
Пример #17
0
        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));
        }
Пример #18
0
        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);
        }
Пример #19
0
        public void Virtual_types_are_public()
        {
            IType testing = BuildRoutine.VirtualType().FromBasic();

            Assert.IsTrue(testing.IsPublic);
        }
Пример #20
0
        public void Virtual_types_cannot_be_void()
        {
            IType testing = BuildRoutine.VirtualType().FromBasic();

            Assert.IsFalse(testing.IsVoid);
        }
Пример #21
0
 private VirtualType VirtualType(string name = "type", string @namespace = "namespace") =>
 BuildRoutine.VirtualType().FromBasic().Name.Set(name).Namespace.Set(@namespace);
Пример #22
0
 public static ConventionBasedCodingStyle AddTypes(this ConventionBasedCodingStyle source,
                                                   params Func <VirtualTypeBuilder, VirtualType>[] typeBuilders
                                                   ) => source.AddTypes(typeBuilders.Select(builder => builder(BuildRoutine.VirtualType())));