Esempio n. 1
0
        public void ShouldBeGeneratedOnceForEveryInterface()
        {
            var provider = new EmptyClassProvider();

            var class1 = provider.GetEmptyClass(typeof(IMyService));
            var class2 = provider.GetEmptyClass <IMyService>();

            Assert.Equal(class1, class2);

            var genericClass1 = provider.GetEmptyClass <Models.Generic.IMyService <int> >();
            var genericClass2 = provider.GetEmptyClass <Models.Generic.IMyService <int> >();

            Assert.Equal(genericClass1, genericClass2);
        }
Esempio n. 2
0
        public void ShouldClassNameBeCountedForInterfaceWithTheSameNameFromDifferentNamespace()
        {
            var provider = new EmptyClassProvider();

            var class1 = provider.GetEmptyClass <Models.Namespace1.IMyServiceName>();
            var class2 = provider.GetEmptyClass <Models.Namespace2.IMyServiceName>();

            Assert.Equal("EmptyMyServiceName", class1.Name);
            Assert.Equal("EmptyMyServiceName1", class2.Name);
            var genericClass1 = provider.GetEmptyClass <Models.Generic.Namespace1.IMyServiceName <int> >();
            var genericClass2 = provider.GetEmptyClass <Models.Generic.Namespace2.IMyServiceName <int> >();

            Assert.Equal("EmptyMyServiceName2", genericClass1.Name);
            Assert.Equal("EmptyMyServiceName3", genericClass2.Name);
        }
        public static void Main(string[] args)
        {
            var emptyClassProvider = new EmptyClassProvider();

            var emptyClass    = emptyClassProvider.GetEmptyClass <IFakeScenario>();
            var emptyObject   = Activator.CreateInstance(emptyClass);
            var emptyScenario = (IFakeScenario)emptyObject;


            // 方法可正常调用,返回值是为对应类型的默认值
            emptyScenario.SetObject("这是一个对象");
            var value = emptyScenario.GetValueObject();

            Console.WriteLine(value == 0); // true
            var reference = emptyScenario.GetReferenceObject();

            Console.WriteLine(reference == null); // true

            // 异步方法总是返回一个异步操作,操作结果为对应类型的默认值
            var task = emptyScenario.AsyncSetObject("这是一个对象");

            Console.WriteLine(task.GetType() == typeof(Task <int>)); // true。对于无返回的任务,生成代码返回 Task.FromResult(0)

            var taskReturnValue = emptyScenario.AsyncSetObjectReturnValue("这是一个对象");

            Console.WriteLine(taskReturnValue.GetType() == typeof(Task <int>)); // true
            Console.WriteLine(taskReturnValue.Result == 0);                     // true

            var taskReturnObject = emptyScenario.AsyncSetObjectReturnReference("这是一个对象");

            Console.WriteLine(taskReturnObject.GetType() == typeof(Task <object>)); // true
            Console.WriteLine(taskReturnObject.Result == null);                     // true

            Action asyncAction = async() =>
            {
                // 异步操作是正常可等待的,异步结果为对应类型的默认值
                await emptyScenario.AsyncSetObject("这是一个对象");

                var returnValue = await emptyScenario.AsyncSetObjectReturnValue("这是一个对象");

                Console.WriteLine(returnValue == 0); // true

                var returnObject = await emptyScenario.AsyncSetObjectReturnReference("这是一个对象");

                Console.WriteLine(returnObject == null); // true
            };

            asyncAction();

            // 属性是语法糖,对应 getter、setter 两个方法
            emptyScenario.ValueProperty = 100;
            Console.WriteLine(emptyScenario.ValueProperty == 0); // true

            // 属性是语法糖,对应 getter、setter 两个方法
            emptyScenario.ReferenceProperty = new object();
            Console.WriteLine(emptyScenario.ReferenceProperty == null); // true

            Console.ReadKey();
        }
Esempio n. 4
0
        public void ShouldClassBeGenerated()
        {
            var provider = new EmptyClassProvider();

            var emptyClass = provider.GetEmptyClass(typeof(IMyService));

            Assert.True(typeof(IMyService).IsAssignableFrom(emptyClass));

            var emptyGenericClassWithoutArgs = provider.GetEmptyClass(typeof(Models.Generic.IMyService <>));
            var emptyGenericClassWithArgs    = emptyGenericClassWithoutArgs.MakeGenericType(typeof(int));

            Assert.True(typeof(Models.Generic.IMyService <int>).IsAssignableFrom(emptyGenericClassWithArgs));

            var emptyGenericClass = provider.GetEmptyClass(typeof(Models.Generic.IMyService <int>));

            Assert.True(typeof(Models.Generic.IMyService <int>).IsAssignableFrom(emptyGenericClass));

            var emptyNestedGenericClass = provider.GetEmptyClass(typeof(Models.Generic.MyService <int> .IMyNestedService <int, MyService>));

            Assert.True(typeof(Models.Generic.MyService <int> .IMyNestedService <int, MyService>).IsAssignableFrom(emptyNestedGenericClass));
        }
Esempio n. 5
0
        public void ShouldValidationFail()
        {
            var provider = new EmptyClassProvider();

            // could not create implementation type for class
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(MyService)));
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass <MyService>());

            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(Models.Generic.MyService <>)));

            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(Models.Generic.MyService <int>)));
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass <Models.Generic.MyService <int> >());

            // could not create implementation type for non-public interface
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(IMyServiceInternal)));
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass <IMyServiceInternal>());

            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(Models.Generic.IMyServiceInternal <>)));

            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(Models.Generic.IMyServiceInternal <int>)));
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass <Models.Generic.IMyServiceInternal <int> >());

            // could not create implementation type for public interface which cantains non-public type parameters
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(Models.Generic.MyService <int> .IMyNestedService <int, IMyServiceInternal>)));
            Assert.Throws <InvalidOperationException>(() => provider.GetEmptyClass(typeof(Models.Generic.MyService <int> .IMyNestedService <int, Models.Generic.IMyServiceInternal <int> >)));
        }