示例#1
0
        public void InstanceMethodTest()
        {
            var args = new List<ObjectInstance>();
              var method = typeof(String).GetMethod("Normalize", Type.EmptyTypes);
              var mTest = new MethodTest(new TimeSpan(), method, "TEST_STRING", args, m_StringInstance);

              Assert.That(mTest.Arguments, Is.EqualTo(args));
              Assert.That(mTest.Result, Is.EqualTo("TEST_STRING"));
              Assert.That(mTest.Method, Is.SameAs(method));
              Assert.That(mTest.Instance, Is.SameAs(m_StringInstance));
        }
 public void Setup()
 {
     m_ReturnAnIntMethod = m_TypeUnderTest.GetMethod("ReturnAnInt");
     m_StaticReturnAnIntTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_ReturnAnIntMethod, 3);
 }
        private String GetMethodTestCode(MethodTest methodTest)
        {
            StringBuilder testCode = new StringBuilder();

            var constructorInfo = methodTest.Method as ConstructorInfo;
            var methodInfo = methodTest.Method as MethodInfo;

            bool methodHasAReturnValue = (constructorInfo != null || !(methodInfo != null && methodInfo.ReturnType == typeof(void)));

            testCode.Append(TestSuiteGenerator.INDENT);
            testCode.Append(GetInvokeMethodCode(methodTest.Method, methodTest.Instance, methodTest.Arguments, methodHasAReturnValue).Replace("\r\n", "\r\n"+ TestSuiteGenerator.INDENT));
            testCode.AppendLine();

            if (methodTest.Instance != null)
            {
                String propTestCode = GetPublicFieldsAndPropertiesTestCode(methodTest.Instance.Instance);
                if (propTestCode != String.Empty)
                {
                    testCode.Append(TestSuiteGenerator.INDENT);
                    testCode.AppendLine(propTestCode);
                }
            }

            if (methodHasAReturnValue)
            {
                if(methodTest.Result != null)
                {
                    testCode.Append(TestSuiteGenerator.INDENT);
                    testCode.AppendFormat("Assert.That({0}, {1});", RESULT_NAME, ObjectInstance.NeedsConstructor(methodTest.Result.GetType()) ?  "Is.Not.Null" : String.Format("Is.EqualTo({0})",CodeWritingUtils.GetObjectCreationExpression(methodTest.Result) ));
                    testCode.AppendLine();

                    String propTestCode = GetPublicFieldsAndPropertiesTestCode(methodTest.Result, RESULT_NAME);
                    if (propTestCode != String.Empty)
                    {
                        testCode.Append(TestSuiteGenerator.INDENT);
                        testCode.AppendLine(propTestCode);
                    }
                }
                else
                {
                    testCode.Append(TestSuiteGenerator.INDENT);
                    testCode.AppendFormat("Assert.That({0}, Is.Null);", RESULT_NAME);
                    testCode.AppendLine();
                }
            }

            return testCode.ToString();
        }
        public void Setup()
        {
            m_TestCodeWriter = new NUnitUnitTestCodeWriter();
            m_TypeUnderTest = typeof(MethodTestsType);
            m_SimpleMethod = m_TypeUnderTest.GetMethod("SimpleMethod");
            m_IntReturningMethod = m_TypeUnderTest.GetMethod("IntReturningMethod");
            m_MethodTakingAnInt = m_TypeUnderTest.GetMethod("MethodTakingAnInt");
            m_MethodTakingAComplexType = m_TypeUnderTest.GetMethod("MethodTakingAComplexType");
            m_MethodReturningAComplexType = m_TypeUnderTest.GetMethod("MethodReturningAComplexType");
            m_MethodReturningNull = m_TypeUnderTest.GetMethod("MethodReturningNull");
            m_MethodCalledWithNull = m_TypeUnderTest.GetMethod("MethodCalledWithNull");

            m_TargetInstanceMeta = new ObjectInstance(m_TargetInstance, new ObjectCreationData(m_TypeUnderTest.GetConstructor(Type.EmptyTypes)));
            m_OtherTypeMeta = new ObjectInstance(m_OtherType, new ObjectCreationData(typeof(OtherType).GetConstructor(Type.EmptyTypes)));
            m_IntMeta = new ObjectInstance(1);

            m_SimpleMethodTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_SimpleMethod, instance: m_TargetInstanceMeta);

            m_IntReturningMethodTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_IntReturningMethod, 3, instance: m_TargetInstanceMeta);
            m_MethodTakingAnIntTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_MethodTakingAnInt, null, new[]{m_IntMeta}, m_TargetInstanceMeta);

            m_MethodTakingAComplexTypeTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_MethodTakingAComplexType, null, new[] { m_OtherTypeMeta }, m_TargetInstanceMeta);
            m_MethodReturningAComplexTypeTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_MethodReturningAComplexType, m_OtherType, instance: m_TargetInstanceMeta);

            m_MethodReturningNullTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_MethodReturningNull, null, instance:m_TargetInstanceMeta);
        }
示例#5
0
        public void StaticMethodTest()
        {
            var args = new List<ObjectInstance>() { m_StringInstance, m_StringInstance };
             var method = typeof(String).GetMethod("Compare", new Type[] { m_StrType, m_StrType });
             var mTest = new MethodTest(new TimeSpan(), method, 1, args);

             Assert.That(mTest.Arguments, Is.EqualTo(args));
             Assert.That(mTest.Result, Is.EqualTo(1));
             Assert.That(mTest.Method, Is.SameAs(method));
             Assert.That(mTest.Instance, Is.Null);
        }
        public void Setup()
        {
            m_TestCodeWriter = new NUnitUnitTestCodeWriter();
            m_TestType = typeof(BasicTestType);
            m_TestTypeDefaultConstructor = m_TestType.GetConstructor(Type.EmptyTypes);
            m_TestTypeInstance = new ObjectInstance(new BasicTestType(), new ObjectCreationData(m_TestTypeDefaultConstructor));
            m_ExceptionThrowingTest = new ExceptionThrowingTest(TimeSpan.FromMilliseconds(1), m_TestType.GetMethod("ExceptionThrower"), new ArgumentOutOfRangeException(), m_TestTypeInstance);

            m_ConstructorTest = new ConstructorTest(TimeSpan.FromMilliseconds(1), m_TestTypeDefaultConstructor, new BasicTestType());

            m_MethodTest = new MethodTest(TimeSpan.FromMilliseconds(1), m_TestType.GetMethod("Method1"), 5, instance:m_TestTypeInstance);
        }