def test_tracebacks_for_exceptions(self):
        bundle = FluentBundle('en-US', [
            FtlResource(dedent_ftl('''
            foo = { $arg }
            '''),
                        filename='firstfile.ftl'),
            FtlResource(dedent_ftl('''

            bar = { $arg }
            '''),
                        filename='secondfile.ftl')
        ])

        # Check what our tracebacks produce if we have an error. This is hard to
        # do, since we catch most errors, so we construct a special value that
        # will blow up despite our best efforts.

        class BadType(FluentNumber, int):
            def format(self, locale):
                1 / 0

        for filename, msg_id, line_number in [("firstfile.ftl", "foo", 2),
                                              ("secondfile.ftl", "bar", 3)]:
            try:
                val, errs = bundle.format(msg_id, {'arg': BadType(0)})
            except ZeroDivisionError:
                tb = traceback.format_exc()
                # We don't get line numbers, but we can at least display the
                # source FTL file and function name
                self.assertIn(
                    'File "{0}", line {1}, in {2}'.format(
                        filename, line_number, msg_id), tb)
            else:
                self.fail('Expected ZeroDivisionError')
 def test_check_messages_junk(self):
     bundle = FluentBundle(
         'en-US', [FtlResource("unfinished", filename='myfile.ftl')])
     checks = bundle.check_messages()
     self.assertEqual(len(checks), 1)
     check1_name, check1_error = checks[0]
     self.assertEqual(check1_name, None)
     self.assertEqual(type(check1_error), FluentJunkFound)
     self.assertEqual(
         check1_error.message,
         'Junk found:\n  myfile.ftl:1:11: Expected token: "="')
     self.assertEqual(check1_error.annotations[0].message,
                      'Expected token: "="')
    def test_has_message_for_term(self):
        bundle = FluentBundle.from_string(
            'en-US', dedent_ftl("""
            -foo = Foo
        """))

        self.assertFalse(bundle.has_message('-foo'))
    def test_format_args(self):
        bundle = FluentBundle.from_string('en-US', 'foo = Foo')
        val, errs = bundle.format('foo')
        self.assertEqual(val, 'Foo')

        val, errs = bundle.format('foo', {})
        self.assertEqual(val, 'Foo')
 def test_format_term(self):
     bundle = FluentBundle.from_string(
         'en-US', dedent_ftl("""
         -foo = Foo
     """))
     self.assertRaises(LookupError, bundle.format, '-foo')
     self.assertRaises(LookupError, bundle.format, 'foo')
    def setUp(self):
        self.bundle = FluentBundle.from_string('en-US', dedent_ftl("""
            message = Message
                    .attr = Message Attribute
            -term = Term
                  .attr = Term Attribute
            -term2 = {
               *[variant1] Term Variant 1
                [variant2] Term Variant 2
             }

            uses-message = { message }
            uses-message-attr = { message.attr }
            uses-term = { -term }

            bad-message-ref = Text { not-a-message }
            bad-message-attr-ref = Text { message.not-an-attr }
            bad-term-ref = Text { -not-a-term }

            self-referencing-message = Text { self-referencing-message }
            cyclic-msg1 = Text1 { cyclic-msg2 }
            cyclic-msg2 = Text2 { cyclic-msg1 }
            self-cyclic-message = Parent { self-cyclic-message.attr }
                                .attr = Attribute { self-cyclic-message }

            self-attribute-ref-ok = Parent { self-attribute-ref-ok.attr }
                                  .attr = Attribute
            self-parent-ref-ok = Parent
                               .attr =  Attribute { self-parent-ref-ok }
            -cyclic-term = { -cyclic-term }
            cyclic-term-message = { -cyclic-term }
        """), use_isolating=False)
Example #7
0
    def setUp(self):
        self.bundle = FluentBundle.from_string('en-US',
                                               dedent_ftl("""
            -brand = Cool Thing
                .status = { $version ->
                    [v2]     available
                   *[v1]     deprecated
                }

            attr-with-arg = { -brand } is { -brand.status(version: "v2") ->
                 [available]   available, yay!
                *[deprecated]  deprecated, sorry
            }

            -other = { $arg ->
                        [a]  ABC
                       *[d]  DEF
                     }

            missing-attr-ref = { -other.missing(arg: "a") ->
                 [ABC]  ABC option
                *[DEF]  DEF option
            }
        """),
                                               use_isolating=False)
 def setUp(self):
     self.bundle = FluentBundle.from_string(
         'en-US',
         dedent_ftl("""
         -brand-short-name = Amaya
         foo = { -brand-short-name }
         with-arg = { $arg }
     """))
Example #9
0
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         msg = Msg is {$arg}
         -foo = {msg}
         ref-foo = {-foo(arg: 1)}
     """),
                                            use_isolating=False)
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         implicit-call    = { $date }
         explicit-call    = { DATETIME($date) }
         call-with-arg    = { DATETIME($date, dateStyle: "long") }
     """),
                                            use_isolating=False)
Example #11
0
 def test_check_messages_duplicate(self):
     bundle = FluentBundle.from_string('en-US', "foo = Foo\n" "foo = Bar\n")
     checks = bundle.check_messages()
     self.assertEqual(checks,
                      [('foo',
                        FluentDuplicateMessageId(
                            "Additional definition for 'foo' discarded."))])
     # Earlier takes precedence
     self.assertEqual(bundle.format('foo')[0], 'Foo')
 def setUp(self):
     self.bundle = FluentBundle.from_string(
         'en-US',
         dedent_ftl("""
         foo = Foo
         bar = { foo } Bar
         baz = { $arg } Baz
         qux = { bar } { baz }
     """))
Example #13
0
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         -foo = {$a} {$b}
         -bar = {-foo(b: 2)}
         -baz = {-foo}
         ref-bar = {-bar(a: 1)}
         ref-baz = {-baz(a: 1)}
     """),
                                            use_isolating=False)
Example #14
0
 def test_message_and_term_separate(self):
     bundle = FluentBundle.from_string(
         'en-US',
         dedent_ftl("""
         foo = Refers to { -foo }
         -foo = Foo
     """))
     val, errs = bundle.format('foo', {})
     self.assertEqual(val, 'Refers to \u2068Foo\u2069')
     self.assertEqual(errs, [])
Example #15
0
    def test_has_message(self):
        bundle = FluentBundle.from_string(
            'en-US',
            dedent_ftl("""
            foo = Foo
            -term = Term
        """))

        self.assertTrue(bundle.has_message('foo'))
        self.assertFalse(bundle.has_message('bar'))
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         foo = Foo
             .attr = Foo Attribute
         bar = { foo } Bar
             .attr = Bar Attribute
         ref-foo = { foo.attr }
         ref-bar = { bar.attr }
     """),
                                            use_isolating=True)
Example #17
0
 def test_with_argument_expression(self):
     bundle = FluentBundle.from_string(
         'en-US',
         dedent_ftl("""
         foo = { $arg ->
             [a] A
            *[b] B
          }
     """))
     val, errs = bundle.format('foo', {'arg': 'a'})
     self.assertEqual(val, "A")
Example #18
0
    def test_has_message_with_attribute(self):
        bundle = FluentBundle.from_string(
            'en-US',
            dedent_ftl("""
            foo = Foo
                .attr = Foo Attribute
        """))

        self.assertTrue(bundle.has_message('foo'))
        self.assertFalse(bundle.has_message('foo.attr'))
        self.assertFalse(bundle.has_message('foo.other-attribute'))
Example #19
0
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         one           =  { 1 }
         one_point_two =  { 1.2 }
         select        =  { 1 ->
            *[0] Zero
             [1] One
          }
     """),
                                            use_isolating=False)
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         foo = Foo { $num }
         bar = { foo }
         baz =
             .attr = Baz Attribute { $num }
         qux = { "a" ->
            *[a]     Baz Variant A { $num }
          }
     """),
                                            use_isolating=False)
Example #21
0
 def test_with_a_non_matching_selector(self):
     bundle = FluentBundle.from_string(
         'en-US',
         dedent_ftl("""
         foo = { "c" ->
             [a] A
            *[b] B
          }
     """))
     val, errs = bundle.format('foo', {})
     self.assertEqual(val, "B")
     self.assertEqual(errs, [])
Example #22
0
    def test_check_messages_compile_errors(self):
        bundle = FluentBundle('en-US', [
            FtlResource(dedent_ftl('''
        foo = { -missing }
            .bar = { -missing }
        '''),
                        filename='myfile.ftl')
        ])
        checks = bundle.check_messages()
        self.assertEqual(len(checks), 2)
        check1_name, check1_error = checks[0]
        self.assertEqual(check1_name, 'foo')
        self.assertEqual(type(check1_error), FluentReferenceError)
        self.assertEqual(check1_error.args[0],
                         'myfile.ftl:2:9: Unknown term: -missing')

        check2_name, check2_error = checks[1]
        self.assertEqual(check2_name, 'foo.bar')
        self.assertEqual(type(check2_error), FluentReferenceError)
        self.assertEqual(check2_error.args[0],
                         'myfile.ftl:3:14: Unknown term: -missing')
Example #23
0
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         -thing = { $count ->
               *[1] one thing
                [2] two things
         }
         thing-no-arg = { -thing }
         thing-no-arg-alt = { -thing() }
         thing-one = { -thing(count: 1) }
         thing-two = { -thing(count: 2) }
     """),
                                            use_isolating=False)
Example #24
0
    def test_external_arguments_in_variants(self):
        # We are testing several things:
        # - that [b] variant doesn't trigger 'Unknown external: arg'
        # - some logic in compiler implementation regarding when variables are looked up,
        #   so that [a] and [c] variants both can find 'arg'.
        bundle = FluentBundle.from_string(
            'en-US',
            dedent_ftl("""
            foo = { $lookup ->
                 [a]    { $arg }
                 [b]    B
                *[c]    { $arg }
             }
        """))
        # No args:
        val1, errs1 = bundle.format('foo', {})
        self.assertEqual(val1, "arg")
        self.assertEqual(errs1, [
            FluentReferenceError("<string>:2:9: Unknown external: lookup"),
            FluentReferenceError("<string>:5:15: Unknown external: arg"),
        ])

        # [a] branch, arg supplied
        val2, errs2 = bundle.format('foo', {'lookup': 'a', 'arg': 'A'})
        self.assertEqual(val2, "A")
        self.assertEqual(errs2, [])

        # [a] branch, arg not supplied
        val3, errs3 = bundle.format('foo', {'lookup': 'a'})
        self.assertEqual(val3, "arg")
        self.assertEqual(
            errs3,
            [FluentReferenceError("<string>:3:15: Unknown external: arg")])

        # [b] branch
        val4, errs4 = bundle.format('foo', {'lookup': 'b'})
        self.assertEqual(val4, "B")
        self.assertEqual(errs4, [])

        # [c] branch, arg supplied
        val5, errs5 = bundle.format('foo', {'lookup': 'c', 'arg': 'C'})
        self.assertEqual(val5, "C")
        self.assertEqual(errs5, [])

        # [c] branch, arg not supplied
        val6, errs6 = bundle.format('foo', {'lookup': 'c'})
        self.assertEqual(val6, "arg")
        self.assertEqual(
            errs6,
            [FluentReferenceError("<string>:5:15: Unknown external: arg")])
Example #25
0
 def test_with_a_missing_selector(self):
     bundle = FluentBundle.from_string(
         'en-US',
         dedent_ftl("""
         foo = { $none ->
             [a] A
            *[b] B
          }
     """))
     val, errs = bundle.format('foo', {})
     self.assertEqual(val, "B")
     self.assertEqual(
         errs,
         [FluentReferenceError("<string>:2:9: Unknown external: none")])
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         implicit-call    = { 123456 }
         implicit-call2   = { $arg }
         defaults         = { NUMBER(123456) }
         percent-style    = { NUMBER(1.234, style: "percent") }
         from-arg         = { NUMBER($arg) }
         merge-params     = { NUMBER($arg, useGrouping: 0) }
         bad-kwarg        = { NUMBER(1, badkwarg: 0) }
         bad-arity        = { NUMBER(1, 2) }
         currency-name    = { NUMBER($arg, currencyDisplay: "name") }
     """),
                                            use_isolating=False)
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         foo = Foo
         bar = Bar
             .attr = { foo } Attribute
         baz = { foo } Baz
             .attr = { foo } Attribute
         qux = Qux
             .attr = { qux } Attribute
         ref-bar = { bar.attr }
         ref-baz = { baz.attr }
         ref-qux = { qux.attr }
     """),
                                            use_isolating=False)
Example #28
0
    def setUp(self):
        self.args_passed = []

        def number_processor(number):
            self.args_passed.append(number)
            return number

        self.bundle = FluentBundle.from_string(
            'en-US',
            dedent_ftl("""
            pass-number = { NUMBER_PROCESSOR(1) }
            pass-arg = { NUMBER_PROCESSOR($arg) }
        """),
            use_isolating=False,
            functions={'NUMBER_PROCESSOR': number_processor},
        )
Example #29
0
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         -thing = { $article ->
               *[definite] the thing
                [indefinite] a thing
                [none] thing
         }
         thing-no-arg = { -thing }
         thing-no-arg-alt = { -thing() }
         thing-with-arg = { -thing(article: "indefinite") }
         thing-positional-arg = { -thing("foo") }
         thing-fallback = { -thing(article: "somethingelse") }
         bad-term = { -missing() }
     """),
                                            use_isolating=False)
 def setUp(self):
     self.bundle = FluentBundle.from_string('en-US',
                                            dedent_ftl("""
         foo = Foo
         bar = Bar
             .attr = Bar Attribute
         baz = { foo } Baz
         qux = { foo } Qux
             .attr = Qux Attribute
         ref-foo = { foo.missing }
         ref-bar = { bar.missing }
         ref-baz = { baz.missing }
         ref-qux = { qux.missing }
         attr-only =
                  .attr  = Attr Only Attribute
         ref-double-missing = { missing.attr }
     """),
                                            use_isolating=False)