This repository was archived by the owner on Jan 1, 2021. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_coffeescript.py
More file actions
Latest commit
executable file
·141 lines (114 loc) · 4.36 KB
/
Copy pathtest_coffeescript.py
File metadata and controls
executable file
·141 lines (114 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from __future__ importunicode_literals, print_function
importsys
importos
importio
importtempfile
fromitertoolsimportproduct
ifsys.version_info< (2, 7):
importunittest2asunittest
else:
importunittest
importdoctest
importexecjs
importcoffeescript
coffee_code="""
# このコメントはasciiで表現できない文字列です(This is a non-ascii comment)
helloworld = "こんにちは世界"
add = (x, y) ->
x + y
"""
hello="こんにちは"
world="世界"
helloworld="こんにちは世界"
splitted_coffee_code= ["""
# このコメントはasciiで表現できない文字列です(This is a non-ascii comment)
helloworld = "こんにちは世界"
""",
"""
add = (x, y) ->
x + y
"""
]
classCoffeeScriptTest(unittest.TestCase):
defsetUp(self):
self.encodings="shift-jis utf-8 euc-jp".split()
defassertExprsSuccess(self, ctx):
self.assertEqual(ctx.call("add", 1, 2), 3)
self.assertEqual(ctx.call("add", hello, world), helloworld)
self.assertEqual(ctx.eval("helloworld"), helloworld)
defassertExprsFail(self, ctx):
withself.assertRaises(execjs.ProgramError):
ctx.call("add", 1, 2)
withself.assertRaises(execjs.ProgramError):
ctx.call("add", hello, world)
withself.assertRaises(execjs.ProgramError):
ctx.eval("helloworld")
deftest_compile(self):
# test bare=True
jscode=coffeescript.compile(coffee_code, bare=True)
ctx=execjs.compile(jscode)
self.assertExprsSuccess(ctx)
# test bare=False
jscode=coffeescript.compile(coffee_code, bare=False)
ctx=execjs.compile(jscode)
self.assertExprsFail(ctx)
defassert_compile_file_success(self, filename, encoding, bare):
jscode=coffeescript.compile_file(filename, encoding=encoding, bare=bare)
ctx=execjs.compile(jscode)
self.assertExprsSuccess(ctx)
defassert_compile_file_fail(self, filename, encoding, bare):
jscode=coffeescript.compile_file(filename, encoding=encoding, bare=bare)
ctx=execjs.compile(jscode)
self.assertExprsFail(ctx)
defassert_compile_file_decode_error(self, filename, encoding, bare):
withself.assertRaises(UnicodeDecodeError):
coffeescript.compile_file(filename, encoding=encoding, bare=bare)
defwrite_temp_files(self, strings, encoding):
paths= []
forsinstrings:
(fd, path) =tempfile.mkstemp()
os.close(fd)
withio.open(path, "w", encoding=encoding) asfp:
fp.write(s)
paths.append(path)
returnpaths
defremove_files(self, paths):
forpinpaths:
os.remove(p)
deftest_compile_files(self):
forencodinginself.encodings:
paths=self.write_temp_files([coffee_code], encoding)
try:
filename=paths[0]
self.assert_compile_file_success(filename, encoding, True)
self.assert_compile_file_fail(filename, encoding, False)
forwrong_encodinginset(self.encodings) -set([encoding]):
self.assert_compile_file_decode_error(
filename, wrong_encoding, True)
self.assert_compile_file_decode_error(
filename, wrong_encoding, False)
finally:
self.remove_files(paths)
deftest_compile_splitted_files(self):
forencodinginself.encodings:
paths=self.write_temp_files(splitted_coffee_code, encoding)
try:
filename=paths
self.assert_compile_file_success(filename, encoding, True)
self.assert_compile_file_fail(filename, encoding, False)
forwrong_encodinginset(self.encodings) -set([encoding]):
self.assert_compile_file_decode_error(
filename, wrong_encoding, True)
self.assert_compile_file_decode_error(
filename, wrong_encoding, False)
finally:
self.remove_files(paths)
defload_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(coffeescript))
returntests
defmain():
unittest.main()
if__name__=="__main__":
main()