Get the style color and number just once
[LibreOffice.git] / sw / qa / uitest / librelogo / compile.py
blobbf4668e8e33275da2c41e9ad9937bda2d72d841a
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from uitest.framework import UITestCase
12 import re
14 class LibreLogoCompileTest(UITestCase):
15 LIBRELOGO_PATH = "vnd.sun.star.script:LibreLogo|LibreLogo.py$%s?language=Python&location=share"
16 LS = "#_@L_i_N_e@_#" # LibreLogo line separator for debug feature "jump to the bad LibreLogo program line"
18 def createMasterScriptProviderFactory(self):
19 xServiceManager = self.xContext.ServiceManager
20 return xServiceManager.createInstanceWithContext(
21 "com.sun.star.script.provider.MasterScriptProviderFactory",
22 self.xContext)
24 def getScript(self, command):
25 xMasterScriptProviderFactory = self.createMasterScriptProviderFactory()
26 document = self.ui_test.get_component()
27 xScriptProvider = xMasterScriptProviderFactory.createScriptProvider(document)
28 xScript = xScriptProvider.getScript(self.LIBRELOGO_PATH %command)
29 self.assertIsNotNone(xScript, "xScript was not loaded")
30 return xScript
32 def test_compile_librelogo(self):
33 # XScript interface to LibreLogo Python compiler
34 xCompile = self.getScript("__compil__")
36 for test in (
37 # BASE COMMANDS
38 ("FORWARD 1 forward 1 fd 1", "forward(1)\nforward(1)\nforward(1)"),
39 ("BACK 1 back 1 bk 1", "backward(1)\nbackward(1)\nbackward(1)"),
40 ("RIGHT 1 right 1 rt 1", "turnright(1)\nturnright(1)\nturnright(1)"),
41 ("LEFT 1 left 1 lt 1", "turnleft(1)\nturnleft(1)\nturnleft(1)"),
42 # COMMENTS
43 ("fd 1; comment\n; full-line comment", "forward(1)\n" + self.LS),
44 # MULTI-LINE COMMAND
45 ("LABEL ~\n10 + 10", "label(10 + 10)\n" + self.LS),
46 # UNIT SPECIFIERS
47 # check specifier "pt" (point)
48 ("fd 1pt", "forward(1)"),
49 # check specifier "pt" (point)
50 ("fd 0.5pt", "forward(0.5)"),
51 # check "in" (inch)
52 ("fd 1in", "forward(72.0)"),
53 # check "cm" (centimeter)
54 ("fd 1cm", "forward(%s)" % (1/(2.54/72))),
55 # check "mm" (millimeter)
56 ("fd 10mm", "forward(%s)" % (1/(2.54/72))),
57 # angle
58 ("rt 90°", "turnright(90)"),
59 # clock-position
60 ("rt 3h", "turnright(90.0)"),
61 # CONDITION
62 ("if a = 1 [ ]", "if a == 1 :\n __checkhalt__()"),
63 # with else
64 ("if a == 1 [ ] [ ]", "if a == 1 :\n __checkhalt__()\nelse:\n __checkhalt__()"),
65 # LOOPS
66 ("repeat 10 [ ]", "for REPCOUNT in range(1, 1+int(10 )):\n __checkhalt__()"),
67 # endless loop
68 ("repeat [ ]", "REPCOUNT0 = 1\nwhile True:\n __checkhalt__()\n REPCOUNT = REPCOUNT0\n REPCOUNT0 += 1"),
69 # loop in loop
70 ("repeat 10 [ repeat REPCOUNT [ a=1 ] ]", "for REPCOUNT in range(1, 1+int(10 )):\n __checkhalt__()\n for REPCOUNT in range(1, 1+int(REPCOUNT )):\n __checkhalt__()\n a=1"),
71 # while
72 ("WHILE REPCOUNT < 10 [ ]", "REPCOUNT2 = 1\nwhile REPCOUNT2 < 10 :\n __checkhalt__()\n REPCOUNT = REPCOUNT2\n REPCOUNT2 += 1"),
73 # for
74 ("FOR i in [1, 2, 3] [ ]", "REPCOUNT4 = 1\nfor i in [1, 2, 3] :\n __checkhalt__()\n REPCOUNT = REPCOUNT4\n REPCOUNT4 += 1"),
75 ("FOR i IN RANGE COUNT 'letters' [ ]", "REPCOUNT6 = 1\nfor i in range(len(u'letters'),) :\n __checkhalt__()\n REPCOUNT = REPCOUNT6\n REPCOUNT6 += 1"),
76 # PROCEDURE
77 ("TO x\nLABEL 2\nEND", "global x\ndef x():\n __checkhalt__()\n %s\n label(2)\n %s" % (((self.LS),)*2)),
78 # FUNCTION
79 ("TO x\nOUTPUT 3\nEND", "global x\ndef x():\n __checkhalt__()\n %s\n return 3\n %s" % (((self.LS),)*2)),
80 # PROCEDURE WITH ARGUMENTS
81 ("TO x y\nLABEL y\nEND\nx 25", "global x\ndef x(y):\n __checkhalt__()\n %s\n label(y)\n %s\n%s\nx(25)" % (((self.LS),)*3)),
82 ("TO x :y :z\nLABEL :y + :z\nEND\nx 25 26", "global x\ndef x(_y, _z):\n __checkhalt__()\n %s\n label(_y + _z)\n %s\n%s\nx(25, 26)" % (((self.LS),)*3)),
83 ("TO x :y :z\nLABEL :y + :z\nEND\nx 25 :w", "global x\ndef x(_y, _z):\n __checkhalt__()\n %s\n label(_y + _z)\n %s\n%s\nx(25, _w)" % (((self.LS),)*3)),
84 # UNICODE VARIABLE NAMES
85 ("Erdős=1", "Erd__u__0151s=1"),
86 # STRING CONSTANTS
87 ("LABEL \"label", "label(u'label')"),
88 ("LABEL “label”", "label(u'label')"),
89 ("LABEL 'label'", "label(u'label')"),
90 ("LABEL ‘label’", "label(u'label')"),
91 # check apostrophe and quote usage within strings
92 ("LABEL “label\’s”", "label(u'label’s')"),
93 ("LABEL ““It\’s quote\’s...\””", "label(u'“It’s quote’s...”')"),
94 ("LABEL ““It\\'s quote\\'s...\””", "label(u'“It\\'s quote\\'s...”')"),
95 # CONVERSION OF LOGO EXPRESSIONS
96 ("a=SIN 100 + COS 100", "a=sin(100 + cos(100))"),
97 ("a=SIN(101) + COS(101)", "a=sin(101) + cos(101)"),
98 ("a=(SIN 102) + (COS 102)", "a=(sin(102)) + (cos(102))"),
99 ("a=SIN 103 + COS 103 - SQRT 103", "a=sin(103 + cos(103 - sqrt(103)))"),
100 ("a=(SIN 104 + COS 104) - SQRT 104", "a=(sin(104 + cos(104))) - sqrt(104)"),
101 # SIN(x) is Python-like, SIN (x) is Logo-like syntax
102 ("a=SIN(105) + COS (105) - SQRT 105", "a=sin(105) + cos((105) - sqrt(105))"),
103 ("a=COUNT [1, 2, 3]", "a=len([1, 2, 3])"),
104 ("PRINT COUNT [1, 2, 3]", "Print(len([1, 2, 3]))"),
105 ("PRINT 'TEXT: ' + 'CHAR'[0] + ' TEXT2: ' + variable[-1]", "Print(u'TEXT: ' + u'CHAR'[0] + u' TEXT2: ' + variable[-1])"),
106 ("PRINT 'TEXT: ' + 'CHAR'[0][n] + ' TEXT2: ' + varia[len k][i+1]", "Print(u'TEXT: ' + u'CHAR'[0][n] + u' TEXT2: ' + varia[len(k)][i+1])"),
107 ("a=SQRT COUNT [1, 2, 3]", "a=sqrt(len([1, 2, 3]))"),
108 ("a=RANGE 1", "a=range(1,)"),
109 ("a=RANGE 1 10", "a=range(1, 10,)"),
110 ("a=RANGE 1 10 5", "a=range(1, 10, 5)"),
111 ("a=RANDOM 40 + 120", "a=Random(40 + 120)"),
112 ("a=RANDOM(40) + 120", "a=Random(40) + 120"),
113 ("a=RANDOM [1, 2, 3]", "a=Random([1, 2, 3])"),
114 ("PRINT RANDOM 40", "Print(Random(40))"),
115 ("FORWARD RANDOM 40", "forward(Random(40))"),
116 ("FORWARD 10 + RANDOM 40 + RANDOM 100", "forward(10 + Random(40 + Random(100)))"),
117 ("a=[sin 90 + cos 15, cos 100 * x, sqrt 25 * 25]", "a=[sin(90 + cos(15)), cos(100 * x), sqrt(25 * 25)]"),
118 ("a=[sin 90 + cos 15, cos 100 * x, sqrt 25 * 25]", "a=[sin(90 + cos(15)), cos(100 * x), sqrt(25 * 25)]"),
119 ("a=[sin(90) + cos 15, cos(100) * x, sqrt(25) * 25]", "a=[sin(90) + cos(15), cos(100) * x, sqrt(25) * 25]"),
120 ("TO x y z\nOUTPUT 3\nEND", "global x\ndef x(y, z):\n __checkhalt__()\n %s\n return 3\n %s" % (((self.LS),)*2)),
121 ("TO x\nOUTPUT 3\nEND", "global x\ndef x():\n __checkhalt__()\n %s\n return 3\n %s" % (((self.LS),)*2)),
122 ("TO f x y z\nOUTPUT x+y+z\nEND\na = [-sin -len f [-cos 45, 6] -len [1, 2, 3] -sin -90", "global f\ndef f(x, y, z):\n __checkhalt__()\n %s\n return x+y+z\n %s\n%s\na = [-sin(-len(f([-cos(45), 6], -len([1, 2, 3]), -sin(-90))))" % (((self.LS),)*3)),
123 ("TO f x y z\nOUTPUT x+y+z\nEND\na = [sin len f [cos 45, 6] [1, 2, 3] sin 90", "global f\ndef f(x, y, z):\n __checkhalt__()\n %s\n return x+y+z\n %s\n%s\na = [sin(len(f([cos(45), 6], [1, 2, 3], sin(90))))" % (((self.LS),)*3)),
124 ("TO f x y z\nLABEL x+y+z\nEND\nf len [1, cos 2, [65]] sqrt len [1, 2, 3, 4] sin 90 * cos 270", "global f\ndef f(x, y, z):\n __checkhalt__()\n %s\n label(x+y+z)\n %s\n%s\nf(len([1, cos(2), [65]]), sqrt(len([1, 2, 3, 4])), sin(90 * cos(270)))" % (((self.LS),)*3)),
125 ("TO f x y z\nLABEL x+y+z\nEND\nf len([1, cos 2, [65]]) sqrt(len [1, 2, 3, 4]) sin(90) * cos 270", "global f\ndef f(x, y, z):\n __checkhalt__()\n %s\n label(x+y+z)\n %s\n%s\nf(len([1, cos(2), [65]]), sqrt(len([1, 2, 3, 4])), sin(90) * cos(270))" % (((self.LS),)*3)),
126 ("TO f x y z\nLABEL x+y+z\nEND\nf (len [1, cos 2, [65]]) (sqrt len [1, 2, 3, 4]) (sin 90) * (cos 270)", "global f\ndef f(x, y, z):\n __checkhalt__()\n %s\n label(x+y+z)\n %s\n%s\nf((len([1, cos(2), [65]])), (sqrt(len([1, 2, 3, 4]))), (sin(90)) * (cos(270)))" % (((self.LS),)*3)),
127 # arbitrary order of function definitions and calls
128 ("f 1 1 f 2 2\nTO f x y\nPRINT x + y\nEND", "global f\nf(1, 1)\nf(2, 2)\n%s\ndef f(x, y):\n __checkhalt__()\n %s\n Print(x + y)\n %s" % (((self.LS),)*3)),
130 compiled = xCompile.invoke((test[0],), (), ())[0]
131 self.assertEqual(test[1], re.sub(r'(\n| +\n)+', '\n', re.sub(r'\( ', '(', compiled)).strip())
133 # vim: set shiftwidth=4 softtabstop=4 expandtab: