aboutsummaryrefslogtreecommitdiff
path: root/test/lua/module/pandoc-types.lua
blob: d9c9f82accc3fd316a8a2be1eef7ae6408cbba8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local tasty = require 'tasty'
local types = require 'pandoc.types'
local Version = types.Version

local assert = tasty.assert
local test = tasty.test_case
local group = tasty.test_group

return {
  group 'Version' {

    group 'constructor' {
      test('has type `userdata`', function ()
        assert.are_same(type(Version {2}), 'userdata')
      end),
      test('accepts list of integers', function ()
        assert.are_same(type(Version {2, 7, 3}), 'userdata')
      end),
      test('accepts a single integer', function ()
        assert.are_same(Version(5), Version {5})
      end),
      test('accepts version as string', function ()
        assert.are_same(
          Version '4.45.1',
          Version {4, 45, 1}
        )
      end),
      test('non-version string is rejected', function ()
        local success, msg = pcall(function () Version '11friends' end)
        assert.is_falsy(success)
        assert.is_truthy(tostring(msg):match('11friends'))
      end)
    },

    group 'comparison' {
      test('smaller (equal) than', function ()
        assert.is_truthy(Version {2, 58, 3} < Version {2, 58, 4})
        assert.is_falsy(Version {2, 60, 1} < Version {2, 59, 2})
        assert.is_truthy(Version {0, 14, 3} < Version {0, 14, 3, 1})
        assert.is_truthy(Version {3, 58, 3} <= Version {4})
        assert.is_truthy(Version {0, 14, 3} <= Version {0, 14, 3, 1})
      end),
      test('larger (equal) than', function ()
        assert.is_truthy(Version{2,58,3} > Version {2, 57, 4})
        assert.is_truthy(Version{2,58,3} > Version {2, 58, 2})
        assert.is_truthy(Version {0, 8} >= Version {0, 8})
        assert.is_falsy(Version {0, 8} >= Version {0, 8, 2})
      end),
      test('equality', function ()
        assert.is_truthy(Version '8.8', Version {8, 8})
      end),
      test('second argument can be a version string', function ()
        assert.is_truthy(Version '8' < '9.1')
        assert.is_falsy(Version '8.8' < '8.7')
      end),
    },

    group 'conversion to string' {
      test('converting from and to string is a noop', function ()
        local version_string = '1.19.4'
        assert.are_equal(tostring(Version(version_string)), version_string)
      end)
    },

    group 'convenience functions' {
      test('throws error if version is too old', function ()
        local actual = Version {2, 8}
        local expected = Version {2, 9}
        assert.error_matches(
          function () actual:must_be_at_least(expected) end,
          'expected version 2.9 or newer, got 2.8'
        )
      end),
      test('does nothing if expected version is older than actual', function ()
        local actual = Version '2.9'
        local expected = Version '2.8'
        actual:must_be_at_least(expected)
      end),
      test('does nothing if expected version equals to actual', function ()
        local actual = Version '2.8'
        local expected = Version '2.8'
        actual:must_be_at_least(expected)
      end)
    }
  }
}