github.com/goplus/igop@v0.17.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  )
    26  
    27  func gopClTest(t *testing.T, gopcode, expected string) {
    28  	gopClTestEx(t, "main.gop", gopcode, expected)
    29  }
    30  
    31  func gopClTestEx(t *testing.T, filename string, gopcode, expected string) {
    32  	ctx := igop.NewContext(0)
    33  	data, err := BuildFile(ctx, filename, gopcode)
    34  	if err != nil {
    35  		t.Fatalf("build gop error: %v", err)
    36  	}
    37  	if string(data) != expected {
    38  		fmt.Println("build gop error:")
    39  		fmt.Println(string(data))
    40  		t.Fail()
    41  	}
    42  }
    43  
    44  func TestGop(t *testing.T) {
    45  	gopClTest(t, `
    46  println "Go+"
    47  `, `package main
    48  
    49  import fmt "fmt"
    50  
    51  func main() {
    52  //line main.gop:2
    53  	fmt.Println("Go+")
    54  }
    55  `)
    56  }
    57  
    58  func TestGopx(t *testing.T) {
    59  	gopClTestEx(t, "Rect.gopx", `
    60  println "Go+"
    61  `, `package main
    62  
    63  import fmt "fmt"
    64  
    65  type Rect struct {
    66  }
    67  
    68  func (this *Rect) Main() {
    69  //line Rect.gopx:2
    70  	fmt.Println("Go+")
    71  }
    72  func main() {
    73  }
    74  `)
    75  	gopClTestEx(t, "Rect.gopx", `
    76  var (
    77  	Buffer
    78  	v int
    79  )
    80  type Buffer struct {
    81  	buf []byte
    82  }
    83  println "Go+"
    84  `, `package main
    85  
    86  import fmt "fmt"
    87  
    88  type Buffer struct {
    89  	buf []byte
    90  }
    91  type Rect struct {
    92  	Buffer
    93  	v int
    94  }
    95  
    96  func (this *Rect) Main() {
    97  //line Rect.gopx:9
    98  	fmt.Println("Go+")
    99  }
   100  func main() {
   101  }
   102  `)
   103  }
   104  
   105  func TestBig(t *testing.T) {
   106  	gopClTest(t, `
   107  a := 1/2r
   108  println a+1/2r
   109  `, `package main
   110  
   111  import (
   112  	fmt "fmt"
   113  	ng "github.com/goplus/gop/builtin/ng"
   114  	big "math/big"
   115  )
   116  
   117  func main() {
   118  //line main.gop:2
   119  	a := ng.Bigrat_Init__2(big.NewRat(1, 2))
   120  //line main.gop:3
   121  	fmt.Println(a.Gop_Add(ng.Bigrat_Init__2(big.NewRat(1, 2))))
   122  }
   123  `)
   124  }
   125  
   126  func TestBuiltin(t *testing.T) {
   127  	igop.RegisterCustomBuiltin("typeof", reflect.TypeOf)
   128  	gopClTest(t, `
   129  v := typeof(100)
   130  println(v)
   131  `, `package main
   132  
   133  import fmt "fmt"
   134  
   135  func main() {
   136  //line main.gop:2
   137  	v := typeof(100)
   138  //line main.gop:3
   139  	fmt.Println(v)
   140  }
   141  `)
   142  }
   143  
   144  func TestIoxLines(t *testing.T) {
   145  	gopClTest(t, `
   146  import "io"
   147  
   148  var r io.Reader
   149  
   150  for line <- lines(r) {
   151  	println line
   152  }
   153  `, `package main
   154  
   155  import (
   156  	fmt "fmt"
   157  	iox "github.com/goplus/gop/builtin/iox"
   158  	io "io"
   159  )
   160  
   161  var r io.Reader
   162  
   163  func main() {
   164  //line main.gop:6
   165  	for _gop_it := iox.Lines(r).Gop_Enum(); ; {
   166  		var _gop_ok bool
   167  		line, _gop_ok := _gop_it.Next()
   168  		if !_gop_ok {
   169  			break
   170  		}
   171  //line main.gop:7
   172  		fmt.Println(line)
   173  	}
   174  }
   175  `)
   176  }
   177  
   178  func TestErrorWrap(t *testing.T) {
   179  	gopClTest(t, `
   180  import (
   181      "strconv"
   182  )
   183  
   184  func add(x, y string) (int, error) {
   185      return strconv.Atoi(x)? + strconv.Atoi(y)?, nil
   186  }
   187  
   188  func addSafe(x, y string) int {
   189      return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0
   190  }
   191  
   192  println add("100", "23")!
   193  
   194  sum, err := add("10", "abc")
   195  println sum, err
   196  
   197  println addSafe("10", "abc")
   198  `, `package main
   199  
   200  import (
   201  	fmt "fmt"
   202  	strconv "strconv"
   203  	errors "github.com/qiniu/x/errors"
   204  )
   205  
   206  func add(x string, y string) (int, error) {
   207  //line main.gop:7
   208  	var _autoGo_1 int
   209  //line main.gop:7
   210  	{
   211  //line main.gop:7
   212  		var _gop_err error
   213  //line main.gop:7
   214  		_autoGo_1, _gop_err = strconv.Atoi(x)
   215  //line main.gop:7
   216  		if _gop_err != nil {
   217  //line main.gop:7
   218  			_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(x)", "main.gop", 7, "main.add")
   219  //line main.gop:7
   220  			return 0, _gop_err
   221  		}
   222  //line main.gop:7
   223  		goto _autoGo_2
   224  	_autoGo_2:
   225  //line main.gop:7
   226  	}
   227  //line main.gop:7
   228  	var _autoGo_3 int
   229  //line main.gop:7
   230  	{
   231  //line main.gop:7
   232  		var _gop_err error
   233  //line main.gop:7
   234  		_autoGo_3, _gop_err = strconv.Atoi(y)
   235  //line main.gop:7
   236  		if _gop_err != nil {
   237  //line main.gop:7
   238  			_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(y)", "main.gop", 7, "main.add")
   239  //line main.gop:7
   240  			return 0, _gop_err
   241  		}
   242  //line main.gop:7
   243  		goto _autoGo_4
   244  	_autoGo_4:
   245  //line main.gop:7
   246  	}
   247  //line main.gop:7
   248  	return _autoGo_1 + _autoGo_3, nil
   249  }
   250  func addSafe(x string, y string) int {
   251  //line main.gop:11
   252  	return func() (_gop_ret int) {
   253  //line main.gop:11
   254  		var _gop_err error
   255  //line main.gop:11
   256  		_gop_ret, _gop_err = strconv.Atoi(x)
   257  //line main.gop:11
   258  		if _gop_err != nil {
   259  //line main.gop:11
   260  			return 0
   261  		}
   262  //line main.gop:11
   263  		return
   264  	}() + func() (_gop_ret int) {
   265  //line main.gop:11
   266  		var _gop_err error
   267  //line main.gop:11
   268  		_gop_ret, _gop_err = strconv.Atoi(y)
   269  //line main.gop:11
   270  		if _gop_err != nil {
   271  //line main.gop:11
   272  			return 0
   273  		}
   274  //line main.gop:11
   275  		return
   276  	}()
   277  }
   278  func main() {
   279  //line main.gop:14
   280  	fmt.Println(func() (_gop_ret int) {
   281  //line main.gop:14
   282  		var _gop_err error
   283  //line main.gop:14
   284  		_gop_ret, _gop_err = add("100", "23")
   285  //line main.gop:14
   286  		if _gop_err != nil {
   287  //line main.gop:14
   288  			_gop_err = errors.NewFrame(_gop_err, "add(\"100\", \"23\")", "main.gop", 14, "main.main")
   289  //line main.gop:14
   290  			panic(_gop_err)
   291  		}
   292  //line main.gop:14
   293  		return
   294  	}())
   295  //line main.gop:16
   296  	sum, err := add("10", "abc")
   297  //line main.gop:17
   298  	fmt.Println(sum, err)
   299  //line main.gop:19
   300  	fmt.Println(addSafe("10", "abc"))
   301  }
   302  `)
   303  }