Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: add tests for manually created builtins
Browse files Browse the repository at this point in the history
fixes bug with JSON encoding

Change-Id: I3f4ed6daf0985cb90a2f5738abc74be6fdf29a57
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2326
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
Marcel van Lohuizen authored and mpvl committed Jun 22, 2019
1 parent 6e02e45 commit 78e755c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cue/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cue

import (
"fmt"
"math/big"
"strings"
"testing"
)
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 78e755c

Please sign in to comment.