diff --git a/cue/builtin_test.go b/cue/builtin_test.go index 83cd80d24..a560b0833 100644 --- a/cue/builtin_test.go +++ b/cue/builtin_test.go @@ -16,6 +16,7 @@ package cue import ( "fmt" + "math/big" "strings" "testing" ) @@ -31,6 +32,11 @@ func TestBuiltins(t *testing.T) { []string{fmt.Sprintf("(%s)", expr)}, }} } + hexToDec := func(s string) string { + var x big.Int + x.SetString(s, 16) + return x.String() + } testCases := []struct { instances []*bimport emit string @@ -128,6 +134,66 @@ func TestBuiltins(t *testing.T) { }, { testExpr(`or([])`), `_|_(builtin:or:empty or)`, + }, { + test("encoding/csv", `csv.Encode([[1,2,3],[4,5],[7,8,9]])`), + `"1,2,3\n4,5\n7,8,9\n"`, + }, { + test("encoding/csv", `csv.Encode([["a", "b"], ["c"]])`), + `"a,b\nc\n"`, + }, { + test("encoding/json", `json.Valid("1")`), + `true`, + }, { + test("encoding/json", `json.Compact("[1, 2]")`), + `"[1,2]"`, + }, { + test("encoding/json", `json.Indent(#"{"a": 1, "b": 2}"#, "", " ")`), + `"{\n \"a\": 1,\n \"b\": 2\n}"`, + }, { + test("encoding/json", `json.Unmarshal("1")`), + `1`, + }, { + test("encoding/json", `json.MarshalStream([{a: 1}, {b: 2}])`), + `"{\"a\":1}\n{\"b\":2}\n"`, + }, { + test("encoding/yaml", `yaml.MarshalStream([{a: 1}, {b: 2}])`), + `"a: 1\n---\nb: 2\n"`, + }, { + test("strings", `strings.ToCamel("AlphaBeta")`), + `"alphaBeta"`, + }, { + test("strings", `strings.ToTitle("alpha")`), + `"Alpha"`, + }, { + test("math/bits", `bits.And(0x10000000000000F0E, 0xF0F7)`), `6`, + }, { + test("math/bits", `bits.Or(0x100000000000000F0, 0x0F)`), + hexToDec("100000000000000FF"), + }, { + test("math/bits", `bits.Xor(0x10000000000000F0F, 0xFF0)`), + hexToDec("100000000000000FF"), + }, { + test("math/bits", `bits.Xor(0xFF0, 0x10000000000000F0F)`), + hexToDec("100000000000000FF"), + }, { + test("math/bits", `bits.Clear(0xF, 0x100000000000008)`), `7`, + }, { + test("math/bits", `bits.Clear(0x1000000000000000008, 0xF)`), + hexToDec("1000000000000000000"), + }, { + test("text/tabwriter", `tabwriter.Write(""" + a\tb\tc + aaa\tbb\tvv + """)`), + `"a b c\naaa bb vv"`, + }, { + test("text/tabwriter", `tabwriter.Write([ + "a\tb\tc", + "aaa\tbb\tvv"])`), + `"a b c\naaa bb vv\n"`, + }, { + test("text/template", `template.Execute("{{.}}-{{.}}", "foo")`), + `"foo-foo"`, }} for _, tc := range testCases { t.Run("", func(t *testing.T) { diff --git a/cue/go.go b/cue/go.go index a57777263..9e97e5245 100644 --- a/cue/go.go +++ b/cue/go.go @@ -24,6 +24,7 @@ import ( "strings" "sync" + "cuelang.org/go/cue/ast" "cuelang.org/go/cue/parser" "cuelang.org/go/cue/token" "cuelang.org/go/internal" @@ -194,6 +195,10 @@ func convert(ctx *context, src source, x interface{}) evaluated { // null by default, but may still be set: *null | _. return makeNullable(&top{src.base()}).(evaluated) + case ast.Expr: + x := newVisitorCtx(ctx, nil, nil, nil) + return ctx.manifest(x.walk(v)) + case *big.Int: n := newNum(src, intKind) n.v.Coeff.Set(v)