public void TestDataFromXls() { if (IntPtr.Size == 8) // Test always fails in 64-bit; no JET engine { return; } MethodInfo method = typeof(TestMethodCommandClass).GetMethod("TestViaXls"); TheoryAttribute attr = (TheoryAttribute)(method.GetCustomAttributes(typeof(TheoryAttribute), false))[0]; List <ITestCommand> commands = new List <ITestCommand>(attr.CreateTestCommands(Reflector.Wrap(method))); Assert.Equal(2, commands.Count); TheoryCommand command1 = Assert.IsType <TheoryCommand>(commands[0]); Assert.Equal(3, command1.Parameters.Length); Assert.Equal <object>(1D, command1.Parameters[0]); Assert.Equal <object>("Foo", command1.Parameters[1]); Assert.Equal <object>("Bar", command1.Parameters[2]); TheoryCommand command2 = Assert.IsType <TheoryCommand>(commands[1]); Assert.Equal(3, command2.Parameters.Length); Assert.Equal <object>(14D, command2.Parameters[0]); Assert.Equal <object>("Biff", command2.Parameters[1]); Assert.Equal <object>("Baz", command2.Parameters[2]); }
private static object[] GetParameterValues(TheoryCommand testCommand, ParameterInfo[] parameterInfos) { var specifiedValues = testCommand.Parameters; var optionalValues = GetOptionalValues(testCommand, parameterInfos); return(specifiedValues.Concat(optionalValues).ToArray()); }
public void TestDataFromOleDb() { if (IntPtr.Size == 8) // Test always fails in 64-bit; no JET engine { return; } string currentDirectory = Directory.GetCurrentDirectory(); try { string executable = Assembly.GetExecutingAssembly().GetLocalCodeBase(); Directory.SetCurrentDirectory(Path.GetDirectoryName(executable)); MethodInfo method = typeof(TestMethodCommandClass).GetMethod("TestViaOleDb"); TheoryAttribute attr = (TheoryAttribute)(method.GetCustomAttributes(typeof(TheoryAttribute), false))[0]; List <ITestCommand> commands = new List <ITestCommand>(attr.CreateTestCommands(Reflector.Wrap(method))); Assert.Equal(2, commands.Count); TheoryCommand command1 = Assert.IsType <TheoryCommand>(commands[0]); Assert.Equal(3, command1.Parameters.Length); Assert.Equal <object>(1D, command1.Parameters[0]); Assert.Equal <object>("Foo", command1.Parameters[1]); Assert.Equal <object>("Bar", command1.Parameters[2]); TheoryCommand command2 = Assert.IsType <TheoryCommand>(commands[1]); Assert.Equal(3, command2.Parameters.Length); Assert.Equal <object>(14D, command2.Parameters[0]); Assert.Equal <object>("Biff", command2.Parameters[1]); Assert.Equal <object>("Baz", command2.Parameters[2]); } finally { Directory.SetCurrentDirectory(currentDirectory); } }
public void NotEnoughData() { TheoryCommand command = new TheoryCommand(Reflector.Wrap(typeof(ParameterSpy).GetMethod("Method")), new object[] { 2 }); Assert.Throws <InvalidOperationException>(() => command.Execute(new ParameterSpy())); }
public void ThrowsExceptionReturnFailedResult() { MethodInfo methodInfo = typeof(TestMethodCommandClass).GetMethod("ThrowsException"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), null); Assert.Throws <InvalidOperationException>(() => command.Execute(new TestMethodCommandClass())); }
public void DisplayNameWithTooFewValues() { MethodInfo methodInfo = typeof(DummyWithAttributes).GetMethod("TheoryMethod"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), new object[] { 42 }); Assert.Equal("My display name(x: 42, y: ???)", command.DisplayName); }
public void DisplayNameWithTooManyValues() { MethodInfo methodInfo = typeof(DummyWithAttributes).GetMethod("TheoryMethod"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), new object[] { 42, 24.5, "Hello!", 'c' }); Assert.Equal("My display name(x: 42, y: 24.5, ???: \"Hello!\", ???: 'c')", command.DisplayName); }
public void StringDataWithEmbeddedNullCreatesValidXml() // CodePlex issue #9755 { string expectedXml = @"<start name=""Xunit1.Extensions.TheoryCommandTests+DummyWithAttributes.StringMethod(s: "\x0\xFFFF")"" type=""Xunit1.Extensions.TheoryCommandTests+DummyWithAttributes"" method=""StringMethod"" />"; TheoryCommand command = new TheoryCommand(Reflector.Wrap(typeof(DummyWithAttributes).GetMethod("StringMethod")), new object[] { "\0\xffff" }); Assert.Equal(expectedXml, command.ToStartXml().OuterXml); }
public void TestMethodReturnPassedResult() { MethodInfo methodInfo = typeof(TestMethodCommandClass).GetMethod("TestMethod"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), null); MethodResult result = command.Execute(new TestMethodCommandClass()); Assert.IsType <PassedResult>(result); }
private static IEnumerable <object> GetOptionalValues(TheoryCommand command, ParameterInfo[] parameterInfos) { return(Enumerable.Range(command.Parameters.Length, parameterInfos.Length - command.Parameters.Length) .ToList().Select(i => { EnsureIsOptional(parameterInfos[i]); return Type.Missing; })); }
public void StringDataWithEmbeddedNullCreatesValidXml() // CodePlex issue #9755, � is not valid XML, so we replace it with \0 { string expectedDisplayName = @"TheoryCommandTests+DummyWithAttributes.StringMethod(s: ""\0"")"; string expectedXml = @"<start name=""TheoryCommandTests+DummyWithAttributes.StringMethod(s: "\0")"" type=""TheoryCommandTests+DummyWithAttributes"" method=""StringMethod"" />"; TheoryCommand command = new TheoryCommand(Reflector.Wrap(typeof(DummyWithAttributes).GetMethod("StringMethod")), new object[] { "\0" }); Assert.Equal(expectedDisplayName, command.DisplayName); Assert.Equal(expectedXml, command.ToStartXml().OuterXml); }
public void ExecuteStubTestFixtureVerifyBeforeAfterTestCalledOnce() { MethodInfo methodInfo = typeof(DisposableSpy).GetMethod("PassedTest"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), null); DisposableSpy.ctorCalled = 0; DisposableSpy.disposeCalled = 0; ITestResult result = command.Execute(new DisposableSpy()); Assert.IsType <PassedResult>(result); }
public void TestDataFromOtherTypeProperty() { MethodInfo method = typeof(TestMethodCommandClass).GetMethod("TestViaOtherTypeProperty"); TheoryAttribute attr = (TheoryAttribute)(method.GetCustomAttributes(typeof(TheoryAttribute), false))[0]; List <ITestCommand> commands = new List <ITestCommand>(attr.CreateTestCommands(Reflector.Wrap(method))); ITestCommand command = Assert.Single(commands); TheoryCommand theoryCommand = Assert.IsType <TheoryCommand>(command); object parameter = Assert.Single(theoryCommand.Parameters); Assert.Equal(3, parameter); }
public void ExecuteCreatesClassAndRunsTest() { MethodInfo methodInfo = typeof(InstrumentedSpy).GetMethod("PassedTest"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), null); InstrumentedSpy.ctorCounter = 0; InstrumentedSpy.passedTestCounter = 0; command.Execute(new InstrumentedSpy()); Assert.Equal(1, InstrumentedSpy.ctorCounter); Assert.Equal(1, InstrumentedSpy.passedTestCounter); }
public void PassesParametersToTest() { MethodInfo methodInfo = typeof(SpyWithDataPassed).GetMethod("Test"); TheoryCommand command = new TheoryCommand(Reflector.Wrap(methodInfo), new object[] { 42, 24.5, "foo" }); SpyWithDataPassed.X = 0; SpyWithDataPassed.Y = 0.0; SpyWithDataPassed.Z = null; command.Execute(new SpyWithDataPassed()); Assert.Equal(42, SpyWithDataPassed.X); Assert.Equal(24.5, SpyWithDataPassed.Y); Assert.Equal("foo", SpyWithDataPassed.Z); }
public void TruncatesVeryLongStrings() { StringBuilder sb = new StringBuilder(500); for (int idx = 0; idx < 50; idx++) { sb.Append("----=----|"); } TheoryCommand command = new TheoryCommand(Reflector.Wrap(typeof(ParameterSpy).GetMethod("Method")), new object[] { 2, sb.ToString() }); MethodResult result = command.Execute(new ParameterSpy()); Assert.IsType <PassedResult>(result); Assert.Equal(@"Xunit1.Extensions.TheoryCommandTests+ParameterSpy.Method(x: 2, y: ""----=----|----=----|----=----|----=----|----=----|""...)", result.DisplayName); }
public void ExceptionThrownWhenInvokingTheoryCommandProperlyReported() { var methodInfo = typeof(TheoryCommandTests.TestMethodCommandClass).GetMethod("ThrowsException"); var command = new TheoryCommand(Reflector.Wrap(methodInfo), null); Exception exception = Record.Exception(() => command.Execute(new TheoryCommandTests.TestMethodCommandClass())); // If you get a test failure here, then there's another missing instance of "throw;" where there // is a call to RethrowWithNoStackTraceLoss. Specifically, for this test, it's in TheoryCommand.Execute // Again, it should look like: // // catch (TargetInvocationException ex) // { // ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException); // throw; // <---- New line // } if (exception == null || exception.GetType() != typeof(TargetInvocationException)) { throw new ExceptionNotBeingRethrownException("TheoryCommand.Execute"); } }
public void ResolvedGenericTypeIsIncludedInDisplayName() { MethodInfo method = typeof(TestMethodCommandClass).GetMethod("GenericTest"); TheoryAttribute attr = (TheoryAttribute)(method.GetCustomAttributes(typeof(TheoryAttribute), false))[0]; List <ITestCommand> commands = new List <ITestCommand>(attr.CreateTestCommands(Reflector.Wrap(method))); Assert.Equal(4, commands.Count); TheoryCommand command1 = Assert.IsType <TheoryCommand>(commands[0]); Assert.Equal(@"Xunit1.Extensions.TheoryAttributeTests+TestMethodCommandClass.GenericTest<Int32>(value: 42)", command1.DisplayName); TheoryCommand command2 = Assert.IsType <TheoryCommand>(commands[1]); Assert.Equal(@"Xunit1.Extensions.TheoryAttributeTests+TestMethodCommandClass.GenericTest<String>(value: ""Hello, world!"")", command2.DisplayName); TheoryCommand command3 = Assert.IsType <TheoryCommand>(commands[2]); Assert.Equal(@"Xunit1.Extensions.TheoryAttributeTests+TestMethodCommandClass.GenericTest<Int32[]>(value: System.Int32[])", command3.DisplayName); // TODO: Would like to see @"TheoryAttributeTests+TestMethodCommandClass.GenericTest<Int32[]>(value: Int32[] { 1, 2, 3 })" TheoryCommand command4 = Assert.IsType <TheoryCommand>(commands[3]); Assert.Equal(@"Xunit1.Extensions.TheoryAttributeTests+TestMethodCommandClass.GenericTest<List<String>>(value: System.Collections.Generic.List`1[System.String])", command4.DisplayName); // TODO: Would like to see @"TheoryAttributeTests+TestMethodCommandClass.GenericTest<List<String>>(value: List<String> { ""a"", ""b"", ""c"" })" }
public PrefixedTreesTheoryCommand(TheoryCommand command, IMethodInfo method, string testCaseDisplayName) : base(method, string.Format("{0} {1}", command.DisplayName, testCaseDisplayName), command.Timeout) { this.command = command; }
public void ExecuteSetsDisplayNameBeforeInvokingActualMethod() { var testMethod = Reflector.Wrap( new Action(() => { throw new InvalidOperationException(); }).Method); var sut = new ParameterizedCommand(new ParameterizedCommandContext( testMethod, Mocked.Of<ISpecimenBuilderFactory>(), Enumerable.Empty<object>())); var expectecd = new TheoryCommand(testMethod, new object[0]).DisplayName; Assert.Throws<InvalidOperationException>( () => sut.Execute(Activator.CreateInstance(testMethod.Class.Type))); Assert.Equal(expectecd, sut.DisplayName); }
public ReleaseCommand(TheoryCommand theory, IMethodInfo method) : base(method, MethodUtility.GetDisplayName(method), MethodUtility.GetTimeoutParameter(method)) { this.theory = theory; }
public void ExecuteSetsCorrectDisplayName() { // Fixture setup var arguments = new object[] { "1", 1 }; var testMethod = Reflector.Wrap(new Action<string, int>((x, y) => { }).Method); var testObject = new object(); var context = Mocked.Of<ITestCommandContext>(x => x.TestMethod == testMethod && x.GetArguments(It.IsAny<ITestMethodContext>()) == arguments); var sut = new ParameterizedCommand(context); var expectecd = new TheoryCommand(testMethod, arguments).DisplayName; // Exercise system sut.Execute(testObject); // Verify outcome Assert.Equal(expectecd, sut.DisplayName); }
public void SettingTheoryTimeoutSetsTimeout() { TheoryCommand command = new TheoryCommand(Reflector.Wrap(typeof(DummyWithAttributes).GetMethod("TimeoutMethod")), null); Assert.Equal(153, command.Timeout); }