1
1
mirror of https://github.com/wader/fq.git synced 2024-11-27 14:14:58 +03:00

interp: Correctly check if _decode_value, add more has/1 tests

This commit is contained in:
Mattias Wadman 2021-10-07 13:35:48 +02:00
parent 691688022f
commit 879bb569f3
2 changed files with 122 additions and 24 deletions

View File

@ -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;

View File

@ -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