Skip to content

Commit 0cd1932

Browse files
authored
Use the tests.RunLuaTestFile in a few more places including preload - where the "shellescape" was also added. (#26)
* Use the tests.RunLuaTestFile in a few more places including preload - where the "shellescape" was also added. Using this splits each group of assertions into a separate test case reported by the go test runner. * Remove tab. * Remove test object (old style) * Update go versions in build matrix Co-authored-by: Sheridan C Rawlins <scr@yahooinc.com>
1 parent b81b386 commit 0cd1932

File tree

12 files changed

+122
-133
lines changed

12 files changed

+122
-133
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
go: ['1.14', '1.15']
15+
go: ['1.14', '1.15', '1.16', '1.17', '1.18']
1616

1717
services: {}
1818
steps:

json/api_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package json
22

33
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/vadv/gopher-lua-libs/tests"
46
"testing"
57

68
inspect "github.com/vadv/gopher-lua-libs/inspect"
7-
lua "github.com/yuin/gopher-lua"
89
)
910

1011
func TestApi(t *testing.T) {
11-
state := lua.NewState()
12-
Preload(state)
13-
inspect.Preload(state)
14-
if err := state.DoFile("./test/test_api.lua"); err != nil {
15-
t.Fatalf("execute test: %s\n", err.Error())
16-
}
12+
preload := tests.SeveralPreloadFuncs(Preload, inspect.Preload)
13+
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "./test/test_api.lua"))
1714
}

json/test/test_api.lua

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
local json = require("json")
22
local inspect = require("inspect")
33

4-
local jsonStringWithNull = [[{"a":{"b":1, "c":null}}]]
5-
local jsonString = [[{"a":{"b":1}}]]
4+
function TestJson(t)
5+
local jsonStringWithNull = [[{"a":{"b":1, "c":null}}]]
6+
local jsonString = [[{"a":{"b":1}}]]
67

7-
local result, err = json.decode(jsonStringWithNull)
8-
if err then error(err) end
9-
if not(result["a"]["b"] == 1) then error("must be decode") end
10-
print("done: json.decode()")
8+
local result, err = json.decode(jsonStringWithNull)
9+
t:Run("decode", function(t)
10+
if err then error(err) end
11+
if result["a"]["b"] ~= 1 then error("must be decode") end
12+
if result["a"]["c"] ~= nil then error("c is not nil") end
13+
print("done: json.decode()")
14+
end)
1115

12-
local result, err = json.encode(result)
13-
if err then error(err) end
14-
if not(result==jsonString) then error("must be encode "..inspect(result)) end
15-
print("done: json.encode()")
16+
local result, err = json.encode(result)
17+
t:Run("encode omits null values", function(t)
18+
if err then error(err) end
19+
if result ~= jsonString then error("must be encode "..inspect(result)) end
20+
print("done: json.encode()")
21+
end)
22+
end

preload.lua

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
1-
require("cert_util")
2-
require("db")
3-
require("filepath")
4-
require("inspect")
5-
require("ioutil")
6-
require("json")
7-
require("regexp")
8-
require("strings")
9-
require("tac")
10-
require("tcp")
11-
require("time")
12-
require("xmlpath")
13-
require("yaml")
14-
require("plugin")
15-
require("runtime")
16-
require("cmd")
17-
require("telegram")
18-
require("zabbix")
19-
require("storage")
20-
require("crypto")
21-
require("goos")
22-
require("humanize")
23-
require("chef")
24-
require("template")
25-
require("pprof")
26-
require("cloudwatch")
27-
require("log")
28-
require("prometheus")
29-
require("pb")
30-
require("stats")
1+
function TestRequireModule(t)
2+
modules = {
3+
"cert_util",
4+
"chef",
5+
"cloudwatch",
6+
"cmd",
7+
"crypto",
8+
"db",
9+
"filepath",
10+
"goos",
11+
"humanize",
12+
"inspect",
13+
"ioutil",
14+
"json",
15+
"log",
16+
"pb",
17+
"plugin",
18+
"pprof",
19+
"prometheus",
20+
"regexp",
21+
"runtime",
22+
"shellescape",
23+
"stats",
24+
"storage",
25+
"strings",
26+
"tac",
27+
"tcp",
28+
"telegram",
29+
"template",
30+
"time",
31+
"xmlpath",
32+
"yaml",
33+
"zabbix",
34+
}
35+
for _, module in ipairs(modules) do
36+
t:Run(module, function(t)
37+
require(module)
38+
end)
39+
end
40+
end
41+

preload_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package libs
22

33
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/vadv/gopher-lua-libs/tests"
46
"testing"
5-
6-
lua "github.com/yuin/gopher-lua"
77
)
88

99
func TestPreload(t *testing.T) {
10-
state := lua.NewState()
11-
Preload(state)
12-
if err := state.DoFile("./preload.lua"); err != nil {
13-
t.Fatalf("execute test: %s\n", err.Error())
14-
}
10+
assert.NotZero(t, tests.RunLuaTestFile(t, Preload, "./preload.lua"))
1511
}

shellescape/api_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package shellescape
22

33
import (
4+
"github.com/stretchr/testify/assert"
45
"github.com/vadv/gopher-lua-libs/inspect"
56
"github.com/vadv/gopher-lua-libs/tests"
6-
lua "github.com/yuin/gopher-lua"
77
"testing"
88
)
99

1010
func TestApi(t *testing.T) {
11-
preloadForTest := func(L *lua.LState) {
12-
Preload(L)
13-
inspect.Preload(L)
14-
}
15-
tests.RunLuaTestFile(t, preloadForTest, "test/test_api.lua")
11+
preload := tests.SeveralPreloadFuncs(Preload, inspect.Preload)
12+
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "test/test_api.lua"))
1613
}

strings/api_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package strings
22

33
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/vadv/gopher-lua-libs/tests"
46
"testing"
5-
6-
lua "github.com/yuin/gopher-lua"
77
)
88

99
func TestApi(t *testing.T) {
10-
state := lua.NewState()
11-
Preload(state)
12-
if err := state.DoFile("./test/test_api.lua"); err != nil {
13-
t.Fatalf("execute test: %s\n", err.Error())
14-
}
10+
assert.NotZero(t, tests.RunLuaTestFile(t, Preload, "./test/test_api.lua"))
1511
}

strings/test/test_api.lua

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@ local strings = require("strings")
22

33
local str = "hello world"
44

5-
local t = strings.split(str, " ")
6-
local count_t = 0
7-
for k, v in pairs(t) do
8-
count_t = count_t + 1
9-
if k == 1 then if not(v == "hello") then error("strings.split()") end end
10-
if k == 2 then if not(v == "world") then error("strings.split()") end end
5+
function TestSplit(t)
6+
local str_parts = strings.split(str, " ")
7+
assert(type(str_parts) == 'table')
8+
assert(#str_parts == 2, string.format("%d ~= 2", #str_parts))
9+
assert(str_parts[1] == "hello", string.format("%s ~= hello", str_parts[1]))
10+
assert(str_parts[2] == "world", string.format("%s ~= world", str_parts[2]))
1111
end
12-
if not(count_t == 2) then error("string.split()") end
13-
print("done: strings.split()")
1412

15-
if not(strings.has_prefix(str, "hello")) then error("strings.has_prefix()") end
16-
if not(strings.has_suffix(str, "world")) then error("strings.has_suffix()") end
17-
print("done: strings.has_suffix, strings.has_prefix")
13+
function TestHasPrefix(t)
14+
assert(strings.has_prefix(str, "hello"), [[not strings.has_prefix("hello")]])
15+
end
16+
17+
function TestHasSuffix(t)
18+
assert(strings.has_suffix(str, "world"), [[not strings.has_suffix("world")]])
19+
end
1820

19-
if not(strings.trim(str, "world") == "hello ") then error("strings.trim()") end
20-
if not(strings.trim(str, "hello ") == "world") then error("strings.trim()") end
21-
if not(strings.trim_prefix(str, "hello ") == "world") then error("strings.trim()") end
22-
if not(strings.trim_suffix(str, "hello ") == "hello world") then error("strings.trim()") end
23-
print("done: strings.trim()")
21+
function TestTrim(t)
22+
assert(strings.trim(str, "world") == "hello ", "strings.trim()")
23+
assert(strings.trim(str, "hello ") == "world", "strings.trim()")
24+
assert(strings.trim_prefix(str, "hello ") == "world", "strings.trim()")
25+
assert(strings.trim_suffix(str, "hello ") == "hello world", "strings.trim()")
26+
end
2427

25-
if not(strings.contains(str, "hello ") == true) then error("strings.contains()") end
26-
print("done: strings.contains()")
28+
function TestContains(t)
29+
assert(strings.contains(str, "hello ") == true, "strings.contains()")
30+
end

tests/assertions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package tests

tests/testutil.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func registerTType(L *lua.LState) {
5656

5757
//RunLuaTestFile fires up a new state, registers the *testing.T and invokes all methods starting with Test.
5858
// This allows the lua test files to operate similar to go tests - see shellescape/test/test_api.lua
59-
func RunLuaTestFile(t *testing.T, preload PreloadFunc, filename string) {
59+
func RunLuaTestFile(t *testing.T, preload PreloadFunc, filename string) (numTests int) {
6060
L := lua.NewState()
6161
registerTType(L)
6262
require.NotNil(t, preload)
@@ -65,13 +65,24 @@ func RunLuaTestFile(t *testing.T, preload PreloadFunc, filename string) {
6565

6666
require.NoError(t, L.DoFile(filename))
6767
L.G.Global.ForEach(func(key lua.LValue, value lua.LValue) {
68-
key_str := lua.LVAsString(key)
69-
if strings.HasPrefix(key_str, "Test") && value.Type() == lua.LTFunction {
70-
t.Run(key_str, func(t *testing.T) {
68+
keyStr := lua.LVAsString(key)
69+
if strings.HasPrefix(keyStr, "Test") && value.Type() == lua.LTFunction {
70+
t.Run(keyStr, func(t *testing.T) {
71+
numTests++
7172
L.Push(value)
7273
L.Push(tLua(L, t))
7374
assert.NoError(t, L.PCall(1, 0, nil))
7475
})
7576
}
7677
})
78+
return
79+
}
80+
81+
//SeveralPreloadFuncs combines several PreloadFuncs to one such as when tests want to preload theirs + inspect
82+
func SeveralPreloadFuncs(preloadFuncs ...PreloadFunc) PreloadFunc {
83+
return func(L *lua.LState) {
84+
for _, preloadFunc := range preloadFuncs {
85+
preloadFunc(L)
86+
}
87+
}
7788
}

0 commit comments

Comments
 (0)