Skip to content

Commit

Permalink
test: move all substitute_vars tests into test_substitute_vars.py
Browse files Browse the repository at this point in the history
Right now there are some tests defined in `test_substitute_vars.py`
and some in `test_directive.py`. Move them all into a single file.
  • Loading branch information
mvo5 committed Jun 21, 2024
1 parent ccdebbd commit 0aa8366
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
28 changes: 1 addition & 27 deletions test/test_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from otk.context import CommonContext
from otk.error import TransformDirectiveArgumentError, TransformDirectiveTypeError
from otk.directive import substitute_vars, include, op_join
from otk.directive import include, op_join


def test_include_unhappy():
Expand Down Expand Up @@ -67,29 +67,3 @@ def test_op_map_join():
d = {"values": [d1, d2]}

assert op_join(ctx, d) == {"foo": "bar", "bar": "foo"}


def test_substitute_vars():
ctx = CommonContext()
ctx.define("str", "bar")
ctx.define("int", 1)
ctx.define("float", 1.1)

assert substitute_vars(ctx, "") == ""
assert substitute_vars(ctx, "${str}") == "bar"
assert substitute_vars(ctx, "a${str}b") == "abarb"
assert substitute_vars(ctx, "${int}") == 1
assert substitute_vars(ctx, "a${int}b") == "a1b"
assert substitute_vars(ctx, "${float}") == 1.1
assert substitute_vars(ctx, "a${float}b") == "a1.1b"


def test_substitute_vars_unhappy():
ctx = CommonContext()
ctx.define("dict", {})

with pytest.raises(TransformDirectiveTypeError):
substitute_vars(ctx, 1)

with pytest.raises(TransformDirectiveTypeError):
substitute_vars(ctx, "a${dict}b")
41 changes: 35 additions & 6 deletions test/test_substitute_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def test_simple_sub_var_nested_fail():
context.define("my_var", [1, 2])
data = "a${my_var}"

expected_error = re.escape(f"string '{data}' resolves to an incorrect type, "
"expected int, float, or str but got list")
expected_error = re.escape(
f"string '{data}' resolves to an incorrect type, " "expected int, float, or str but got list"
)

with pytest.raises(TransformDirectiveTypeError, match=expected_error):
substitute_vars(context, data)
Expand All @@ -48,8 +49,9 @@ def test_sub_var_multiple_fail():
data = "${a}-${b}"

# the a will be replaced but the b will cause an error
expected_error = re.escape(r"string 'foo-${b}' resolves to an incorrect type, "
"expected int, float, or str but got list")
expected_error = re.escape(
r"string 'foo-${b}' resolves to an incorrect type, " "expected int, float, or str but got list"
)

# Fails due to non-str type
with pytest.raises(TransformDirectiveTypeError, match=expected_error):
Expand All @@ -58,9 +60,36 @@ def test_sub_var_multiple_fail():
data = "${a}-${c}"

# the a will be replaced but the c will cause an error
expected_error = re.escape(r"string 'foo-${c}' resolves to an incorrect type, "
"expected int, float, or str but got dict")
expected_error = re.escape(
r"string 'foo-${c}' resolves to an incorrect type, " "expected int, float, or str but got dict"
)

# Fails due to non-str type
with pytest.raises(TransformDirectiveTypeError, match=expected_error):
substitute_vars(context, data)


def test_substitute_vars():
ctx = CommonContext()
ctx.define("str", "bar")
ctx.define("int", 1)
ctx.define("float", 1.1)

assert substitute_vars(ctx, "") == ""
assert substitute_vars(ctx, "${str}") == "bar"
assert substitute_vars(ctx, "a${str}b") == "abarb"
assert substitute_vars(ctx, "${int}") == 1
assert substitute_vars(ctx, "a${int}b") == "a1b"
assert substitute_vars(ctx, "${float}") == 1.1
assert substitute_vars(ctx, "a${float}b") == "a1.1b"


def test_substitute_vars_unhappy():
ctx = CommonContext()
ctx.define("dict", {})

with pytest.raises(TransformDirectiveTypeError):
substitute_vars(ctx, 1)

with pytest.raises(TransformDirectiveTypeError):
substitute_vars(ctx, "a${dict}b")

0 comments on commit 0aa8366

Please sign in to comment.