Exemplo n.º 1
0
    def test_write_multiple(self):
        root = mktemp()
        definitions = {
            'Node': (
                Attr(attr='left'),
                Text(value=' '),
                Attr(attr='op'),
                Text(value=' '),
                Attr(attr='right'),
                Text(value=';'),
            )
        }

        # the program node; attributes are assigned to mimic a real one
        program1 = Node()
        program1.left, program1.op, program1.right = ('foo', '=', 'true')
        program1.sourcepath = join(root, 'program1.js')
        program1._token_map = {
            'foo': [(0, 1, 1)],
            '=': [(4, 1, 5)],
            'true': [(6, 1, 7)],
        }
        program2 = Node()
        program2.left, program2.op, program2.right = ('bar', '=', 'false')
        program2.sourcepath = join(root, 'program2.js')
        program2._token_map = {
            'bar': [(0, 1, 1)],
            '=': [(4, 1, 5)],
            'false': [(6, 1, 7)],
        }

        # streams
        output_stream = StringIO()
        output_stream.name = join(root, 'packed.js')
        sourcemap_stream = StringIO()
        sourcemap_stream.name = join(root, 'packed.js.map')

        unparser = BaseUnparser(definitions)
        io.write(unparser, [program1, program2],
                 output_stream,
                 sourcemap_stream,
                 source_mapping_url=None)

        self.assertEqual('foo = true;bar = false;', output_stream.getvalue())

        sourcemap = json.loads(sourcemap_stream.getvalue())
        self.assertEqual(
            {
                "version": 3,
                "sources": ["program1.js", "program2.js"],
                "names": [],
                "mappings": "AAAA,WCAA",
                "file": "packed.js"
            }, sourcemap)
Exemplo n.º 2
0
    def test_write_no_sourcemap(self):
        root = mktemp()
        definitions = {
            'Node': (
                Attr(attr='left'),
                Attr(attr='op'),
                Attr(attr='right'),
            )
        }

        # the program node; attributes are assigned to mimic a real one
        program = Node()
        program.left, program.op, program.right = ('foo', '=', 'true')
        program.sourcepath = join(root, 'original.js')
        program._token_map = {
            'foo': [(0, 1, 1)],
            '=': [(4, 1, 5)],
            'true': [(6, 1, 7)],
        }

        output_stream = StringIO()
        output_stream.name = join(root, 'processed.js')

        unparser = BaseUnparser(definitions)
        io.write(unparser, program, output_stream)
        self.assertEqual('foo=true', output_stream.getvalue())
Exemplo n.º 3
0
    def test_write_sourcemap_omitted(self):
        root = mktemp()
        definitions = {
            'Node': (
                Attr(attr='left'),
                Attr(attr='op'),
                Attr(attr='right'),
            )
        }

        # the program node; attributes are assigned to mimic a real one
        program = Node()
        program.left, program.op, program.right = ('foo', '=', 'true')
        program.sourcepath = join(root, 'original.js')
        program._token_map = {
            'foo': [(0, 1, 1)],
            '=': [(4, 1, 5)],
            'true': [(6, 1, 7)],
        }

        # streams
        output_stream = StringIO()
        output_stream.name = join(root, 'processed.js')
        sourcemap_stream = StringIO()
        sourcemap_stream.name = join(root, 'processed.js.map')

        unparser = BaseUnparser(definitions)
        io.write(unparser,
                 program,
                 output_stream,
                 sourcemap_stream,
                 source_mapping_url=None)

        sourcemap = json.loads(sourcemap_stream.getvalue())
        self.assertEqual(
            {
                "version": 3,
                "sources": ["original.js"],
                "names": [],
                "mappings": "AAAA,GAAI,CAAE",
                "file": "processed.js"
            }, sourcemap)
        self.assertEqual('foo=true', output_stream.getvalue())
Exemplo n.º 4
0
    def test_write_callables(self):
        closed = []

        class Stream(StringIO):
            # don't actually close the stream so it can be read later by
            # the tests
            def close(self):
                closed.append(self)

        # streams
        root = mktemp()
        output_stream = Stream()
        output_stream.name = join(root, 'packed.js')
        sourcemap_stream = Stream()
        sourcemap_stream.name = join(root, 'packed.js.map')

        def f_output_stream():
            return output_stream

        def f_sourcemap_stream():
            return sourcemap_stream

        definitions = {
            'Node': (
                Attr(attr='left'),
                Text(value=' '),
                Attr(attr='op'),
                Text(value=' '),
                Attr(attr='right'),
                Text(value=';'),
            )
        }

        # the program node; attributes are assigned to mimic a real one
        program1 = Node()
        program1.left, program1.op, program1.right = ('foo', '=', 'true')
        program1.sourcepath = join(root, 'program1.js')
        program1._token_map = {
            'foo': [(0, 1, 1)],
            '=': [(4, 1, 5)],
            'true': [(6, 1, 7)],
        }
        program2 = Node()
        program2.left, program2.op, program2.right = ('bar', '=', 'false')
        program2.sourcepath = join(root, 'program2.js')
        program2._token_map = {
            'bar': [(0, 1, 1)],
            '=': [(4, 1, 5)],
            'false': [(6, 1, 7)],
        }

        unparser = BaseUnparser(definitions)
        io.write(unparser, [program1, program2],
                 f_output_stream,
                 f_sourcemap_stream,
                 source_mapping_url=None)

        self.assertIn(output_stream, closed)
        self.assertIn(sourcemap_stream, closed)
        self.assertEqual('foo = true;bar = false;', output_stream.getvalue())

        sourcemap = json.loads(sourcemap_stream.getvalue())
        self.assertEqual(
            {
                "version": 3,
                "sources": ["program1.js", "program2.js"],
                "names": [],
                "mappings": "AAAA,WCAA",
                "file": "packed.js"
            }, sourcemap)