github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/get_doc_test.go (about) 1 /* 2 Golang To Github Markdown Utility: gotomd 3 Copyright (C) 2023, 2024 Leslie Dancsecs 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 // Program runs a specific go test transforming the output 20 // to github compatible markdown. This is used within this 21 // project to help automate keeping the README.md up to date 22 // when an example changes. 23 24 package main 25 26 import ( 27 "os" 28 "testing" 29 30 "github.com/dancsecs/sztest" 31 ) 32 33 func Test_GetDoc_MarkGoCode(t *testing.T) { 34 chk := sztest.CaptureNothing(t) 35 defer chk.Release() 36 37 chk.Str( 38 markGoCode("ABC"), 39 "```go\nABC\n```", 40 ) 41 42 chk.Str( 43 markGoCode("ABC\n"), 44 "```go\nABC\n```", 45 ) 46 } 47 48 func Test_GetDoc_MarkBashCode(t *testing.T) { 49 chk := sztest.CaptureNothing(t) 50 defer chk.Release() 51 52 chk.Str( 53 markBashCode("ABC"), 54 "```bash\nABC\n```", 55 ) 56 57 chk.Str( 58 markBashCode("ABC\n"), 59 "```bash\nABC\n```", 60 ) 61 } 62 63 func Test_GetDoc_GetGoDcl_NoItems(t *testing.T) { 64 chk := sztest.CaptureNothing(t) 65 defer chk.Release() 66 67 s, err := getDocDecl(example1Path) 68 chk.Err(err, ErrMissingAction.Error()) 69 chk.Str(s, "") 70 } 71 72 func Test_GetDoc_GetGoDcl_Package(t *testing.T) { 73 chk := sztest.CaptureNothing(t) 74 defer chk.Release() 75 76 s, err := getDocDecl(example1Path + pkgLabel) 77 chk.NoErr(err) 78 chk.Str( 79 s, 80 markGoCode(pkgLabel+" "+example1+"\n"), 81 ) 82 } 83 84 func Test_GetDoc_GetGoDcl_InvalidItem(t *testing.T) { 85 chk := sztest.CaptureNothing(t) 86 defer chk.Release() 87 88 s, err := getDocDecl(example1Path + "unknownItem") 89 chk.Err(err, ErrUnknownObject.Error()+": unknownItem") 90 chk.Str(s, "") 91 } 92 93 func Test_GetDoc_GetGoDcl_OneItem(t *testing.T) { 94 chk := sztest.CaptureNothing(t) 95 defer chk.Release() 96 97 chk.AddSub( 98 pkgLabel+` .*$`, 99 pkgLabel+" ."+string(os.PathSeparator)+example1, 100 ) 101 102 s, err := getDocDecl(example1Path + "TimesTwo") 103 chk.NoErr(err) 104 chk.Str( 105 s, 106 markGoCode("func TimesTwo(i int) int\n"), 107 ) 108 } 109 110 func Test_GetDoc_GetGoDcl_TwoItems(t *testing.T) { 111 chk := sztest.CaptureNothing(t) 112 defer chk.Release() 113 114 chk.AddSub( 115 pkgLabel+` .*$`, 116 pkgLabel+" ."+string(os.PathSeparator)+example1, 117 ) 118 119 s, err := getDocDecl(example1Path + "TimesTwo TimesThree") 120 chk.NoErr(err) 121 chk.Str( 122 s, 123 markGoCode("func TimesTwo(i int) int\nfunc TimesThree(i int) int\n"), 124 ) 125 } 126 127 func Test_GetDoc_GetGoDclSingle_NoItems(t *testing.T) { 128 chk := sztest.CaptureNothing(t) 129 defer chk.Release() 130 131 s, err := getDocDeclSingle(example1Path) 132 chk.Err(err, ErrMissingAction.Error()) 133 chk.Str(s, "") 134 } 135 136 func Test_GetDoc_GetGoDclSingle_PackageNoItems(t *testing.T) { 137 chk := sztest.CaptureNothing(t) 138 defer chk.Release() 139 140 s, err := getDocDeclSingle(example1Path + pkgLabel) 141 chk.NoErr(err) 142 chk.Str( 143 s, 144 markGoCode(pkgLabel+" "+example1+"\n"), 145 ) 146 } 147 148 func Test_GetDoc_GetGoDclSingle_InvalidItem(t *testing.T) { 149 chk := sztest.CaptureNothing(t) 150 defer chk.Release() 151 152 s, err := getDocDeclSingle(example1Path + "unknownItem") 153 chk.Err(err, ErrUnknownObject.Error()+": unknownItem") 154 chk.Str(s, "") 155 } 156 157 func Test_GetDoc_GetGoDclSingle_OneItem(t *testing.T) { 158 chk := sztest.CaptureNothing(t) 159 defer chk.Release() 160 161 line, err := getDocDeclSingle(example1Path + "TimesTwo") 162 163 chk.AddSub( 164 pkgLabel+` .*$`, 165 pkgLabel+" ."+string(os.PathSeparator)+example1, 166 ) 167 chk.NoErr(err) 168 chk.Str( 169 line, 170 markGoCode("func TimesTwo(i int) int\n"), 171 ) 172 } 173 174 func Test_GetDoc_GetGoDclSingle_TwoItems(t *testing.T) { 175 chk := sztest.CaptureNothing(t) 176 defer chk.Release() 177 178 chk.AddSub( 179 pkgLabel+` .*$`, 180 pkgLabel+" ."+string(os.PathSeparator)+example1, 181 ) 182 183 s, err := getDocDeclSingle(example1Path + "TimesTwo TimesThree") 184 chk.NoErr(err) 185 chk.Str( 186 s, 187 markGoCode("func TimesTwo(i int) int\nfunc TimesThree(i int) int\n"), 188 ) 189 } 190 191 func Test_GetDoc_GetGoDclNatural_InvalidItem(t *testing.T) { 192 chk := sztest.CaptureNothing(t) 193 defer chk.Release() 194 195 s, err := getDocDeclNatural(example1Path + "unknownItem") 196 chk.Err(err, ErrUnknownObject.Error()+": unknownItem") 197 chk.Str(s, "") 198 } 199 200 func Test_GetDoc_GetGoDclNatural_OneItem(t *testing.T) { 201 chk := sztest.CaptureNothing(t) 202 defer chk.Release() 203 204 line, err := getDocDeclNatural(example1Path + "TimesTwo") 205 206 chk.AddSub( 207 pkgLabel+` .*$`, 208 pkgLabel+" ."+string(os.PathSeparator)+example1, 209 ) 210 chk.NoErr(err) 211 chk.Str( 212 line, 213 markGoCode( 214 "// TimesTwo returns the value times two.\n"+ 215 "func TimesTwo(i int) int", 216 ), 217 ) 218 } 219 220 func Test_GetDoc_GetGoDclNatural_TwoItems(t *testing.T) { 221 chk := sztest.CaptureNothing(t) 222 defer chk.Release() 223 224 line, err := getDocDeclNatural(example1Path + "TimesTwo TimesThree") 225 226 chk.AddSub( 227 pkgLabel+` .*$`, 228 pkgLabel+" ."+string(os.PathSeparator)+example1, 229 ) 230 chk.NoErr(err) 231 chk.Str( 232 line, 233 markGoCode( 234 "// TimesTwo returns the value times two.\n"+ 235 "func TimesTwo(i int) int\n"+ 236 "\n"+ 237 "// TimesThree returns the value times three.\n"+ 238 "func TimesThree(i int) int", 239 ), 240 ) 241 } 242 243 func Test_GetDoc_GetDoc_TwoItems(t *testing.T) { 244 chk := sztest.CaptureNothing(t) 245 defer chk.Release() 246 247 chk.AddSub( 248 pkgLabel+` .*$`, 249 pkgLabel+" ."+string(os.PathSeparator)+example1, 250 ) 251 252 s, err := getDoc(example1Path + "TimesTwo TimesThree") 253 chk.NoErr(err) 254 chk.Str( 255 s, 256 ""+ 257 markGoCode("func TimesTwo(i int) int")+"\n\n"+ 258 "TimesTwo returns the value times two.\n"+ 259 "\n"+ 260 markGoCode("func TimesThree(i int) int")+"\n\n"+ 261 "TimesThree returns the value times three.", 262 ) 263 }