public void ExecuteForClassInitializeThrowingExceptionShouldReturnUnitTestResultWithFailedOutcome() { // Arrange. var tai = new TestAssemblyInfo(typeof(DummyTestClass).Assembly); var constructorInfo = typeof(DummyTestClass).GetConstructors().Single(); var classAttribute = new UTF.TestClassAttribute(); var testContextProperty = typeof(DummyTestClass).GetProperty("TestContext"); var tci = new TestClassInfo( type: typeof(DummyTestClass), constructor: constructorInfo, testContextProperty: testContextProperty, classAttribute: classAttribute, parent: tai) { ClassInitializeMethod = typeof(TestMethodRunnerTests).GetMethod( "InitMethodThrowingException", BindingFlags.Static | BindingFlags.NonPublic) }; var testMethodInfo = new TestableTestmethodInfo(this.methodInfo, tci, this.testMethodOptions, () => { throw new Exception("DummyException"); }); var testMethodRunner = new TestMethodRunner(testMethodInfo, this.testMethod, this.testContextImplementation, false); // Act. var results = testMethodRunner.Execute(); // Assert. Assert.AreEqual(AdapterTestOutcome.Failed, results[0].Outcome); StringAssert.Contains(results[0].ErrorMessage, "System.ArgumentException: Value does not fall within the expected range."); }
public void GetTemplateTest() { testClassInfo = generator.GetTemplate(code); Assert.IsNotNull(testClassInfo); generatedRoot = CSharpSyntaxTree.ParseText(testClassInfo.Content).GetRoot(); List <ClassDeclarationSyntax> generatedClasses = new List <ClassDeclarationSyntax>(generatedRoot.DescendantNodes().OfType <ClassDeclarationSyntax>()); List <ClassDeclarationSyntax> sourceClasses = new List <ClassDeclarationSyntax>(root.DescendantNodes().OfType <ClassDeclarationSyntax>()); Assert.AreEqual(sourceClasses.Count, generatedClasses.Count); for (int i = 0; i < sourceClasses.Count; i++) { ClassDeclarationSyntax sourceClass = sourceClasses[i]; ClassDeclarationSyntax generatedClass = generatedClasses[i]; List <MethodDeclarationSyntax> sourceMethods = new List <MethodDeclarationSyntax>( sourceClass.DescendantNodes().OfType <MethodDeclarationSyntax>() .Where(method => method.Modifiers. Any(modifer => modifer.ToString() == "public"))); List <MethodDeclarationSyntax> generatedMethods = new List <MethodDeclarationSyntax>( generatedClass.DescendantNodes().OfType <MethodDeclarationSyntax>() .Where(method => method.Modifiers. Any(modifer => modifer.ToString() == "public"))); Assert.AreEqual(sourceMethods.Count, generatedMethods.Count); for (int j = 0; j < sourceMethods.Count; j++) { MethodDeclarationSyntax sourceMethod = sourceMethods[j]; MethodDeclarationSyntax generatedMethod = generatedMethods[j]; Assert.IsTrue(generatedMethod.Identifier.ToString().Contains(sourceMethod.Identifier.ToString())); } } }
public static void OutTask() { Console.WriteLine("----Ковариантность и Контравариантность----\n" + "Ковариантность делегата предполагает, что возвращаемым типом может быть более производный тип.\n" + "Контрвариантность делегата предполагает, что типом параметра может быть более универсальный тип.\n"); Console.WriteLine("Пример ковариантности"); TestGenerator testGenerator; testGenerator = BuildTest; // ковариантность UnitTest test = testGenerator(1, "1+1=", "2"); TestClass testNormal = (TestClass)testGenerator(1, "1+1=", "2"); Console.WriteLine("Базовый класс имеет в себе только 2 поля и 1 метод:"); Console.WriteLine(test.Number); Console.WriteLine(test.Condition); Console.WriteLine(test.TestCondition()); Console.WriteLine("Наследуемый класс имеет в себе 3 поля и 2 метода:"); Console.WriteLine(testNormal.Number); Console.WriteLine(testNormal.Condition); Console.WriteLine(testNormal.Answer); Console.WriteLine(testNormal.TestCondition()); Console.WriteLine(testNormal.TestAnswer()); Console.WriteLine("Пример контравариантности"); TestClassInfo info = TestInfo; info(testNormal); }
protected UsingDirectiveSyntax[] CreateClassUsings(TestClassInfo typeInfo, List <UsingDirectiveSyntax> fileUsings) { return(new List <UsingDirectiveSyntax>(fileUsings) { UsingDirective(IdentifierName(typeInfo.Namespace)) }.ToArray()); }
protected MemberDeclarationSyntax CreateTestClassWithNamespaceDeclaration(TestClassInfo typeInfo) { return(NamespaceDeclaration( IdentifierName(typeInfo.Namespace + ".Test")) .WithMembers( SingletonList <MemberDeclarationSyntax>( CreateClassDeclaration(typeInfo)))); }
/// <summary> /// Gets the class cleanup lifecycle for the class, if set. /// </summary> /// <param name="classInfo">The class to inspect.</param> /// <returns>Returns <see cref="ClassCleanupBehavior"/> if provided, otherwise <c>null</c>.</returns> internal virtual ClassCleanupBehavior?GetClassCleanupBehavior(TestClassInfo classInfo) { if (classInfo.ClassCleanupMethod == null) { return(null); } var sequencingAttribute = GetCustomAttributes(classInfo.ClassCleanupMethod, typeof(ClassCleanupAttribute), true)?.FirstOrDefault() as ClassCleanupAttribute; return(sequencingAttribute?.CleanupBehavior); }
public TestMethodRunnerTests() { var constructorInfo = typeof(DummyTestClass).GetConstructors().Single(); this.methodInfo = typeof(DummyTestClass).GetMethods().Single(m => m.Name.Equals("DummyTestMethod")); var classAttribute = new UTF.TestClassAttribute(); this.testMethodAttribute = new UTF.TestMethodAttribute(); var testContextProperty = typeof(DummyTestClass).GetProperty("TestContext"); var testAssemblyInfo = new TestAssemblyInfo(typeof(DummyTestClass).Assembly); this.testMethod = new TestMethod("dummyTestName", "dummyClassName", "dummyAssemblyName", false); this.testContextImplementation = new TestContextImplementation(this.testMethod, new StringWriter(), new Dictionary <string, object>()); this.testClassInfo = new TestClassInfo( type: typeof(DummyTestClass), constructor: constructorInfo, testContextProperty: testContextProperty, classAttribute: classAttribute, parent: testAssemblyInfo); this.globaltestMethodOptions = new TestMethodOptions() { Timeout = 3600 * 1000, Executor = this.testMethodAttribute, TestContext = this.testContextImplementation, ExpectedException = null }; var globalTestMethodInfo = new TestMethodInfo( this.methodInfo, this.testClassInfo, this.globaltestMethodOptions); var testMethodInfo = new TestableTestmethodInfo(this.methodInfo, this.testClassInfo, this.testMethodOptions, null); this.globalTestMethodRunner = new TestMethodRunner(globalTestMethodInfo, this.testMethod, this.testContextImplementation, false); this.testMethodOptions = new TestMethodOptions() { Timeout = 200, Executor = this.testMethodAttribute, TestContext = this.testContextImplementation, ExpectedException = null }; this.mockReflectHelper = new Mock <ReflectHelper>(); // Reset test hooks DummyTestClass.TestConstructorMethodBody = () => { }; DummyTestClass.TestContextSetterBody = value => { }; DummyTestClass.TestInitializeMethodBody = value => { }; DummyTestClass.TestMethodBody = instance => { }; DummyTestClass.TestCleanupMethodBody = value => { }; }
protected FieldDeclarationSyntax CreateTestClassInstanceFieldDeclaration(TestClassInfo classInfo) { return(FieldDeclaration( VariableDeclaration( IdentifierName(classInfo.Name)) .WithVariables( SingletonSeparatedList <VariableDeclaratorSyntax>( VariableDeclarator( Identifier(CreateVariableName(classInfo.Name, true)))))) .WithModifiers( TokenList( Token(SyntaxKind.PrivateKeyword)))); }
protected ExpressionStatementSyntax CreateTestClassInitializeExpression(TestClassInfo classInfo) { return(ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName(CreateVariableName(classInfo.Name, true)), ObjectCreationExpression( IdentifierName(classInfo.Name)) .WithArgumentList( ArgumentList( SeparatedList <ArgumentSyntax>( CreateArguments(classInfo.Constructor.Parameters))))))); }
protected TestClassInfo CreateClassInfo(ClassDeclarationSyntax classDeclaration) { TestClassInfo typeInfo = new TestClassInfo(classDeclaration.Identifier.ValueText, ((NamespaceDeclarationSyntax)classDeclaration.Parent).Name.ToString(), GetMaxedConstructor(classDeclaration)); foreach (MethodDeclarationSyntax methodDeclaration in classDeclaration.DescendantNodes() .OfType <MethodDeclarationSyntax>() .Where((methodDeclaration) => methodDeclaration.Modifiers.Any((modifier) => modifier.IsKind(SyntaxKind.PublicKeyword)))) { typeInfo.Methods.Add(CreateMethodInfo(methodDeclaration)); } return(typeInfo); }
public TestClassInfoTests() { this.testClassType = typeof(DummyTestClass); this.testClassConstructor = this.testClassType.GetConstructors().First(); this.testContextProperty = this.testClassType.GetProperties().First(); this.testClassAttribute = (UTF.TestClassAttribute) this.testClassType.GetCustomAttributes().First(); this.testAssemblyInfo = new TestAssemblyInfo(); this.testClassInfo = new TestClassInfo( this.testClassType, this.testClassConstructor, this.testContextProperty, this.testClassAttribute, this.testAssemblyInfo); this.testContext = new Mock <UTFExtension.TestContext>().Object; }
protected ClassDeclarationSyntax CreateClassDeclaration(TestClassInfo typeInfo) { return(ClassDeclaration(typeInfo.Name + "Test") .WithAttributeLists( SingletonList <AttributeListSyntax>( AttributeList( SingletonSeparatedList <AttributeSyntax>( Attribute( IdentifierName("TestClass")))))) .WithModifiers( TokenList( Token(SyntaxKind.PublicKeyword))) .WithMembers( List <MemberDeclarationSyntax>( new MemberDeclarationSyntax[] { CreateTestClassInstanceFieldDeclaration(typeInfo).WithSemicolonToken(emptyLineToken), CreateTestInitializeMethodDeclaration(typeInfo) }.Concat(typeInfo.Methods.Select((methodInfo) => CreateTestMethodDeclaration(methodInfo, typeInfo)))))); }
protected MethodDeclarationSyntax CreateTestInitializeMethodDeclaration(TestClassInfo classInfo) { return(MethodDeclaration( PredefinedType( Token(SyntaxKind.VoidKeyword)), Identifier("TestInitialize")) .WithAttributeLists( SingletonList <AttributeListSyntax>( AttributeList( SingletonSeparatedList <AttributeSyntax>( Attribute( IdentifierName("TestInitialize")))))) .WithModifiers( TokenList( Token(SyntaxKind.PublicKeyword))) .WithBody( Block( classInfo.Constructor.Parameters.Select((parameter) => CreateVariableInitializeExpression(parameter)) .Concat(new StatementSyntax[] { CreateTestClassInitializeExpression(classInfo) }) ))); }
public TestMethodRunnerTests() { var constructorInfo = typeof(DummyTestClass).GetConstructors().Single(); this.methodInfo = typeof(DummyTestClass).GetMethods().Single(m => m.Name.Equals("DummyTestMethod")); var classAttribute = new UTF.TestClassAttribute(); this.testMethodAttribute = new UTF.TestMethodAttribute(); var testContextProperty = typeof(DummyTestClass).GetProperty("TestContext"); var testAssemblyInfo = new TestAssemblyInfo(); this.testMethod = new TestMethod("dummyTestName", "dummyClassName", "dummyAssemblyName", false); this.testContextImplementation = new TestContextImplementation(this.testMethod, null, new Dictionary <string, object>()); this.testClassInfo = new TestClassInfo( type: typeof(DummyTestClass), constructor: constructorInfo, testContextProperty: testContextProperty, classAttribute: classAttribute, parent: testAssemblyInfo); var globalTestMethodInfo = new TestMethodInfo( this.methodInfo, timeout: 3600 * 1000, executor: this.testMethodAttribute, expectedException: null, parent: this.testClassInfo, testContext: this.testContextImplementation); this.globalTestMethodRunner = new TestMethodRunner(globalTestMethodInfo, this.testMethod, this.testContextImplementation, false); // Reset test hooks DummyTestClass.TestConstructorMethodBody = () => { }; DummyTestClass.TestContextSetterBody = value => { }; DummyTestClass.TestInitializeMethodBody = value => { }; DummyTestClass.TestMethodBody = instance => { }; DummyTestClass.TestCleanupMethodBody = value => { }; }
protected MethodDeclarationSyntax CreateTestMethodDeclaration(TestMethodInfo methodInfo, TestClassInfo classInfo) { List <LocalDeclarationStatementSyntax> arrangeBody = methodInfo.Parameters.Select((parameter) => CreateVariableInitializeExpression(parameter)).ToList(); if (arrangeBody.Count != 0) { arrangeBody[arrangeBody.Count - 1] = arrangeBody[arrangeBody.Count - 1].WithSemicolonToken(emptyLineToken); } List <StatementSyntax> actAssertBody = new List <StatementSyntax>(); if (methodInfo.ReturnType.Typename != "void") { actAssertBody.Add(CreateActualDeclaration(methodInfo, classInfo).WithSemicolonToken(emptyLineToken)); actAssertBody.Add(CreateExpectedDeclaration(methodInfo.ReturnType)); actAssertBody.Add(CreateAreEqualExpression(methodInfo.ReturnType)); } actAssertBody.Add(failExpression); return(MethodDeclaration( PredefinedType( Token(SyntaxKind.VoidKeyword)), Identifier(methodInfo.Name + "Test")) .WithAttributeLists( SingletonList <AttributeListSyntax>( AttributeList( SingletonSeparatedList <AttributeSyntax>( Attribute( IdentifierName("TestMethod")))))) .WithModifiers( TokenList( Token(SyntaxKind.PublicKeyword))) .WithBody( Block( arrangeBody.Concat(actAssertBody)))); }
protected LocalDeclarationStatementSyntax CreateActualDeclaration(TestMethodInfo methodInfo, TestClassInfo classInfo) { return(LocalDeclarationStatement( VariableDeclaration( IdentifierName(methodInfo.ReturnType.Typename)) .WithVariables( SingletonSeparatedList <VariableDeclaratorSyntax>( VariableDeclarator( Identifier(actualVariableName)) .WithInitializer( EqualsValueClause( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName(CreateVariableName(classInfo.Name, true)), IdentifierName(methodInfo.Name))) .WithArgumentList( ArgumentList( SeparatedList <ArgumentSyntax>( CreateArguments(methodInfo.Parameters)))))))))); }
internal TestableTestmethodInfo(MethodInfo testMethod, TestClassInfo parent, TestMethodOptions testMethodOptions, Func <UTF.TestResult> invoke) : base(testMethod, parent, testMethodOptions) { this.invokeTest = invoke; }
internal TestableTestmethodInfo(MethodInfo testMethod, int timeout, UTF.TestMethodAttribute executor, TestClassInfo parent, TestContextImplementation testContext, Func <UTF.TestResult> invoke) : base(testMethod, timeout, executor, null, parent, testContext) { this.invokeTest = invoke; }