|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gvcgo/version-manager/internal/luapi/lua_global" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + lua "github.com/yuin/gopher-lua" |
| 10 | +) |
| 11 | + |
| 12 | +var pluginScript = ` |
| 13 | +sdk_name = "lua" |
| 14 | +plugin_name = "lua" |
| 15 | +plugin_version = "0.0.1" |
| 16 | +prequisite = "miniconda" |
| 17 | +homepage = "https://www.lua.org/" |
| 18 | +function crawl() |
| 19 | + print("Crawling...") |
| 20 | +end |
| 21 | +` |
| 22 | + |
| 23 | +func ExecuteLuaScriptL(script string) (*lua.LState, error) { |
| 24 | + ll := lua_global.NewLua() |
| 25 | + L := ll.GetLState() |
| 26 | + err := L.DoString(script) |
| 27 | + return L, err |
| 28 | +} |
| 29 | + |
| 30 | +func TestDoesLuaItemExist(t *testing.T) { |
| 31 | + if l, err := ExecuteLuaScriptL(pluginScript); err != nil { |
| 32 | + t.Error(err) |
| 33 | + } else { |
| 34 | + ok1 := DoesLuaItemExist(l, SDKName) |
| 35 | + assert.Equal(t, true, ok1, "should be true") |
| 36 | + ok2 := DoesLuaItemExist(l, LuaConfItem("none")) |
| 37 | + assert.Equal(t, false, ok2, "should be false") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestGetLuaConfItem(t *testing.T) { |
| 42 | + if l, err := ExecuteLuaScriptL(pluginScript); err != nil { |
| 43 | + t.Error(err) |
| 44 | + } else { |
| 45 | + sdkName := GetLuaConfItemString(l, SDKName) |
| 46 | + sdkNameShouldBe := "lua" |
| 47 | + assert.Equal(t, sdkNameShouldBe, sdkName, fmt.Sprintf("sdk_name should be: %s", sdkNameShouldBe)) |
| 48 | + s := GetLuaConfItemString(l, LuaConfItem("none")) |
| 49 | + noneShouldBe := "" |
| 50 | + assert.Equal(t, noneShouldBe, s, fmt.Sprintf("none should be: %s", noneShouldBe)) |
| 51 | + } |
| 52 | +} |
0 commit comments