diff --git a/fluent.runtime/fluent/runtime/resolver.py b/fluent.runtime/fluent/runtime/resolver.py index 1b41e450..bf95aa08 100644 --- a/fluent.runtime/fluent/runtime/resolver.py +++ b/fluent.runtime/fluent/runtime/resolver.py @@ -315,11 +315,17 @@ class VariantExpression(FTL.VariantExpression, BaseResolver): def __call__(self, env): message = lookup_reference(self.ref, env) - # TODO What to do if message is not a VariantList? - # Need test at least. - assert isinstance(message, VariantList) + if isinstance(message, FluentNoneResolver): + # We have already reported the reference error in lookup_reference + return message(env) variant_name = self.key.name + if not isinstance(message, VariantList): + # Term without variants, return term contents but also report error + env.errors.append(FluentReferenceError("Unknown variant: {0}" + .format(variant_name))) + return message(env) + return message(env, variant_name) diff --git a/fluent.runtime/tests/format/test_variants.py b/fluent.runtime/tests/format/test_variants.py index 01fdb6cd..5c15e656 100644 --- a/fluent.runtime/tests/format/test_variants.py +++ b/fluent.runtime/tests/format/test_variants.py @@ -17,10 +17,13 @@ def setUp(self): [a] A *[b] B } + -non-variant = Term with no variants foo = { -variant } bar = { -variant[a] } baz = { -variant[b] } qux = { -variant[c] } + goo = { -missing[a] } + quux = { -non-variant[a] } """)) def test_returns_the_default_variant(self): @@ -45,3 +48,18 @@ def test_choose_missing_variant(self): self.assertEqual( errs, [FluentReferenceError("Unknown variant: c")]) + + def test_choose_missing_term(self): + val, errs = self.ctx.format('goo', {}) + self.assertEqual(val, '-missing') + self.assertEqual(len(errs), 1) + self.assertEqual( + errs, + [FluentReferenceError("Unknown term: -missing")]) + + def test_variant_with_non_variant_term(self): + val, errs = self.ctx.format('quux', {}) + self.assertEqual(val, 'Term with no variants') + self.assertEqual( + errs, + [FluentReferenceError("Unknown variant: a")])