github.com/banzaicloud/operator-tools@v0.28.10/pkg/docgen/docgen_test.go (about) 1 // Copyright © 2020 Banzai Cloud 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 docgen_test 16 17 import ( 18 "io/ioutil" 19 "path/filepath" 20 "runtime" 21 "testing" 22 23 "github.com/MakeNowJust/heredoc" 24 "github.com/andreyvit/diff" 25 "github.com/banzaicloud/operator-tools/pkg/docgen" 26 "github.com/banzaicloud/operator-tools/pkg/utils" 27 "github.com/go-logr/logr" 28 ) 29 30 var logger logr.Logger 31 32 func init() { 33 logger = utils.Log 34 } 35 36 func TestGenParse(t *testing.T) { 37 _, filename, _, _ := runtime.Caller(0) 38 currentDir := filepath.Dir(filename) 39 40 var testData = []struct { 41 docItem docgen.DocItem 42 expected string 43 }{ 44 { 45 docItem: docgen.DocItem{ 46 Name: "sample", 47 SourcePath: filepath.Join(currentDir, "testdata", "sample.go"), 48 DestPath: filepath.Join(currentDir, "../../build/_test/docgen"), 49 }, 50 expected: heredoc.Doc(` 51 ## Sample 52 53 ### field1 (string, optional) {#sample-field1} 54 55 Default: - 56 `), 57 }, 58 { 59 docItem: docgen.DocItem{ 60 Name: "sample-default", 61 SourcePath: filepath.Join(currentDir, "testdata", "sample_default.go"), 62 DestPath: filepath.Join(currentDir, "../../build/_test/docgen"), 63 DefaultValueFromTagExtractor: func(tag string) string { 64 return docgen.GetPrefixedValue(tag, `asd:\"default:(.*)\"`) 65 }, 66 }, 67 expected: heredoc.Doc(` 68 ## SampleDefault 69 70 ### field1 (string, optional) {#sampledefault-field1} 71 72 Default: testval 73 `), 74 }, 75 { 76 docItem: docgen.DocItem{ 77 Name: "sample-codeblock", 78 SourcePath: filepath.Join(currentDir, "testdata", "sample_codeblock.go"), 79 DestPath: filepath.Join(currentDir, "../../build/_test/docgen"), 80 DefaultValueFromTagExtractor: func(tag string) string { 81 return docgen.GetPrefixedValue(tag, `asd:\"default:(.*)\"`) 82 }, 83 }, 84 expected: heredoc.Doc(` 85 ## Sample 86 87 ### field1 (string, optional) {#sample-field1} 88 89 Description 90 {{< highlight yaml >}} 91 test: code block 92 some: more lines 93 indented: line 94 {{< /highlight >}} 95 96 97 Default: - 98 `), 99 }, 100 } 101 102 for _, item := range testData { 103 parser := docgen.GetDocumentParser(item.docItem, logger) 104 err := parser.Generate() 105 if err != nil { 106 t.Fatalf("%+v", err) 107 } 108 109 bytes, err := ioutil.ReadFile(filepath.Join(item.docItem.DestPath, item.docItem.Name+".md")) 110 if err != nil { 111 t.Fatalf("%+v", err) 112 } 113 114 if a, e := diff.TrimLinesInString(string(bytes)), diff.TrimLinesInString(item.expected); a != e { 115 t.Errorf("Result does not match (-actual vs +expected):\n%v\nActual: %s", diff.LineDiff(a, e), string(bytes)) 116 } 117 } 118 }