def check_variants(self, variants): # Check for duplicate variants warned = set() for left in range(len(variants) - 1): if left in warned: continue left_key = variants[left].key key_string = None for right in range(left + 1, len(variants)): if left_key.equals(variants[right].key): if key_string is None: key_string = serialize_variant_key(left_key) self.messages.append(('warning', left_key.span.start, MSGS['duplicate-variant'].format( name=key_string))) warned.add(right) self.messages.append( ('warning', variants[right].key.span.start, MSGS['duplicate-variant'].format(name=key_string))) # Check for plural categories known_plurals = plurals.get_plural(self.locale) if known_plurals: known_plurals = set(known_plurals) # Ask for known plurals, but check for plurals w/out `other`. # `other` is used for all kinds of things. check_plurals = known_plurals.copy() check_plurals.discard('other') given_plurals = set(serialize_variant_key(v.key) for v in variants) if given_plurals & check_plurals: missing_plurals = sorted(known_plurals - given_plurals) if missing_plurals: self.messages.append( ('warning', variants[0].key.span.start, MSGS['missing-plural'].format( categories=', '.join(missing_plurals))))
def test_invalid_expression(self): with self.assertRaisesRegex(Exception, 'Unknown variant key type'): serialize_variant_key(None) with self.assertRaisesRegex(Exception, 'Unknown variant key type'): serialize_variant_key(object())
def pretty_variant_key(text, index): parser = FluentParser() entry = parser.parse_entry(dedent_ftl(text)) variants = entry.value.elements[0].expression.variants return serialize_variant_key(variants[index].key)