Skip to content

Commit 248794e

Browse files
committed
pkg: generate per-directory tests from builtin_test.go
- 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 <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent abce145 commit 248794e

File tree

43 files changed

+1678
-556
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1678
-556
lines changed

cue/builtin_test.go

Lines changed: 0 additions & 556 deletions
Large diffs are not rendered by default.

genpkgtests.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"go/ast"
8+
"go/parser"
9+
"go/token"
10+
"io/ioutil"
11+
"os"
12+
13+
"strconv"
14+
15+
"github.com/rogpeppe/go-internal/txtar"
16+
)
17+
18+
func main() {
19+
fset := token.NewFileSet()
20+
f, err := parser.ParseFile(fset, "./cue/builtin_test.go", nil, 0)
21+
if err != nil {
22+
fmt.Println(err)
23+
return
24+
}
25+
26+
m := map[string]*txtar.Archive{}
27+
count := map[string]int{}
28+
29+
ast.Inspect(f, func(n ast.Node) bool {
30+
call, ok := n.(*ast.CallExpr)
31+
if !ok {
32+
return true
33+
}
34+
ident, ok := call.Fun.(*ast.Ident)
35+
if !ok || ident.Name != "test" {
36+
return true
37+
}
38+
39+
str := call.Args[0].(*ast.BasicLit)
40+
pkg, _ := strconv.Unquote(str.Value)
41+
a := &txtar.Archive{
42+
Comment: []byte(
43+
"# generated from the original tests.\n# Henceforth it may be nicer to group tests into separate files."),
44+
Files: []txtar.File{{Name: "in.cue"}},
45+
}
46+
m[pkg] = a
47+
return false
48+
})
49+
50+
ast.Inspect(f, func(n ast.Node) bool {
51+
call, ok := n.(*ast.CallExpr)
52+
if !ok {
53+
return true
54+
}
55+
ident, ok := call.Fun.(*ast.Ident)
56+
if !ok || ident.Name != "test" {
57+
return true
58+
}
59+
60+
str := call.Args[0].(*ast.BasicLit)
61+
pkg, _ := strconv.Unquote(str.Value)
62+
str = call.Args[1].(*ast.BasicLit)
63+
expr, err := strconv.Unquote(str.Value)
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
a := m[pkg]
69+
count[pkg]++
70+
71+
a.Files[0].Data = append(a.Files[0].Data,
72+
fmt.Sprintf("t%d: %s\n", count[pkg], expr)...)
73+
74+
return false
75+
})
76+
77+
for key, a := range m {
78+
os.Mkdir(fmt.Sprintf("pkg/%s/testdata", key), 0755)
79+
p := fmt.Sprintf("pkg/%s/testdata/gen.txtar", key)
80+
ioutil.WriteFile(p, txtar.Format(a), 0644)
81+
}
82+
}

pkg/crypto/md5/md5_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package md5_test
16+
17+
import (
18+
"testing"
19+
20+
"cuelang.org/go/pkg/internal/builtintest"
21+
)
22+
23+
func TestBuiltin(t *testing.T) {
24+
builtintest.Run("md5", t)
25+
}

pkg/crypto/md5/testdata/gen.txtar

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated from the original tests.
2+
# Henceforth it may be nicer to group tests into separate files.
3+
-- in.cue --
4+
import "crypto/md5"
5+
6+
t1: len(md5.Sum("hash me"))
7+
-- out/md5 --
8+
(struct){
9+
t1: (int){ 16 }
10+
}

pkg/crypto/sha1/sha1_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sha1_test
16+
17+
import (
18+
"testing"
19+
20+
"cuelang.org/go/pkg/internal/builtintest"
21+
)
22+
23+
func TestBuiltin(t *testing.T) {
24+
builtintest.Run("sha1", t)
25+
}

pkg/crypto/sha1/testdata/gen.txtar

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated from the original tests.
2+
# Henceforth it may be nicer to group tests into separate files.
3+
-- in.cue --
4+
import "crypto/sha1"
5+
6+
t1: len(sha1.Sum("hash me"))
7+
-- out/sha1 --
8+
(struct){
9+
t1: (int){ 20 }
10+
}

pkg/crypto/sha256/sha256_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sha256_test
16+
17+
import (
18+
"testing"
19+
20+
"cuelang.org/go/pkg/internal/builtintest"
21+
)
22+
23+
func TestBuiltin(t *testing.T) {
24+
builtintest.Run("sha256", t)
25+
}

pkg/crypto/sha256/testdata/gen.txtar

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# generated from the original tests.
2+
# Henceforth it may be nicer to group tests into separate files.
3+
-- in.cue --
4+
import "crypto/sha256"
5+
6+
t1: sha256.Sum256("hash me")
7+
t2: len(sha256.Sum256("hash me"))
8+
t3: len(sha256.Sum224("hash me"))
9+
-- out/sha256 --
10+
(struct){
11+
t1: (bytes){ '\xeb \x1a\xf5\xaa\xf0\xd6\x06)\xd3Ҧ\x1eFl\xfc\x0f\xed\xb5\x17\xad\xd81\xec\xacR5\xe1کc\xd6' }
12+
t2: (int){ 32 }
13+
t3: (int){ 28 }
14+
}

pkg/crypto/sha512/sha512_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sha512_test
16+
17+
import (
18+
"testing"
19+
20+
"cuelang.org/go/pkg/internal/builtintest"
21+
)
22+
23+
func TestBuiltin(t *testing.T) {
24+
builtintest.Run("sha512", t)
25+
}

pkg/crypto/sha512/testdata/gen.txtar

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# generated from the original tests.
2+
# Henceforth it may be nicer to group tests into separate files.
3+
-- in.cue --
4+
import "crypto/sha512"
5+
6+
t1: len(sha512.Sum512("hash me"))
7+
t2: len(sha512.Sum384("hash me"))
8+
t3: len(sha512.Sum512_224("hash me"))
9+
t4: len(sha512.Sum512_256("hash me"))
10+
-- out/sha512 --
11+
(struct){
12+
t1: (int){ 64 }
13+
t2: (int){ 48 }
14+
t3: (int){ 28 }
15+
t4: (int){ 32 }
16+
}

0 commit comments

Comments
 (0)