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")])
def test_falls_back_for_msg_with_pattern_value_and_no_attributes(self): val, errs = self.bundle.format('ref-baz', {}) self.assertEqual(val, 'Foo Baz') self.assertEqual(errs, [ FluentReferenceError( '<string>:10:13: Unknown attribute: baz.missing') ])
def test_falls_back_for_msg_with_pattern_value_and_other_attributes(self): val, errs = self.bundle.format('ref-qux', {}) self.assertEqual(val, 'Foo Qux') self.assertEqual(errs, [ FluentReferenceError( '<string>:11:13: Unknown attribute: qux.missing') ])
def test_placeable_bad_message_attr(self): val, errs = self.bundle.format('bad-message-attr-ref', {}) self.assertEqual(val, 'Text Message') self.assertEqual(len(errs), 1) self.assertEqual( errs, [FluentReferenceError("<string>:16:31: Unknown attribute: message.not-an-attr")])
def test_placeable_bad_term(self): val, errs = self.bundle.format('bad-term-ref', {}) self.assertEqual(val, 'Text -not-a-term') self.assertEqual(len(errs), 1) self.assertEqual( errs, [FluentReferenceError("<string>:17:23: Unknown term: -not-a-term")])
def test_from_arg_missing(self): val, errs = self.bundle.format('from-arg', {}) self.assertEqual(val, "arg") self.assertEqual(len(errs), 1) self.assertEqual( errs, [FluentReferenceError('<string>:6:29: Unknown external: arg')])
def test_placeable_bad_message(self): val, errs = self.bundle.format('bad-message-ref', {}) self.assertEqual(val, 'Text not-a-message') self.assertEqual(len(errs), 1) self.assertEqual( errs, [FluentReferenceError("<string>:15:26: Unknown message: not-a-message")])
def test_falls_back_for_msg_with_string_value_and_other_attributes(self): val, errs = self.bundle.format('ref-bar', {}) self.assertEqual(val, 'Bar') self.assertEqual(errs, [ FluentReferenceError( '<string>:9:13: Unknown attribute: bar.missing') ])
def test_missing_message_and_attribute(self): val, errs = self.bundle.format('ref-double-missing', {}) self.assertEqual(val, 'missing.attr') self.assertEqual(errs, [ FluentReferenceError( '<string>:14:24: Unknown attribute: missing.attr') ])
def test_falls_back_for_msg_with_string_value_and_no_attributes(self): val, errs = self.bundle.format('ref-foo', {}) self.assertEqual(val, 'Foo') self.assertEqual(errs, [ FluentReferenceError( '<string>:8:13: Unknown attribute: foo.missing') ])
def test_missing_attr(self): # We should fall back to the parent, and still pass the args. val, errs = self.bundle.format('missing-attr-ref', {}) self.assertEqual(val, 'ABC option') self.assertEqual(errs, [ FluentReferenceError( '<string>:18:22: Unknown attribute: -other.missing') ])
def test_ref_term_attribute_missing(self): val, errs = self.bundle.format('ref-term-attr-missing') self.assertEqual(val, "Other") self.assertEqual(len(errs), 1) self.assertEqual(errs, [ FluentReferenceError( '<string>:15:27: Unknown attribute: -my-term.missing') ])
def test_missing_function_call(self): code, errs = compile_messages_to_python( """ foo = { MISSING(123) } """, self.locale) self.assertCodeEqual( code, """ def foo(message_args, errors): errors.append(FluentReferenceError('Unknown function: MISSING')) return 'MISSING()' """), self.assertEqual( errs, [('foo', FluentReferenceError('Unknown function: MISSING'))])
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 test_single_message_bad_reference(self): code, errs = compile_messages_to_python( """ bar = { foo } """, self.locale) # We already know that foo does not exist, so we can hard code the error # into the function for the runtime error. self.assertCodeEqual( code, """ def bar(message_args, errors): errors.append(FluentReferenceError('<string>:2:9: Unknown message: foo')) return 'foo' """) # And we should get a compile time error: self.assertEqual(errs, [ ('bar', FluentReferenceError("<string>:2:9: Unknown message: foo")) ])
def test_with_a_missing_selector(self): val, errs = self.bundle.format('foo-arg', {}) self.assertEqual(val, "B") self.assertEqual( errs, [FluentReferenceError("<string>:7:13: Unknown external: count")])
def test_term_with_missing_term_reference(self): val, errs = self.bundle.format('uses-bad-term', {}) self.assertEqual(val, 'Something wrong -missing') self.assertEqual( errs, [FluentReferenceError('<string>:13:33: Unknown term: -missing', )])
def test_falls_back_to_name_of_function(self): val, errs = self.bundle.format("missing", {}) self.assertEqual(val, "MISSING()") self.assertEqual(errs, [FluentReferenceError("Unknown function: MISSING")])
def test_missing_with_no_args_dict(self): val, errs = self.bundle.format('foo') self.assertEqual(val, 'arg') self.assertEqual( errs, [FluentReferenceError('<string>:2:9: Unknown external: arg')])
def test_bad_term(self): val, errs = self.bundle.format('bad-term', {}) self.assertEqual(val, '-missing') self.assertEqual( errs, [FluentReferenceError('<string>:12:14: Unknown term: -missing')])
def test_with_a_missing_selector(self): val, errs = self.bundle.format('baz', {}) self.assertEqual(val, "A") self.assertEqual( errs, [FluentReferenceError("<string>:12:9: Unknown external: num")])