Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
37 changes: 0 additions & 37 deletions argparse/internal/include_all_lua.go

This file was deleted.

15 changes: 5 additions & 10 deletions argparse/loader.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package argparse

//go:generate go run internal/include_all_lua.go

import (
"encoding/base64"

lua "github.com/yuin/gopher-lua"
)

// TODO(scr): move to embed once minimum supported go version is 1.16
//go:generate go run github.com/logrusorgru/textFileToGoConst@latest -in argparse.lua -o lua_const.go -c lua_argparse

// Preload adds inspect to the given Lua state's package.preload table. After it
// has been preloaded, it can be loaded using require:
//
// local inspect = require("inspect")
// local inspect = require("inspect")
func Preload(L *lua.LState) {
L.PreloadModule("argparse", Loader)
}

// Loader is the module loader function.
func Loader(L *lua.LState) int {
code, err := base64.StdEncoding.DecodeString(lua_argparse)
if err != nil {
panic(err.Error())
}
if err := L.DoString(string(code)); err != nil {
if err := L.DoString(lua_argparse); err != nil {
L.RaiseError("load library 'argparse' error: %s", err.Error())
}
return 1
Expand Down
8 changes: 6 additions & 2 deletions argparse/lua_const.go

Large diffs are not rendered by default.

File renamed without changes.
37 changes: 0 additions & 37 deletions inspect/internal/include_all_lua.go

This file was deleted.

13 changes: 4 additions & 9 deletions inspect/loader.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package inspect

//go:generate go run internal/include_all_lua.go
// TODO(scr): move to embed once minimum supported go version is 1.16
//go:generate go run github.com/logrusorgru/textFileToGoConst@latest -in inspect.lua -o lua_const.go -c lua_inspect

import (
"encoding/base64"

lua "github.com/yuin/gopher-lua"
)

// Preload adds inspect to the given Lua state's package.preload table. After it
// has been preloaded, it can be loaded using require:
//
// local inspect = require("inspect")
// local inspect = require("inspect")
func Preload(L *lua.LState) {
L.PreloadModule("inspect", Loader)
}

// Loader is the module loader function.
func Loader(L *lua.LState) int {
code, err := base64.StdEncoding.DecodeString(lua_inspect)
if err != nil {
panic(err.Error())
}
if err := L.DoString(string(code)); err != nil {
if err := L.DoString(lua_inspect); err != nil {
L.RaiseError("load library 'inspect' error: %s", err.Error())
}
return 1
Expand Down
341 changes: 339 additions & 2 deletions inspect/lua_const.go

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Package tests

Support for writing lua tests like go tests; any function starting with Test will be run.

## Example go code

```go
func TestApi(t *testing.T) {
preload := tests.SeveralPreloadFuncs(
inspect.Preload,
strings.Preload,
)
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "./test/test_api.lua"))
}
```

## Example lua code

```lua
function TestFoo(t)
t:Log("foo bar baz")
assert(someVariable, tostring(someVariable))
expected = 2
assert(somethingElse == expected, string.format("%d ~= %d", somethingElse, expected))
end

function TestMaybe(t)
if os.getenv('SKIP_IT') then
t:Skip("Skipped because SKIP_IT is defined")
end
assert(theActualTest, tostring(theActualTest))
end
```

## Example use of suite

To simulate [testify.suite](https://pkg.go.dev/github.com/stretchr/testify/suite) hooks, a simple suite implementation
is preloaded as well

```lua
local suite = require 'suite'

local MySuite = suite.Suite:new {
setupCount = 0,
setupSuiteCount = 0,
tearDownCount = 0,
tearDownSuiteCount = 0,
}

function MySuite:SetupSuite()
self.setupSuiteCount = self.setupSuiteCount + 1
end

function MySuite:TearDownSuite()
self.tearDownSuiteCount = self.tearDownSuiteCount + 1
end

function MySuite:SetupTest()
self.setupCount = self.setupCount + 1
end

function MySuite:TearDownTest()
self.tearDownCount = self.tearDownCount + 1
end

function MySuite:TestFoobar()
-- T is available from superclass for suites; not passed in as arg
self:T():Log('TestFoobar')
end

function MySuite:TestBaz()
self:T():Log('TestBaz')

self:Run('sub1', function()
self:T():Log('sub1')
end)

self:Run('sub2', function()
self:T():Log('sub2')
end)
end

function TestSuite(t)
-- Same mechanism for test discovery is used, but then the suite is run as sub tests via suite.Run
suite.Run(t, MySuite)

-- Called for every test: two tests so should be 2
assert(MySuite.setupCount == 2, tostring(MySuite.setupCount))
assert(MySuite.tearDownCount == 2, tostring(MySuite.tearDownCount))

-- Called only once for the suite so should be 1
assert(MySuite.setupSuiteCount == 1, tostring(MySuite.setupSuiteCount))
assert(MySuite.tearDownSuiteCount == 1, tostring(MySuite.tearDownSuiteCount))
end
```
1 change: 0 additions & 1 deletion tests/assertions.go

This file was deleted.

67 changes: 67 additions & 0 deletions tests/lua_const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// GENERATED BY textFileToGoConst
// GitHub: github.com/logrusorgru/textFileToGoConst
// input file: suite.lua
// generated: Sat Nov 5 20:27:23 PDT 2022

package tests

const lua_suite = `local strings = require 'strings'

local suite = {
t = nil,
Suite = {},
}

function suite.Run(t, testSuite)
-- testSuite must be subclass of suite.Suite, so will have this method
testSuite:SetT(t)

if testSuite.SetupSuite then
testSuite:SetupSuite()
end
for k, v in pairs(testSuite) do
if strings.has_prefix(k, "Test") then
if testSuite.SetupTest then
testSuite:SetupTest()
end
t:Run(k, function(t)
testSuite:SetT(t)
v(testSuite)
end)
testSuite:SetT(t)
if testSuite.TearDownTest then
testSuite:TearDownTest()
end
end
end
if testSuite.TearDownSuite then
testSuite:TearDownSuite()
end
end

function suite.Suite:new(o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end

function suite.Suite:T()
return self.t
end

function suite.Suite:SetT(t)
self.t = t
end

function suite.Suite:Run(name, f)
local t = self:T()
self:T():Run(name, function(t)
self:SetT(t)
f(self)
end)
self:SetT(t)
end

return suite
`
59 changes: 59 additions & 0 deletions tests/suite.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local strings = require 'strings'

local suite = {
t = nil,
Suite = {},
}

function suite.Run(t, testSuite)
-- testSuite must be subclass of suite.Suite, so will have this method
testSuite:SetT(t)

if testSuite.SetupSuite then
testSuite:SetupSuite()
end
for k, v in pairs(testSuite) do
if strings.has_prefix(k, "Test") then
if testSuite.SetupTest then
testSuite:SetupTest()
end
t:Run(k, function(t)
testSuite:SetT(t)
v(testSuite)
end)
testSuite:SetT(t)
if testSuite.TearDownTest then
testSuite:TearDownTest()
end
end
end
if testSuite.TearDownSuite then
testSuite:TearDownSuite()
end
end

function suite.Suite:new(o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end

function suite.Suite:T()
return self.t
end

function suite.Suite:SetT(t)
self.t = t
end

function suite.Suite:Run(name, f)
local t = self:T()
self:T():Run(name, function(t)
self:SetT(t)
f(self)
end)
self:SetT(t)
end

return suite
15 changes: 15 additions & 0 deletions tests/testdata/test_api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local goos = require 'goos'

function TestTempDir(t)
-- Ensures that a tempdir created in subtests doesn't exist any longer after the test is run
local tempDir = ''

t:Run('createTmpDir', function(t)
tempDir = t:TempDir()
stat = goos.stat(tempDir)
assert(stat, tempDir .. " does not exist")
assert(stat.is_dir, tempDir .. " is not a dir")
end)
assert(tempDir ~= '', tempDir)
assert(not goos.stat(tempDir), tempDir .. " exists after the subtest completes")
end
Loading