|
1 | 1 | local utils = require("pytrize.utils") |
2 | 2 |
|
3 | | -describe("min", function() |
4 | | - it("returns the smaller value", function() |
5 | | - assert.are.equal(1, utils.min(1, 2)) |
6 | | - assert.are.equal(1, utils.min(2, 1)) |
7 | | - end) |
| 3 | +describe("with_buf", function() |
| 4 | + it("loads a file into a buffer, calls fn, and cleans up", function() |
| 5 | + local tmp = vim.fn.tempname() .. ".py" |
| 6 | + local f = io.open(tmp, "w") |
| 7 | + f:write("x = 1\n") |
| 8 | + f:close() |
8 | 9 |
|
9 | | - it("returns the value when equal", function() |
10 | | - assert.are.equal(5, utils.min(5, 5)) |
11 | | - end) |
| 10 | + local result = utils.with_buf(tmp, function(bufnr) |
| 11 | + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 12 | + return lines[1] |
| 13 | + end, { force_delete = true }) |
12 | 14 |
|
13 | | - it("works with negative numbers", function() |
14 | | - assert.are.equal(-3, utils.min(-3, -1)) |
15 | | - end) |
16 | | -end) |
| 15 | + assert.are.equal("x = 1", result) |
| 16 | + |
| 17 | + -- buffer should have been cleaned up |
| 18 | + assert.are.equal(-1, vim.fn.bufnr(tmp)) |
| 19 | + vim.fn.delete(tmp) |
| 20 | + end) |
17 | 21 |
|
18 | | -describe("max", function() |
19 | | - it("returns the larger value", function() |
20 | | - assert.are.equal(2, utils.max(1, 2)) |
21 | | - assert.are.equal(2, utils.max(2, 1)) |
22 | | - end) |
| 22 | + it("propagates errors from fn", function() |
| 23 | + local tmp = vim.fn.tempname() .. ".py" |
| 24 | + local f = io.open(tmp, "w") |
| 25 | + f:write("x = 1\n") |
| 26 | + f:close() |
23 | 27 |
|
24 | | - it("returns the value when equal", function() |
25 | | - assert.are.equal(5, utils.max(5, 5)) |
26 | | - end) |
| 28 | + assert.has_error(function() |
| 29 | + utils.with_buf(tmp, function(_) |
| 30 | + error("test error") |
| 31 | + end, { force_delete = true }) |
| 32 | + end) |
27 | 33 |
|
28 | | - it("works with negative numbers", function() |
29 | | - assert.are.equal(-1, utils.max(-3, -1)) |
30 | | - end) |
| 34 | + vim.fn.delete(tmp) |
| 35 | + end) |
31 | 36 | end) |
0 commit comments