Skip to content

Commit c917e62

Browse files
add tests for cond operators
1 parent 616705c commit c917e62

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
local helpers = require("test.functional.helpers")(after_each)
2+
3+
local works = function(snippets, opts) end
4+
5+
describe("expand_conditions", function()
6+
-- apparently clear() needs to run before anything else...
7+
helpers.clear()
8+
helpers.exec("set rtp+=" .. os.getenv("LUASNIP_SOURCE"))
9+
it("simple", function()
10+
local function foo()
11+
return helpers.exec_lua([[
12+
local cond = require("luasnip.extras.expand_conditions").make_condition
13+
local c = cond(function() return true end)
14+
return c() == true
15+
]])
16+
end
17+
assert.has_no.errors(foo)
18+
assert.is_true(foo())
19+
end)
20+
describe("logic ops", function()
21+
describe("and", function()
22+
local function foo(b1, b2)
23+
-- Attention: use this only when testing (otherwise (pot.
24+
-- malicious) users might inject code)
25+
return helpers.exec_lua(([[
26+
local cond = require("luasnip.extras.expand_conditions").make_condition
27+
local c = cond(function() return %s end) * cond(function() return %s end)
28+
return c() == %s
29+
]]):format(tostring(b1),tostring(b2), tostring(b1 and b2)))
30+
end
31+
for _,ele in ipairs{{true,true}, {true,false}, {false,true}, {false,false}} do
32+
it(("%s and %s"):format(tostring(ele[1]), tostring(ele[2])), function()
33+
local test = function() return foo(ele[1], ele[2]) end
34+
assert.has_no.errors(test) assert.is_true(test())
35+
end)
36+
end
37+
end)
38+
describe("or", function()
39+
local function foo(b1, b2)
40+
-- Attention: use this only when testing (otherwise (pot.
41+
-- malicious) users might inject code)
42+
return helpers.exec_lua(([[
43+
local cond = require("luasnip.extras.expand_conditions").make_condition
44+
local c = cond(function() return %s end) + cond(function() return %s end)
45+
return c() == %s
46+
]]):format(tostring(b1),tostring(b2), tostring(b1 or b2)))
47+
end
48+
for _,ele in ipairs{{true,true}, {true,false}, {false,true}, {false,false}} do
49+
it(("%s or %s"):format(tostring(ele[1]), tostring(ele[2])), function()
50+
local test = function() return foo(ele[1], ele[2]) end
51+
assert.has_no.errors(test) assert.is_true(test())
52+
end)
53+
end
54+
end)
55+
describe("xor", function()
56+
local function foo(b1, b2)
57+
-- Attention: use this only when testing (otherwise (pot.
58+
-- malicious) users might inject code)
59+
return helpers.exec_lua(([[
60+
local cond = require("luasnip.extras.expand_conditions").make_condition
61+
local c = cond(function() return %s end) ^ cond(function() return %s end)
62+
return c() == %s
63+
]]):format(tostring(b1),tostring(b2), tostring((b1 and not b2) or (not b1 and b2))))
64+
end
65+
for _,ele in ipairs{{true,true}, {true,false}, {false,true}, {false,false}} do
66+
it(("%s xor %s"):format(tostring(ele[1]), tostring(ele[2])), function()
67+
local test = function() return foo(ele[1], ele[2]) end
68+
assert.has_no.errors(test) assert.is_true(test())
69+
end)
70+
end
71+
end)
72+
-- describe("xnor", function()
73+
-- local function foo(b1, b2)
74+
-- -- Attention: use this only when testing (otherwise (pot.
75+
-- -- malicious) users might inject code)
76+
-- return helpers.exec_lua(([[
77+
-- local cond = require("luasnip.extras.expand_conditions").make_condition
78+
-- local c = cond(function() return %s end) == cond(function() return %s end)
79+
-- return c() == %s
80+
-- ]]):format(tostring(b1),tostring(b2), tostring(b1 == b2)))
81+
-- end
82+
-- for _,ele in ipairs{{true,true}, {true,false}, {false,true}, {false,false}} do
83+
-- it(("%s xnor %s"):format(tostring(ele[1]), tostring(ele[2])), function()
84+
-- local test = function() return foo(ele[1], ele[2]) end
85+
-- assert.has_no.errors(test) assert.is_true(test())
86+
-- end)
87+
-- end
88+
-- end)
89+
describe("not", function()
90+
local function foo(b1, b2)
91+
-- Attention: use this only when testing (otherwise (pot.
92+
-- malicious) users might inject code)
93+
return helpers.exec_lua(([[
94+
local cond = require("luasnip.extras.expand_conditions").make_condition
95+
local c = -cond(function() return %s end)
96+
return c() == %s
97+
]]):format(tostring(b1), tostring(not b1)))
98+
end
99+
for _,ele in ipairs{{true}, {false}} do
100+
it(("not %s"):format(tostring(ele[1])), function()
101+
local test = function() return foo(ele[1]) end
102+
assert.has_no.errors(test)
103+
assert.is_true(test())
104+
end)
105+
end
106+
end)
107+
end)
108+
end)

0 commit comments

Comments
 (0)