Skip to content

Commit

Permalink
more tests for endpoint/Lua functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sapslaj committed Jan 15, 2024
1 parent 205820e commit 4b396c6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
50 changes: 49 additions & 1 deletion endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,58 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
lua "github.com/yuin/gopher-lua"
)

func TestLuaTableConversions(t *testing.T) {
func TestToLuaTable(t *testing.T) {
state := lua.NewState()
defer state.Close()
err := state.DoFile("test_lua/test_to_lua_table.lua")
require.NoError(t, err)
testFunc := state.Get(-1).(*lua.LFunction)

endpoint := &Endpoint{
Hostname: "test-host",
IPv4s: []string{"192.0.2.1"},
IPv6s: []string{"2001:db8::1"},
RecordTTL: 60,
SourceProperties: map[string]any{
"source_prop": "prop",
},
ProviderProperties: map[string]any{
"provider_prop": "prop",
},
}
ltEndpoint := endpoint.ToLuaTable(state)
co, _ := state.NewThread()
st, err, _ := state.Resume(co, testFunc, ltEndpoint)
require.NoError(t, err)
require.Equal(t, st, lua.ResumeOK)
}

func TestFromLuaTable(t *testing.T) {
state := lua.NewState()
defer state.Close()
err := state.DoFile("test_lua/test_from_lua_table.lua")
require.NoError(t, err)
testFunc := state.Get(-1).(*lua.LFunction)
co, _ := state.NewThread()
st, err, values := state.Resume(co, testFunc)
require.NoError(t, err)
require.Equal(t, st, lua.ResumeOK)
ltEndpoint := values[0].(*lua.LTable)
endpoint := FromLuaTable(state, ltEndpoint)
assert.Equal(t, "test-host", endpoint.Hostname)
assert.Equal(t, []string{"192.0.2.1"}, endpoint.IPv4s)
assert.Equal(t, []string{"2001:db8::1"}, endpoint.IPv6s)
assert.Equal(t, int64(60), endpoint.RecordTTL)
assert.Equal(t, map[string]any{"source_prop": "prop"}, endpoint.SourceProperties)
assert.Equal(t, map[string]any{"provider_prop": "prop"}, endpoint.ProviderProperties)
}

func TestLuaTableDiff(t *testing.T) {
tests := map[string]struct {
endpoint *Endpoint
}{
Expand Down
14 changes: 14 additions & 0 deletions endpoint/test_lua/test_from_lua_table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
return function ()
return {
hostname = "test-host",
ipv4s = { "192.0.2.1" },
ipv6s = { "2001:db8::1" },
record_ttl = 60,
source_properties = {
source_prop = "prop",
},
provider_properties = {
provider_prop = "prop",
},
}
end
14 changes: 14 additions & 0 deletions endpoint/test_lua/test_to_lua_table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local assert_equal = function(a, b)
if not a == b then
error(string.format("mismatch: '%s' != '%s'.", a, b))
end
end

return function (endpoint)
assert_equal(endpoint.hostname, "test-host")
assert_equal(endpoint.ipv4s[1], "192.0.2.1")
assert_equal(endpoint.ipv6s[1], "2001:db8::1")
assert_equal(endpoint.record_ttl, 60)
assert_equal(endpoint.source_properties.source_prop, "prop")
assert_equal(endpoint.provider_properties.provider_prop, "prop")
end

0 comments on commit 4b396c6

Please sign in to comment.