github / src / tests / sharp / test_syntax.py @ c89c6b5b
1 |
# Copyright (c) 2016, LE GOFF Vincent |
---|---|
2 |
# All rights reserved.
|
3 |
|
4 |
# Redistribution and use in source and binary forms, with or without
|
5 |
# modification, are permitted provided that the following conditions are met:
|
6 |
|
7 |
# * Redistributions of source code must retain the above copyright notice, this
|
8 |
# list of conditions and the following disclaimer.
|
9 |
|
10 |
# * Redistributions in binary form must reproduce the above copyright notice,
|
11 |
# this list of conditions and the following disclaimer in the documentation
|
12 |
# and/or other materials provided with the distribution.
|
13 |
|
14 |
# * Neither the name of ytranslate nor the names of its
|
15 |
# contributors may be used to endorse or promote products derived from
|
16 |
# this software without specific prior written permission.
|
17 |
|
18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21 |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26 |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28 |
|
29 |
import unittest |
30 |
|
31 |
from sharp.engine import SharpScript |
32 |
|
33 |
class TestSyntax(unittest.TestCase): |
34 |
|
35 |
"""Unittest for the SharpScript syntax."""
|
36 |
|
37 |
def setUp(self): |
38 |
"""Create the SharpScript instance."""
|
39 |
self.engine = SharpScript(None, None) |
40 |
|
41 |
def test_single(self): |
42 |
"""Test a single statement."""
|
43 |
# Test a statement on one line with one argument
|
44 |
statements = self.engine.feed("#play file.wav") |
45 |
self.assertEqual(statements, ["play('file.wav')"]) |
46 |
|
47 |
# Test a statement with an new line and no argument
|
48 |
statements = self.engine.feed("#stop\n") |
49 |
self.assertEqual(statements, ["stop()"]) |
50 |
|
51 |
# Same test, but with some useless spaces
|
52 |
statements = self.engine.feed(" #stop \n ") |
53 |
self.assertEqual(statements, ["stop()"]) |
54 |
|
55 |
# Test a statement with arguments surrounded by braces
|
56 |
statements = self.engine.feed("#macro {Alt + Enter} north") |
57 |
self.assertEqual(statements, ["macro('Alt + Enter', 'north')"]) |
58 |
|
59 |
# Same test but with some useless spaces
|
60 |
statements = self.engine.feed(" #macro {Alt + Enter} north\n ") |
61 |
self.assertEqual(statements, ["macro('Alt + Enter', 'north')"]) |
62 |
|
63 |
# Test what happens without function names
|
64 |
statements = self.engine.feed("say Hello all!\n ") |
65 |
self.assertEqual(statements, ["send('say Hello all!')"]) |
66 |
|
67 |
def test_multiple(self): |
68 |
"""Test multiple statements at once."""
|
69 |
# Test two statements on two lines without useless spaces
|
70 |
statements = self.engine.feed("#play file.wav\n#stop") |
71 |
self.assertEqual(statements, ["play('file.wav')", "stop()"]) |
72 |
|
73 |
# Same test, but with some useless spaces
|
74 |
statements = self.engine.feed(" #play file.wav \n#stop \n ") |
75 |
self.assertEqual(statements, ["play('file.wav')", "stop()"]) |
76 |
|
77 |
def test_python(self): |
78 |
"""Test Python syntax embeeded in SharpScript."""
|
79 |
statements = self.engine.feed("""#trigger {Should it work?} {+ |
80 |
var = 2 + 3
|
81 |
print var
|
82 |
}""")
|
83 |
self.assertEqual(statements, [
|
84 |
"trigger('Should it work?', compile('var = 2 + 3\nprint var', " \
|
85 |
"'SharpScript', 'exec'))"
|
86 |
]) |
87 |
|
88 |
def test_flag(self): |
89 |
"""Test the SharpScript syntax with flags in funciton calls."""
|
90 |
statements = self.engine.feed("#say {A message} -braille +speech") |
91 |
self.assertEqual(statements, [
|
92 |
"say('A message', braille=False, speech=True)"
|
93 |
]) |
94 |
|
95 |
def test_python_top(self): |
96 |
"""Test Python code in SharpScript at the top level."""
|
97 |
statements = self.engine.feed("""{+ |
98 |
print 1
|
99 |
print 3
|
100 |
}""")
|
101 |
self.assertEqual(statements, [
|
102 |
"compile('print 1\nprint 3', 'SharpScript', 'exec')",
|
103 |
]) |
104 |
|
105 |
def test_semicolons(self): |
106 |
"""Test the semi-colons."""
|
107 |
# A test with simple text
|
108 |
statements = self.engine.feed("#macro F1 north;south;;east") |
109 |
self.assertEqual(statements, ["macro('F1', 'north\nsouth;east')"]) |
110 |
|
111 |
# A test with SharpScript
|
112 |
statements = self.engine.feed("#trigger ok {#play new.wav;#stop}") |
113 |
self.assertEqual(statements, [
|
114 |
"trigger('ok', '#play new.wav\n#stop')",
|
115 |
]) |
116 |
|
117 |
def test_escape_sharp(self): |
118 |
"""Test the escaped sharp symbol."""
|
119 |
# A sharp escaping with plain text
|
120 |
statements = self.engine.feed("##out") |
121 |
self.assertEqual(statements, ["send('#out')"]) |
122 |
|
123 |
# A sharp escaping with SharpScript
|
124 |
statements = self.engine.feed("#macro ##ok") |
125 |
self.assertEqual(statements, ["macro('#ok')"]) |
- « Previous
- 1
- 2
- 3
- Next »