1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Diagnostics;
9
using ICSharpCode.Core;
10
using ICSharpCode.PythonBinding;
11
using ICSharpCode.Scripting.Tests.Utils;
12
using ICSharpCode.SharpDevelop.Dom;
13
using ICSharpCode.SharpDevelop.Gui;
14
using ICSharpCode.UnitTesting;
15
using NUnit.Framework;
16
using PythonBinding.Tests.Utils;
17
using UnitTesting.Tests.Utils;
19
namespace PythonBinding.Tests.Testing
22
public class PythonTestRunnerRunsSelectedTestMethodTestFixture
24
MockCSharpProject project;
25
PythonTestRunner testRunner;
26
MockProcessRunner processRunner;
27
MockTestResultsMonitor testResultsMonitor;
28
SelectedTests selectedTests;
29
MockMethod methodToTest;
30
PythonAddInOptions options;
31
MockScriptingFileService fileService;
32
StringBuilder responseFileText;
33
StringWriter responseFileStringWriter;
34
PythonStandardLibraryPath standardLibraryPath;
35
MockMessageService messageService;
44
void CreateTestRunner()
46
processRunner = new MockProcessRunner();
47
testResultsMonitor = new MockTestResultsMonitor();
48
options = new PythonAddInOptions(new Properties());
49
options.PythonFileName = @"c:\ironpython\ipy.exe";
50
fileService = new MockScriptingFileService();
51
messageService = new MockMessageService();
52
standardLibraryPath = new PythonStandardLibraryPath(@"c:\python\lib");
53
PythonTestRunnerContext context = new PythonTestRunnerContext(processRunner,
60
testRunner = new PythonTestRunner(context);
63
void CreateTestMethod()
65
project = new MockCSharpProject();
66
MockClass c = new MockClass("MyNamespace.MyTestClass");
67
methodToTest = new MockMethod(c, "MyTestMethod");
70
void RunTestsOnSelectedTestMethod()
72
fileService.SetTempFileName(@"d:\temp\tmp66.tmp");
73
CreateTemporaryResponseFileWriter();
75
selectedTests = new SelectedTests(project, null, null, methodToTest);
76
testRunner.Start(selectedTests);
79
void CreateTemporaryResponseFileWriter()
81
responseFileText = new StringBuilder();
82
responseFileStringWriter = new StringWriter(responseFileText);
83
fileService.SetTextWriter(responseFileStringWriter);
87
public void TestRunnerProcessFileNameIsIronPythonConsoleExeTakenFromAddInOptions()
89
RunTestsOnSelectedTestMethod();
91
string expectedFileName = @"c:\ironpython\ipy.exe";
92
Assert.AreEqual(expectedFileName, processRunner.CommandPassedToStartMethod);
96
public void CommandLineArgumentHasSharpDevelopTestPythonScriptAndResponseFileName()
98
AddInPathHelper helper = new AddInPathHelper("PythonBinding");
99
AddIn addin = helper.CreateDummyAddInInsideAddInTree();
100
addin.FileName = @"c:\sharpdevelop\addins\pythonbinding\pythonbinding.addin";
102
RunTestsOnSelectedTestMethod();
104
string expectedCommandLine =
105
"\"c:\\sharpdevelop\\addins\\pythonbinding\\TestRunner\\sdtest.py\" " +
106
"\"@d:\\temp\\tmp66.tmp\"";
108
Assert.AreEqual(expectedCommandLine, processRunner.CommandArgumentsPassedToStartMethod);
112
public void ResponseFileCreatedUsingTempFileName()
114
RunTestsOnSelectedTestMethod();
116
Assert.AreEqual(@"d:\temp\tmp66.tmp", fileService.CreateTextWriterInfoPassedToCreateTextWriter.FileName);
120
public void ResponseFileCreatedWithUtf8Encoding()
122
RunTestsOnSelectedTestMethod();
124
Assert.AreEqual(Encoding.UTF8, fileService.CreateTextWriterInfoPassedToCreateTextWriter.Encoding);
128
public void ResponseFileCreatedWithAppendSetToFalse()
130
RunTestsOnSelectedTestMethod();
132
Assert.IsFalse(fileService.CreateTextWriterInfoPassedToCreateTextWriter.Append);
136
public void DisposingTestRunnerDeletesTemporaryResponseFile()
138
fileService.FileNameDeleted = null;
139
RunTestsOnSelectedTestMethod();
140
testRunner.Dispose();
142
string expectedFileName = @"d:\temp\tmp66.tmp";
143
Assert.AreEqual(expectedFileName, fileService.FileNameDeleted);
147
public void DisposingTestRunnerDisposesTestResultsMonitor()
149
RunTestsOnSelectedTestMethod();
150
testRunner.Dispose();
151
Assert.IsTrue(testResultsMonitor.IsDisposeMethodCalled);
155
public void ResponseFileTextContainsPythonLibraryPathAndTestResultsFileNameAndFullyQualifiedTestMethod()
157
standardLibraryPath.Path = String.Empty;
158
options.PythonLibraryPath = @"c:\python26\lib";
159
testResultsMonitor.FileName = @"c:\temp\test-results.txt";
160
RunTestsOnSelectedTestMethod();
162
string expectedText =
163
"/p:\"c:\\python26\\lib\"\r\n" +
164
"/r:\"c:\\temp\\test-results.txt\"\r\n" +
165
"MyNamespace.MyTestClass.MyTestMethod\r\n";
167
Assert.AreEqual(expectedText, responseFileText.ToString());
171
public void ResponseFileTextDoesNotContainPythonLibraryPathIfItIsAnEmptyString()
173
options.PythonLibraryPath = String.Empty;
174
standardLibraryPath.Path = String.Empty;
175
testResultsMonitor.FileName = @"c:\temp\test-results.txt";
176
RunTestsOnSelectedTestMethod();
178
string expectedText =
179
"/r:\"c:\\temp\\test-results.txt\"\r\n" +
180
"MyNamespace.MyTestClass.MyTestMethod\r\n";
182
Assert.AreEqual(expectedText, responseFileText.ToString());
186
public void ResponseFileTextContainsPythonLibraryPathFromPythonStandardLibraryPathObjectIfNotDefinedInAddInOptions()
188
standardLibraryPath.Path = @"c:\python\lib";
189
options.PythonLibraryPath = String.Empty;
190
testResultsMonitor.FileName = @"c:\temp\test-results.txt";
191
RunTestsOnSelectedTestMethod();
193
string expectedText =
194
"/p:\"c:\\python\\lib\"\r\n" +
195
"/r:\"c:\\temp\\test-results.txt\"\r\n" +
196
"MyNamespace.MyTestClass.MyTestMethod\r\n";
198
Assert.AreEqual(expectedText, responseFileText.ToString());
202
public void ResponseFileTextWriterDisposedAfterTestsRun()
204
RunTestsOnSelectedTestMethod();
205
Assert.Throws<ObjectDisposedException>(delegate { responseFileStringWriter.Write("test"); });
209
public void ProcessRunnerWorkingDirectoryIsDirectoryContainingProject()
211
RunTestsOnSelectedTestMethod();
213
string expectedDirectory = @"c:\projects\MyTests";
214
string actualDirectory = processRunner.WorkingDirectory;
216
Assert.AreEqual(expectedDirectory, actualDirectory);
220
public void PythonTestResultReturnedFromTestFinishedEvent()
222
TestResult testResult = null;
223
testRunner.TestFinished += delegate(object source, TestFinishedEventArgs e) {
224
testResult = e.Result;
226
TestResult testResultToFire = new TestResult("test");
227
testResultsMonitor.FireTestFinishedEvent(testResultToFire);
229
Assert.IsInstanceOf(typeof(PythonTestResult), testResult);