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 23
Expand file tree
/
Copy pathtest_coffeescript.py
More file actions
executable file
·141 lines (114 loc) · 4.36 KB
/
test_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__ import unicode_literals, print_function
import sys
import os
import io
import tempfile
from itertools import product
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
import doctest
import execjs
import coffeescript
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
"""
]
class CoffeeScriptTest(unittest.TestCase):
def setUp(self):
self.encodings = "shift-jis utf-8 euc-jp".split()
def assertExprsSuccess(self, ctx):
self.assertEqual(ctx.call("add", 1, 2), 3)
self.assertEqual(ctx.call("add", hello, world), helloworld)
self.assertEqual(ctx.eval("helloworld"), helloworld)
def assertExprsFail(self, ctx):
with self.assertRaises(execjs.ProgramError):
ctx.call("add", 1, 2)
with self.assertRaises(execjs.ProgramError):
ctx.call("add", hello, world)
with self.assertRaises(execjs.ProgramError):
ctx.eval("helloworld")
def test_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)
def assert_compile_file_success(self, filename, encoding, bare):
jscode = coffeescript.compile_file(filename, encoding=encoding, bare=bare)
ctx = execjs.compile(jscode)
self.assertExprsSuccess(ctx)
def assert_compile_file_fail(self, filename, encoding, bare):
jscode = coffeescript.compile_file(filename, encoding=encoding, bare=bare)
ctx = execjs.compile(jscode)
self.assertExprsFail(ctx)
def assert_compile_file_decode_error(self, filename, encoding, bare):
with self.assertRaises(UnicodeDecodeError):
coffeescript.compile_file(filename, encoding=encoding, bare=bare)
def write_temp_files(self, strings, encoding):
paths = []
for s in strings:
(fd, path) = tempfile.mkstemp()
os.close(fd)
with io.open(path, "w", encoding=encoding) as fp:
fp.write(s)
paths.append(path)
return paths
def remove_files(self, paths):
for p in paths:
os.remove(p)
def test_compile_files(self):
for encoding in self.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)
for wrong_encoding in set(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)
def test_compile_splitted_files(self):
for encoding in self.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)
for wrong_encoding in set(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)
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(coffeescript))
return tests
def main():
unittest.main()
if __name__ == "__main__":
main()