From 8523bb01b24424249aa409ea577388a1ea10d70a Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Wed, 20 Oct 2021 21:40:07 +0200 Subject: Lua: marshal Attr values as userdata - Adds a new `pandoc.AttributeList()` constructor, which creates the associative attribute list that is used as the third component of `Attr` values. Values of this type can often be passed to constructors instead of `Attr` values. - `AttributeList` values can no longer be indexed numerically. --- test/Tests/Lua.hs | 2 +- test/lua/module/pandoc.lua | 55 ++++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 22 deletions(-) (limited to 'test') diff --git a/test/Tests/Lua.hs b/test/Tests/Lua.hs index 5538915a7..d3694d8a9 100644 --- a/test/Tests/Lua.hs +++ b/test/Tests/Lua.hs @@ -204,7 +204,7 @@ tests = map (localOption (QuickCheckTests 20)) [Para [Str "ignored"]]) Lua.getfield Lua.top "attr" Lua.liftIO . assertEqual "no accessor" (("hi", ["moin"], []) :: Attr) - =<< Lua.peek Lua.top + =<< Lua.peek @Attr Lua.top , testCase "module `pandoc.system` is present" . runLuaTest $ do Lua.getglobal' "pandoc.system" diff --git a/test/lua/module/pandoc.lua b/test/lua/module/pandoc.lua index fa1748c18..a1bcd53fe 100644 --- a/test/lua/module/pandoc.lua +++ b/test/lua/module/pandoc.lua @@ -11,34 +11,32 @@ end return { group 'Attr' { group 'Constructor' { + test('pandoc.Attr is a function', function () + assert.are_equal(type(pandoc.Attr), 'function') + end), test('returns null-Attr if no arguments are given', function () local attr = pandoc.Attr() assert.are_equal(attr.identifier, '') assert.are_same(attr.classes, {}) - assert.are_same(attr.attributes, {}) + assert.are_same(#attr.attributes, 0) end), test( 'accepts string-indexed table or list of pairs as attributes', function () - local attributes_list = pandoc.List:new {{'one', '1'}, {'two', '2'}} - local attr_from_list = pandoc.Attr('', {}, attributes_list:clone()) + local attributes_list = {{'one', '1'}, {'two', '2'}} + local attr_from_list = pandoc.Attr('', {}, attributes_list) - assert.are_same( - pandoc.List:new(attr_from_list.attributes), - attributes_list - ) + assert.are_equal(attr_from_list.attributes.one, '1') + assert.are_equal(attr_from_list.attributes.two, '2') local attributes_table = {one = '1', two = '2'} local attr_from_table = pandoc.Attr('', {}, attributes_table) - - local assoc_list_from_table = - pandoc.List:new(attr_from_table.attributes) - -- won't work in general, but does in this special case - table.sort(assoc_list_from_table, function(x, y) return x[1]