Skip to content

Commit

Permalink
pkg: generate per-directory tests from builtin_test.go
Browse files Browse the repository at this point in the history
- from original tests in cue
- keep some of the original tests around to test builtin logic

Change-Id: I84897b45016034d871b731ba3e76ae9a0cd8a8d0
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6883
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Aug 20, 2020
1 parent abce145 commit 248794e
Show file tree
Hide file tree
Showing 43 changed files with 1,678 additions and 556 deletions.
556 changes: 0 additions & 556 deletions cue/builtin_test.go

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions genpkgtests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// +build ignore

package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"

"strconv"

"github.com/rogpeppe/go-internal/txtar"
)

func main() {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "./cue/builtin_test.go", nil, 0)
if err != nil {
fmt.Println(err)
return
}

m := map[string]*txtar.Archive{}
count := map[string]int{}

ast.Inspect(f, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
ident, ok := call.Fun.(*ast.Ident)
if !ok || ident.Name != "test" {
return true
}

str := call.Args[0].(*ast.BasicLit)
pkg, _ := strconv.Unquote(str.Value)
a := &txtar.Archive{
Comment: []byte(
"# generated from the original tests.\n# Henceforth it may be nicer to group tests into separate files."),
Files: []txtar.File{{Name: "in.cue"}},
}
m[pkg] = a
return false
})

ast.Inspect(f, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
ident, ok := call.Fun.(*ast.Ident)
if !ok || ident.Name != "test" {
return true
}

str := call.Args[0].(*ast.BasicLit)
pkg, _ := strconv.Unquote(str.Value)
str = call.Args[1].(*ast.BasicLit)
expr, err := strconv.Unquote(str.Value)
if err != nil {
panic(err)
}

a := m[pkg]
count[pkg]++

a.Files[0].Data = append(a.Files[0].Data,
fmt.Sprintf("t%d: %s\n", count[pkg], expr)...)

return false
})

for key, a := range m {
os.Mkdir(fmt.Sprintf("pkg/%s/testdata", key), 0755)
p := fmt.Sprintf("pkg/%s/testdata/gen.txtar", key)
ioutil.WriteFile(p, txtar.Format(a), 0644)
}
}
25 changes: 25 additions & 0 deletions pkg/crypto/md5/md5_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package md5_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("md5", t)
}
10 changes: 10 additions & 0 deletions pkg/crypto/md5/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "crypto/md5"

t1: len(md5.Sum("hash me"))
-- out/md5 --
(struct){
t1: (int){ 16 }
}
25 changes: 25 additions & 0 deletions pkg/crypto/sha1/sha1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sha1_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("sha1", t)
}
10 changes: 10 additions & 0 deletions pkg/crypto/sha1/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "crypto/sha1"

t1: len(sha1.Sum("hash me"))
-- out/sha1 --
(struct){
t1: (int){ 20 }
}
25 changes: 25 additions & 0 deletions pkg/crypto/sha256/sha256_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sha256_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("sha256", t)
}
14 changes: 14 additions & 0 deletions pkg/crypto/sha256/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "crypto/sha256"

t1: sha256.Sum256("hash me")
t2: len(sha256.Sum256("hash me"))
t3: len(sha256.Sum224("hash me"))
-- out/sha256 --
(struct){
t1: (bytes){ '\xeb \x1a\xf5\xaa\xf0\xd6\x06)\xd3Ҧ\x1eFl\xfc\x0f\xed\xb5\x17\xad\xd81\xec\xacR5\xe1کc\xd6' }
t2: (int){ 32 }
t3: (int){ 28 }
}
25 changes: 25 additions & 0 deletions pkg/crypto/sha512/sha512_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sha512_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("sha512", t)
}
16 changes: 16 additions & 0 deletions pkg/crypto/sha512/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "crypto/sha512"

t1: len(sha512.Sum512("hash me"))
t2: len(sha512.Sum384("hash me"))
t3: len(sha512.Sum512_224("hash me"))
t4: len(sha512.Sum512_256("hash me"))
-- out/sha512 --
(struct){
t1: (int){ 64 }
t2: (int){ 48 }
t3: (int){ 28 }
t4: (int){ 32 }
}
25 changes: 25 additions & 0 deletions pkg/encoding/base64/base64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package base64_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("base64", t)
}
26 changes: 26 additions & 0 deletions pkg/encoding/base64/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "encoding/base64"

t1: base64.Encode(null, "foo")
t2: base64.Decode(null, base64.Encode(null, "foo"))
t3: base64.Decode(null, "foo")
t4: base64.Decode({}, "foo")
-- out/base64 --
Errors:
error in call to encoding/base64.Decode: base64: unsupported encoding: cannot use value {} (type struct) as null
error in call to encoding/base64.Decode: illegal base64 data at input byte 0

Result:
(_|_){
// [eval]
t1: (string){ "Zm9v" }
t2: (bytes){ 'foo' }
t3: (_|_){
// [eval] error in call to encoding/base64.Decode: illegal base64 data at input byte 0
}
t4: (_|_){
// [eval] error in call to encoding/base64.Decode: base64: unsupported encoding: cannot use value {} (type struct) as null
}
}
25 changes: 25 additions & 0 deletions pkg/encoding/csv/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package csv_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("csv", t)
}
25 changes: 25 additions & 0 deletions pkg/encoding/csv/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "encoding/csv"

t1: csv.Decode("1,2,3\n4,5,6")
t2: csv.Encode([[1,2,3],[4,5],[7,8,9]])
t3: csv.Encode([["a", "b"], ["c"]])
-- out/csv --
(struct){
t1: (#list){
0: (#list){
0: (string){ "1" }
1: (string){ "2" }
2: (string){ "3" }
}
1: (#list){
0: (string){ "4" }
1: (string){ "5" }
2: (string){ "6" }
}
}
t2: (string){ "1,2,3\n4,5\n7,8,9\n" }
t3: (string){ "a,b\nc\n" }
}
25 changes: 25 additions & 0 deletions pkg/encoding/hex/hex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hex_test

import (
"testing"

"cuelang.org/go/pkg/internal/builtintest"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("hex", t)
}
23 changes: 23 additions & 0 deletions pkg/encoding/hex/testdata/gen.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "encoding/hex"

t1: hex.Encode("foo")
t2: hex.Decode(hex.Encode("foo"))
t3: hex.Decode("foo")
t4: hex.Dump('foo')
-- out/hex --
Errors:
error in call to encoding/hex.Decode: encoding/hex: invalid byte: U+006F 'o'

Result:
(_|_){
// [eval]
t1: (string){ "666f6f" }
t2: (bytes){ 'foo' }
t3: (_|_){
// [eval] error in call to encoding/hex.Decode: encoding/hex: invalid byte: U+006F 'o'
}
t4: (string){ "00000000 66 6f 6f |foo|\n" }
}

0 comments on commit 248794e

Please sign in to comment.