1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/*
Copyright 2015 The Kubernetes 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 generator_test
import (
"bytes"
"strings"
"testing"
"k8s.io/gengo/v2/generator"
"k8s.io/gengo/v2/namer"
"k8s.io/gengo/v2/parser"
)
func construct(t *testing.T, patterns ...string) *generator.Context {
b := parser.New()
if err := b.LoadPackages(patterns...); err != nil {
t.Fatalf("unexpected error: %v", err)
return nil
}
c, err := generator.NewContext(b, namer.NameSystems{
"public": namer.NewPublicNamer(0),
"private": namer.NewPrivateNamer(0),
}, "public")
if err != nil {
t.Fatal(err)
}
return c
}
func TestSnippetWriter(t *testing.T) {
c := construct(t, "./testdata/snippet_writer")
b := &bytes.Buffer{}
err := generator.NewSnippetWriter(b, c, "$", "$").
Do("$.|public$$.|private$", c.Order[0]).
Error()
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if e, a := "Blahblah", b.String(); e != a {
t.Errorf("Expected %q, got %q", e, a)
}
err = generator.NewSnippetWriter(b, c, "$", "$").
Do("$.|public", c.Order[0]).
Error()
if err == nil {
t.Errorf("expected error on invalid template")
} else {
// Dear reader, I apologize for making the worst change
// detection test in the history of ever.
if e, a := "snippet_writer_test.go", err.Error(); !strings.Contains(a, e) {
t.Errorf("Expected %q but didn't find it in %q", e, a)
}
}
}
func TestArgsWith(t *testing.T) {
orig := generator.Args{
"a": "aaa",
"b": "bbb",
}
withC := orig.With("c", "ccc")
if len(orig) != 2 {
t.Errorf("unexpected change to 'orig': %v", orig)
}
if len(withC) != 3 {
t.Errorf("expected 'withC' to have 3 values: %v", withC)
}
withDE := withC.WithArgs(generator.Args{
"d": "ddd",
"e": "eee",
})
if len(orig) != 2 {
t.Errorf("unexpected change to 'orig': %v", orig)
}
if len(withC) != 3 {
t.Errorf("unexpected change to 'withC': %v", orig)
}
if len(withDE) != 5 {
t.Errorf("expected 'withDE' to have 5 values: %v", withC)
}
withNewA := orig.With("a", "AAA")
if orig["a"] != "aaa" {
t.Errorf("unexpected change to 'orig': %v", orig)
}
if withNewA["a"] != "AAA" {
t.Errorf("expected 'withNewA[\"a\"]' to be \"AAA\": %v", withNewA)
}
withNewAB := orig.WithArgs(generator.Args{
"a": "AAA",
"b": "BBB",
})
if orig["a"] != "aaa" || orig["b"] != "bbb" {
t.Errorf("unexpected change to 'orig': %v", orig)
}
if withNewAB["a"] != "AAA" {
t.Errorf("expected 'withNewAB[\"a\"]' to be \"AAA\": %v", withNewAB)
}
if withNewAB["b"] != "BBB" {
t.Errorf("expected 'withNewAB[\"b\"]' to be \"BBB\": %v", withNewAB)
}
}
|