Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed regression with variant expressions involving missing terms #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions fluent.runtime/fluent/runtime/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
18 changes: 18 additions & 0 deletions fluent.runtime/tests/format/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")])