Compare commits
No commits in common. "d501a3af2517e04669731eea646155b3e4ffb531" and "db2c3fa5c4497374fce11301b8871512580af7b4" have entirely different histories.
d501a3af25
...
db2c3fa5c4
|
@ -1,20 +0,0 @@
|
||||||
name: Test
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- feature/*
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: docker
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: 1.22.4
|
|
||||||
- name: Build
|
|
||||||
run: |
|
|
||||||
make test
|
|
|
@ -88,85 +88,17 @@ func eqBuiltin(ctx context.Context, args invocationArgs) (object, error) {
|
||||||
l := args.args[0]
|
l := args.args[0]
|
||||||
r := args.args[1]
|
r := args.args[1]
|
||||||
|
|
||||||
return boolObject(objectsEqual(l, r)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func neBuiltin(ctx context.Context, args invocationArgs) (object, error) {
|
|
||||||
if err := args.expectArgn(2); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
l := args.args[0]
|
|
||||||
r := args.args[1]
|
|
||||||
|
|
||||||
return boolObject(!objectsEqual(l, r)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var errObjectsNotEqual = errors.New("objects not equal")
|
|
||||||
|
|
||||||
func objectsEqual(l, r object) bool {
|
|
||||||
if l == nil || r == nil {
|
|
||||||
return l == nil && r == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch lv := l.(type) {
|
switch lv := l.(type) {
|
||||||
case strObject:
|
case strObject:
|
||||||
if rv, ok := r.(strObject); ok {
|
if rv, ok := r.(strObject); ok {
|
||||||
return lv == rv
|
return boolObject(lv == rv), nil
|
||||||
}
|
}
|
||||||
case intObject:
|
case intObject:
|
||||||
if rv, ok := r.(intObject); ok {
|
if rv, ok := r.(intObject); ok {
|
||||||
return lv == rv
|
return boolObject(lv == rv), nil
|
||||||
}
|
|
||||||
case boolObject:
|
|
||||||
if rv, ok := r.(boolObject); ok {
|
|
||||||
return lv == rv
|
|
||||||
}
|
|
||||||
case listable:
|
|
||||||
rv, ok := r.(listable)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if lv.Len() != rv.Len() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i := 0; i < lv.Len(); i++ {
|
|
||||||
if !objectsEqual(lv.Index(i), rv.Index(i)) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return boolObject(false), nil
|
||||||
case hashable:
|
|
||||||
rv, ok := r.(hashable)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if lv.Len() != rv.Len() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if err := lv.Each(func(k string, lkv object) error {
|
|
||||||
rkv := rv.Value(k)
|
|
||||||
if rkv == nil {
|
|
||||||
return errObjectsNotEqual
|
|
||||||
} else if !objectsEqual(lkv, rkv) {
|
|
||||||
return errObjectsNotEqual
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
case OpaqueObject:
|
|
||||||
rv, ok := r.(OpaqueObject)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return lv.v == rv.v
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func concatBuiltin(ctx context.Context, args invocationArgs) (object, error) {
|
func concatBuiltin(ctx context.Context, args invocationArgs) (object, error) {
|
||||||
|
|
10
ucl/inst.go
10
ucl/inst.go
|
@ -61,8 +61,6 @@ func New(opts ...InstOption) *Inst {
|
||||||
rootEC.addCmd("reduce", invokableFunc(reduceBuiltin))
|
rootEC.addCmd("reduce", invokableFunc(reduceBuiltin))
|
||||||
|
|
||||||
rootEC.addCmd("eq", invokableFunc(eqBuiltin))
|
rootEC.addCmd("eq", invokableFunc(eqBuiltin))
|
||||||
rootEC.addCmd("ne", invokableFunc(neBuiltin))
|
|
||||||
|
|
||||||
rootEC.addCmd("add", invokableFunc(addBuiltin))
|
rootEC.addCmd("add", invokableFunc(addBuiltin))
|
||||||
|
|
||||||
rootEC.addCmd("cat", invokableFunc(concatBuiltin))
|
rootEC.addCmd("cat", invokableFunc(concatBuiltin))
|
||||||
|
@ -90,14 +88,6 @@ func New(opts ...InstOption) *Inst {
|
||||||
return inst
|
return inst
|
||||||
}
|
}
|
||||||
|
|
||||||
func (inst *Inst) SetVar(name string, value any) {
|
|
||||||
obj, err := fromGoValue(value)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
inst.rootEC.setOrDefineVar(name, obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (inst *Inst) Out() io.Writer {
|
func (inst *Inst) Out() io.Writer {
|
||||||
if inst.out == nil {
|
if inst.out == nil {
|
||||||
return os.Stdout
|
return os.Stdout
|
||||||
|
|
|
@ -118,8 +118,6 @@ func toGoValue(obj object) (interface{}, bool) {
|
||||||
return string(v), true
|
return string(v), true
|
||||||
case intObject:
|
case intObject:
|
||||||
return int(v), true
|
return int(v), true
|
||||||
case boolObject:
|
|
||||||
return bool(v), true
|
|
||||||
case listObject:
|
case listObject:
|
||||||
xs := make([]interface{}, 0, len(v))
|
xs := make([]interface{}, 0, len(v))
|
||||||
for _, va := range v {
|
for _, va := range v {
|
||||||
|
@ -161,8 +159,6 @@ func fromGoValue(v any) (object, error) {
|
||||||
return strObject(t), nil
|
return strObject(t), nil
|
||||||
case int:
|
case int:
|
||||||
return intObject(t), nil
|
return intObject(t), nil
|
||||||
case bool:
|
|
||||||
return boolObject(t), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fromGoReflectValue(reflect.ValueOf(v))
|
return fromGoReflectValue(reflect.ValueOf(v))
|
||||||
|
|
|
@ -229,16 +229,16 @@ func TestBuiltins_Break(t *testing.T) {
|
||||||
expr string
|
expr string
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{desc: "break unconditionally returning nothing", expr: `
|
//{desc: "break unconditionally returning nothing", expr: `
|
||||||
foreach ["1" "2" "3"] { |v|
|
// foreach ["1" "2" "3"] { |v|
|
||||||
break
|
// break
|
||||||
echo $v
|
// echo $v
|
||||||
}`, want: "(nil)\n"},
|
// }`, want: "(nil)\n"},
|
||||||
{desc: "break conditionally returning nothing", expr: `
|
//{desc: "break conditionally returning nothing", expr: `
|
||||||
foreach ["1" "2" "3"] { |v|
|
// foreach ["1" "2" "3"] { |v|
|
||||||
echo $v
|
// echo $v
|
||||||
if (eq $v "2") { break }
|
// if (eq $v "2") { break }
|
||||||
}`, want: "1\n2\n(nil)\n"},
|
// }`, want: "1\n2\n(nil)\n"},
|
||||||
{desc: "break inner loop only returning nothing", expr: `
|
{desc: "break inner loop only returning nothing", expr: `
|
||||||
foreach ["a" "b"] { |u|
|
foreach ["a" "b"] { |u|
|
||||||
foreach ["1" "2" "3"] { |v|
|
foreach ["1" "2" "3"] { |v|
|
||||||
|
@ -246,11 +246,11 @@ func TestBuiltins_Break(t *testing.T) {
|
||||||
if (eq $v "2") { break }
|
if (eq $v "2") { break }
|
||||||
}
|
}
|
||||||
}`, want: "a1\na2\nb1\nb2\n(nil)\n"},
|
}`, want: "a1\na2\nb1\nb2\n(nil)\n"},
|
||||||
{desc: "break returning value", expr: `
|
//{desc: "break returning value", expr: `
|
||||||
echo (foreach ["1" "2" "3"] { |v|
|
// echo (foreach ["1" "2" "3"] { |v|
|
||||||
echo $v
|
// echo $v
|
||||||
if (eq $v "2") { break "hello" }
|
// if (eq $v "2") { break "hello" }
|
||||||
})`, want: "1\n2\nhello\n(nil)\n"},
|
// })`, want: "1\n2\nhello\n(nil)\n"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -891,72 +891,3 @@ func TestBuiltins_Reduce(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuiltins_EqNe(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
desc string
|
|
||||||
expr string
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{desc: "equal strs 1", expr: `eq "hello" "hello"`, want: true},
|
|
||||||
{desc: "equal strs 2", expr: `eq "bla" "bla"`, want: true},
|
|
||||||
{desc: "equal strs 3", expr: `eq "" ""`, want: true},
|
|
||||||
{desc: "equal ints 1", expr: `eq 123 123`, want: true},
|
|
||||||
{desc: "equal ints 2", expr: `eq -21 -21`, want: true},
|
|
||||||
{desc: "equal ints 3", expr: `eq 0 0`, want: true},
|
|
||||||
{desc: "equal lists 1", expr: `eq [1 2 3] [1 2 3]`, want: true},
|
|
||||||
{desc: "equal lists 2", expr: `eq ["foo" "bar"] ["foo" "bar"]`, want: true},
|
|
||||||
{desc: "equal lists 3", expr: `eq [] []`, want: true},
|
|
||||||
{desc: "equal hashes 1", expr: `eq ["this":1 "that":"thing"] ["that":"thing" "this":1]`, want: true},
|
|
||||||
{desc: "equal hashes 2", expr: `eq ["foo":"bar"] ["foo":"bar"]`, want: true},
|
|
||||||
{desc: "equal bools 1", expr: `eq true true`, want: true},
|
|
||||||
{desc: "equal bools 2", expr: `eq false false`, want: true},
|
|
||||||
{desc: "equal nil 1", expr: `eq () ()`, want: true},
|
|
||||||
{desc: "equal opaque 1", expr: `eq $hello $hello`, want: true},
|
|
||||||
{desc: "equal opaque 2", expr: `eq $world $world`, want: true},
|
|
||||||
|
|
||||||
{desc: "not equal strs 1", expr: `eq "hello" "world"`, want: false},
|
|
||||||
{desc: "not equal strs 2", expr: `eq "bla" "BLA"`, want: false},
|
|
||||||
{desc: "not equal int 1", expr: `eq 131 313`, want: false},
|
|
||||||
{desc: "not equal int 2", expr: `eq -2 2`, want: false},
|
|
||||||
{desc: "not equal lists 1", expr: `eq [1 2 3] [1 2]`, want: false},
|
|
||||||
{desc: "not equal lists 2", expr: `eq ["123" "234"] [123 234]`, want: false},
|
|
||||||
{desc: "not equal hashes 1", expr: `eq ["this":1 "that":"thing"] ["that":"thing"]`, want: false},
|
|
||||||
{desc: "not equal hashes 2", expr: `eq ["this":1 "that":"thing"] ["this":1 "that":"thing" "other":"thing"]`, want: false},
|
|
||||||
{desc: "not equal hashes 3", expr: `eq ["this":1 "that":"thing"] ["this":"1" "that":"other"]`, want: false},
|
|
||||||
{desc: "not equal opaque 1", expr: `eq $hello $world`, want: false},
|
|
||||||
{desc: "not equal opaque 2", expr: `eq $hello "hello"`, want: false},
|
|
||||||
|
|
||||||
{desc: "not equal types 1", expr: `eq "123" 123`, want: false},
|
|
||||||
{desc: "not equal types 2", expr: `eq 0 ""`, want: false},
|
|
||||||
{desc: "not equal types 3", expr: `eq [] [:]`, want: false},
|
|
||||||
{desc: "not equal types 4", expr: `eq ["23"] "23"`, want: false},
|
|
||||||
{desc: "not equal types 5", expr: `eq true ()`, want: false},
|
|
||||||
{desc: "not equal types 6", expr: `eq () false`, want: false},
|
|
||||||
{desc: "not equal types 7", expr: `eq () "yes"`, want: false},
|
|
||||||
{desc: "not equal types 8", expr: `eq () $world`, want: false},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.desc, func(t *testing.T) {
|
|
||||||
type testProxyObject struct {
|
|
||||||
v string
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
outW := bytes.NewBuffer(nil)
|
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
|
||||||
inst.SetVar("hello", Opaque(testProxyObject{v: "hello"}))
|
|
||||||
inst.SetVar("world", Opaque(testProxyObject{v: "world"}))
|
|
||||||
|
|
||||||
eqRes, err := inst.Eval(ctx, tt.expr)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.want, eqRes)
|
|
||||||
|
|
||||||
neRes, err := inst.Eval(ctx, strings.ReplaceAll(tt.expr, "eq", "ne"))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, !tt.want, neRes)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue