diff --git a/dump_test.go b/dump_test.go index 5a65a2a..cf01be8 100644 --- a/dump_test.go +++ b/dump_test.go @@ -11,7 +11,7 @@ import ( ) func TestUndumpThenDumpReturnsTheSameFunction(t *testing.T) { - _, err := exec.LookPath("luac") + _, err := exec.LookPath("luac5.2") if err != nil { t.Skipf("testing dump requires luac: %s", err) } diff --git a/parser_test.go b/parser_test.go index 36171e3..d555d63 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,6 +1,7 @@ package lua import ( + "math" "os/exec" "path/filepath" "reflect" @@ -46,9 +47,9 @@ func TestEmptyString(t *testing.T) { } func TestParserExhaustively(t *testing.T) { - _, err := exec.LookPath("luac") + _, err := exec.LookPath("luac5.2") if err != nil { - t.Skipf("exhaustively testing the parser requires luac: %s", err) + t.Skipf("exhaustively testing the parser requires luac5.2: %s", err) } l := NewState() matches, err := filepath.Glob(filepath.Join("lua-tests", "*.lua")) @@ -73,8 +74,8 @@ func protectedTestParser(l *State, t *testing.T, source string) { }() t.Log("Compiling " + source) binary := strings.TrimSuffix(source, ".lua") + ".bin" - if err := exec.Command("luac", "-o", binary, source).Run(); err != nil { - t.Fatalf("luac failed to compile %s: %s", source, err) + if err := exec.Command("luac5.2", "-o", binary, source).Run(); err != nil { + t.Fatalf("luac5.2 failed to compile %s: %s", source, err) } t.Log("Parsing " + source) bin := load(l, t, binary) @@ -86,22 +87,48 @@ func protectedTestParser(l *State, t *testing.T, source string) { } func expectEqual(t *testing.T, x, y interface{}, m string) { + t.Helper() if x != y { t.Errorf("%s doesn't match: %v, %v\n", m, x, y) } } func expectDeepEqual(t *testing.T, x, y interface{}, m string) bool { + t.Helper() if reflect.DeepEqual(x, y) { return true } if reflect.TypeOf(x).Kind() == reflect.Slice && reflect.ValueOf(y).Len() == 0 && reflect.ValueOf(x).Len() == 0 { return true } + // The following exception is necessary because Go stdlib math + // functions may be less precise than the C equivalents. In + // particular constants[153] in lua-tests/attrib.lua:360 + // which is the result of "10^33" gives different results. See + // https://github.com/golang/go/issues/25270 for discussion. + if m == "constants" { + xc, yc := x.([]value), y.([]value) + if len(xc) != len(yc) { + return false + } + for i := range xc { + if fx, ok := xc[i].(float64); ok { + fy, ok2 := yc[i].(float64) + if !ok2 || !similarFloat64(fx, fy) { + return false + } + } + } + return true + } t.Errorf("%s doesn't match: %v, %v\n", m, x, y) return false } +func similarFloat64(x, y float64) bool { + return x == y || math.Nextafter(x, y) == y +} + func compareClosures(t *testing.T, a, b *luaClosure) { expectEqual(t, a.upValueCount(), b.upValueCount(), "upvalue count") comparePrototypes(t, a.prototype, b.prototype)