Fixed nil panic in not
All checks were successful
Build / build (push) Successful in 2m27s

This commit is contained in:
Leon Mika 2025-06-18 23:57:14 +02:00
parent 58195738ba
commit 4ab94410b7
2 changed files with 30 additions and 1 deletions

View file

@ -283,7 +283,7 @@ func notBuiltin(ctx context.Context, args invocationArgs) (Object, error) {
return nil, err
}
return BoolObject(!args.args[0].Truthy()), nil
return BoolObject(!isTruthy(args.args[0])), nil
}
var errObjectsNotEqual = errors.New("objects not equal")

View file

@ -1766,6 +1766,35 @@ func TestBuiltins_Cat(t *testing.T) {
}
}
func TestBuiltins_Not(t *testing.T) {
tests := []struct {
desc string
expr string
want any
}{
{desc: "not 1", expr: `not 1`, want: false},
{desc: "not 2", expr: `not 0`, want: true},
{desc: "not 3", expr: `not ()`, want: true},
{desc: "not 4", expr: `not $true`, want: false},
{desc: "not 5", expr: `not $false`, want: 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.EvalString(ctx, tt.expr)
assert.NoError(t, err)
assert.Equal(t, tt.want, eqRes)
})
}
}
func TestBuiltins_NotNil(t *testing.T) {
tests := []struct {
desc string