2022-06-01 14:17:27 -07:00
|
|
|
package gcplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gochan-org/gochan/pkg/config"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
luar "layeh.com/gopher-luar"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
versionStr = `return _GOCHAN_VERSION
|
2022-06-01 15:33:46 -07:00
|
|
|
`
|
|
|
|
structPassingStr = `print(string.format("Receiving post from %q", post.Name))
|
2022-12-30 15:27:19 -08:00
|
|
|
print(string.format("Message before changing: %q", post.MessageRaw))
|
|
|
|
post.MessageRaw = "Message modified by a plugin\n"
|
|
|
|
post.Message = "Message modified by a plugin<br />"
|
|
|
|
print(string.format("Modified message text: %q", post.MessageText))`
|
2022-06-01 14:17:27 -07:00
|
|
|
)
|
|
|
|
|
2022-06-01 15:33:46 -07:00
|
|
|
func initPluginTests() {
|
2022-06-01 14:17:27 -07:00
|
|
|
config.SetVersion("3.1")
|
|
|
|
initLua()
|
2022-06-01 15:33:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionFunction(t *testing.T) {
|
|
|
|
initPluginTests()
|
2022-06-01 14:17:27 -07:00
|
|
|
err := lState.DoString(versionStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
testingVersionStr := lState.Get(-1).(lua.LString)
|
|
|
|
if testingVersionStr != "3.1" {
|
|
|
|
t.Fatalf("%q != \"3.1\"", testingVersionStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructPassing(t *testing.T) {
|
2022-06-01 15:33:46 -07:00
|
|
|
initPluginTests()
|
2022-06-01 14:17:27 -07:00
|
|
|
p := &gcsql.Post{
|
2022-10-11 14:26:31 -07:00
|
|
|
Name: "Joe Poster",
|
|
|
|
Email: "joeposter@gmail.com",
|
|
|
|
Message: "Message test<br />",
|
|
|
|
MessageRaw: "Message text\n",
|
2022-06-01 14:17:27 -07:00
|
|
|
}
|
|
|
|
lState.SetGlobal("post", luar.New(lState, p))
|
2022-06-01 15:33:46 -07:00
|
|
|
err := lState.DoString(structPassingStr)
|
2022-06-01 14:17:27 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
2022-10-11 14:26:31 -07:00
|
|
|
t.Logf("Modified message text after Lua: %q", p.MessageRaw)
|
|
|
|
if p.MessageRaw != "Message modified by a plugin\n" || p.Message != "Message modified by a plugin<br />" {
|
2022-06-01 15:33:46 -07:00
|
|
|
t.Fatal("message was not properly modified by plugin")
|
|
|
|
}
|
2022-06-01 14:17:27 -07:00
|
|
|
}
|