testdata_legacy.go (5854B)
1 package testdata 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path" 8 9 testdataloader "github.com/peteole/testdata-loader" 10 11 "git.defalsify.org/vise.git/vm" 12 ) 13 14 type genFunc func() error 15 16 var ( 17 BaseDir = testdataloader.GetBasePath() 18 DataDir = "" 19 dirLock = false 20 ) 21 22 func outLegacy(sym string, b []byte, tpl string, data map[string]string) error { 23 fp := path.Join(DataDir, sym) 24 err := ioutil.WriteFile(fp, []byte(tpl), 0644) 25 if err != nil { 26 return err 27 } 28 29 fb := sym + ".bin" 30 fp = path.Join(DataDir, fb) 31 err = ioutil.WriteFile(fp, b, 0644) 32 if err != nil { 33 return err 34 } 35 36 if data == nil { 37 return nil 38 } 39 40 for k, v := range data { 41 fb := k + ".txt" 42 fp = path.Join(DataDir, fb) 43 err = ioutil.WriteFile(fp, []byte(v), 0644) 44 if err != nil { 45 return err 46 } 47 } 48 49 return nil 50 } 51 52 func root() error { 53 b := []byte{} 54 b = vm.NewLine(b, vm.MOUT, []string{"do the foo", "1"}, nil, nil) 55 b = vm.NewLine(b, vm.MOUT, []string{"go to the bar", "2"}, nil, nil) 56 b = vm.NewLine(b, vm.MOUT, []string{"language template", "3"}, nil, nil) 57 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 58 b = vm.NewLine(b, vm.INCMP, []string{"foo", "1"}, nil, nil) 59 b = vm.NewLine(b, vm.INCMP, []string{"bar", "2"}, nil, nil) 60 b = vm.NewLine(b, vm.INCMP, []string{"lang", "3"}, nil, nil) 61 62 tpl := "hello world" 63 64 return out("root", b, tpl, nil) 65 } 66 67 func foo() error { 68 b := []byte{} 69 b = vm.NewLine(b, vm.MOUT, []string{"to foo", "0"}, nil, nil) 70 b = vm.NewLine(b, vm.MOUT, []string{"go bar", "1"}, nil, nil) 71 b = vm.NewLine(b, vm.MOUT, []string{"see long", "2"}, nil, nil) 72 b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil) 73 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 74 b = vm.NewLine(b, vm.INCMP, []string{"_", "0"}, nil, nil) 75 b = vm.NewLine(b, vm.INCMP, []string{"baz", "1"}, nil, nil) 76 b = vm.NewLine(b, vm.INCMP, []string{"long", "2"}, nil, nil) 77 78 data := make(map[string]string) 79 data["inky"] = "one" 80 81 tpl := `this is in foo 82 83 it has more lines` 84 85 return out("foo", b, tpl, data) 86 } 87 88 func bar() error { 89 b := []byte{} 90 b = vm.NewLine(b, vm.LOAD, []string{"pinky"}, []byte{0}, nil) 91 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 92 b = vm.NewLine(b, vm.INCMP, []string{"^", "*"}, nil, nil) 93 94 tpl := "this is bar - any input will return to top" 95 96 data := make(map[string]string) 97 data["pinky"] = "two" 98 99 return out("bar", b, tpl, data) 100 } 101 102 func baz() error { 103 b := []byte{} 104 b = vm.NewLine(b, vm.MAP, []string{"inky"}, nil, nil) 105 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 106 107 tpl := "this is baz which uses the var {{.inky}} in the template." 108 109 return out("baz", b, tpl, nil) 110 } 111 112 func long() error { 113 b := []byte{} 114 b = vm.NewLine(b, vm.MOUT, []string{"back", "0"}, nil, nil) 115 b = vm.NewLine(b, vm.MNEXT, []string{"nexxt", "00"}, nil, nil) 116 b = vm.NewLine(b, vm.MPREV, []string{"prevvv", "11"}, nil, nil) 117 b = vm.NewLine(b, vm.LOAD, []string{"longdata"}, []byte{0x00}, nil) 118 b = vm.NewLine(b, vm.MAP, []string{"longdata"}, nil, nil) 119 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 120 b = vm.NewLine(b, vm.INCMP, []string{"_", "0"}, nil, nil) 121 b = vm.NewLine(b, vm.INCMP, []string{">", "00"}, nil, nil) 122 b = vm.NewLine(b, vm.INCMP, []string{"<", "11"}, nil, nil) 123 124 tpl := `data 125 {{.longdata}}` 126 127 data := make(map[string]string) 128 data["longdata"] = `INKY 12 129 PINKY 5555 130 BLINKY 3t7 131 CLYDE 11 132 TINKYWINKY 22 133 DIPSY 666 134 LALA 111 135 POO 222 136 ` 137 138 return out("long", b, tpl, data) 139 } 140 141 func defaultCatch() error { 142 b := []byte{} 143 b = vm.NewLine(b, vm.MOUT, []string{"back", "0"}, nil, nil) 144 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 145 b = vm.NewLine(b, vm.INCMP, []string{"_", "*"}, nil, nil) 146 147 tpl := "invalid input" 148 149 return out("_catch", b, tpl, nil) 150 } 151 152 func lang() error { 153 b := []byte{} 154 b = vm.NewLine(b, vm.MOUT, []string{"back", "0"}, nil, nil) 155 b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil) 156 b = vm.NewLine(b, vm.MAP, []string{"inky"}, nil, nil) 157 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 158 b = vm.NewLine(b, vm.INCMP, []string{"_", "*"}, nil, nil) 159 160 tpl := "this changes with language {{.inky}}" 161 err := out("lang", b, tpl, nil) 162 if err != nil { 163 return err 164 } 165 166 tpl = "dette endrer med språket {{.inky}}" 167 fp := path.Join(DataDir, "lang_nor") 168 err = os.WriteFile(fp, []byte(tpl), 0600) 169 if err != nil { 170 return err 171 } 172 173 menu := "tilbake" 174 fp = path.Join(DataDir, "back_menu_nor") 175 return os.WriteFile(fp, []byte(menu), 0600) 176 } 177 178 func nothing() error { 179 b := vm.NewLine(nil, vm.LOAD, []string{"quit"}, []byte{0x00}, nil) 180 b = vm.NewLine(b, vm.RELOAD, []string{"quit"}, nil, nil) 181 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 182 183 fp := path.Join(DataDir, "nothing.bin") 184 err := os.WriteFile(fp, b, 0600) 185 return err 186 } 187 188 func something() error { 189 b := vm.NewLine(nil, vm.HALT, nil, nil, nil) 190 191 tpl := "mmmm, something..." 192 return out("something", b, tpl, nil) 193 } 194 195 func generateLegacy() error { 196 out = outLegacy 197 err := os.MkdirAll(DataDir, 0755) 198 if err != nil { 199 return err 200 } 201 202 fns := []genFunc{root, foo, bar, baz, long, lang, nothing, something, defaultCatch} 203 for _, fn := range fns { 204 err = fn() 205 if err != nil { 206 return err 207 } 208 } 209 return nil 210 } 211 212 // Generate outputs bytecode, templates and content symbols to a temporary directory. 213 // 214 // This directory can in turn be used as data source for the the resource.FsResource object. 215 func GenerateLegacy() (string, error) { 216 dir, err := ioutil.TempDir("", "vise_testdata_") 217 if err != nil { 218 return "", err 219 } 220 DataDir = dir 221 dirLock = true 222 err = generateLegacy() 223 return dir, err 224 } 225 226 227 // Generate outputs bytecode, templates and content symbols to a specified directory. 228 // 229 // The directory must exist, and must not have been used already in the same code execution. 230 // 231 // This directory can in turn be used as data source for the the resource.FsResource object. 232 func GenerateLegacyTo(dir string) error { 233 if dirLock { 234 return fmt.Errorf("directory already overridden") 235 } 236 DataDir = dir 237 dirLock = true 238 return generateLegacy() 239 }