From a72d1a6ae6fc59920deb34946f8226b977dfc4bb Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Tue, 24 Oct 2023 10:56:13 +0200 Subject: [PATCH] =?UTF-8?q?feat(table):=20=E2=9C=A8=20first=20implementati?= =?UTF-8?q?on=20of=20pointer=20system=20in=20zencode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit More testing is needed before merging --- src/lua/zencode_table.lua | 36 +++++++++++++++++++++ test/zencode/table.bats | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 test/zencode/table.bats diff --git a/src/lua/zencode_table.lua b/src/lua/zencode_table.lua index 977491667..578ce4c29 100644 --- a/src/lua/zencode_table.lua +++ b/src/lua/zencode_table.lua @@ -225,3 +225,39 @@ When("create copy of last element from ''", function(obj_name) end new_codec('copy_of_last_element', n_codec) end) + +When("set pointer to ''", function(obj_name) + local obj, obj_codec = have(obj_name) + empty "pointer" + zencode_assert(obj_codec.zentype ~= "e", "Cannot set pointer to an element") + ACK.pointer = obj + new_codec("pointer", nil, obj_name) +end) + +When("enter '' with pointer", function(key_name) + local key_o, key_codec = mayhave(key_name) + local p, p_codec = have("pointer") + local key_s + if key_o then + key_s = get_encoding_function(key_codec.encoding)(key_o) + else + key_s = key_name + end + if p_codec.zentype == "a" then + key_s = tonumber(key_s) + zencode_assert(key_s, "Pointer is an array but key is not a position number: "..key_name) + end + zencode_assert(p[key_s], "Cannot find "..key_s.." in pointer") + zencode_assert(luatype(p[key_s]) == "table", + "Cannot enter a "..luatype(p[key_s])..", is not a table") + -- do not point to elements + zencode_assert(not p_codec.schema, "Cannot set pointer to an element") + ACK.pointer = p[key_s] + local n_codec = {encoding = p_codec.encoding} + if p_codec.schema then + n_codec.schema = p_codec.schema + n_codec.zentype = "e" + end + CODEC.pointer = nil + new_codec("pointer", n_codec) +end) diff --git a/test/zencode/table.bats b/test/zencode/table.bats new file mode 100644 index 000000000..1882322a9 --- /dev/null +++ b/test/zencode/table.bats @@ -0,0 +1,68 @@ +load ../bats_setup +load ../bats_zencode +SUBDOC=table + + +@test "move with pointer" { +cat <