From 879bb569f3606a2c833e30a7528d26d7e38cab8d Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Thu, 7 Oct 2021 13:35:48 +0200 Subject: [PATCH] interp: Correctly check if _decode_value, add more has/1 tests --- pkg/interp/internal.jq | 12 +-- pkg/interp/testdata/value.fqtest | 134 ++++++++++++++++++++++++++----- 2 files changed, 122 insertions(+), 24 deletions(-) diff --git a/pkg/interp/internal.jq b/pkg/interp/internal.jq index 4e30b2a8..2b703784 100644 --- a/pkg/interp/internal.jq +++ b/pkg/interp/internal.jq @@ -36,17 +36,17 @@ def _eval($expr; $filename; f; on_error; on_compile_error): end ); +def _is_decode_value: + try has("_root") catch false; + # TODO: error value preview def _expected_decode_value: error("expected a decode value but got: \(. | type) (\(. | tostring))"); # TODO: helper? _is_decode_value? def _decode_value(f): - ( . as $c - | try has("._root") - catch ($c | _expected_decode_value) - | $c - | f - ); + if _is_decode_value then f + else _expected_decode_value + end; def _error_str: "error: \(.)"; def _errorln: ., "\n" | stderr; diff --git a/pkg/interp/testdata/value.fqtest b/pkg/interp/testdata/value.fqtest index bc24495b..77eeedd4 100644 --- a/pkg/interp/testdata/value.fqtest +++ b/pkg/interp/testdata/value.fqtest @@ -35,6 +35,16 @@ mp3> . | (root, buffer_root, format_root, parent | ._path | path_to_expr), [pare "." "." [] +mp3> .headers[0].magic | (root, buffer_root, format_root, parent | ._path | path_to_expr), [parents | ._path | path_to_expr] +"." +"." +".headers[0]" +".headers[0]" +[ + ".headers[0]", + ".headers", + "." +] mp3> .frames[0].side_info.granules[1] | (root, buffer_root, format_root, parent | ._path | path_to_expr), [parents | ._path | path_to_expr] "." "." @@ -59,40 +69,128 @@ mp3> 123 | parents error: expected a decode value but got: number (123) mp3> ^D $ fq -d mp3 -i . /test.mp3 -mp3> has("_start") +mp3> . as $c | ("headers", 0, "abc") as $n | $n, try ($c | has($n)) catch . +"headers" true -mp3> has("_stop") +0 +"cannot check whether object has a key: 0" +"abc" +false +mp3> .headers as $c | (0, "abc") as $n | $n, try ($c | has($n)) catch . +0 true -mp3> has("_len") +"abc" +"cannot check whether array has a key: abc" +mp3> .headers[0].magic as $c | (0, "abc") as $n | $n, try ($c | has($n)) catch . +0 +"has cannot be applied to: string" +"abc" +"has cannot be applied to: string" +mp3> . as $c | ("_start", "_stop", "_len", "_name", "_root", "_buffer_root", "_format_root", "_parent", "_symbol", "_description", "_path", "_bits", "_bytes", "_error", "_unknown", "_format", "_abc") as $n | $n, try ($c | has($n)) catch . +"_start" true -mp3> has("_name") +"_stop" true -mp3> has("_root") +"_len" true -mp3> has("_buffer_root") +"_name" true -mp3> has("_format_root") +"_root" true -mp3> has("_parent") +"_buffer_root" true -mp3> has("_symbol") +"_format_root" true -mp3> has("_description") +"_parent" true -mp3> has("_path") +"_symbol" true -mp3> has("_bits") +"_description" true -mp3> has("_bytes") +"_path" true -mp3> has("_error") +"_bits" true -mp3> has("_unknown") +"_bytes" true -mp3> has("_format") +"_error" true -mp3> has("_abc") -error: expected a extkey but got: _abc +"_unknown" +true +"_format" +true +"_abc" +"expected a extkey but got: _abc" +mp3> .headers as $c | ("_start", "_stop", "_len", "_name", "_root", "_buffer_root", "_format_root", "_parent", "_symbol", "_description", "_path", "_bits", "_bytes", "_error", "_unknown", "_format", "_abc") as $n | $n, try ($c | has($n)) catch . +"_start" +true +"_stop" +true +"_len" +true +"_name" +true +"_root" +true +"_buffer_root" +true +"_format_root" +true +"_parent" +true +"_symbol" +true +"_description" +true +"_path" +true +"_bits" +true +"_bytes" +true +"_error" +true +"_unknown" +true +"_format" +true +"_abc" +"expected a extkey but got: _abc" +mp3> .headers[0].magic as $c | ("_start", "_stop", "_len", "_name", "_root", "_buffer_root", "_format_root", "_parent", "_symbol", "_description", "_path", "_bits", "_bytes", "_error", "_unknown", "_format", "_abc") as $n | $n, try ($c | has($n)) catch . +"_start" +true +"_stop" +true +"_len" +true +"_name" +true +"_root" +true +"_buffer_root" +true +"_format_root" +true +"_parent" +true +"_symbol" +true +"_description" +true +"_path" +true +"_bits" +true +"_bytes" +true +"_error" +true +"_unknown" +true +"_format" +true +"_abc" +"expected a extkey but got: _abc" mp3> ^D $ fq -d mp3 -i . /test.mp3 mp3> ._start