github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/wrappers_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func Test_runChunk(t *testing.T) {
    10  
    11  	if !QuotasAvailable {
    12  		t.Skip("Skipping as build does not enforce quotas")
    13  		return
    14  	}
    15  
    16  	type args struct {
    17  		source string
    18  		rtCtx  RuntimeContextDef
    19  	}
    20  	tests := []struct {
    21  		name           string
    22  		args           args
    23  		want           Value
    24  		wantErr        bool
    25  		skipIfNoQuotas bool
    26  	}{
    27  		{
    28  			name: "run out of cpu",
    29  			args: args{
    30  				source: `while true do end`,
    31  				rtCtx:  RuntimeContextDef{HardLimits: RuntimeResources{Cpu: 10000}},
    32  			},
    33  			want:    NilValue,
    34  			wantErr: true,
    35  		},
    36  		{
    37  			name: "return value",
    38  			args: args{
    39  				source: `return 42`,
    40  				rtCtx:  RuntimeContextDef{HardLimits: RuntimeResources{Cpu: 10000}},
    41  			},
    42  			want:    IntValue(42),
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name: "error in execution",
    47  			args: args{
    48  				source: `return {} + 1`,
    49  				rtCtx:  RuntimeContextDef{HardLimits: RuntimeResources{Cpu: 10000}},
    50  			},
    51  			want:    NilValue,
    52  			wantErr: true,
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			got, err := RunChunk1([]byte(tt.args.source), tt.args.rtCtx, os.Stdout)
    58  			if (err != nil) != tt.wantErr {
    59  				t.Errorf("runChunk() error = %v, wantErr %v", err, tt.wantErr)
    60  				return
    61  			}
    62  			if !reflect.DeepEqual(got, tt.want) {
    63  				t.Errorf("runChunk() = %v, want %v", got, tt.want)
    64  			}
    65  		})
    66  	}
    67  }