summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-05-01 17:14:32 +0100
committerMichael Williamson <mike@zwobble.org>2023-05-01 17:14:32 +0100
commit86cebe2a57dc213fb4e5bf4db4f51d01ca40182e (patch)
treeba6895a3c387ad51b7a0599c395ddad7e3b09a7b
parent294bcf6b3db792176bb41c2e81a2ce3761500544 (diff)
Add input_value() and input_text()
-rw-r--r--jq.pyx12
-rw-r--r--tests/jq_tests.py16
2 files changed, 26 insertions, 2 deletions
diff --git a/jq.pyx b/jq.pyx
index a75e5c9..a280687 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -246,9 +246,17 @@ cdef class _Program(object):
def input(self, value=_NO_VALUE, text=_NO_VALUE):
if (value is _NO_VALUE) == (text is _NO_VALUE):
raise ValueError("Either the value or text argument should be set")
- string_input = text if text is not _NO_VALUE else json.dumps(value)
- return _ProgramWithInput(self._jq_state_pool, string_input.encode("utf8"))
+ if text is not _NO_VALUE:
+ return self.input_text(text)
+ else:
+ return self.input_value(value)
+
+ def input_value(self, value):
+ return self.input_text(json.dumps(value))
+
+ def input_text(self, text):
+ return _ProgramWithInput(self._jq_state_pool, text.encode("utf8"))
@property
def program_string(self):
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index ebb7a6c..d97b1c2 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -65,6 +65,22 @@ def test_when_text_argument_is_used_then_input_is_treated_as_json_text():
)
+def test_input_can_be_value():
+ program = jq.compile(".")
+
+ result = program.input_value("42").first()
+
+ assert_equal("42", result)
+
+
+def test_input_can_be_text():
+ program = jq.compile(".")
+
+ result = program.input_text("42").first()
+
+ assert_equal(42, result)
+
+
def test_when_text_method_is_used_on_result_then_output_is_serialised_to_json_string():
assert_equal(
'"42"',