|
| 1 | +---Protocol conformance tests for backends. Backends can require this module |
| 2 | +---in their own test suites to verify they implement the protocol correctly. |
| 3 | +--- |
| 4 | +---Each test is a `{name, fn}` pair. `fn()` returns `true` on pass, or an error |
| 5 | +---string on failure. Backends can iterate them in any test framework: |
| 6 | +--- |
| 7 | +---```lua |
| 8 | +---local protocol_tests = require('smart-splits.protocol_tests') |
| 9 | +---for _, test in ipairs(protocol_tests.tests(my_backend)) do |
| 10 | +--- it(test.name, function() |
| 11 | +--- local result = test.fn() |
| 12 | +--- if result ~= true then |
| 13 | +--- error(result) |
| 14 | +--- end |
| 15 | +--- end) |
| 16 | +---end |
| 17 | +---``` |
| 18 | +--- |
| 19 | +---Or run them all at once without a framework: |
| 20 | +--- |
| 21 | +---```lua |
| 22 | +---local results = require('smart-splits.protocol_tests').run(my_backend) |
| 23 | +---for _, r in ipairs(results) do |
| 24 | +--- print(r.ok and 'PASS' or 'FAIL', r.name, r.ok == true and '' or r.ok) |
| 25 | +---end |
| 26 | +---``` |
| 27 | + |
| 28 | +local Backend = require('smart-splits.backend') |
| 29 | + |
| 30 | +local DIRECTIONS = { 'left', 'right', 'up', 'down' } |
| 31 | + |
| 32 | +local M = {} |
| 33 | + |
| 34 | +---@class SmartSplitsProtocolTest |
| 35 | +---@field name string |
| 36 | +---@field fn fun():true|string returns true on pass, error message on failure |
| 37 | + |
| 38 | +---@param backend SmartSplitsBackend |
| 39 | +---@return SmartSplitsProtocolTest[] |
| 40 | +function M.tests(backend) |
| 41 | + local tests = {} |
| 42 | + |
| 43 | + table.insert(tests, { |
| 44 | + name = 'passes structural validation', |
| 45 | + fn = function() |
| 46 | + local err = Backend.validate(backend) |
| 47 | + if err then |
| 48 | + return err |
| 49 | + end |
| 50 | + return true |
| 51 | + end, |
| 52 | + }) |
| 53 | + |
| 54 | + table.insert(tests, { |
| 55 | + name = 'detect() returns a boolean', |
| 56 | + fn = function() |
| 57 | + local ok, result = pcall(backend.detect) |
| 58 | + if not ok then |
| 59 | + return ('detect() errored: %s'):format(result) |
| 60 | + end |
| 61 | + if type(result) ~= 'boolean' then |
| 62 | + return ('detect() returned %s, expected boolean'):format(type(result)) |
| 63 | + end |
| 64 | + return true |
| 65 | + end, |
| 66 | + }) |
| 67 | + |
| 68 | + for _, direction in ipairs(DIRECTIONS) do |
| 69 | + table.insert(tests, { |
| 70 | + name = ('move(%q, {}) returns a boolean'):format(direction), |
| 71 | + fn = function() |
| 72 | + local ok, result = pcall(backend.move, direction, {}) |
| 73 | + if not ok then |
| 74 | + return ('move(%q) errored: %s'):format(direction, result) |
| 75 | + end |
| 76 | + if type(result) ~= 'boolean' then |
| 77 | + return ('move(%q) returned %s, expected boolean'):format(direction, type(result)) |
| 78 | + end |
| 79 | + return true |
| 80 | + end, |
| 81 | + }) |
| 82 | + end |
| 83 | + |
| 84 | + if backend.resize then |
| 85 | + for _, direction in ipairs(DIRECTIONS) do |
| 86 | + table.insert(tests, { |
| 87 | + name = ('resize(%q, {amount=1}) returns a boolean'):format(direction), |
| 88 | + fn = function() |
| 89 | + local ok, result = pcall(backend.resize, direction, { amount = 1 }) |
| 90 | + if not ok then |
| 91 | + return ('resize(%q) errored: %s'):format(direction, result) |
| 92 | + end |
| 93 | + if type(result) ~= 'boolean' then |
| 94 | + return ('resize(%q) returned %s, expected boolean'):format(direction, type(result)) |
| 95 | + end |
| 96 | + return true |
| 97 | + end, |
| 98 | + }) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + if backend.activate then |
| 103 | + table.insert(tests, { |
| 104 | + name = 'activate() does not error', |
| 105 | + fn = function() |
| 106 | + local ok, err = pcall(backend.activate) |
| 107 | + if not ok then |
| 108 | + return ('activate() errored: %s'):format(err) |
| 109 | + end |
| 110 | + return true |
| 111 | + end, |
| 112 | + }) |
| 113 | + end |
| 114 | + |
| 115 | + if backend.health then |
| 116 | + table.insert(tests, { |
| 117 | + name = 'health() does not error', |
| 118 | + fn = function() |
| 119 | + local ok, err = pcall(backend.health) |
| 120 | + if not ok then |
| 121 | + return ('health() errored: %s'):format(err) |
| 122 | + end |
| 123 | + return true |
| 124 | + end, |
| 125 | + }) |
| 126 | + end |
| 127 | + |
| 128 | + return tests |
| 129 | +end |
| 130 | + |
| 131 | +---@class SmartSplitsProtocolTestResult |
| 132 | +---@field name string |
| 133 | +---@field ok true|string |
| 134 | + |
| 135 | +---@param backend SmartSplitsBackend |
| 136 | +---@return SmartSplitsProtocolTestResult[] |
| 137 | +function M.run(backend) |
| 138 | + local results = {} |
| 139 | + for _, test in ipairs(M.tests(backend)) do |
| 140 | + local ok = test.fn() |
| 141 | + table.insert(results, { name = test.name, ok = ok }) |
| 142 | + end |
| 143 | + return results |
| 144 | +end |
| 145 | + |
| 146 | +return M |
0 commit comments