~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Python/PythonBinding/Test/Testing/PythonTestRunnerRunsSelectedTestMethodTestFixture.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.IO;
 
7
using System.Text;
 
8
 
 
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;
 
18
 
 
19
namespace PythonBinding.Tests.Testing
 
20
{
 
21
        [TestFixture]
 
22
        public class PythonTestRunnerRunsSelectedTestMethodTestFixture
 
23
        {
 
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;
 
36
                
 
37
                [SetUp]
 
38
                public void Init()
 
39
                {
 
40
                        CreateTestRunner();
 
41
                        CreateTestMethod();
 
42
                }
 
43
                
 
44
                void CreateTestRunner()
 
45
                {
 
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, 
 
54
                                testResultsMonitor, 
 
55
                                messageService,
 
56
                                options,
 
57
                                standardLibraryPath,
 
58
                                fileService);
 
59
                        
 
60
                        testRunner = new PythonTestRunner(context);
 
61
                }
 
62
                
 
63
                void CreateTestMethod()
 
64
                {
 
65
                        project = new MockCSharpProject();
 
66
                        MockClass c = new MockClass("MyNamespace.MyTestClass");
 
67
                        methodToTest = new MockMethod(c, "MyTestMethod");
 
68
                }
 
69
                
 
70
                void RunTestsOnSelectedTestMethod()
 
71
                {
 
72
                        fileService.SetTempFileName(@"d:\temp\tmp66.tmp");
 
73
                        CreateTemporaryResponseFileWriter();
 
74
                        
 
75
                        selectedTests = new SelectedTests(project, null, null, methodToTest);
 
76
                        testRunner.Start(selectedTests);
 
77
                }
 
78
                
 
79
                void CreateTemporaryResponseFileWriter()
 
80
                {
 
81
                        responseFileText = new StringBuilder();
 
82
                        responseFileStringWriter = new StringWriter(responseFileText);
 
83
                        fileService.SetTextWriter(responseFileStringWriter);
 
84
                }
 
85
                
 
86
                [Test]
 
87
                public void TestRunnerProcessFileNameIsIronPythonConsoleExeTakenFromAddInOptions()
 
88
                {
 
89
                        RunTestsOnSelectedTestMethod();
 
90
                        
 
91
                        string expectedFileName = @"c:\ironpython\ipy.exe";
 
92
                        Assert.AreEqual(expectedFileName, processRunner.CommandPassedToStartMethod);
 
93
                }
 
94
                
 
95
                [Test]
 
96
                public void CommandLineArgumentHasSharpDevelopTestPythonScriptAndResponseFileName()
 
97
                {
 
98
                        AddInPathHelper helper = new AddInPathHelper("PythonBinding");
 
99
                        AddIn addin = helper.CreateDummyAddInInsideAddInTree();
 
100
                        addin.FileName = @"c:\sharpdevelop\addins\pythonbinding\pythonbinding.addin";
 
101
                        
 
102
                        RunTestsOnSelectedTestMethod();
 
103
                        
 
104
                        string expectedCommandLine =
 
105
                                "\"c:\\sharpdevelop\\addins\\pythonbinding\\TestRunner\\sdtest.py\" " +
 
106
                                "\"@d:\\temp\\tmp66.tmp\"";
 
107
                        
 
108
                        Assert.AreEqual(expectedCommandLine, processRunner.CommandArgumentsPassedToStartMethod);
 
109
                }
 
110
                
 
111
                [Test]
 
112
                public void ResponseFileCreatedUsingTempFileName()
 
113
                {
 
114
                        RunTestsOnSelectedTestMethod();
 
115
                        
 
116
                        Assert.AreEqual(@"d:\temp\tmp66.tmp", fileService.CreateTextWriterInfoPassedToCreateTextWriter.FileName);
 
117
                }
 
118
                
 
119
                [Test]
 
120
                public void ResponseFileCreatedWithUtf8Encoding()
 
121
                {
 
122
                        RunTestsOnSelectedTestMethod();
 
123
                        
 
124
                        Assert.AreEqual(Encoding.UTF8, fileService.CreateTextWriterInfoPassedToCreateTextWriter.Encoding);
 
125
                }
 
126
                
 
127
                [Test]
 
128
                public void ResponseFileCreatedWithAppendSetToFalse()
 
129
                {
 
130
                        RunTestsOnSelectedTestMethod();
 
131
                        
 
132
                        Assert.IsFalse(fileService.CreateTextWriterInfoPassedToCreateTextWriter.Append);
 
133
                }
 
134
                
 
135
                [Test]
 
136
                public void DisposingTestRunnerDeletesTemporaryResponseFile()
 
137
                {
 
138
                        fileService.FileNameDeleted = null;
 
139
                        RunTestsOnSelectedTestMethod();
 
140
                        testRunner.Dispose();
 
141
                        
 
142
                        string expectedFileName = @"d:\temp\tmp66.tmp";
 
143
                        Assert.AreEqual(expectedFileName, fileService.FileNameDeleted);
 
144
                }
 
145
                
 
146
                [Test]
 
147
                public void DisposingTestRunnerDisposesTestResultsMonitor()
 
148
                {
 
149
                        RunTestsOnSelectedTestMethod();
 
150
                        testRunner.Dispose();
 
151
                        Assert.IsTrue(testResultsMonitor.IsDisposeMethodCalled);
 
152
                }
 
153
                
 
154
                [Test]
 
155
                public void ResponseFileTextContainsPythonLibraryPathAndTestResultsFileNameAndFullyQualifiedTestMethod()
 
156
                {
 
157
                        standardLibraryPath.Path = String.Empty;
 
158
                        options.PythonLibraryPath = @"c:\python26\lib";
 
159
                        testResultsMonitor.FileName = @"c:\temp\test-results.txt";
 
160
                        RunTestsOnSelectedTestMethod();
 
161
                        
 
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";
 
166
                        
 
167
                        Assert.AreEqual(expectedText, responseFileText.ToString());
 
168
                }
 
169
                
 
170
                [Test]
 
171
                public void ResponseFileTextDoesNotContainPythonLibraryPathIfItIsAnEmptyString()
 
172
                {
 
173
                        options.PythonLibraryPath = String.Empty;
 
174
                        standardLibraryPath.Path = String.Empty;
 
175
                        testResultsMonitor.FileName = @"c:\temp\test-results.txt";
 
176
                        RunTestsOnSelectedTestMethod();
 
177
                        
 
178
                        string expectedText =
 
179
                                "/r:\"c:\\temp\\test-results.txt\"\r\n" +
 
180
                                "MyNamespace.MyTestClass.MyTestMethod\r\n";
 
181
                        
 
182
                        Assert.AreEqual(expectedText, responseFileText.ToString());
 
183
                }
 
184
                
 
185
                [Test]
 
186
                public void ResponseFileTextContainsPythonLibraryPathFromPythonStandardLibraryPathObjectIfNotDefinedInAddInOptions()
 
187
                {
 
188
                        standardLibraryPath.Path = @"c:\python\lib";
 
189
                        options.PythonLibraryPath = String.Empty;
 
190
                        testResultsMonitor.FileName = @"c:\temp\test-results.txt";
 
191
                        RunTestsOnSelectedTestMethod();
 
192
                        
 
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";
 
197
                        
 
198
                        Assert.AreEqual(expectedText, responseFileText.ToString());
 
199
                }
 
200
                
 
201
                [Test]
 
202
                public void ResponseFileTextWriterDisposedAfterTestsRun()
 
203
                {
 
204
                        RunTestsOnSelectedTestMethod();
 
205
                        Assert.Throws<ObjectDisposedException>(delegate { responseFileStringWriter.Write("test"); });
 
206
                }
 
207
                
 
208
                [Test]
 
209
                public void ProcessRunnerWorkingDirectoryIsDirectoryContainingProject()
 
210
                {
 
211
                        RunTestsOnSelectedTestMethod();
 
212
                        
 
213
                        string expectedDirectory = @"c:\projects\MyTests";
 
214
                        string actualDirectory = processRunner.WorkingDirectory;
 
215
                        
 
216
                        Assert.AreEqual(expectedDirectory, actualDirectory);
 
217
                }
 
218
                
 
219
                [Test]
 
220
                public void PythonTestResultReturnedFromTestFinishedEvent()
 
221
                {
 
222
                        TestResult testResult = null;
 
223
                        testRunner.TestFinished += delegate(object source, TestFinishedEventArgs e) { 
 
224
                                testResult = e.Result;
 
225
                        };
 
226
                        TestResult testResultToFire = new TestResult("test");
 
227
                        testResultsMonitor.FireTestFinishedEvent(testResultToFire);
 
228
                        
 
229
                        Assert.IsInstanceOf(typeof(PythonTestResult), testResult);
 
230
                }
 
231
        }
 
232
}