Debian Codegraph @

v2/generator/snippet_writer_test.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package generator_test
18
19 import (
20 "bytes"
21 "strings"
22 "testing"
23
24 "k8s.io/gengo/v2/generator"
25 "k8s.io/gengo/v2/namer"
26 "k8s.io/gengo/v2/parser"
27 )
28
29 func construct(t *testing.T, patterns ...string) *generator.Context {
30 b := parser.New()
31
32 if err := b.LoadPackages(patterns...); err != nil {
33 t.Fatalf("unexpected error: %v", err)
34 return nil
35 }
36 c, err := generator.NewContext(b, namer.NameSystems{
37 "public": namer.NewPublicNamer(0),
38 "private": namer.NewPrivateNamer(0),
39 }, "public")
40 if err != nil {
41 t.Fatal(err)
42 }
43 return c
44 }
45
46 func TestSnippetWriter(t *testing.T) {
47 c := construct(t, "./testdata/snippet_writer")
48 b := &bytes.Buffer{}
49 err := generator.NewSnippetWriter(b, c, "$", "$").
50 Do("$.|public$$.|private$", c.Order[0]).
51 Error()
52 if err != nil {
53 t.Errorf("Unexpected error %v", err)
54 }
55 if e, a := "Blahblah", b.String(); e != a {
56 t.Errorf("Expected %q, got %q", e, a)
57 }
58
59 err = generator.NewSnippetWriter(b, c, "$", "$").
60 Do("$.|public", c.Order[0]).
61 Error()
62 if err == nil {
63 t.Errorf("expected error on invalid template")
64 } else {
65 // Dear reader, I apologize for making the worst change
66 // detection test in the history of ever.
67 if e, a := "snippet_writer_test.go", err.Error(); !strings.Contains(a, e) {
68 t.Errorf("Expected %q but didn't find it in %q", e, a)
69 }
70 }
71 }
72
73 func TestArgsWith(t *testing.T) {
74 orig := generator.Args{
75 "a": "aaa",
76 "b": "bbb",
77 }
78
79 withC := orig.With("c", "ccc")
80 if len(orig) != 2 {
81 t.Errorf("unexpected change to 'orig': %v", orig)
82 }
83 if len(withC) != 3 {
84 t.Errorf("expected 'withC' to have 3 values: %v", withC)
85 }
86
87 withDE := withC.WithArgs(generator.Args{
88 "d": "ddd",
89 "e": "eee",
90 })
91 if len(orig) != 2 {
92 t.Errorf("unexpected change to 'orig': %v", orig)
93 }
94 if len(withC) != 3 {
95 t.Errorf("unexpected change to 'withC': %v", orig)
96 }
97 if len(withDE) != 5 {
98 t.Errorf("expected 'withDE' to have 5 values: %v", withC)
99 }
100
101 withNewA := orig.With("a", "AAA")
102 if orig["a"] != "aaa" {
103 t.Errorf("unexpected change to 'orig': %v", orig)
104 }
105 if withNewA["a"] != "AAA" {
106 t.Errorf("expected 'withNewA[\"a\"]' to be \"AAA\": %v", withNewA)
107 }
108
109 withNewAB := orig.WithArgs(generator.Args{
110 "a": "AAA",
111 "b": "BBB",
112 })
113 if orig["a"] != "aaa" || orig["b"] != "bbb" {
114 t.Errorf("unexpected change to 'orig': %v", orig)
115 }
116 if withNewAB["a"] != "AAA" {
117 t.Errorf("expected 'withNewAB[\"a\"]' to be \"AAA\": %v", withNewAB)
118 }
119 if withNewAB["b"] != "BBB" {
120 t.Errorf("expected 'withNewAB[\"b\"]' to be \"BBB\": %v", withNewAB)
121 }
122 }