|
1 |
| ---[[ |
2 |
| -local lu = require("luaunit") |
3 |
| -
|
4 |
| -local context_manager = require("plenary.context_manager") |
5 |
| -local debug_utils = require("plenary.debug_utils") |
6 |
| -local Path = require("plenary.path") |
| 1 | +local context_manager = require "plenary.context_manager" |
| 2 | +local debug_utils = require "plenary.debug_utils" |
| 3 | +local Path = require "plenary.path" |
7 | 4 |
|
8 | 5 | local with = context_manager.with
|
9 | 6 | local open = context_manager.open
|
10 | 7 |
|
11 |
| -local README_STR_PATH = vim.fn.fnamemodify(debug_utils.sourced_filepath(), ":h:h:h:h") .. "/README.md" |
| 8 | +local README_STR_PATH = vim.fn.fnamemodify(debug_utils.sourced_filepath(), ":h:h:h") .. "/README.md" |
12 | 9 | local README_FIRST_LINE = "# plenary.nvim"
|
13 | 10 |
|
14 |
| -TestContextManager = {} |
| 11 | +describe("context_manager", function() |
| 12 | + it("works with objects", function() |
| 13 | + local obj_manager = { |
| 14 | + enter = function(self) |
| 15 | + self.result = 10 |
| 16 | + return self.result |
| 17 | + end, |
15 | 18 |
|
16 |
| -function TestContextManager:testWorksWithObj() |
17 |
| - local obj_manager = { |
18 |
| - enter = function(self) |
19 |
| - self.result = 10 |
20 |
| - return self.result |
21 |
| - end, |
| 19 | + exit = function() end, |
| 20 | + } |
22 | 21 |
|
23 |
| - exit = function() |
24 |
| - end, |
25 |
| - } |
| 22 | + local result = with(obj_manager, function(obj) |
| 23 | + return obj |
| 24 | + end) |
26 | 25 |
|
27 |
| - local result = with(obj_manager, function(obj) |
28 |
| - return obj |
| 26 | + assert.are.same(10, result) |
| 27 | + assert.are.same(obj_manager.result, result) |
29 | 28 | end)
|
30 | 29 |
|
| 30 | + it("works with coroutine", function() |
| 31 | + local co = function() |
| 32 | + coroutine.yield(10) |
| 33 | + end |
31 | 34 |
|
32 |
| - lu.assertEquals(10, result) |
33 |
| - lu.assertEquals(obj_manager.result, result) |
34 |
| -end |
| 35 | + local result = with(co, function(obj) |
| 36 | + return obj |
| 37 | + end) |
| 38 | + |
| 39 | + assert.are.same(10, result) |
| 40 | + end) |
35 | 41 |
|
| 42 | + it("does not work with coroutine with extra yields", function() |
| 43 | + local co = function() |
| 44 | + coroutine.yield(10) |
36 | 45 |
|
37 |
| -function TestContextManager:testWorksWithCoroutine() |
38 |
| - local co = function() |
39 |
| - coroutine.yield(10) |
40 |
| - end |
| 46 | + -- Can't yield twice. That'd be bad and wouldn't make any sense. |
| 47 | + coroutine.yield(10) |
| 48 | + end |
41 | 49 |
|
42 |
| - local result = with(co, function(obj) |
43 |
| - return obj |
| 50 | + assert.has.error_match(function() |
| 51 | + with(co, function(obj) |
| 52 | + return obj |
| 53 | + end) |
| 54 | + end, "Should not yield anymore, otherwise that would make things complicated") |
44 | 55 | end)
|
45 | 56 |
|
46 |
| - lu.assertEquals(10, result) |
47 |
| -end |
| 57 | + it("reads from files with open", function() |
| 58 | + local result = with(open(README_STR_PATH), function(reader) |
| 59 | + return reader:read() |
| 60 | + end) |
48 | 61 |
|
49 |
| -function TestContextManager:testDoesNotWorkWithCoroutineWithExtraYields() |
50 |
| - local co = function() |
51 |
| - coroutine.yield(10) |
| 62 | + assert.are.same(result, README_FIRST_LINE) |
| 63 | + end) |
52 | 64 |
|
53 |
| - -- Can't yield twice. That'd be bad and wouldn't make any sense. |
54 |
| - coroutine.yield(10) |
55 |
| - end |
| 65 | + it("reads from Paths with open", function() |
| 66 | + local p = Path:new(README_STR_PATH) |
56 | 67 |
|
57 |
| - lu.assertError(function() |
58 |
| - with(co, function(obj) |
59 |
| - return obj |
| 68 | + local result = with(open(p), function(reader) |
| 69 | + return reader:read() |
60 | 70 | end)
|
| 71 | + |
| 72 | + assert.are.same(result, README_FIRST_LINE) |
61 | 73 | end)
|
62 |
| -end |
63 | 74 |
|
64 |
| -function TestContextManager:testOpenWorks() |
65 |
| - local result = with(open(README_STR_PATH), function(reader) |
66 |
| - return reader:read() |
| 75 | + it("calls exit on error with objects", function() |
| 76 | + local entered = false |
| 77 | + local exited = false |
| 78 | + local obj_manager = { |
| 79 | + enter = function(self) |
| 80 | + entered = true |
| 81 | + end, |
| 82 | + |
| 83 | + exit = function(self) |
| 84 | + exited = true |
| 85 | + end, |
| 86 | + } |
| 87 | + |
| 88 | + assert.has.error_match(function() |
| 89 | + with(obj_manager, function(obj) |
| 90 | + assert(false, "failed in callback") |
| 91 | + end) |
| 92 | + end, "failed in callback") |
| 93 | + |
| 94 | + assert.is["true"](entered) |
| 95 | + assert.is["true"](exited) |
67 | 96 | end)
|
68 | 97 |
|
69 |
| - lu.assertEquals(result, README_FIRST_LINE) |
70 |
| -end |
| 98 | + it("calls exit on error with coroutines", function() |
| 99 | + local entered = false |
| 100 | + local exited = false |
| 101 | + local co = function() |
| 102 | + entered = true |
| 103 | + coroutine.yield(nil) |
| 104 | + |
| 105 | + exited = true |
| 106 | + end |
71 | 107 |
|
72 |
| -function TestContextManager:testOpenWorksWithPath() |
73 |
| - local p = Path:new(README_STR_PATH) |
| 108 | + assert.has.error_match(function() |
| 109 | + with(co, function(obj) |
| 110 | + assert(false, "failed in callback") |
| 111 | + end) |
| 112 | + end, "failed in callback") |
74 | 113 |
|
75 |
| - local result = with(open(p), function(reader) |
76 |
| - return reader:read() |
| 114 | + assert.is["true"](entered) |
| 115 | + assert.is["true"](exited) |
77 | 116 | end)
|
78 | 117 |
|
79 |
| - lu.assertEquals(result, README_FIRST_LINE) |
80 |
| -end |
81 |
| ---]] |
| 118 | + it("fails from enter error with objects", function() |
| 119 | + local exited = false |
| 120 | + local obj_manager = { |
| 121 | + enter = function(self) |
| 122 | + assert(false, "failed in enter") |
| 123 | + end, |
| 124 | + |
| 125 | + exit = function(self) |
| 126 | + exited = true |
| 127 | + end, |
| 128 | + } |
| 129 | + |
| 130 | + local ran_callback = false |
| 131 | + assert.has.error_match(function() |
| 132 | + with(obj_manager, function(obj) |
| 133 | + ran_callback = true |
| 134 | + end) |
| 135 | + end, "failed in enter") |
| 136 | + |
| 137 | + assert.is["false"](ran_callback) |
| 138 | + assert.is["false"](exited) |
| 139 | + end) |
| 140 | + |
| 141 | + it("fails from enter error with coroutines", function() |
| 142 | + local exited = false |
| 143 | + local co = function() |
| 144 | + assert(false, "failed in enter") |
| 145 | + coroutine.yield(nil) |
| 146 | + |
| 147 | + exited = true |
| 148 | + end |
| 149 | + |
| 150 | + local ran_callback = false |
| 151 | + assert.has.error_match(function() |
| 152 | + with(co, function(obj) |
| 153 | + ran_callback = true |
| 154 | + end) |
| 155 | + end, "Should have yielded in coroutine.") |
| 156 | + |
| 157 | + assert.is["false"](ran_callback) |
| 158 | + assert.is["false"](exited) |
| 159 | + end) |
| 160 | + |
| 161 | + it("fails from exit error with objects", function() |
| 162 | + local entered = false |
| 163 | + local obj_manager = { |
| 164 | + enter = function(self) |
| 165 | + entered = true |
| 166 | + end, |
| 167 | + |
| 168 | + exit = function(self) |
| 169 | + assert(false, "failed in exit") |
| 170 | + end, |
| 171 | + } |
| 172 | + |
| 173 | + local ran_callback = false |
| 174 | + assert.has.error_match(function() |
| 175 | + with(obj_manager, function(obj) |
| 176 | + ran_callback = true |
| 177 | + end) |
| 178 | + end, "failed in exit") |
| 179 | + |
| 180 | + assert.is["true"](entered) |
| 181 | + assert.is["true"](ran_callback) |
| 182 | + end) |
| 183 | + |
| 184 | + it("fails from exit error with coroutines", function() |
| 185 | + local entered = false |
| 186 | + local co = function() |
| 187 | + entered = true |
| 188 | + coroutine.yield(nil) |
| 189 | + |
| 190 | + assert(false, "failed in exit") |
| 191 | + end |
| 192 | + |
| 193 | + local ran_callback = false |
| 194 | + assert.has.error_match(function() |
| 195 | + with(co, function(obj) |
| 196 | + ran_callback = true |
| 197 | + end) |
| 198 | + end, "Should be done") |
| 199 | + |
| 200 | + assert.is["true"](entered) |
| 201 | + assert.is["true"](ran_callback) |
| 202 | + end) |
| 203 | +end) |
0 commit comments