This commit is contained in:
parent
ee2e9464a7
commit
d111d84dbf
|
@ -56,7 +56,7 @@ func New(opts ...InstOption) *Inst {
|
||||||
rootEC.root = rootEC
|
rootEC.root = rootEC
|
||||||
|
|
||||||
rootEC.addCmd("echo", invokableFunc(echoBuiltin))
|
rootEC.addCmd("echo", invokableFunc(echoBuiltin))
|
||||||
rootEC.addCmd("var", invokableFunc(setBuiltin))
|
rootEC.addCmd("set", invokableFunc(setBuiltin))
|
||||||
rootEC.addCmd("toUpper", invokableFunc(toUpperBuiltin))
|
rootEC.addCmd("toUpper", invokableFunc(toUpperBuiltin))
|
||||||
rootEC.addCmd("len", invokableFunc(lenBuiltin))
|
rootEC.addCmd("len", invokableFunc(lenBuiltin))
|
||||||
rootEC.addCmd("keys", invokableFunc(keysBuiltin))
|
rootEC.addCmd("keys", invokableFunc(keysBuiltin))
|
||||||
|
|
|
@ -192,7 +192,7 @@ func TestBuiltins_If(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -382,7 +382,7 @@ func TestBuiltins_Try(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
if tt.wantErr != "" {
|
if tt.wantErr != "" {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
@ -422,7 +422,7 @@ func TestBuiltins_ForEach(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -466,7 +466,7 @@ func TestBuiltins_Break(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -507,7 +507,7 @@ func TestBuiltins_Continue(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -587,7 +587,7 @@ func TestBuiltins_Procs(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -712,7 +712,7 @@ func TestBuiltins_Return(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -774,7 +774,7 @@ func TestBuiltins_Seq(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -821,7 +821,7 @@ func TestBuiltins_Map(t *testing.T) {
|
||||||
outW := bytes.NewBuffer(nil)
|
outW := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
inst := New(WithOut(outW), WithTestBuiltin())
|
inst := New(WithOut(outW), WithTestBuiltin())
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -921,7 +921,7 @@ func TestBuiltins_Index(t *testing.T) {
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -980,7 +980,7 @@ func TestBuiltins_Len(t *testing.T) {
|
||||||
missing: "missing",
|
missing: "missing",
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
err := EvalAndDisplay(ctx, inst, tt.expr)
|
err := evalAndDisplay(ctx, inst, tt.expr)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, outW.String())
|
assert.Equal(t, tt.want, outW.String())
|
||||||
|
@ -1437,3 +1437,32 @@ func TestBuiltins_Cat(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func evalAndDisplay(ctx context.Context, inst *Inst, expr string) error {
|
||||||
|
res, err := inst.eval(ctx, expr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return displayResult(ctx, inst, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayResult(ctx context.Context, inst *Inst, res Object) (err error) {
|
||||||
|
switch v := res.(type) {
|
||||||
|
case nil:
|
||||||
|
if _, err = fmt.Fprintln(inst.out, "(nil)"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case Listable:
|
||||||
|
for i := 0; i < v.Len(); i++ {
|
||||||
|
if err = displayResult(ctx, inst, v.Index(i)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if _, err = fmt.Fprintln(inst.out, v.String()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue