示例#1
0
    def test_differ_with_spans(self):
        parser = FluentParser()

        strings = [
            ("foo = Foo", "foo =   Foo"),
            ("foo = { $arg }", "foo = {  $arg  }"),
        ]

        messages = [(parser.parse_entry(a), parser.parse_entry(b))
                    for a, b in strings]

        for a, b in messages:
            self.assertFalse(a.equals(b, ignored_fields=None))
示例#2
0
    def test_parser_without_spans(self):
        parser = FluentParser(with_spans=False)

        strings = [
            ("foo = Foo", "foo = Foo"),
            ("foo = Foo", "foo =   Foo"),
            ("foo = { $arg }", "foo = {  $arg  }"),
        ]

        messages = [(parser.parse_entry(a), parser.parse_entry(b))
                    for a, b in strings]

        for a, b in messages:
            self.assertTrue(a.equals(b))
示例#3
0
    def test_default_behavior(self):
        parser = FluentParser()

        strings = [
            ("foo = Foo", "foo = Foo"),
            ("foo = Foo", "foo =   Foo"),
            ("foo = { $arg }", "foo = {  $arg  }"),
        ]

        messages = [(parser.parse_entry(a), parser.parse_entry(b))
                    for a, b in strings]

        for a, b in messages:
            self.assertTrue(a.equals(b))
示例#4
0
class TestOrderEquals(unittest.TestCase):
    def setUp(self):
        self.parser = FluentParser()

    def parse_ftl_entry(self, string):
        return self.parser.parse_entry(dedent_ftl(string))

    def test_attributes(self):
        message1 = self.parse_ftl_entry("""\
            foo
                .attr1 = Attr1
                .attr2 = Attr2
        """)
        message2 = self.parse_ftl_entry("""\
            foo
                .attr2 = Attr2
                .attr1 = Attr1
        """)

        self.assertTrue(message1.equals(message2))
        self.assertTrue(message2.equals(message1))

    def test_variants(self):
        message1 = self.parse_ftl_entry("""\
            foo =
                { $num ->
                    [a] A
                   *[b] B
                }
        """)
        message2 = self.parse_ftl_entry("""\
            foo =
                { $num ->
                   *[b] B
                    [a] A
                }
        """)

        self.assertTrue(message1.equals(message2))
        self.assertTrue(message2.equals(message1))

    def test_variants_with_numbers(self):
        message1 = self.parse_ftl_entry("""\
            foo =
                { $num ->
                    [1] A
                   *[b] B
                }
        """)
        message2 = self.parse_ftl_entry("""\
            foo =
                { $num ->
                   *[b] B
                    [1] A
                }
        """)

        self.assertTrue(message1.equals(message2))
        self.assertTrue(message2.equals(message1))
示例#5
0
class TestIgnoredFields(unittest.TestCase):
    def setUp(self):
        self.parser = FluentParser()

    def parse_ftl_entry(self, string):
        return self.parser.parse_entry(dedent_ftl(string))

    def test_ignore_value(self):
        a = self.parse_ftl_entry("foo = Foo")
        b = self.parse_ftl_entry("foo = Bar")

        self.assertTrue(a.equals(b, ignored_fields=['value']))

    def test_ignore_value_span(self):
        a = self.parse_ftl_entry("foo = Foo")
        b = self.parse_ftl_entry("foo = Foobar")

        self.assertTrue(a.equals(b, ignored_fields=['span', 'value']))
        self.assertFalse(a.equals(b, ignored_fields=['value']))

    def test_ignore_comments(self):
        a = self.parse_ftl_entry("""\
            # Comment A
            foo = Foo
        """)
        b = self.parse_ftl_entry("""\
            # Comment B
            foo = Foo
        """)
        c = self.parse_ftl_entry("""\
            # Comment CC
            foo = Foo
        """)

        self.assertTrue(a.equals(b, ignored_fields=['comment']))
        self.assertFalse(a.equals(c, ignored_fields=['comment']))
        self.assertTrue(a.equals(c, ignored_fields=['comment', 'span']))
示例#6
0
class TestEntryEqualToSelf(unittest.TestCase):
    def setUp(self):
        self.parser = FluentParser()

    def parse_ftl_entry(self, string):
        return self.parser.parse_entry(dedent_ftl(string))

    def test_same_simple_message(self):
        message1 = self.parse_ftl_entry("""\
            foo = Foo
        """)

        self.assertTrue(message1.equals(message1))
        self.assertTrue(message1.equals(message1.clone()))

    def test_same_selector_message(self):
        message1 = self.parse_ftl_entry("""\
            foo =
                { $num ->
                    [one] One
                    [two] Two
                    [few] Few
                    [many] Many
                   *[other] Other
                }
        """)

        self.assertTrue(message1.equals(message1))
        self.assertTrue(message1.equals(message1.clone()))

    def test_same_complex_placeable_message(self):
        message1 = self.parse_ftl_entry("""\
            foo = Foo { NUMBER($num, style: "decimal") } Bar
        """)

        self.assertTrue(message1.equals(message1))
        self.assertTrue(message1.equals(message1.clone()))

    def test_same_message_with_attribute(self):
        message1 = self.parse_ftl_entry("""\
            foo =
                .attr = Attr
        """)

        self.assertTrue(message1.equals(message1))
        self.assertTrue(message1.equals(message1.clone()))

    def test_same_message_with_attributes(self):
        message1 = self.parse_ftl_entry("""\
            foo =
                .attr1 = Attr 1
                .attr2 = Attr 2
        """)

        self.assertTrue(message1.equals(message1))
        self.assertTrue(message1.equals(message1.clone()))

    def test_same_junk(self):
        message1 = self.parse_ftl_entry("""\
            foo = Foo {
        """)

        self.assertTrue(message1.equals(message1))
        self.assertTrue(message1.equals(message1.clone()))
示例#7
0
class TestParseEntry(unittest.TestCase):
    maxDiff = None

    def setUp(self):
        self.parser = FluentParser(with_spans=False)

    def test_simple_message(self):
        input = """\
            foo = Foo
        """
        output = {
            "comment": None,
            "value": {
                "elements": [{
                    "span": None,
                    "type": "TextElement",
                    "value": "Foo"
                }],
                "span": None,
                "type": "Pattern"
            },
            "attributes": [],
            "type": "Message",
            "span": None,
            "id": {
                "span": None,
                "type": "Identifier",
                "name": "foo"
            }
        }

        message = self.parser.parse_entry(dedent_ftl(input))
        self.assertEqual(message.to_json(), output)

    def test_ignore_attached_comment(self):
        input = """\
            # Attached Comment
            foo = Foo
        """
        output = {
            "comment": None,
            "value": {
                "elements": [{
                    "span": None,
                    "type": "TextElement",
                    "value": "Foo"
                }],
                "span": None,
                "type": "Pattern"
            },
            "attributes": [],
            "type": "Message",
            "id": {
                "name": "foo",
                "span": None,
                "type": "Identifier"
            },
            "span": None,
            "type": "Message"
        }

        message = self.parser.parse_entry(dedent_ftl(input))
        self.assertEqual(message.to_json(), output)

    def test_return_junk(self):
        input = """\
            # Attached Comment
            junk
        """
        output = {
            "content":
            "junk\n",
            "annotations": [{
                "arguments": ["="],
                "code": "E0003",
                "message": "Expected token: \"=\"",
                "span": {
                    "end": 23,
                    "start": 23,
                    "type": "Span"
                },
                "type": "Annotation"
            }],
            "span":
            None,
            "type":
            "Junk"
        }

        message = self.parser.parse_entry(dedent_ftl(input))
        self.assertEqual(message.to_json(), output)

    def test_ignore_all_valid_comments(self):
        input = """\
            # Attached Comment
            ## Group Comment
            ### Resource Comment
            foo = Foo
        """
        output = {
            "comment": None,
            "value": {
                "elements": [{
                    "span": None,
                    "type": "TextElement",
                    "value": "Foo"
                }],
                "span": None,
                "type": "Pattern"
            },
            "attributes": [],
            "span": None,
            "type": "Message",
            "id": {
                "name": "foo",
                "span": None,
                "type": "Identifier"
            }
        }

        message = self.parser.parse_entry(dedent_ftl(input))
        self.assertEqual(message.to_json(), output)

    def test_do_not_ignore_invalid_comments(self):
        input = """\
        # Attached Comment
        ##Invalid Comment
        """
        output = {
            "content":
            "##Invalid Comment\n",
            "annotations": [{
                "arguments": [" "],
                "code": "E0003",
                "message": "Expected token: \" \"",
                "span": {
                    "end": 21,
                    "start": 21,
                    "type": "Span"
                },
                "type": "Annotation"
            }],
            "span":
            None,
            "type":
            "Junk"
        }

        message = self.parser.parse_entry(dedent_ftl(input))
        self.assertEqual(message.to_json(), output)
示例#8
0
def parse_literal(input):
    parser = FluentParser(with_spans=False)
    ast = parser.parse_entry(input)
    expr = ast.value.elements[0].expression
    return expr.parse()