예제 #1
0
    def test_trivial(self):
        code = d("""\
                    a = 1
                 """)
        result = lambda x: collections.deque(x, maxlen=10)
        values = LiveSource(code).get_values()

        self.assertEqual(values[1], result([('a', 1)]))
예제 #2
0
    def test_init(self):
        code = 'test_code'
        max_deep = 1

        result = LiveSource(code, max_deep)

        self.assertEqual(result.code, code)
        self.assertEqual(result.lst, max_deep)
예제 #3
0
class LiveSourceTestCase(unittest.TestCase):
    @patch('livesource.livesource.LSTree', MagicMock())
    def setUp(self):
        self.source = LiveSource('test_code')
        self.source.lst.globals = {}
        self.source.lst.locals = {'__livesource_listing': ''}
        self.source.lst.visit = MagicMock(side_effect=lambda x: x)

    @patch('livesource.livesource.LSTree', MagicMock(side_effect=lambda x: x))
    def test_init(self):
        code = 'test_code'
        max_deep = 1

        result = LiveSource(code, max_deep)

        self.assertEqual(result.code, code)
        self.assertEqual(result.lst, max_deep)

    def test_get_values(self):
        self.source._parse = MagicMock(return_value='')

        result = self.source.get_values()

        self.assertEqual(result, '')

    @unittest.skip("Not Implemented")
    def test_set_variable(self):
        pass

    def test_update(self):
        code = 'new_code'

        self.source.update(code)

        self.assertEqual(self.source.code, code)

    @patch('ast.parse', MagicMock(side_effect=lambda x: 'parsed {}'.format(x)))
    @patch('ast.fix_missing_locations', MagicMock(side_effect=lambda x: x))
    def test__parse(self):
        result = self.source._parse()

        self.assertEqual(result, 'parsed test_code')
예제 #4
0
    def test_trivial(self):
        code = d("""\
                    a = 1
                 """)
        result = d("""\
                    a = 1
                    __livesource_listing[1].append(('a', a, ))
                   """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #5
0
    def test_swap(self):
        code = d("""\
                    a, b = b, a
                 """)
        result = d("""\
                    a, b = b, a
                    __livesource_listing[1].append(('a', a, ))
                    __livesource_listing[1].append(('b', b, ))
                   """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #6
0
    def test_format(self):
        code = d("""\
                    print "a{}".format("b")
                 """)
        result = d("""\
                    print "a{}".format("b")
                    __livesource_listing[1].append((None, " ".join(("a{}".format("b"),)),))

                 """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #7
0
    def test_concatenate(self):
        code = d("""\
                    print "a" + "b"
                 """)
        result = d("""\
                    print "a" + "b"
                    __livesource_listing[1].append((None, " ".join(("a" + "b",)),))

                 """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #8
0
    def test_trivial(self):
        code = d("""\
                    if x:
                        pass
                 """)
        result = d("""\
                    if x:
                        __livesource_listing[1].append((None, x,))
                        pass
                 """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #9
0
    def test_attribute(self):
        code = d("""\
                    a.x = (2,)
                    a.x.y = [1, 2]
                 """)
        result = d("""\
                    a.x = (2,)
                    __livesource_listing[1].append(('a.x', a.x, ))
                    a.x.y = [1, 2]
                    __livesource_listing[2].append(('a.x.y', a.x.y, ))
                   """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #10
0
    def test_multiline(self):
        code = d("""\
                    a = 1
                    b = 2
                    c = 3
                 """)
        result = d("""\
                    a = 1
                    __livesource_listing[1].append(('a', a, ))
                    b = 2
                    __livesource_listing[2].append(('b', b, ))
                    c = 3
                    __livesource_listing[3].append(('c', c, ))
                   """)

        parsed_tree = ast.dump(LiveSource(code)._parse())
        expected_tree = ast.dump(ast.parse(result))

        self.assertEqual(parsed_tree, expected_tree)
예제 #11
0
 def setUp(self):
     self.source = LiveSource('test_code')
     self.source.lst.globals = {}
     self.source.lst.locals = {'__livesource_listing': ''}
     self.source.lst.visit = MagicMock(side_effect=lambda x: x)