Exemple #1
0
def test_if_else():
    assert (templaty.evaluate("{% if True %}Yes!{% endif %}") == "Yes!")
    assert (templaty.evaluate("{% if False %}Yes!{% endif %}") == "")
    assert (templaty.evaluate("{% if True %}Yes!{% else %}No!{% endif %}") ==
            "Yes!")
    assert (templaty.evaluate("{% if False %}Yes!{% else %}No!{% endif %}") ==
            "No!")
Exemple #2
0
def test_elif():
    assert (templaty.evaluate(
        "{% if 1 == 2 %}Wrong.{% elif 1 == 1 %}Right{% endif %}") == "Right")
    assert (templaty.evaluate(
        "{% if 1 == 2 %}Wrong.{% elif 1 == 3 %}Still wrong.{% endif %}") == "")
    assert (templaty.evaluate(
        "{% if 1 == 2 %}Wrong.{% elif 1 == 3 %}Still wrong.{% else %}Right{% endif %}"
    ) == "Right")
Exemple #3
0
def do_save_cmd(args):
    for pattern in args.files:
        for path in glob(pattern):
            print(f"Saving {path} ...")
            with open(path, 'r') as f:
                contents = f.read()
            try:
                with open(path + '.output', 'r') as f:
                    expected = f.read()
            except FileNotFoundError:
                expected = ''
            new_expected = templaty.evaluate(contents, filename=path)
            if expected != new_expected:
                write_diff(expected, new_expected)
            with open(path + '.output', 'w') as f:
                f.write(new_expected)
    return 0
Exemple #4
0
def do_run_cmd(args):
    exit_code = 0
    for pattern in args.files if 'files' in args else ['test-snippets/*.tply']:
        for path in glob(pattern):
            filename = os.path.basename(path)
            print(f"Checking {filename} ...")
            with open(path, 'r') as f:
                contents = f.read()
            actual = templaty.evaluate(contents, filename=path)
            try:
                with open(path + '.output', 'r') as f:
                    expected = f.read()
            except FileNotFoundError:
                print(f"Warning: {filename} does not have an expected output. Run 'test.py save {path}' to add it.")
                write_diff('', actual)
                continue
            if actual != expected:
                print("Error: output does not correspond with saved state")
                write_diff(actual, expected)
                exit_code = 1
    return exit_code
Exemple #5
0
def test_variable_existence():
    assert (templaty.evaluate("{{'foo' in globals()}}", {'foo': 42}) == 'True')
    assert (templaty.evaluate("{{'foo' in globals()}}") == 'False')
Exemple #6
0
def test_slice_expression():
    assert (templaty.evaluate("{{foo[1:3][0]}}", {'foo': [1, 2, 3]}) == '2')
    assert (templaty.evaluate("{{foo[1:3][1]}}", {'foo': [1, 2, 3]}) == '3')
Exemple #7
0
def test_index_expression():
    assert (templaty.evaluate("{{foo[1]}}", {'foo': [1, 2, 3]}) == '2')
Exemple #8
0
def test_chain_operator():
    assert (templaty.evaluate("{{'foo-bar' |> snake |> upper}}") == 'FOO_BAR')
Exemple #9
0
def test_member_access():
    assert (templaty.evaluate(
        "{{foo.bar.baz}}",
        {'foo': SimpleNamespace(bar=SimpleNamespace(baz=42))}) == '42')