git.iko.soy/typeable/pandoc
on 467ea4e help sign in
typeable/pandoc/test/lua/module/pandoc-types.lua
112 lines 3.8 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 assert.error_matches(
30 function () Version '11friends' end,
31 '11friends'
32 )
33 end)
34 },
36 group 'comparison' {
37 test('smaller (equal) than', function ()
38 assert.is_truthy(Version {2, 58, 3} < Version {2, 58, 4})
39 assert.is_falsy(Version {2, 60, 1} < Version {2, 59, 2})
40 assert.is_truthy(Version {0, 14, 3} < Version {0, 14, 3, 1})
41 assert.is_truthy(Version {3, 58, 3} <= Version {4})
42 assert.is_truthy(Version {0, 14, 3} <= Version {0, 14, 3, 1})
43 end),
44 test('larger (equal) than', function ()
45 assert.is_truthy(Version{2,58,3} > Version {2, 57, 4})
46 assert.is_truthy(Version{2,58,3} > Version {2, 58, 2})
47 assert.is_truthy(Version {0, 8} >= Version {0, 8})
48 assert.is_falsy(Version {0, 8} >= Version {0, 8, 2})
49 end),
50 test('equality', function ()
51 assert.is_truthy(Version '8.8', Version {8, 8})
52 end),
53 test('second argument can be a version string', function ()
54 assert.is_truthy(Version '8' < '9.1')
55 assert.is_falsy(Version '8.8' < '8.7')
56 end),
57 },
59 group 'list-like behavior' {
60 test('can access version component numbers', function ()
61 local version = Version '2.7.3'
62 assert.is_nil(version[0])
63 assert.are_equal(version[1], 2)
64 assert.are_equal(version[2], 7)
65 assert.are_equal(version[3], 3)
66 end),
67 test('can be iterated over', function ()
68 local version_list = {2, 7, 3}
69 local final_index = 0
70 for i, v in pairs(Version(version_list)) do
71 assert.are_equal(v, version_list[i])
72 final_index = i
73 end
74 assert.are_equal(final_index, 3)
75 end),
76 test('length is the number of components', function ()
77 assert.are_equal(#(Version '0'), 1)
78 assert.are_equal(#(Version '1.6'), 2)
79 assert.are_equal(#(Version '8.7.5'), 3)
80 assert.are_equal(#(Version '2.9.1.5'), 4)
81 end)
82 },
84 group 'conversion to string' {
85 test('converting from and to string is a noop', function ()
86 local version_string = '1.19.4'
87 assert.are_equal(tostring(Version(version_string)), version_string)
88 end)
89 },
91 group 'convenience functions' {
92 test('throws error if version is too old', function ()
93 local actual = Version {2, 8}
94 local expected = Version {2, 9}
95 assert.error_matches(
96 function () actual:must_be_at_least(expected) end,
97 'expected version 2.9 or newer, got 2.8'
98 )
99 end),
100 test('does nothing if expected version is older than actual', function ()
101 local actual = Version '2.9'
102 local expected = Version '2.8'
103 actual:must_be_at_least(expected)
104 end),
105 test('does nothing if expected version equals to actual', function ()
106 local actual = Version '2.8'
107 local expected = Version '2.8'
108 actual:must_be_at_least(expected)
109 end)
110 }
111 }