summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2021-08-01 18:07:40 +0100
committerMichael Williamson <mike@zwobble.org>2021-08-01 18:19:48 +0100
commit41ebdfae4caadeb9ca7d14ef8af45345dfe5099d (patch)
tree394c8bc8842b9e3fbb7349d20946920bf12117b8 /tests
parent7a9b0dbc049c1bedb5b8bb0c9a2b766e88fbdf51 (diff)
Replace nose with py.test
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/jq_old_tests.py48
-rw-r--r--tests/jq_tests.py115
-rw-r--r--tests/tools.py6
4 files changed, 60 insertions, 109 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/jq_old_tests.py b/tests/jq_old_tests.py
index ea3cabf..f7e1f0d 100644
--- a/tests/jq_old_tests.py
+++ b/tests/jq_old_tests.py
@@ -2,85 +2,74 @@
from __future__ import unicode_literals
-from nose.tools import istest, assert_equal, assert_raises
-
from jq import jq
+from .tools import assert_equal
-@istest
-def output_of_dot_operator_is_input():
+def test_output_of_dot_operator_is_input():
assert_equal(
"42",
jq(".").transform("42")
)
-@istest
-def can_add_one_to_each_element_of_an_array():
+def test_can_add_one_to_each_element_of_an_array():
assert_equal(
[2, 3, 4],
jq("[.[]+1]").transform([1, 2, 3])
)
-@istest
-def can_use_regexes():
+def test_can_use_regexes():
assert_equal(
True,
jq('test(".*")').transform("42")
)
-@istest
-def input_string_is_parsed_to_json_if_raw_input_is_true():
+def test_input_string_is_parsed_to_json_if_raw_input_is_true():
assert_equal(
42,
jq(".").transform(text="42")
)
-@istest
-def output_is_serialised_to_json_string_if_text_output_is_true():
+def test_output_is_serialised_to_json_string_if_text_output_is_true():
assert_equal(
'"42"',
jq(".").transform("42", text_output=True)
)
-@istest
-def elements_in_text_output_are_separated_by_newlines():
+def test_elements_in_text_output_are_separated_by_newlines():
assert_equal(
"1\n2\n3",
jq(".[]").transform([1, 2, 3], text_output=True)
)
-@istest
-def first_output_element_is_returned_if_multiple_output_is_false_but_there_are_multiple_output_elements():
+def test_first_output_element_is_returned_if_multiple_output_is_false_but_there_are_multiple_output_elements():
assert_equal(
2,
jq(".[]+1").transform([1, 2, 3])
)
-@istest
-def multiple_output_elements_are_returned_if_multiple_output_is_true():
+def test_multiple_output_elements_are_returned_if_multiple_output_is_true():
assert_equal(
[2, 3, 4],
jq(".[]+1").transform([1, 2, 3], multiple_output=True)
)
-@istest
-def multiple_inputs_in_raw_input_are_separated_by_newlines():
+def test_multiple_inputs_in_raw_input_are_separated_by_newlines():
assert_equal(
[2, 3, 4],
jq(".+1").transform(text="1\n2\n3", multiple_output=True)
)
-@istest
-def value_error_is_raised_if_program_is_invalid():
+def test_value_error_is_raised_if_program_is_invalid():
try:
jq("!")
assert False, "Expected error"
@@ -89,8 +78,7 @@ def value_error_is_raised_if_program_is_invalid():
assert_equal(str(error), expected_error_str)
-@istest
-def value_error_is_raised_if_input_cannot_be_processed_by_program():
+def test_value_error_is_raised_if_input_cannot_be_processed_by_program():
program = jq(".x")
try:
program.transform(1)
@@ -100,8 +88,7 @@ def value_error_is_raised_if_input_cannot_be_processed_by_program():
assert_equal(str(error), expected_error_str)
-@istest
-def errors_do_not_leak_between_transformations():
+def test_errors_do_not_leak_between_transformations():
program = jq(".x")
try:
program.transform(1)
@@ -112,8 +99,7 @@ def errors_do_not_leak_between_transformations():
assert_equal(1, program.transform({"x": 1}))
-@istest
-def value_error_is_raised_if_input_is_not_valid_json():
+def test_value_error_is_raised_if_input_is_not_valid_json():
program = jq(".x")
try:
program.transform(text="!!")
@@ -123,16 +109,14 @@ def value_error_is_raised_if_input_is_not_valid_json():
assert_equal(str(error), expected_error_str)
-@istest
-def unicode_strings_can_be_used_as_input():
+def test_unicode_strings_can_be_used_as_input():
assert_equal(
"‽",
jq(".").transform(text='"‽"')
)
-@istest
-def unicode_strings_can_be_used_as_programs():
+def test_unicode_strings_can_be_used_as_programs():
assert_equal(
"Dragon‽",
jq('.+"‽"').transform(text='"Dragon"')
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index 5b20391..b5bbbd1 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -2,29 +2,25 @@
from __future__ import unicode_literals
-from nose.tools import istest, assert_equal, assert_is, assert_raises
-
import jq
+from .tools import assert_equal, assert_is
-@istest
-def output_of_dot_operator_is_input():
+def test_output_of_dot_operator_is_input():
assert_equal(
"42",
jq.compile(".").input("42").first()
)
-@istest
-def can_add_one_to_each_element_of_an_array():
+def test_can_add_one_to_each_element_of_an_array():
assert_equal(
[2, 3, 4],
jq.compile("[.[]+1]").input([1, 2, 3]).first()
)
-@istest
-def can_use_regexes():
+def test_can_use_regexes():
assert_equal(
True,
jq.compile('test(".*")').input("42").first()
@@ -41,48 +37,42 @@ def can_use_regexes():
)
-@istest
-def when_text_argument_is_used_then_input_is_treated_as_json_text():
+def test_when_text_argument_is_used_then_input_is_treated_as_json_text():
assert_equal(
42,
jq.compile(".").input(text="42").first()
)
-@istest
-def when_text_method_is_used_on_result_then_output_is_serialised_to_json_string():
+def test_when_text_method_is_used_on_result_then_output_is_serialised_to_json_string():
assert_equal(
'"42"',
jq.compile(".").input("42").text()
)
-@istest
-def elements_in_text_output_are_separated_by_newlines():
+def test_elements_in_text_output_are_separated_by_newlines():
assert_equal(
"1\n2\n3",
jq.compile(".[]").input([1, 2, 3]).text()
)
-@istest
-def when_first_method_is_used_on_result_then_first_element_of_result_is_returned():
+def test_when_first_method_is_used_on_result_then_first_element_of_result_is_returned():
assert_equal(
2,
jq.compile(".[]+1").input([1, 2, 3]).first()
)
-@istest
-def when_all_method_is_used_on_result_then_all_elements_are_returned_in_list():
+def test_when_all_method_is_used_on_result_then_all_elements_are_returned_in_list():
assert_equal(
[2, 3, 4],
jq.compile(".[]+1").input([1, 2, 3]).all()
)
-@istest
-def can_treat_execute_result_as_iterable():
+def test_can_treat_execute_result_as_iterable():
iterator = iter(jq.compile(".[]+1").input([1, 2, 3]))
assert_equal(2, next(iterator))
assert_equal(3, next(iterator))
@@ -90,8 +80,7 @@ def can_treat_execute_result_as_iterable():
assert_equal("end", next(iterator, "end"))
-@istest
-def can_execute_same_program_again_before_consuming_output_of_first_execution():
+def test_can_execute_same_program_again_before_consuming_output_of_first_execution():
program = jq.compile(".[]+1")
first = iter(program.input([1, 2, 3]))
assert_equal(2, next(first))
@@ -103,8 +92,7 @@ def can_execute_same_program_again_before_consuming_output_of_first_execution():
assert_equal(14, next(second))
-@istest
-def iterators_from_same_program_and_input_are_independent():
+def test_iterators_from_same_program_and_input_are_independent():
program_with_input = jq.compile(".[]+1").input([1, 2, 3])
first = iter(program_with_input)
assert_equal(2, next(first))
@@ -116,16 +104,14 @@ def iterators_from_same_program_and_input_are_independent():
assert_equal(4, next(second))
-@istest
-def multiple_inputs_in_text_input_are_separated_by_newlines():
+def test_multiple_inputs_in_text_input_are_separated_by_newlines():
assert_equal(
[2, 3, 4],
jq.compile(".+1").input(text="1\n2\n3").all()
)
-@istest
-def value_error_is_raised_if_program_is_invalid():
+def test_value_error_is_raised_if_program_is_invalid():
try:
jq.compile("!")
assert False, "Expected error"
@@ -134,8 +120,7 @@ def value_error_is_raised_if_program_is_invalid():
assert_equal(str(error), expected_error_str)
-@istest
-def value_error_is_raised_if_input_cannot_be_processed_by_program():
+def test_value_error_is_raised_if_input_cannot_be_processed_by_program():
program = jq.compile(".x")
try:
program.input(1).all()
@@ -145,8 +130,7 @@ def value_error_is_raised_if_input_cannot_be_processed_by_program():
assert_equal(str(error), expected_error_str)
-@istest
-def errors_do_not_leak_between_transformations():
+def test_errors_do_not_leak_between_transformations():
program = jq.compile(".x")
try:
program.input(1).all()
@@ -157,8 +141,7 @@ def errors_do_not_leak_between_transformations():
assert_equal(1, program.input({"x": 1}).first())
-@istest
-def value_error_is_raised_if_input_is_not_valid_json():
+def test_value_error_is_raised_if_input_is_not_valid_json():
program = jq.compile(".x")
try:
program.input(text="!!").first()
@@ -168,58 +151,49 @@ def value_error_is_raised_if_input_is_not_valid_json():
assert_equal(str(error), expected_error_str)
-@istest
-def unicode_strings_can_be_used_as_input():
+def test_unicode_strings_can_be_used_as_input():
assert_equal(
"‽",
jq.compile(".").input(text='"‽"').first()
)
-@istest
-def unicode_strings_can_be_used_as_programs():
+def test_unicode_strings_can_be_used_as_programs():
assert_equal(
"Dragon‽",
jq.compile('.+"‽"').input(text='"Dragon"').first()
)
-@istest
-def compiling_with_args_should_set_predefined_variables():
+def test_compiling_with_args_should_set_predefined_variables():
assert_equal(
123,
jq.compile("$a + $b + .", args={"a": 100, "b": 20}).input(3).first()
)
-@istest
-def repr_of_compile_result_is_compilation_string():
+def test_repr_of_compile_result_is_compilation_string():
program = jq.compile(".")
repr_string = repr(program)
assert_equal("jq.compile({!r})".format("."), repr_string)
-@istest
-def program_string_can_be_retrieved_from_program():
+def test_program_string_can_be_retrieved_from_program():
program = jq.compile(".")
assert_equal(".", program.program_string)
-@istest
class TestJvToPython(object):
- @istest
- def program_preserves_null(self):
+ def test_program_preserves_null(self):
program = jq.compile(".")
assert_is(None, program.input(text='null').first())
- @istest
- def program_preserves_bools(self):
+ def test_program_preserves_bools(self):
program = jq.compile(".")
assert_is(False, program.input(text="false").first())
assert_is(True, program.input(text="true").first())
- @istest
- def program_preserves_ints(self):
+ def test_program_preserves_ints(self):
program = jq.compile(".")
assert_equal(int, type(program.input(text="0").first()))
assert_equal(0, program.input(text="0").first())
@@ -228,8 +202,7 @@ class TestJvToPython(object):
assert_equal(12345, program.input(text="12345").first())
assert_equal(-12345, program.input(text="-12345").first())
- @istest
- def program_preserves_floats(self):
+ def test_program_preserves_floats(self):
program = jq.compile(".")
assert_equal(float, type(program.input(text="1.1").first()))
assert_equal(3.14159, program.input(text="3.14159").first())
@@ -237,16 +210,14 @@ class TestJvToPython(object):
assert_equal(42E100, program.input(text="42E100").first())
assert_equal(-42E100, program.input(text="-42E100").first())
- @istest
- def program_preserves_strings(self):
+ def test_program_preserves_strings(self):
program = jq.compile(".")
assert_equal(type(""), type(program.input(text='""').first()))
assert_equal("", program.input(text='""').first())
assert_equal("x", program.input(text='"x"').first())
assert_equal("xyz", program.input(text='"xyz"').first())
- @istest
- def program_preserves_arrays(self):
+ def test_program_preserves_arrays(self):
program = jq.compile(".")
assert_equal(list, type(program.input(text='[]').first()))
@@ -271,8 +242,7 @@ class TestJvToPython(object):
assert_equal([{"": []}], program.input(text='[{"": []}]').first())
assert_equal([{"1": [], "2": []}], program.input(text='[{"1": [], "2": []}]').first())
- @istest
- def program_preserves_objects(self):
+ def test_program_preserves_objects(self):
program = jq.compile(".")
assert_equal(dict, type(program.input(text='{}').first()))
@@ -302,46 +272,38 @@ class TestJvToPython(object):
assert_equal({"": {"": {}}}, program.input(text='{"": {"": {}}}').first())
-@istest
-class ConvenienceFunctions(object):
- @istest
- def first_function_with_json_value_input_returns_first_output_element(self):
+class TestConvenienceFunctions(object):
+ def test_first_function_with_json_value_input_returns_first_output_element(self):
output = jq.first(".[] + 1", [1, 2, 3])
assert_equal(2, output)
- @istest
- def first_function_with_json_text_input_returns_first_output_element(self):
+ def test_first_function_with_json_text_input_returns_first_output_element(self):
output = jq.first(".[] + 1", text="[1, 2, 3]")
assert_equal(2, output)
- @istest
- def text_function_with_json_value_input_returns_all_output_elements_as_string(self):
+ def test_text_function_with_json_value_input_returns_all_output_elements_as_string(self):
output = jq.text(".[] + 1", [1, 2, 3])
assert_equal("2\n3\n4", output)
- @istest
- def text_function_with_json_text_input_returns_all_output_elements_as_string(self):
+ def test_text_function_with_json_text_input_returns_all_output_elements_as_string(self):
output = jq.text(".[] + 1", text="[1, 2, 3]")
assert_equal("2\n3\n4", output)
- @istest
- def all_function_with_json_value_input_returns_all_output_elements_in_list(self):
+ def test_all_function_with_json_value_input_returns_all_output_elements_in_list(self):
output = jq.all(".[] + 1", [1, 2, 3])
assert_equal([2, 3, 4], output)
- @istest
- def all_function_with_json_text_input_returns_all_output_element_in_list(self):
+ def test_all_function_with_json_text_input_returns_all_output_element_in_list(self):
output = jq.all(".[] + 1", text="[1, 2, 3]")
assert_equal([2, 3, 4], output)
- @istest
- def iter_function_with_json_value_input_returns_all_output_elements_in_iterator(self):
+ def test_iter_function_with_json_value_input_returns_all_output_elements_in_iterator(self):
iterator = jq.iter(".[] + 1", [1, 2, 3])
assert_equal(2, next(iterator))
@@ -349,8 +311,7 @@ class ConvenienceFunctions(object):
assert_equal(4, next(iterator))
assert_equal("end", next(iterator, "end"))
- @istest
- def iter_function_with_json_text_input_returns_all_output_element_in_iterator(self):
+ def test_iter_function_with_json_text_input_returns_all_output_element_in_iterator(self):
iterator = jq.iter(".[] + 1", text="[1, 2, 3]")
assert_equal(2, next(iterator))
diff --git a/tests/tools.py b/tests/tools.py
new file mode 100644
index 0000000..0e318e9
--- /dev/null
+++ b/tests/tools.py
@@ -0,0 +1,6 @@
+def assert_equal(expected, actual):
+ assert expected == actual
+
+
+def assert_is(expected, actual):
+ assert expected is actual