From bb18cc0419177ba24388e6525ddedd2a0ecfce51 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Sun, 3 Mar 2019 05:12:02 +0300 Subject: [PATCH 1/2] Fixed regression with variant expressions involving missing terms --- fluent.runtime/fluent/runtime/resolver.py | 6 +++--- fluent.runtime/tests/format/test_variants.py | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/fluent.runtime/fluent/runtime/resolver.py b/fluent.runtime/fluent/runtime/resolver.py index 1b41e450..27c3ab41 100644 --- a/fluent.runtime/fluent/runtime/resolver.py +++ b/fluent.runtime/fluent/runtime/resolver.py @@ -315,9 +315,9 @@ 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 not isinstance(message, VariantList): + # Must be FluentNoneResolver + return message(env) variant_name = self.key.name 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..67e91988 100644 --- a/fluent.runtime/tests/format/test_variants.py +++ b/fluent.runtime/tests/format/test_variants.py @@ -21,6 +21,7 @@ def setUp(self): bar = { -variant[a] } baz = { -variant[b] } qux = { -variant[c] } + goo = { -missing[a] } """)) def test_returns_the_default_variant(self): @@ -45,3 +46,11 @@ 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")]) From c854401f9e9d59e77076f38f744743bf4515f317 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Mon, 4 Mar 2019 11:13:02 +0300 Subject: [PATCH 2/2] Fixed handling of VariantExpression used with terms that have no variants --- fluent.runtime/fluent/runtime/resolver.py | 10 ++++++++-- fluent.runtime/tests/format/test_variants.py | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/fluent.runtime/fluent/runtime/resolver.py b/fluent.runtime/fluent/runtime/resolver.py index 27c3ab41..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) - if not isinstance(message, VariantList): - # Must be FluentNoneResolver + 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 67e91988..5c15e656 100644 --- a/fluent.runtime/tests/format/test_variants.py +++ b/fluent.runtime/tests/format/test_variants.py @@ -17,11 +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): @@ -54,3 +56,10 @@ def test_choose_missing_term(self): 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")])