github.com/crossplane/upjet@v1.3.0/pkg/types/comments/comment_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package comments 6 7 import ( 8 "reflect" 9 "testing" 10 11 "github.com/crossplane/crossplane-runtime/pkg/test" 12 "github.com/google/go-cmp/cmp" 13 "github.com/pkg/errors" 14 15 "github.com/crossplane/upjet/pkg/config" 16 "github.com/crossplane/upjet/pkg/types/markers" 17 ) 18 19 func TestComment_Build(t *testing.T) { 20 tftag := "-" 21 type args struct { 22 text string 23 opts []Option 24 } 25 type want struct { 26 out string 27 mopts markers.Options 28 err error 29 } 30 31 cases := map[string]struct { 32 args 33 want 34 }{ 35 "OnlyTextNoMarker": { 36 args: args{ 37 text: "hello world!", 38 }, 39 want: want{ 40 out: "// hello world!\n", 41 mopts: markers.Options{}, 42 }, 43 }, 44 "MultilineTextNoMarker": { 45 args: args{ 46 text: `hello world! 47 this is a test 48 yes, this is a test`, 49 }, 50 want: want{ 51 out: `// hello world! 52 // this is a test 53 // yes, this is a test 54 `, 55 mopts: markers.Options{}, 56 }, 57 }, 58 "TextWithUpjetMarker": { 59 args: args{ 60 text: `hello world! 61 +upjet:crd:field:TFTag=- 62 `, 63 }, 64 want: want{ 65 out: `// hello world! 66 // +upjet:crd:field:TFTag=- 67 `, 68 mopts: markers.Options{ 69 UpjetOptions: markers.UpjetOptions{ 70 FieldTFTag: &tftag, 71 }, 72 }, 73 }, 74 }, 75 "TextWithOtherMarker": { 76 args: args{ 77 text: `hello world! 78 +kubebuilder:validation:Required 79 `, 80 }, 81 want: want{ 82 out: `// hello world! 83 // +kubebuilder:validation:Required 84 `, 85 mopts: markers.Options{}, 86 }, 87 }, 88 "CommentWithUpjetOptions": { 89 args: args{ 90 text: `hello world!`, 91 opts: []Option{ 92 WithTFTag("-"), 93 }, 94 }, 95 want: want{ 96 out: `// hello world! 97 // +upjet:crd:field:TFTag=- 98 `, 99 mopts: markers.Options{ 100 UpjetOptions: markers.UpjetOptions{ 101 FieldTFTag: &tftag, 102 }, 103 }, 104 }, 105 }, 106 "CommentWithMixedOptions": { 107 args: args{ 108 text: `hello world!`, 109 opts: []Option{ 110 WithTFTag("-"), 111 WithReferenceConfig(config.Reference{ 112 Type: reflect.TypeOf(Comment{}).String(), 113 }), 114 }, 115 }, 116 want: want{ 117 out: `// hello world! 118 // +upjet:crd:field:TFTag=- 119 // +crossplane:generate:reference:type=comments.Comment 120 `, 121 mopts: markers.Options{ 122 UpjetOptions: markers.UpjetOptions{ 123 FieldTFTag: &tftag, 124 }, 125 CrossplaneOptions: markers.CrossplaneOptions{ 126 Reference: config.Reference{ 127 Type: "comments.Comment", 128 }, 129 }, 130 }, 131 }, 132 }, 133 "CommentWithUnsupportedUpjetMarker": { 134 args: args{ 135 text: `hello world! 136 +upjet:crd:field:TFTag=- 137 +upjet:unsupported:key=value 138 `, 139 }, 140 want: want{ 141 err: errors.New("cannot parse as a upjet prefix: +upjet:unsupported:key=value"), 142 }, 143 }, 144 } 145 for name, tc := range cases { 146 t.Run(name, func(t *testing.T) { 147 c, gotErr := New(tc.text, tc.opts...) 148 if diff := cmp.Diff(tc.want.err, gotErr, test.EquateErrors()); diff != "" { 149 t.Fatalf("comment.New(...): -want error, +got error: %s", diff) 150 } 151 if gotErr != nil { 152 return 153 } 154 if diff := cmp.Diff(tc.want.mopts, c.Options); diff != "" { 155 t.Errorf("comment.New(...) opts = %v, want %v", c.Options, tc.want.mopts) 156 } 157 got := c.Build() 158 if diff := cmp.Diff(tc.want.out, got); diff != "" { 159 t.Errorf("Build() out = %v, want %v", got, tc.want.out) 160 } 161 }) 162 } 163 }