diff --git a/ucl/testbuiltins_test.go b/ucl/testbuiltins_test.go index 0a74351..32aadf1 100644 --- a/ucl/testbuiltins_test.go +++ b/ucl/testbuiltins_test.go @@ -1140,3 +1140,84 @@ func TestBuiltins_AddSubMupDivMod(t *testing.T) { }) } } + +func TestBuiltins_AndOrNot(t *testing.T) { + tests := []struct { + desc string + expr string + want any + wantErr bool + }{ + {desc: "and 1", expr: `and $true $true`, want: true}, + {desc: "and 2", expr: `and $false $true`, want: false}, + {desc: "and 3", expr: `and $false $false`, want: false}, + {desc: "or 1", expr: `or $true $true`, want: true}, + {desc: "or 2", expr: `or $false $true`, want: true}, + {desc: "or 3", expr: `or $false $false`, want: false}, + {desc: "not 1", expr: `not $true`, want: false}, + {desc: "not 2", expr: `not $false`, want: true}, + {desc: "not 3", expr: `not $false $true`, want: true}, + + {desc: "short circuit and 1", expr: `and "hello" "world"`, want: "world"}, + {desc: "short circuit and 2", expr: `and () "world"`, want: false}, + {desc: "short circuit or 1", expr: `or "hello" "world"`, want: "hello"}, + {desc: "short circuit or 2", expr: `or () "world"`, want: "world"}, + + {desc: "bad and 1", expr: `and "one"`, wantErr: true}, + {desc: "bad and 2", expr: `and`, wantErr: true}, + {desc: "bad or 1", expr: `or "one"`, wantErr: true}, + {desc: "bad or 2", expr: `or`, wantErr: true}, + {desc: "bad not 2", expr: `not`, wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + ctx := context.Background() + outW := bytes.NewBuffer(nil) + + inst := New(WithOut(outW), WithTestBuiltin()) + inst.SetVar("true", true) + inst.SetVar("false", false) + + eqRes, err := inst.Eval(ctx, tt.expr) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, eqRes) + } + }) + } +} + +func TestBuiltins_Cat(t *testing.T) { + tests := []struct { + desc string + expr string + want any + }{ + {desc: "cat 1", expr: `cat "hello, " "world"`, want: "hello, world"}, + {desc: "cat 2", expr: `cat "hello, " "world " "and stuff"`, want: "hello, world and stuff"}, + {desc: "cat 3", expr: `cat "int = " 123`, want: "int = 123"}, + {desc: "cat 4", expr: `cat "bool = " $true`, want: "bool = true"}, + {desc: "cat 5", expr: `cat "array = " []`, want: "array = []"}, + {desc: "cat 6", expr: `cat "array = " [1 3 2 4]`, want: "array = [1 3 2 4]"}, + {desc: "cat 7", expr: `cat 1 $true 3 [4]`, want: "1true3[4]"}, + {desc: "cat 8", expr: `cat`, want: ""}, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + ctx := context.Background() + outW := bytes.NewBuffer(nil) + + inst := New(WithOut(outW), WithTestBuiltin()) + inst.SetVar("true", true) + inst.SetVar("false", false) + + eqRes, err := inst.Eval(ctx, tt.expr) + assert.NoError(t, err) + assert.Equal(t, tt.want, eqRes) + }) + } +}