git.iko.soy/typeable/pandoc
on 247b1cd help sign in
typeable/pandoc/test/lua/module/pandoc-types.lua
111 lines 3.9 kB history raw
1local tasty = require 'tasty'
2local types = require 'pandoc.types'
3local Version = types.Version
5local assert = tasty.assert
6local test = tasty.test_case
7local group = tasty.test_group
9return {
10 group 'Version' {
12 group 'constructor' {
13 test('has type `userdata`', function ()
14 assert.are_same(type(Version {2}), 'userdata')
15 end),
16 test('accepts list of integers', function ()
17 assert.are_same(type(Version {2, 7, 3}), 'userdata')
18 end),
19 test('accepts a single integer', function ()
20 assert.are_same(Version(5), Version {5})
21 end),
22 test('accepts version as string', function ()
23 assert.are_same(
24 Version '4.45.1',
25 Version {4, 45, 1}
26 )
27 end),
28 test('non-version string is rejected', function ()
29 local success, msg = pcall(function () Version '11friends' end)
30 assert.is_falsy(success)
31 assert.is_truthy(tostring(msg):match('11friends'))
32 end)
33 },
35 group 'comparison' {
36 test('smaller (equal) than', function ()
37 assert.is_truthy(Version {2, 58, 3} < Version {2, 58, 4})
38 assert.is_falsy(Version {2, 60, 1} < Version {2, 59, 2})
39 assert.is_truthy(Version {0, 14, 3} < Version {0, 14, 3, 1})
40 assert.is_truthy(Version {3, 58, 3} <= Version {4})
41 assert.is_truthy(Version {0, 14, 3} <= Version {0, 14, 3, 1})
42 end),
43 test('larger (equal) than', function ()
44 assert.is_truthy(Version{2,58,3} > Version {2, 57, 4})
45 assert.is_truthy(Version{2,58,3} > Version {2, 58, 2})
46 assert.is_truthy(Version {0, 8} >= Version {0, 8})
47 assert.is_falsy(Version {0, 8} >= Version {0, 8, 2})
48 end),
49 test('equality', function ()
50 assert.is_truthy(Version '8.8', Version {8, 8})
51 end),
52 test('second argument can be a version string', function ()
53 assert.is_truthy(Version '8' < '9.1')
54 assert.is_falsy(Version '8.8' < '8.7')
55 end),
56 },
58 group 'list-like behavior' {
59 test('can access version component numbers', function ()
60 local version = Version '2.7.3'
61 assert.is_nil(version[0])
62 assert.are_equal(version[1], 2)
63 assert.are_equal(version[2], 7)
64 assert.are_equal(version[3], 3)
65 end),
66 test('can be iterated over', function ()
67 local version_list = {2, 7, 3}
68 local final_index = 0
69 for i, v in pairs(Version(version_list)) do
70 assert.are_equal(v, version_list[i])
71 final_index = i
72 end
73 assert.are_equal(final_index, 3)
74 end),
75 test('length is the number of components', function ()
76 assert.are_equal(#(Version '0'), 1)
77 assert.are_equal(#(Version '1.6'), 2)
78 assert.are_equal(#(Version '8.7.5'), 3)
79 assert.are_equal(#(Version '2.9.1.5'), 4)
80 end)
81 },
83 group 'conversion to string' {
84 test('converting from and to string is a noop', function ()
85 local version_string = '1.19.4'
86 assert.are_equal(tostring(Version(version_string)), version_string)
87 end)
88 },
90 group 'convenience functions' {
91 test('throws error if version is too old', function ()
92 local actual = Version {2, 8}
93 local expected = Version {2, 9}
94 assert.error_matches(
95 function () actual:must_be_at_least(expected) end,
96 'expected version 2.9 or newer, got 2.8'
97 )
98 end),
99 test('does nothing if expected version is older than actual', function ()
100 local actual = Version '2.9'
101 local expected = Version '2.8'
102 actual:must_be_at_least(expected)
103 end),
104 test('does nothing if expected version equals to actual', function ()
105 local actual = Version '2.8'
106 local expected = Version '2.8'
107 actual:must_be_at_least(expected)
108 end)
109 }
110 }