Exemple #1
0
    def test_operation(self):
        fn = ops.bind_to_function("reportSum")
        result = fn(None, None, (Literal(56), Literal(72)))
        self.assertEqual(result, Literal(56 + 72))

        fn = ops.bind_to_function("nonexistentFunction")
        self.assertEquals(None, None, fn)
Exemple #2
0
    def test_simple_custom_command__glide_in_square(self):
        """Makes a sprite glide around in a square"""

        filename = "simple_custom_command__glide_in_square.xml"

        self.do_test_script(filename,
                            post_check={"num_turns": Literal(34)},
                            injection={"num_turns": Literal(30)})
Exemple #3
0
    def test_repeat_block(self):
        """Increment a counter five times in a loop.

        set count to 0
        repeat 5
            change count by 1

        The final result should be 5."""

        self.do_test_script("simple_repeat_loop.xml", {"count": Literal(0)},
                            {"count": Literal(5)})
Exemple #4
0
    def test_custom_recursive_block__long_adder(self):
        """Test custom block that recursively adds two numbers"""

        filename = "custom_recursive_block__long_adder.xml"

        self.do_test_script(filename,
                            injection={
                                "start": Literal(77),
                                "depth": Literal(5)
                            },
                            post_check={"result": Literal(82)})
Exemple #5
0
    def test_nested_repeat_block(self):
        """Increment a counter in a loop that is within a loop

        set count to 0
        repeat 10
            repeat 5
                change count by 1

        The final result should be 50."""

        self.do_test_script("nested_repeat_loops.xml", {"count": Literal(0)},
                            {"count": Literal(50)})
Exemple #6
0
    def test_calling_custom_reporter_block(self):
        """Calls a custom reported block.

        The block is "add a to b" and does as the name says.

        The code is:
        set result to (add 5 to 7)
        """

        filename = "simple_custom_reporter_block.xml"

        self.do_test_script(filename, {"result": Literal(0)},
                            {"result": Literal(12)})
Exemple #7
0
    def do_test_fibonacci_sequence(self, filename):
        """Tests a script that calculates a fibonacci sequence"""

        # first, second = 0, 1
        # expected_results = [first, second]
        # for i in range(10):
        #     next = first + second
        #     expected_results.append(next)
        #     first, second = second, next
        #
        # for i, expected in enumerate(expected_results):
        #     self.do_test_script(filename,
        #                         injection={"input": Literal(i)},
        #                         post_check={"result": Literal(expected)})

        # No need for such a long and thorough test
        self.do_test_script(filename,
                            injection={"input": Literal(10)},
                            post_check={"result": Literal(55)})
Exemple #8
0
    def test_variable(self):
        v = Variable()
        self.assertEqual(0, v.value().as_number())
        self.assertEqual("", v.value().as_string())

        v.deserialize(
            ElementTree.XML(
                '<variable name="foo"><l>hello world</l></variable>'))
        self.assertEqual(v.name, "foo")
        self.assertEqual("hello world", v.value().as_string())
        self.assertEqual(v.contents, Literal("hello world"))
Exemple #9
0
    def test_bad_repeat_blocks(self):
        """Try out various pathological loops.

        set count to 0			count is 0
        repeat count            repeats 0x
            change count by 1
        repeat 0                repeats 0x
            change count by 1
        repeat -1               repeats 0x
            change count by 1
        repeat 0.5              repeats 0x
            change count by 1
        change count by 1       count goes from 0 -> 1
        repeat count            repeats 1x
            change count by 1

        The final result should be 2."""

        self.do_test_script("bad_repeat_loops.xml", {"count": Literal(0)},
                            {"count": Literal(2)})
Exemple #10
0
def next_token(stream, table, symtab):
    while stream.filled and stream.is_space():
        stream.advance()
    if not stream.filled:
        return None
    if stream.current == u'#':
        while stream.filled and stream.current != u'\n':
            stream.advance()
        return next_token(stream, table, symtab)
    start = stream.position
    if stream.is_sym():
        string = stream.advance()
        while stream.is_sym() or stream.is_digit():
            string += stream.advance()
        name = table.get(string, symtab[u'symbol'])
        return Literal(start, stream.position, name, string)
    elif stream.is_digit():
        string = stream.advance()
        if string == u'0' and stream.filled and stream.current == u'x':
            stream.advance()
            string = u""
            while stream.is_hex():
                string += stream.advance()
            return Literal(start, stream.position, symtab[u'hex'], string)
        while stream.is_digit():
            string += stream.advance()
        if stream.filled and stream.current == u'.' and not stream.pair_ahead(
                table):
            string += stream.advance()
            while stream.is_digit():
                string += stream.advance()
            return Literal(start, stream.position, symtab[u'float'], string)
        return Literal(start, stream.position, symtab[u'int'], string)
    elif stream.current in (u'"', u"'"):
        terminal = stream.advance()
        string = u""
        while stream.filled and stream.current != terminal:
            if stream.current == u'\\':
                stream.advance()
                string += escape_sequence(stream)
            else:
                string += stream.advance()
        if not stream.filled:
            raise Error(u"%s: Broken string literal" % start.repr())
        assert terminal == stream.advance()
        return Literal(start, stream.position, symtab[u'string'], string)
    elif stream.current in table:
        string = stream.advance()
        while stream.filled and string + stream.current in table:
            string += stream.advance()
        name = table[string]
        return Literal(start, stream.position, name, string)
    else:
        string = stream.advance()
        return Literal(start, stream.position, symtab[u'symbol'], string)
Exemple #11
0
    def test_simple_custom_reporter__return_self(self):
        """Calls a script that takes an input variable,
        runs a custom block, and sets result to the value input"""

        filename = "simple_custom_reporter__return_self.xml"

        self.do_test_script(filename,
                            post_check={"result": Literal(77)},
                            injection={"input": Literal(77)})

        self.do_test_script(filename,
                            post_check={"result": Literal(-92)},
                            injection={"input": Literal(-92)})

        self.do_test_script(filename,
                            post_check={"result": Literal(True)},
                            injection={"input": Literal(True)})

        self.do_test_script(filename,
                            post_check={"result": Literal("Testing")},
                            injection={"input": Literal("Testing")})
Exemple #12
0
    def test_literal_repr(self):
        x = Literal()
        self.assertEqual(x, eval(repr(x)))

        x = Literal(12)
        self.assertEqual(x, eval(repr(x)))

        x = Literal("text")
        self.assertEqual(x, eval(repr(x)))

        x = Literal(True)
        self.assertEqual(x, eval(repr(x)))

        x = Literal(False)
        self.assertEqual(x, eval(repr(x)))

        x = Literal(None)
        self.assertEqual(x, eval(repr(x)))
Exemple #13
0
    def test_if_block(self):
        """Tests some if blocks

        Tests doIf, doIfElse, and reportEquals

        set result to "meh"
        if feeling == "happy"
            set result to "smile"
        else
            if feeling == "sad"
                set result to "frown"
        """

        filename = "if_test.xml"

        self.do_test_script(filename, {}, {"result": Literal("smile")},
                            {"feeling": Literal("happy")})

        self.do_test_script(filename, {}, {"result": Literal("frown")},
                            {"feeling": Literal("sad")})

        self.do_test_script(filename, {}, {"result": Literal("meh")},
                            {"feeling": Literal("whatever")})
Exemple #14
0
    def test_literal_values(self):
        l = Literal()
        self.assertEquals(l.value, None)
        self.assertEqual(0, l.as_number())
        self.assertEqual("", l.as_string())
        self.assertEqual(False, l.as_bool())

        l.deserialize(ElementTree.XML('<l>15.75</l>'))
        self.assertEquals(l.value, 15.75)
        self.assertEqual(15.75, l.as_number())
        self.assertEqual("15.75", l.as_string())
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<l>-20.0</l>'))
        self.assertEquals(l.value, -20.0)
        self.assertEqual(-20.0, l.as_number())
        self.assertEqual("-20", l.as_string())  # no trailing zero
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<l>hello world</l>'))
        self.assertEquals(l.value, "hello world")
        self.assertEqual(0, l.as_number())
        self.assertEqual("hello world", l.as_string())
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<l></l>'))
        self.assertEquals(l.value, None)
        self.assertEqual(0, l.as_number())
        self.assertEqual("", l.as_string())
        self.assertEqual(False, l.as_bool())

        l.deserialize(ElementTree.XML('<l/>'))
        self.assertEquals(l.value, None)
        self.assertEqual(0, l.as_number())
        self.assertEqual("", l.as_string())
        self.assertEqual(False, l.as_bool())

        l.deserialize(ElementTree.XML('<bool>true</bool>'))
        self.assertEquals(l.value, True)
        self.assertEqual(1, l.as_number())
        self.assertEqual("true", l.as_string())
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<bool>false</bool>'))
        self.assertEquals(l.value, False)
        self.assertEqual(0, l.as_number())
        self.assertEqual("false", l.as_string())
        self.assertEqual(False, l.as_bool())
 def test_literal_with_option(self):
     xml = "<l><option>space</option></l>"
     l = Literal()
     l.deserialize(ElementTree.XML(xml))
     new_xml = ElementTree.tostring(l.serialize())
     self.compare_xml(xml, new_xml, True, "literal_option.xml")
    def test_literal_values(self):
        l = Literal()
        self.assertEquals(l.value, None)
        self.assertEqual(0, l.as_number())
        self.assertEqual("", l.as_string())
        self.assertEqual(False, l.as_bool())

        l.deserialize(ElementTree.XML('<l>15.75</l>'))
        self.assertEquals(l.value, 15.75)
        self.assertEqual(15.75, l.as_number())
        self.assertEqual("15.75", l.as_string())
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<l>-20.0</l>'))
        self.assertEquals(l.value, -20.0)
        self.assertEqual(-20.0, l.as_number())
        self.assertEqual("-20", l.as_string())  # no trailing zero
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<l>hello world</l>'))
        self.assertEquals(l.value, "hello world")
        self.assertEqual(0, l.as_number())
        self.assertEqual("hello world", l.as_string())
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<l></l>'))
        self.assertEquals(l.value, None)
        self.assertEqual(0, l.as_number())
        self.assertEqual("", l.as_string())
        self.assertEqual(False, l.as_bool())

        l.deserialize(ElementTree.XML('<l/>'))
        self.assertEquals(l.value, None)
        self.assertEqual(0, l.as_number())
        self.assertEqual("", l.as_string())
        self.assertEqual(False, l.as_bool())

        l.deserialize(ElementTree.XML('<bool>true</bool>'))
        self.assertEquals(l.value, True)
        self.assertEqual(1, l.as_number())
        self.assertEqual("true", l.as_string())
        self.assertEqual(True, l.as_bool())

        l.deserialize(ElementTree.XML('<bool>false</bool>'))
        self.assertEquals(l.value, False)
        self.assertEqual(0, l.as_number())
        self.assertEqual("false", l.as_string())
        self.assertEqual(False, l.as_bool())
Exemple #17
0
    def test_literal_comparisons(self):
        self.assertEqual(Literal(10), Literal(10))
        self.assertEqual(Literal(10), Literal("10"))
        self.assertEqual(Literal("10"), Literal("10"))
        self.assertEqual(Literal("10"), Literal(10))

        self.assertNotEqual(Literal(10), Literal(20))
        self.assertNotEqual(Literal(10), Literal("20"))
        self.assertNotEqual(Literal("10"), Literal("20"))
        self.assertNotEqual(Literal("10"), Literal(20))

        self.assertNotEqual(Literal(10), Literal("ten"))

        self.assertEqual(Literal("apple"), Literal("ApPlE"))

        self.assertTrue(Literal(10) < Literal(20))
        self.assertTrue(Literal(10) < Literal("20"))
        self.assertTrue(Literal("10") < Literal("20"))
        self.assertTrue(Literal("10") < Literal(20))
        self.assertTrue(Literal("Apple") < Literal("Banana"))
        self.assertTrue(Literal("A") < Literal("B"))
        self.assertTrue(Literal("a") < Literal("B"))
        self.assertTrue(Literal("A") < Literal("b"))
        self.assertTrue(Literal("a") < Literal("b"))

        self.assertFalse(Literal(10) > Literal(20))
        self.assertFalse(Literal(10) > Literal("20"))
        self.assertFalse(Literal("10") > Literal("20"))
        self.assertFalse(Literal("10") > Literal(20))
        self.assertFalse(Literal("Apple") > Literal("Banana"))
        self.assertFalse(Literal("A") > Literal("B"))
        self.assertFalse(Literal("a") > Literal("B"))
        self.assertFalse(Literal("A") > Literal("b"))
        self.assertFalse(Literal("a") > Literal("b"))

        self.assertTrue(Literal(10) < Literal("ten"))  # string comparison
Exemple #18
0
 def test_variable_repr(self):
     x = Variable("name", Literal("val"))
     self.assertEqual(x, eval(repr(x)))
Exemple #19
0
 def test_variables_repr(self):
     v = Variables(
         OrderedDict([('x', Variable('x', Literal(47.0))),
                      ('y', Variable('y', Literal(52.0)))]))
     self.assertEqual(v, eval(repr(v)))
Exemple #20
0
 def test_literal_with_option(self):
     xml = "<l><option>space</option></l>"
     l = Literal()
     l.deserialize(ElementTree.XML(xml))
     new_xml = ElementTree.tostring(l.serialize())
     self.compare_xml(xml, new_xml, True, "literal_option.xml")