"Fossies" - the Fresh Open Source Software Archive

Member "pandoc-2.7.3/test/lua/module/pandoc.lua" (12 Jun 2019, 4812 Bytes) of package /linux/www/pandoc-2.7.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 local tasty = require 'tasty'
    2 
    3 local test = tasty.test_case
    4 local group = tasty.test_group
    5 local assert = tasty.assert
    6 
    7 function os_is_windows ()
    8   return package.config:sub(1,1) == '\\'
    9 end
   10 
   11 return {
   12   group 'Attr' {
   13     group 'Constructor' {
   14       test('returns null-Attr if no arguments are given', function ()
   15         local attr = pandoc.Attr()
   16         assert.are_equal(attr.identifier, '')
   17         assert.are_same(attr.classes, {})
   18         assert.are_same(attr.attributes, {})
   19       end),
   20       test(
   21         'accepts string-indexed table or list of pairs as attributes',
   22         function ()
   23           local attributes_list = pandoc.List:new {{'one', '1'}, {'two', '2'}}
   24           local attr_from_list = pandoc.Attr('', {}, attributes_list:clone())
   25 
   26           assert.are_same(
   27             pandoc.List:new(attr_from_list.attributes),
   28             attributes_list
   29           )
   30 
   31           local attributes_table = {one = '1', two = '2'}
   32           local attr_from_table = pandoc.Attr('', {}, attributes_table)
   33 
   34           local assoc_list_from_table =
   35             pandoc.List:new(attr_from_table.attributes)
   36           -- won't work in general, but does in this special case
   37           table.sort(assoc_list_from_table, function(x, y) return x[1]<y[1] end)
   38           assert.are_same(
   39             assoc_list_from_table,
   40             attributes_list
   41           )
   42         end
   43       )
   44     },
   45     group 'AttributeList' {
   46       test('allows access via fields', function ()
   47         local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
   48         assert.are_equal(attributes.a, '1')
   49         assert.are_equal(attributes.b, '2')
   50       end),
   51       test('allows access to pairs via numerical indexing', function ()
   52         local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
   53         assert.are_same(attributes[1], {'a', '1'})
   54         assert.are_same(attributes[2], {'b', '2'})
   55       end),
   56       test('adds entries by field name', function ()
   57         local attributes = pandoc.Attr('',{}, {{'c', '1'}, {'d', '2'}}).attributes
   58         attributes.e = '3'
   59         assert.are_same(
   60           -- checking the full AttributeList would "duplicate" entries
   61           setmetatable(attributes, nil),
   62           {{'c', '1'}, {'d', '2'}, {'e', '3'}}
   63         )
   64       end),
   65       test('deletes entries by field name', function ()
   66         local attributes = pandoc.Attr('',{}, {a = '1', b = '2'}).attributes
   67         attributes.a = nil
   68         assert.is_nil(attributes.a)
   69         local assoc_list = setmetatable(attributes, nil)
   70         assert.are_same(assoc_list, {{'b', '2'}})
   71       end),
   72       test('remains unchanged if deleted key did not exist', function ()
   73         local assoc_list = pandoc.List:new {{'alpha', 'x'}, {'beta', 'y'}}
   74         local attributes = pandoc.Attr('', {}, assoc_list:clone()).attributes
   75         attributes.a = nil
   76         assert.are_same(pandoc.List:new(attributes), assoc_list)
   77       end),
   78       test('gives key-value pairs when iterated-over', function ()
   79         local attributes = {width = '11', height = '22', name = 'test'}
   80         local attr = pandoc.Attr('', {}, attributes)
   81         local count = 0
   82         for k, v in pairs(attr.attributes) do
   83           assert.are_equal(attributes[k], v)
   84           count = count + 1
   85         end
   86         assert.are_equal(count, 3)
   87       end)
   88     },
   89   },
   90   group 'pipe' {
   91     test('external string processing', function ()
   92       if os_is_windows() then
   93         local pipe_result = pandoc.pipe('find', {'hi'}, 'hi')
   94         assert.are_equal('hi', pipe_result:match '%a+')
   95       else
   96         local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
   97         assert.are_equal('bbc', pipe_result:match '%a+')
   98       end
   99     end),
  100     test('failing pipe', function ()
  101       if os_is_windows() then
  102         local success, err = pcall(pandoc.pipe, 'find', {'/a'}, 'hi')
  103         assert.is_falsy(success)
  104         assert.are_equal('find', err.command)
  105         assert.is_truthy(err.error_code ~= 0)
  106       else
  107         local success, err = pcall(pandoc.pipe, 'false', {}, 'abc')
  108         assert.is_falsy(success)
  109         assert.are_equal('false', err.command)
  110         assert.are_equal(1, err.error_code)
  111         assert.are_equal('', err.output)
  112       end
  113     end)
  114   },
  115 
  116   group 'read' {
  117     test('Markdown', function ()
  118       local valid_markdown = '*Hello*, World!\n'
  119       local expected = pandoc.Pandoc({
  120           pandoc.Para {
  121             pandoc.Emph { pandoc.Str 'Hello' },
  122             pandoc.Str ',',
  123             pandoc.Space(),
  124             pandoc.Str 'World!'
  125           }
  126       })
  127       assert.are_same(expected, pandoc.read(valid_markdown))
  128     end),
  129     test('failing read', function ()
  130       assert.error_matches(
  131         function () pandoc.read('foo', 'nosuchreader') end,
  132         'Unknown reader: nosuchreader'
  133       )
  134     end)
  135   },
  136 }