Example #1
0
func TestPullTableFromLua(t *testing.T) {

	for _, tt := range fromLuaRecTT {
		want, code := tt.want, tt.code
		l := lua.NewState()

		var got interface{}
		var err error

		l.Register("pullTable", func(l *lua.State) int {
			got, err = util.PullTable(l, 1)
			return 0
		})
		lua.LoadString(l, code)
		l.Call(0, 0)

		if err != nil {
			t.Fatalf("pulling table, %v", err)
		}

		if !reflect.DeepEqual(want, got) {
			t.Fatalf("maps are not equal, expected %v, got %v", want, got)
		}
	}

}
Example #2
0
func TestPullTableFailsWhenNotATable(t *testing.T) {
	l := lua.NewState()

	l.PushString("not a table")
	_, err := util.PullTable(l, l.Top())

	if err == nil {
		t.Fatalf("strings should not be convertible to tables")
	}
}
Example #3
0
func TestPullTableFailsGracefullyOnUnconvertableValues(t *testing.T) {
	l := lua.NewState()

	l.NewTable()
	l.PushGoClosure(func(l *lua.State) int { return 0 }, 0)
	l.SetField(-2, "foo")

	_, err := util.PullTable(l, l.Top())

	if err == nil {
		t.Fatalf("should not be able to convert closure")
	}
}
Example #4
0
func TestPullTableFailsGracefullyOnCyclicStructures(t *testing.T) {
	l := lua.NewState()

	l.NewTable()
	l.PushValue(-1)
	l.SetField(-2, "foo")

	_, err := util.PullTable(l, l.Top())

	if err == nil {
		t.Fatalf("cyclic tables should not not be convertible to maps")
	}
}
Example #5
0
func TestPullTableWithArraysFromLua(t *testing.T) {
	require := func(l *lua.State) {
		util.Open(l)
		l.Register("validateTable", func(l *lua.State) int {
			actual, err := util.PullTable(l, -1)
			if err != nil {
				t.Fatalf("failed to pull table: %s", err.Error())
			}

			expected := map[string]interface{}{
				"foo": []interface{}{
					"I",
					"love",
					map[string]interface{}{
						"lua": []interface{}{"LuaJIT", "go-lua"},
						"go":  []interface{}{"6g", "gcc-go"},
					},
				},
			}
			if !reflect.DeepEqual(expected, actual) {
				t.Fatalf("expected %v, actual %v", expected, actual)
			}

			return 0
		})
	}
	luatesting.RunLuaTestString(t, require, `
		validateTable({
			foo = array({
			  "I",
				"love",
				{
					lua = array({"LuaJIT", "go-lua"}),
					go = array({"6g", "gcc-go"}),
				},
			})
		})
	`)
}
		}
	},

	"collection": func(db *mgo.Database) lua.Function {
		return func(l *lua.State) int {
			pushMongoCollection(l, db, lua.CheckString(l, 1))

			return 1
		}
	},

	"command": func(db *mgo.Database) lua.Function {
		return func(l *lua.State) int {
			var result interface{}

			cmd, err := util.PullTable(l, 1)

			if err != nil {
				lua.Errorf(l, "%s", err.Error())
				panic("unreachable")
			}

			err = db.Run(cmd, &result)

			if err != nil {
				lua.Errorf(l, "%s", err.Error())
				panic("unreachable")
			}

			util.DeepPush(l, result)
	"github.com/clbanning/mxj"
	"github.com/telemetryapp/go-lua"
	"github.com/telemetryapp/goluago/util"
)

var xmlLibrary = []lua.RegistryFunction{
	{
		"encode",
		func(l *lua.State) int {
			lua.CheckAny(l, 1)

			var v interface{}
			var err error

			if l.IsTable(1) {
				if v, err = util.PullTable(l, 1); err != nil {
					lua.Errorf(l, "%s", err)
					panic("unreachable")
				}
			} else {
				lua.Errorf(l, "Only tables can be converted to XML")
				panic("unreachable")
			}

			res, err := mxj.Map(v.(map[string]interface{})).Xml()

			if err != nil {
				lua.Errorf(l, "%s", err.Error())
				panic("unreachable")
			}