File tree Expand file tree Collapse file tree 5 files changed +89
-0
lines changed
Expand file tree Collapse file tree 5 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -39,3 +39,4 @@ luac.out
3939* .x86_64
4040* .hex
4141
42+ .DS_Store
Original file line number Diff line number Diff line change 1+ package = ' switch'
2+ version = ' 1.0-0'
3+ source = {
4+ url = ' https://github.com/ryanplusplus/switch.lua/archive/v1.0-0.tar.gz' ,
5+ dir = ' switch.lua-1.0-0/src'
6+ }
7+ description = {
8+ summary = ' Lua implementation of a switch statement.' ,
9+ homepage = ' https://github.com/ryanplusplus/switch.lua/' ,
10+ license = ' MIT <http://opensource.org/licenses/MIT>'
11+ }
12+ dependencies = {
13+ ' lua >= 5.1'
14+ }
15+ build = {
16+ type = ' builtin' ,
17+ modules = {
18+ [' switch' ] = ' switch.lua'
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package = ' switch'
2+ version = ' git-0'
3+ source = {
4+ url = ' git://github.com/ryanplusplus/switch.lua.git' ,
5+ dir = ' src'
6+ }
7+ description = {
8+ summary = ' Lua implementation of a switch statement.' ,
9+ homepage = ' https://github.com/ryanplusplus/switch.lua/' ,
10+ license = ' MIT <http://opensource.org/licenses/MIT>'
11+ }
12+ dependencies = {
13+ ' lua >= 5.1'
14+ }
15+ build = {
16+ type = ' builtin' ,
17+ modules = {
18+ [' switch' ] = ' switch.lua'
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ describe (' switch' , function ()
2+ local switch = require ' switch'
3+
4+ it (' should call the corresponding function with the switched value' , function ()
5+ local f = spy .new (load ' ' )
6+
7+ switch (' hello' , {
8+ goodbye = error ,
9+ hello = f
10+ })
11+
12+ assert .spy (f ).was_called_with (' hello' )
13+ end )
14+
15+ it (' should not call any functions if no value matches' , function ()
16+ switch (' hello' , {
17+ goodbye = error
18+ })
19+ end )
20+
21+ it (' should allow a default case to be provided' , function ()
22+ local f = spy .new (load ' ' )
23+
24+ switch (' lua' , {
25+ goodbye = error ,
26+ hello = error ,
27+ [switch .default ] = f
28+ })
29+
30+ assert .spy (f ).was_called_with (' lua' )
31+ end )
32+ end )
Original file line number Diff line number Diff line change 1+ local default_case = {}
2+
3+ local function switch (value , cases )
4+ local default = cases [default_case ] or load ' '
5+ return setmetatable (cases , {
6+ __index = function ()
7+ return default
8+ end
9+ })[value ](value )
10+ end
11+
12+ return setmetatable ({
13+ default = default_case
14+ }, {
15+ __call = function (_ , ...) return switch (... ) end
16+ })
You can’t perform that action at this time.
0 commit comments