github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lib_test.go (about) 1 package runtime 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/arnodel/golua/scanner" 8 ) 9 10 func TestRuntime_CompileAndLoadLuaChunkOrExp(t *testing.T) { 11 r := New(os.Stdout) 12 type args struct { 13 name string 14 source []byte 15 scannerOptions []scanner.Option 16 } 17 tests := []struct { 18 name string 19 args args 20 wantErr bool 21 wantUnexpectedEOF bool 22 }{ 23 { 24 name: "a valid expresssion", 25 args: args{ 26 name: "exp", 27 source: []byte("1+1"), 28 }, 29 }, 30 { 31 name: "a valid chunk", 32 args: args{ 33 name: "chunk", 34 source: []byte("f()\nprint('hello')"), 35 }, 36 }, 37 { 38 name: "invalid chunk containing expression", 39 args: args{ 40 name: "nogood", 41 source: []byte("x+2\nprint(z)"), 42 }, 43 wantErr: true, 44 }, 45 { 46 name: "unfinished expresssion", 47 args: args{ 48 name: "uexp", 49 source: []byte("x+4-"), 50 }, 51 wantErr: true, 52 wantUnexpectedEOF: true, 53 }, 54 { 55 name: "unfinished statement", 56 args: args{ 57 name: "ustat", 58 source: []byte("do a = 2"), 59 }, 60 wantErr: true, 61 wantUnexpectedEOF: true, 62 }, 63 // TODO: Add test cases. 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 _, err := r.CompileAndLoadLuaChunkOrExp(tt.args.name, tt.args.source, NilValue, tt.args.scannerOptions...) 68 if (err != nil) != tt.wantErr { 69 t.Errorf("Runtime.CompileLuaChunkOrExp() error = %v, wantErr %v", err, tt.wantErr) 70 return 71 } 72 if tt.wantUnexpectedEOF && !ErrorIsUnexpectedEOF(err) { 73 t.Errorf("Runtime.CompileLuaChunkOrExp() IsUnexpectedEOF = %v, want %v", err, tt.wantUnexpectedEOF) 74 } 75 }) 76 } 77 } 78 79 // TestSetIndexNoNewIndex tests setting new indices of values without 80 // a __newindex metamethod. 81 func TestSetIndexNoNewIndex(t *testing.T) { 82 r := New(os.Stdout) 83 intValue := IntValue(42) 84 meta := NewTable() 85 udValue := UserDataValue(NewUserData([]int{}, meta)) 86 if err := SetIndex(r.MainThread(), intValue, intValue, intValue); err == nil { 87 t.Error("expected error indexing int value") 88 } 89 if err := SetIndex(r.MainThread(), udValue, intValue, intValue); err == nil { 90 t.Error("expected error indexing userdata value") 91 } 92 }