Skip to content

Commit c45fba5

Browse files
committed
Tests for NotepadPlusWrapper
1 parent e1d1b6d commit c45fba5

3 files changed

Lines changed: 299 additions & 4 deletions

File tree

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py
211211
<ClInclude Include="..\python_tests\tests\SearchUTF8TestCase.py">
212212
<FileType>Document</FileType>
213213
</ClInclude>
214-
<ClInclude Include="..\python_tests\tests\NotepadTestCase.py">
214+
<ClInclude Include="..\python_tests\tests\NotepadWrapperTestCase.py">
215215
<FileType>Document</FileType>
216216
</ClInclude>
217217
<ClInclude Include="..\python_tests\tests\ScintillaCallbackTestCase.py">

PythonScript/project/PythonScript2010.vcxproj.filters

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,6 @@
324324
<ClInclude Include="..\src\GroupNotFoundException.h">
325325
<Filter>Header Files</Filter>
326326
</ClInclude>
327-
<ClInclude Include="..\python_tests\tests\NotepadTestCase.py">
328-
<Filter>PythonTests\Tests</Filter>
329-
</ClInclude>
330327
<ClInclude Include="..\python_tests\tests\ScintillaCallbackTestCase.py">
331328
<Filter>PythonTests\Tests</Filter>
332329
</ClInclude>
@@ -348,6 +345,9 @@
348345
<ClInclude Include="..\python_tests\tests\ScintillaWrapperTestCase.py">
349346
<Filter>PythonTests\Tests</Filter>
350347
</ClInclude>
348+
<ClInclude Include="..\python_tests\tests\NotepadWrapperTestCase.py">
349+
<Filter>PythonTests\Tests</Filter>
350+
</ClInclude>
351351
</ItemGroup>
352352
<ItemGroup>
353353
<ResourceCompile Include="..\res\PythonScript.rc">
Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import unittest
4+
import os
5+
import shlex
6+
import tempfile
7+
from Npp import *
8+
9+
class NotepadTestCase(unittest.TestCase):
10+
def setUp(self):
11+
self.files_to_delete = []
12+
13+
def tearDown(self):
14+
for file in self.files_to_delete:
15+
os.remove(file)
16+
17+
def check_file_contents(self, filename, expectedContents):
18+
f = open(filename, "rb")
19+
actualContents = f.read()
20+
f.close()
21+
self.assertEqual(actualContents, expectedContents)
22+
23+
def get_temp_filename(self):
24+
(file, filename) = tempfile.mkstemp()
25+
os.close(file)
26+
self.files_to_delete.append(filename)
27+
return filename
28+
29+
def test_setEncoding(self):
30+
notepad.new()
31+
notepad.setEncoding(BUFFERENCODING.UTF8)
32+
encoding = notepad.getEncoding()
33+
self.assertEqual(encoding, BUFFERENCODING.UTF8)
34+
notepad.setEncoding(BUFFERENCODING.ANSI)
35+
encoding = notepad.getEncoding()
36+
self.assertEqual(encoding, BUFFERENCODING.ANSI)
37+
notepad.close()
38+
39+
def test_setLangType(self):
40+
notepad.new()
41+
buffer1 = notepad.getCurrentBufferID()
42+
notepad.setLangType(LANGTYPE.C)
43+
44+
notepad.new()
45+
buffer2 = notepad.getCurrentBufferID()
46+
notepad.setLangType(LANGTYPE.PHP)
47+
48+
notepad.activateBufferID(buffer1)
49+
newBuffer1Lang = notepad.getLangType()
50+
notepad.activateBufferID(buffer2)
51+
newBuffer2Lang = notepad.getLangType()
52+
53+
notepad.close()
54+
notepad.activateBufferID(buffer1)
55+
notepad.close()
56+
57+
self.assertEqual(newBuffer1Lang, LANGTYPE.C)
58+
self.assertEqual(newBuffer2Lang, LANGTYPE.PHP)
59+
60+
def test_save(self):
61+
filename = self.get_temp_filename()
62+
notepad.new(filename)
63+
# setEncoding doesn't work when the file has a name
64+
notepad.runMenuCommand('Encoding', 'Encode in ANSI')
65+
editor.write('Hello world - save')
66+
notepad.save()
67+
notepad.close()
68+
self.check_file_contents(filename, 'Hello world - save')
69+
70+
def test_saveAs(self):
71+
notepad.new()
72+
notepad.setEncoding(BUFFERENCODING.ANSI)
73+
editor.write('Hello world - saveAs')
74+
filename = self.get_temp_filename()
75+
notepad.saveAs(filename)
76+
notepad.close()
77+
self.check_file_contents(filename, 'Hello world - saveAs')
78+
79+
80+
def test_saveAsCopy(self):
81+
notepad.new()
82+
notepad.setEncoding(BUFFERENCODING.ANSI)
83+
editor.write('Hello world - saveAsCopy')
84+
realFilename = self.get_temp_filename()
85+
copyFilename = self.get_temp_filename()
86+
notepad.saveAs(realFilename)
87+
notepad.saveAsCopy(copyFilename)
88+
editor.write('-OriginalChanged')
89+
notepad.save()
90+
notepad.close()
91+
self.check_file_contents(realFilename, 'Hello world - saveAsCopy-OriginalChanged')
92+
self.check_file_contents(copyFilename, 'Hello world - saveAsCopy')
93+
94+
def test_open(self):
95+
filename = self.get_temp_filename()
96+
f = open(filename, "w")
97+
f.write('Test - open')
98+
f.close()
99+
notepad.open(filename)
100+
text = editor.getText()
101+
notepad.close()
102+
103+
self.assertEqual(text, 'Test - open')
104+
105+
106+
def test_getCurrentView(self):
107+
notepad.new()
108+
editor.write('View 1')
109+
view1BufferID = notepad.getCurrentBufferID()
110+
notepad.new()
111+
view2BufferID = notepad.getCurrentBufferID()
112+
editor.write('View 2')
113+
notepad.runMenuCommand('Move/Clone current document', 'Move to other view')
114+
inView2 = notepad.getCurrentView()
115+
view2Text = editor.getText()
116+
notepad.activateBufferID(view1BufferID)
117+
inView1 = notepad.getCurrentView()
118+
view1Text = editor.getText()
119+
120+
# tidy up - should really be it's own case with a tearDown - TODO
121+
notepad.activateBufferID(view2BufferID)
122+
editor.setSavePoint()
123+
notepad.close()
124+
notepad.activateBufferID(view1BufferID)
125+
editor.setSavePoint()
126+
notepad.close()
127+
128+
self.assertEqual(inView2, 1)
129+
self.assertEqual(inView1, 0)
130+
self.assertEqual(view1Text, 'View 1')
131+
self.assertEqual(view2Text, 'View 2')
132+
133+
134+
def test_getCurrentLang(self):
135+
notepad.new()
136+
notepad.setLangType(LANGTYPE.PHP)
137+
lang = notepad.getCurrentLang()
138+
notepad.close()
139+
self.assertEqual(lang, LANGTYPE.PHP)
140+
141+
def test_getFiles(self):
142+
# Grab the list of files already open
143+
beforeFiles = notepad.getFiles()
144+
notepad.new()
145+
editor.write('File 1')
146+
file1 = self.get_temp_filename()
147+
notepad.saveAs(file1)
148+
bufferID1 = notepad.getCurrentBufferID()
149+
index1 = notepad.getCurrentDocIndex(0)
150+
notepad.new()
151+
editor.write('File 2')
152+
file2 = self.get_temp_filename()
153+
notepad.saveAs(file2)
154+
bufferID2 = notepad.getCurrentBufferID()
155+
index2 = notepad.getCurrentDocIndex(0)
156+
157+
files = notepad.getFiles()
158+
# remove the files that were open before we started
159+
for entry in beforeFiles:
160+
files.remove(entry)
161+
162+
# python temp files are all lowercase, so make the filenames from getFiles lowercase too
163+
lowercaseFiles = []
164+
for entry in files:
165+
lowercaseFiles.append((entry[0].lower(), entry[1], entry[2], entry[3]))
166+
167+
# clean up
168+
notepad.activateBufferID(bufferID1)
169+
editor.setSavePoint()
170+
notepad.close()
171+
notepad.activateBufferID(bufferID2)
172+
editor.setSavePoint()
173+
notepad.close()
174+
175+
self.assertIn((file1, bufferID1, index1, 0), lowercaseFiles)
176+
self.assertIn((file2, bufferID2, index2, 0), lowercaseFiles)
177+
self.assertEqual(len(files), 2)
178+
179+
180+
def test_saveLoadSession(self):
181+
# Create and open two files
182+
file1 = self.get_temp_filename()
183+
file2 = self.get_temp_filename()
184+
notepad.open(file1)
185+
editor.write('File 1 session')
186+
notepad.save()
187+
notepad.open(file2)
188+
editor.write('File 2 session')
189+
notepad.save()
190+
191+
# Save the session
192+
sessionFile = self.get_temp_filename()
193+
notepad.saveSession(sessionFile, [file1, file2])
194+
195+
# Close the files
196+
notepad.activateFile(file1)
197+
notepad.close()
198+
notepad.activateFile(file2)
199+
notepad.close()
200+
201+
# Load the session back
202+
notepad.loadSession(sessionFile)
203+
204+
# Check the files are there again
205+
notepad.activateFile(file1)
206+
file1Content = editor.getText()
207+
notepad.close()
208+
notepad.activateFile(file2)
209+
file2Content = editor.getText()
210+
notepad.close()
211+
212+
self.assertEqual(file1Content, 'File 1 session')
213+
self.assertEqual(file2Content, 'File 2 session')
214+
215+
def test_getSessionFiles(self):
216+
# Create and open two files
217+
file1 = self.get_temp_filename()
218+
file2 = self.get_temp_filename()
219+
notepad.open(file1)
220+
editor.write('File 1 session')
221+
notepad.save()
222+
notepad.open(file2)
223+
editor.write('File 2 session')
224+
notepad.save()
225+
226+
# Save the session
227+
sessionFile = self.get_temp_filename()
228+
notepad.saveSession(sessionFile, [file1, file2])
229+
sessionFiles = notepad.getSessionFiles(sessionFile)
230+
231+
notepad.activateFile(file1)
232+
notepad.close()
233+
notepad.activateFile(file2)
234+
notepad.close()
235+
236+
self.assertEqual(sessionFiles, [file1, file2])
237+
238+
def test_saveCurrentSession(self):
239+
240+
# Create and open two files
241+
file1 = self.get_temp_filename()
242+
file2 = self.get_temp_filename()
243+
notepad.open(file1)
244+
editor.write('File 1 session')
245+
notepad.save()
246+
notepad.open(file2)
247+
editor.write('File 2 session')
248+
notepad.save()
249+
250+
sessionFile = self.get_temp_filename()
251+
notepad.saveCurrentSession(sessionFile)
252+
253+
notepad.activateFile(file1)
254+
notepad.close()
255+
notepad.activateFile(file2)
256+
notepad.close()
257+
258+
sessionFiles = notepad.getSessionFiles(sessionFile)
259+
lowercaseSessionFiles = [file.lower() for file in sessionFiles]
260+
261+
self.assertIn(file1, lowercaseSessionFiles)
262+
self.assertIn(file2, lowercaseSessionFiles)
263+
264+
265+
def test_reloadFile(self):
266+
notepad.new()
267+
editor.write('Reload test')
268+
filename = self.get_temp_filename()
269+
notepad.saveAs(filename)
270+
f = open(filename, "w")
271+
f.write('Updated outside')
272+
f.close()
273+
beforeReload = editor.getText()
274+
notepad.reloadFile(filename, False)
275+
afterReload = editor.getText()
276+
notepad.close()
277+
278+
self.assertEqual(beforeReload, 'Reload test')
279+
self.assertEqual(afterReload, 'Updated outside')
280+
281+
def test_getPluginConfigDir(self):
282+
dir = notepad.getPluginConfigDir()
283+
self.assertTrue(dir.lower().endswith('plugins\\config'))
284+
285+
def test_nppCommandLineDir(self):
286+
dir = notepad.getNppDir()
287+
commandLine = notepad.getCommandLine()
288+
289+
nppExe = shlex.split(commandLine)[0]
290+
nppDirOfExe = os.path.dirname(nppExe)
291+
292+
self.assertEqual(dir, nppDirOfExe)
293+
294+
295+
suite = unittest.TestLoader().loadTestsFromTestCase(NotepadTestCase)

0 commit comments

Comments
 (0)