def test_check_templates_spot_duplicates(): """ check_templates spots duplicate keys in different subdirectories """ test_files = { "dir1/": { "en-gb.json": { "key1": "hippopotamus", "key2": "giraffe" } }, "dir2/": { "en-gb.json": { "key1": "hippopotamus", # duplicate but equal string "key2": "camel" # inconsistent string } } } expected = {"key1": "hippopotamus", "key2": "giraffe"} with throwaway_dir() as dirpath: populate_with_fake_files(dirpath, test_files) string_store = check_templates.StringStore() string_store.scan_json_files(dirpath, "en-gb.json", nested=True) assert string_store.all_strings == expected assert string_store.string_owner == {"key1": "dir1", "key2": "dir1"} assert len(string_store.problems) == 2 for problem in string_store.problems: assert problem.desc == "Duplicate (from dir1)"
def test_check_templates_find_strings(): """ check_templates finds translatable strings embedded in various ways """ test_files = { "test/": { "syntax1.html": """ <div> {% ptrans test-key1 %} body1 {% endptrans %} {% ptrans test-key--5 %} body5 {% endptrans %} </div> """, "quotes.html": ''' <div> {{ ptrans_get(locale, 'test-key2', 'body2') }} {{ ptrans_get(locale, "test-key3", "body3") }} {{ ptrans_get(locale, "test-key4", """ multi-line body4 {insert} """, insert=0) }} </div> ''' } } expected = { "test-key1": "body1", "test-key2": "body2", "test-key3": "body3", "test-key4": "multi-line body4 {insert}", "test-key--5": "body5", } with throwaway_dir() as dirpath: populate_with_fake_files(dirpath, test_files) string_store = check_templates.StringStore() string_store.scan_templates(dirpath) assert string_store.all_strings == expected assert string_store.new_strings == set(expected) # all new
def test_check_templates_spot_changed_string(): """ check_templates can spot strings different in template and JSON file """ test_files = { "templates/": { "test/": { "key1.html": """ <div>{% ptrans key1 %}rhinoceros{% endptrans %}</div> """ }, }, "localisation/": { "test/": { "en-gb.json": { "key1": { "value": "hippopotamus", "comment": "why not" } }, } } } with throwaway_dir() as dirpath: populate_with_fake_files(dirpath, test_files) string_store = check_templates.StringStore() string_store.scan_json_files(os.path.join(dirpath, "localisation"), "en-gb.json", nested=True) assert string_store.all_strings == {"key1": "hippopotamus"} string_store.scan_templates(os.path.join(dirpath, "templates")) assert len(string_store.problems) == 1 problem = string_store.problems[0] assert problem.strid == "key1" assert problem.body == "rhinoceros" assert problem.desc == "Changed string?" assert problem.serious is True