From 03b9f3d5f34633325141d48999a3222c6a34c796 Mon Sep 17 00:00:00 2001
From: Albert Krewinkel <albert@zeitkraut.de>
Date: Tue, 11 Jun 2019 11:05:57 +0200
Subject: [PATCH] Lua pandoc module: better tests for Attr and AttributeList

---
 test/lua/module/pandoc.lua | 65 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/test/lua/module/pandoc.lua b/test/lua/module/pandoc.lua
index b9509cdb6..0bb4af9aa 100644
--- a/test/lua/module/pandoc.lua
+++ b/test/lua/module/pandoc.lua
@@ -9,6 +9,71 @@ function os_is_windows ()
 end
 
 return {
+  group 'Attr' {
+    group 'Constructor' {
+      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, {})
+      end),
+      test(
+        'accepts string-indexed table or list of pairs as attributes',
+        function ()
+          local attributes_table = {one = '1', two = '2'}
+          local attributes_list = pandoc.List:new {{'one', '1'}, {'two', '2'}}
+          local attr_from_table = pandoc.Attr('', {}, attributes_table)
+          local attr_from_list = pandoc.Attr('', {}, attributes_list:clone())
+          assert.are_same(
+            pandoc.List:new(attr_from_table.attributes),
+            attributes_list
+          )
+          assert.are_same(
+            pandoc.List:new(attr_from_list.attributes),
+            attributes_list
+          )
+        end
+      )
+    },
+    group 'AttributeList' {
+      test('allows access via fields', function ()
+        local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
+        assert.are_equal(attributes.a, '1')
+        assert.are_equal(attributes.b, '2')
+      end),
+      test('allows access to pairs via numerical indexing', function ()
+        local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
+        assert.are_same(attributes[1], {'a', '1'})
+        assert.are_same(attributes[2], {'b', '2'})
+      end),
+      test('adds entries by field name', function ()
+        local attributes = pandoc.Attr('',{}, {{'c', '1'}, {'d', '2'}}).attributes
+        attributes.e = '3'
+        assert.are_same(
+          -- checking the full AttributeList would "duplicate" entries
+          setmetatable(attributes, nil),
+          {{'c', '1'}, {'d', '2'}, {'e', '3'}}
+        )
+      end),
+      test('deletes entries by field name', function ()
+        local attributes = pandoc.Attr('',{}, {a = '1', b = '2'}).attributes
+        attributes.a = nil
+        assert.is_nil(attributes.a)
+        local assoc_list = setmetatable(attributes, nil)
+        assert.are_same(assoc_list, {{'b', '2'}})
+      end),
+      test('gives key-value pairs when iterated-over', function ()
+        local attributes = {width = '11', height = '22', name = 'test'}
+        local attr = pandoc.Attr('', {}, attributes)
+        local count = 0
+        for k, v in pairs(attr.attributes) do
+          assert.are_equal(attributes[k], v)
+          count = count + 1
+        end
+        assert.are_equal(count, 3)
+      end)
+    },
+  },
   group 'pipe' {
     test('external string processing', function ()
       if os_is_windows() then