github.com/goplus/igop@v0.25.0/gopbuild/build_test.go (about) 1 /* 2 * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved. 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 gopbuild 18 19 import ( 20 "fmt" 21 "reflect" 22 "testing" 23 24 "github.com/goplus/igop" 25 _ "github.com/goplus/igop/pkg/bytes" 26 ) 27 28 func gopClTest(t *testing.T, gopcode, expected string) { 29 gopClTestEx(t, "main.gop", gopcode, expected) 30 } 31 32 func gopClTestEx(t *testing.T, filename string, gopcode, expected string) { 33 ctx := igop.NewContext(0) 34 data, err := BuildFile(ctx, filename, gopcode) 35 if err != nil { 36 t.Fatalf("build gop error: %v", err) 37 } 38 if string(data) != expected { 39 fmt.Println("build gop error:") 40 fmt.Println(string(data)) 41 t.Fail() 42 } 43 } 44 45 func TestGop(t *testing.T) { 46 gopClTest(t, ` 47 println "Go+" 48 `, `package main 49 50 import "fmt" 51 //line main.gop:2 52 func main() { 53 //line main.gop:2:1 54 fmt.Println("Go+") 55 } 56 `) 57 } 58 59 func TestGox(t *testing.T) { 60 gopClTestEx(t, "Rect.gox", ` 61 println "Go+" 62 `, `package main 63 64 import "fmt" 65 66 type Rect struct { 67 } 68 //line Rect.gox:2 69 func (this *Rect) Main() { 70 //line Rect.gox:2:1 71 fmt.Println("Go+") 72 } 73 func main() { 74 } 75 `) 76 gopClTestEx(t, "Rect.gox", ` 77 var ( 78 Buffer 79 v int 80 ) 81 type Buffer struct { 82 buf []byte 83 } 84 println "Go+" 85 `, `package main 86 87 import "fmt" 88 89 type Buffer struct { 90 buf []byte 91 } 92 type Rect struct { 93 Buffer 94 v int 95 } 96 //line Rect.gox:9 97 func (this *Rect) Main() { 98 //line Rect.gox:9:1 99 fmt.Println("Go+") 100 } 101 func main() { 102 } 103 `) 104 gopClTestEx(t, "Rect.gox", ` 105 var ( 106 *Buffer 107 v int 108 ) 109 type Buffer struct { 110 buf []byte 111 } 112 println "Go+" 113 `, `package main 114 115 import "fmt" 116 117 type Buffer struct { 118 buf []byte 119 } 120 type Rect struct { 121 *Buffer 122 v int 123 } 124 //line Rect.gox:9 125 func (this *Rect) Main() { 126 //line Rect.gox:9:1 127 fmt.Println("Go+") 128 } 129 func main() { 130 } 131 `) 132 gopClTestEx(t, "Rect.gox", ` 133 import "bytes" 134 var ( 135 *bytes.Buffer 136 v int 137 ) 138 println "Go+" 139 `, `package main 140 141 import ( 142 "bytes" 143 "fmt" 144 ) 145 146 type Rect struct { 147 *bytes.Buffer 148 v int 149 } 150 //line Rect.gox:7 151 func (this *Rect) Main() { 152 //line Rect.gox:7:1 153 fmt.Println("Go+") 154 } 155 func main() { 156 } 157 `) 158 gopClTestEx(t, "Rect.gox", ` 159 import "bytes" 160 var ( 161 bytes.Buffer 162 v int 163 ) 164 println "Go+" 165 `, `package main 166 167 import ( 168 "bytes" 169 "fmt" 170 ) 171 172 type Rect struct { 173 bytes.Buffer 174 v int 175 } 176 //line Rect.gox:7 177 func (this *Rect) Main() { 178 //line Rect.gox:7:1 179 fmt.Println("Go+") 180 } 181 func main() { 182 } 183 `) 184 } 185 186 func TestBig(t *testing.T) { 187 gopClTest(t, ` 188 a := 1/2r 189 println a+1/2r 190 `, `package main 191 192 import ( 193 "fmt" 194 "github.com/goplus/gop/builtin/ng" 195 "math/big" 196 ) 197 //line main.gop:2 198 func main() { 199 //line main.gop:2:1 200 a := ng.Bigrat_Init__2(big.NewRat(1, 2)) 201 //line main.gop:3:1 202 fmt.Println((ng.Bigrat).Gop_Add(a, ng.Bigrat_Init__2(big.NewRat(1, 2)))) 203 } 204 `) 205 } 206 207 func TestBuiltin(t *testing.T) { 208 igop.RegisterCustomBuiltin("typeof", reflect.TypeOf) 209 gopClTest(t, ` 210 v := typeof(100) 211 println(v) 212 `, `package main 213 214 import "fmt" 215 //line main.gop:2 216 func main() { 217 //line main.gop:2:1 218 v := typeof(100) 219 //line main.gop:3:1 220 fmt.Println(v) 221 } 222 `) 223 } 224 225 func TestIoxLines(t *testing.T) { 226 gopClTest(t, ` 227 import "io" 228 229 var r io.Reader 230 231 for line <- lines(r) { 232 println line 233 } 234 `, `package main 235 236 import ( 237 "fmt" 238 "github.com/goplus/gop/builtin/iox" 239 "io" 240 ) 241 242 var r io.Reader 243 //line main.gop:6 244 func main() { 245 //line main.gop:6:1 246 for _gop_it := iox.Lines(r).Gop_Enum(); ; { 247 var _gop_ok bool 248 line, _gop_ok := _gop_it.Next() 249 if !_gop_ok { 250 break 251 } 252 //line main.gop:7:1 253 fmt.Println(line) 254 } 255 } 256 `) 257 } 258 259 func TestErrorWrap(t *testing.T) { 260 gopClTest(t, ` 261 import ( 262 "strconv" 263 ) 264 265 func add(x, y string) (int, error) { 266 return strconv.Atoi(x)? + strconv.Atoi(y)?, nil 267 } 268 269 func addSafe(x, y string) int { 270 return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0 271 } 272 273 println add("100", "23")! 274 275 sum, err := add("10", "abc") 276 println sum, err 277 278 println addSafe("10", "abc") 279 `, `package main 280 281 import ( 282 "fmt" 283 "github.com/qiniu/x/errors" 284 "strconv" 285 ) 286 //line main.gop:6:1 287 func add(x string, y string) (int, error) { 288 //line main.gop:7:1 289 var _autoGo_1 int 290 //line main.gop:7:1 291 { 292 //line main.gop:7:1 293 var _gop_err error 294 //line main.gop:7:1 295 _autoGo_1, _gop_err = strconv.Atoi(x) 296 //line main.gop:7:1 297 if _gop_err != nil { 298 //line main.gop:7:1 299 _gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(x)", "main.gop", 7, "main.add") 300 //line main.gop:7:1 301 return 0, _gop_err 302 } 303 //line main.gop:7:1 304 goto _autoGo_2 305 _autoGo_2: 306 //line main.gop:7:1 307 } 308 //line main.gop:7:1 309 var _autoGo_3 int 310 //line main.gop:7:1 311 { 312 //line main.gop:7:1 313 var _gop_err error 314 //line main.gop:7:1 315 _autoGo_3, _gop_err = strconv.Atoi(y) 316 //line main.gop:7:1 317 if _gop_err != nil { 318 //line main.gop:7:1 319 _gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(y)", "main.gop", 7, "main.add") 320 //line main.gop:7:1 321 return 0, _gop_err 322 } 323 //line main.gop:7:1 324 goto _autoGo_4 325 _autoGo_4: 326 //line main.gop:7:1 327 } 328 //line main.gop:7:1 329 return _autoGo_1 + _autoGo_3, nil 330 } 331 //line main.gop:10:1 332 func addSafe(x string, y string) int { 333 //line main.gop:11:1 334 return func() (_gop_ret int) { 335 //line main.gop:11:1 336 var _gop_err error 337 //line main.gop:11:1 338 _gop_ret, _gop_err = strconv.Atoi(x) 339 //line main.gop:11:1 340 if _gop_err != nil { 341 //line main.gop:11:1 342 return 0 343 } 344 //line main.gop:11:1 345 return 346 }() + func() (_gop_ret int) { 347 //line main.gop:11:1 348 var _gop_err error 349 //line main.gop:11:1 350 _gop_ret, _gop_err = strconv.Atoi(y) 351 //line main.gop:11:1 352 if _gop_err != nil { 353 //line main.gop:11:1 354 return 0 355 } 356 //line main.gop:11:1 357 return 358 }() 359 } 360 //line main.gop:14 361 func main() { 362 //line main.gop:14:1 363 fmt.Println(func() (_gop_ret int) { 364 //line main.gop:14:1 365 var _gop_err error 366 //line main.gop:14:1 367 _gop_ret, _gop_err = add("100", "23") 368 //line main.gop:14:1 369 if _gop_err != nil { 370 //line main.gop:14:1 371 _gop_err = errors.NewFrame(_gop_err, "add(\"100\", \"23\")", "main.gop", 14, "main.main") 372 //line main.gop:14:1 373 panic(_gop_err) 374 } 375 //line main.gop:14:1 376 return 377 }()) 378 //line main.gop:16:1 379 sum, err := add("10", "abc") 380 //line main.gop:17:1 381 fmt.Println(sum, err) 382 //line main.gop:19:1 383 fmt.Println(addSafe("10", "abc")) 384 } 385 `) 386 } 387 388 func TestGsh(t *testing.T) { 389 gopClTestEx(t, "exec.gsh", ` 390 gop "run", "./foo" 391 exec "gop run ./foo" 392 exec "FOO=100 gop run ./foo" 393 exec {"FOO": "101"}, "gop", "run", "./foo" 394 exec "gop", "run", "./foo" 395 exec "ls $HOME" 396 ls "${HOME}" 397 398 `, `package main 399 400 import "github.com/qiniu/x/gsh" 401 402 type exec struct { 403 gsh.App 404 } 405 //line exec.gsh:2 406 func (this *exec) MainEntry() { 407 //line exec.gsh:2:1 408 this.Gop_Exec("gop", "run", "./foo") 409 //line exec.gsh:3:1 410 this.Exec__1("gop run ./foo") 411 //line exec.gsh:4:1 412 this.Exec__1("FOO=100 gop run ./foo") 413 //line exec.gsh:5:1 414 this.Exec__0(map[string]string{"FOO": "101"}, "gop", "run", "./foo") 415 //line exec.gsh:6:1 416 this.Exec__2("gop", "run", "./foo") 417 //line exec.gsh:7:1 418 this.Exec__1("ls $HOME") 419 //line exec.gsh:8:1 420 this.Gop_Exec("ls", this.Gop_Env("HOME")) 421 } 422 func (this *exec) Main() { 423 gsh.Gopt_App_Main(this) 424 } 425 func main() { 426 new(exec).Main() 427 } 428 `) 429 }