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
111 lines (86 loc) · 3.96 KB
/
Copy pathtest_coffeescript.py
File metadata and controls
111 lines (86 loc) · 3.96 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
#!python3
#encoding:utf-8
from __future__ import unicode_literals
import doctest
import unittest
import os
import io
import tempfile
import itertools
import execjs
import coffeescript
cfcode = """
#このコメントはasciiで表現できない文字列です(This is a non-ascii comment)
helloworld = "こんにちは世界"
add = (x, y) ->
x + y
"""
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
with io.open(join(dirname(coffeescript.__file__), "coffee-script.js")) 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 itertools.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()
for compiler, encoding, runtime in itertools.product(self.compilers, encodings, self.runtimes):
compile_file = compiler.compile_file
(fd, filename) = tempfile.mkstemp()
os.close(fd)
try:
with io.open(filename, "w", encoding=encoding) as fp:
fp.write(cfcode)
jscode = coffeescript.compile_file(filename,
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(filename,
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()