forked from doloopwhile/Python-CoffeeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_coffeescript.py
More file actions
executable file
·126 lines (101 loc) · 4.23 KB
/
test_coffeescript.py
File metadata and controls
executable file
·126 lines (101 loc) · 4.23 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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals, print_function
import doctest
import unittest
import os
import io
import tempfile
from itertools import product
import execjs
import coffeescript
coffee_codes = [
"""
# このコメントはasciiで表現できない文字列です(This is a non-ascii comment)
add = (x, y) ->
x + y
""",
"""
helloworld = "こんにちは世界"
"""
]
hello = "こんにちは"
world = "世界"
helloworld = "こんにちは世界"
class CoffeeScriptTest(unittest.TestCase):
def setUp(self):
self.runtimes = list(execjs.available_runtimes().values())
self.compilers = []
self.compilers.append(coffeescript) # default compiler
from os.path import join, dirname
script_path = join(dirname(coffeescript.__file__), "coffee-script.js")
with io.open(script_path) as fp:
compiler_script = fp.read()
for runtime in self.runtimes:
self.compilers.append(
coffeescript.Compiler(compiler_script, runtime))
def test_compile(self):
for compiler, runtime in product(self.compilers, self.runtimes):
compile = compiler.compile
jscode = compile(cfcode, bare=True)
ctx = runtime.compile(jscode)
self.assertEqual(ctx.call("add", 1, 2), 3)
self.assertEqual(ctx.call("add", hello, world), helloworld)
self.assertEqual(ctx.eval("helloworld"), helloworld)
jscode = compile(cfcode, bare=False)
ctx = runtime.compile(jscode)
with self.assertRaises(execjs.ProgramError):
self.assertEqual(ctx.call("add", 1, 2), 3)
with self.assertRaises(execjs.ProgramError):
self.assertEqual(ctx.call("add", hello, world), helloworld)
with self.assertRaises(execjs.ProgramError):
self.assertEqual(ctx.eval("helloworld"), helloworld)
def test_compile_files(self):
encodings = "shift-jis utf-8 euc-jp".split()
combinations_of_configs = product(
self.compilers,
encodings,
self.runtimes
)
for compiler, encoding, runtime in combinations_of_configs:
compile_file = compiler.compile_file
filenames = []
for i, _ in enumerate(coffee_codes):
(fd, fn) = tempfile.mkstemp()
filenames.append(fn)
os.close(fd)
try:
for coffee_code, filename in zip(coffee_codes, filenames):
with io.open(filename, "w", encoding=encoding) as fp:
fp.write(coffee_code)
jscode = coffeescript.compile_file(
filenames, encoding=encoding, bare=True)
ctx = runtime.compile(jscode)
self.assertEqual(ctx.call("add", 1, 2), 3)
self.assertEqual(ctx.call("add", hello, world), helloworld)
self.assertEqual(ctx.eval("helloworld"), helloworld)
jscode = coffeescript.compile_file(
filenames, encoding=encoding, bare=False)
ctx = runtime.compile(jscode)
with self.assertRaises(execjs.ProgramError):
self.assertEqual(ctx.call("add", 1, 2), 3)
with self.assertRaises(execjs.ProgramError):
self.assertEqual(ctx.call("add", hello, world), helloworld)
with self.assertRaises(execjs.ProgramError):
self.assertEqual(ctx.eval("helloworld"), helloworld)
for wrong_encoding in set(encodings) - set([encoding]):
with self.assertRaises(UnicodeDecodeError):
coffeescript.compile_file(
filename, encoding=wrong_encoding, bare=True)
with self.assertRaises(UnicodeDecodeError):
coffeescript.compile_file(
filename, encoding=wrong_encoding, bare=False)
finally:
os.remove(filename)
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(coffeescript))
return tests
def main():
unittest.main()
if __name__ == "__main__":
main()