github.com/goplus/gop@v1.2.6/cl/compile_test.go (about)

     1  /*
     2   * Copyright (c) 2021 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 cl_test
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"sync"
    23  	"testing"
    24  
    25  	"github.com/goplus/gogen"
    26  	"github.com/goplus/gop"
    27  	"github.com/goplus/gop/cl"
    28  	"github.com/goplus/gop/parser"
    29  	"github.com/goplus/gop/parser/fsx/memfs"
    30  	"github.com/goplus/gop/scanner"
    31  	"github.com/goplus/gop/token"
    32  	"github.com/goplus/mod/env"
    33  )
    34  
    35  const (
    36  	gopRootDir = ".."
    37  )
    38  
    39  var (
    40  	gblFset     *token.FileSet
    41  	gblConf     *cl.Config
    42  	gblConfLine *cl.Config
    43  )
    44  
    45  func init() {
    46  	gogen.SetDebug(gogen.DbgFlagAll)
    47  	cl.SetDebug(cl.DbgFlagAll | cl.FlagNoMarkAutogen)
    48  	gblFset = token.NewFileSet()
    49  	imp := gop.NewImporter(nil, &env.Gop{Root: gopRootDir, Version: "1.0"}, gblFset)
    50  	gblConf = &cl.Config{
    51  		Fset:          gblFset,
    52  		Importer:      imp,
    53  		Recorder:      gopRecorder{},
    54  		LookupClass:   lookupClass,
    55  		LookupPub:     lookupPub,
    56  		C2goBase:      "github.com/goplus/gop/cl/internal",
    57  		NoFileLine:    true,
    58  		NoAutoGenMain: true,
    59  	}
    60  	gblConfLine = &cl.Config{
    61  		Fset:          gblFset,
    62  		Importer:      imp,
    63  		Recorder:      gopRecorder{},
    64  		LookupClass:   lookupClass,
    65  		LookupPub:     lookupPub,
    66  		C2goBase:      "github.com/goplus/gop/cl/internal",
    67  		NoFileLine:    false,
    68  		NoAutoGenMain: true,
    69  	}
    70  }
    71  
    72  func gopClNamedTest(t *testing.T, name string, gopcode, expected string) {
    73  	t.Run(name, func(t *testing.T) {
    74  		gopClTest(t, gopcode, expected)
    75  	})
    76  }
    77  
    78  func gopClTest(t *testing.T, gopcode, expected string) {
    79  	gopClTestEx(t, gblConf, "main", gopcode, expected)
    80  }
    81  
    82  func gopClTestFile(t *testing.T, gopcode, expected string, fname string) {
    83  	fs := memfs.SingleFile("/foo", fname, gopcode)
    84  	gopClTestFS(t, gblConf, fs, "main", expected)
    85  }
    86  
    87  func gopClTestEx(t *testing.T, conf *cl.Config, pkgname, gopcode, expected string) {
    88  	fs := memfs.SingleFile("/foo", "bar.gop", gopcode)
    89  	gopClTestFS(t, conf, fs, pkgname, expected)
    90  }
    91  
    92  func gopMixedClTest(t *testing.T, pkgname, gocode, gopcode, expected string, outline ...bool) {
    93  	conf := *gblConf
    94  	conf.Outline = (outline != nil && outline[0])
    95  	fs := memfs.TwoFiles("/foo", "a.go", gocode, "b.gop", gopcode)
    96  	gopClTestFS(t, &conf, fs, pkgname, expected)
    97  }
    98  
    99  func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, expected string) {
   100  	cl.SetDisableRecover(true)
   101  	defer cl.SetDisableRecover(false)
   102  
   103  	pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", parser.Config{Mode: parser.ParseComments})
   104  	if err != nil {
   105  		scanner.PrintError(os.Stderr, err)
   106  		t.Fatal("ParseFSDir:", err)
   107  	}
   108  	bar := pkgs[pkgname]
   109  	pkg, err := cl.NewPackage("github.com/goplus/gop/cl", bar, conf)
   110  	if err != nil {
   111  		t.Fatal("NewPackage:", err)
   112  	}
   113  	var b bytes.Buffer
   114  	err = pkg.WriteTo(&b)
   115  	if err != nil {
   116  		t.Fatal("gogen.WriteTo failed:", err)
   117  	}
   118  	result := b.String()
   119  	if result != expected {
   120  		t.Fatalf("\nResult:\n%s\nExpected:\n%s\n", result, expected)
   121  	}
   122  }
   123  
   124  func TestTypeDoc(t *testing.T) {
   125  	gopClTest(t, `
   126  type (
   127  	// doc
   128  	A int
   129  )
   130  `, `package main
   131  // doc
   132  type A int
   133  `)
   134  }
   135  
   136  func TestUnsafe(t *testing.T) {
   137  	gopClTest(t, `
   138  import "unsafe"
   139  
   140  println unsafe.Sizeof(0)
   141  `, `package main
   142  
   143  import (
   144  	"fmt"
   145  	"unsafe"
   146  )
   147  
   148  func main() {
   149  	fmt.Println(unsafe.Sizeof(0))
   150  }
   151  `)
   152  }
   153  
   154  func Test_CastSlice_Issue1240(t *testing.T) {
   155  	gopClTest(t, `
   156  type fvec []float64
   157  type foo float64
   158  a := []float64([1, 2])
   159  b := fvec([1, 2])
   160  c := foo([1, 2])
   161  d := fvec([])
   162  println a, b, c, d
   163  `, `package main
   164  
   165  import "fmt"
   166  
   167  type fvec []float64
   168  type foo float64
   169  
   170  func main() {
   171  	a := []float64{1, 2}
   172  	b := fvec{1, 2}
   173  	c := foo([]int{1, 2})
   174  	d := fvec{}
   175  	fmt.Println(a, b, c, d)
   176  }
   177  `)
   178  }
   179  
   180  func TestUnderscoreRedeclared_Issue1197(t *testing.T) {
   181  	gopClTest(t, `
   182  func() (_ [2]int) { type _ int; return }()
   183  `, `package main
   184  
   185  func main() {
   186  	func() (_ [2]int) {
   187  		return
   188  	}()
   189  }
   190  `)
   191  }
   192  
   193  func TestInterfaceBugNilUnderlying_Issue1198(t *testing.T) {
   194  	gopClTest(t, `
   195  import "runtime"
   196  
   197  type Outer interface{ Inner }
   198  
   199  type impl struct{}
   200  
   201  func New() Outer { return &impl{} }
   202  
   203  type Inner interface {
   204  	DoStuff() error
   205  }
   206  
   207  func (a *impl) DoStuff() error {
   208  	return nil
   209  }
   210  
   211  func main() {
   212  	var outer Outer = New()
   213  }
   214  `, `package main
   215  
   216  type Outer interface {
   217  	Inner
   218  }
   219  type impl struct {
   220  }
   221  type Inner interface {
   222  	DoStuff() error
   223  }
   224  
   225  func (a *impl) DoStuff() error {
   226  	return nil
   227  }
   228  func New() Outer {
   229  	return &impl{}
   230  }
   231  func main() {
   232  	var outer Outer = New()
   233  }
   234  `)
   235  }
   236  
   237  func TestInterfaceBugNilUnderlying_Issue1196(t *testing.T) {
   238  	gopClTest(t, `
   239  func main() {
   240  	i := I(A{})
   241  
   242  	b := make(chan I, 1)
   243  	b <- B{}
   244  
   245  	var ok bool
   246  	i, ok = <-b
   247  }
   248  
   249  type I interface{ M() int }
   250  
   251  type T int
   252  
   253  func (T) M() int { return 0 }
   254  
   255  type A struct{ T }
   256  type B struct{ T }
   257  `, `package main
   258  
   259  type I interface {
   260  	M() int
   261  }
   262  type T int
   263  type A struct {
   264  	T
   265  }
   266  type B struct {
   267  	T
   268  }
   269  
   270  func main() {
   271  	i := I(A{})
   272  	b := make(chan I, 1)
   273  	b <- B{}
   274  	var ok bool
   275  	i, ok = <-b
   276  }
   277  func (T) M() int {
   278  	return 0
   279  }
   280  `)
   281  }
   282  
   283  func TestMyIntInc_Issue1195(t *testing.T) {
   284  	gopClTest(t, `
   285  type MyInt int
   286  var c MyInt
   287  c++
   288  `, `package main
   289  
   290  type MyInt int
   291  
   292  var c MyInt
   293  
   294  func main() {
   295  	c++
   296  }
   297  `)
   298  }
   299  
   300  func TestAutoPropMixedName_Issue1194(t *testing.T) {
   301  	gopClTest(t, `
   302  type Point struct {
   303  	Min, Max int
   304  }
   305  
   306  type Obj struct {
   307  	bbox Point
   308  }
   309  
   310  func (o *Obj) Bbox() Point {
   311  	return o.bbox
   312  }
   313  
   314  func (o *Obj) Points() [2]int{
   315  	return [2]int{o.bbox.Min, o.bbox.Max}
   316  }
   317  `, `package main
   318  
   319  type Point struct {
   320  	Min int
   321  	Max int
   322  }
   323  type Obj struct {
   324  	bbox Point
   325  }
   326  
   327  func (o *Obj) Bbox() Point {
   328  	return o.bbox
   329  }
   330  func (o *Obj) Points() [2]int {
   331  	return [2]int{o.bbox.Min, o.bbox.Max}
   332  }
   333  `)
   334  }
   335  
   336  func TestShiftUntypedInt_Issue1193(t *testing.T) {
   337  	gopClTest(t, `
   338  func GetValue(shift uint) uint {
   339  	return 1 << shift
   340  }`, `package main
   341  
   342  func GetValue(shift uint) uint {
   343  	return 1 << shift
   344  }
   345  `)
   346  }
   347  
   348  func TestInitFunc(t *testing.T) {
   349  	gopClTest(t, `
   350  
   351  func init() {}
   352  func init() {}
   353  `, `package main
   354  
   355  func init() {
   356  }
   357  func init() {
   358  }
   359  `)
   360  }
   361  
   362  func TestSlogan(t *testing.T) {
   363  	gopClTest(t, `
   364  fields := ["engineering", "STEM education", "data science"]
   365  println "The Go+ Language for", fields.join(", ")
   366  `, `package main
   367  
   368  import (
   369  	"fmt"
   370  	"strings"
   371  )
   372  
   373  func main() {
   374  	fields := []string{"engineering", "STEM education", "data science"}
   375  	fmt.Println("The Go+ Language for", strings.Join(fields, ", "))
   376  }
   377  `)
   378  }
   379  
   380  func TestAssignPrintln(t *testing.T) {
   381  	gopClTest(t, `
   382  p := println
   383  p "Hello world"
   384  `, `package main
   385  
   386  import "fmt"
   387  
   388  func main() {
   389  	p := fmt.Println
   390  	p("Hello world")
   391  }
   392  `)
   393  }
   394  
   395  func TestRedefineBuiltin(t *testing.T) {
   396  	gopClTest(t, `
   397  func main() {
   398  	const a = append + len
   399  }
   400  
   401  const (
   402  	append = iota
   403  	len
   404  )
   405  `, `package main
   406  
   407  const (
   408  	append = iota
   409  	len
   410  )
   411  
   412  func main() {
   413  	const a = append + len
   414  }
   415  `)
   416  }
   417  
   418  func TestTypeConvIssue804(t *testing.T) {
   419  	gopClTest(t, `
   420  c := make(chan int)
   421  d := (chan<- int)(c)
   422  e := (<-chan int)(c)
   423  f := (*int)(nil)
   424  a := c == d
   425  b := c == e
   426  `, `package main
   427  
   428  func main() {
   429  	c := make(chan int)
   430  	d := (chan<- int)(c)
   431  	e := (<-chan int)(c)
   432  	f := (*int)(nil)
   433  	a := c == d
   434  	b := c == e
   435  }
   436  `)
   437  }
   438  
   439  func TestUntypedFloatIssue798(t *testing.T) {
   440  	gopClTest(t, `
   441  func isPow10(x uint64) bool {
   442  	switch x {
   443  	case 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
   444  		1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19:
   445  		return true
   446  	}
   447  	return false
   448  }
   449  `, `package main
   450  
   451  func isPow10(x uint64) bool {
   452  	switch x {
   453  	case 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19:
   454  		return true
   455  	}
   456  	return false
   457  }
   458  `)
   459  }
   460  
   461  func TestInterfaceIssue795(t *testing.T) {
   462  	gopClTest(t, `
   463  type I interface {
   464  	a(s string) I
   465  	b(s string) string
   466  }
   467  
   468  type T1 int
   469  
   470  func (t T1) a(s string) I {
   471  	return t
   472  }
   473  
   474  func (T1) b(s string) string {
   475  	return s
   476  }
   477  `, `package main
   478  
   479  type I interface {
   480  	a(s string) I
   481  	b(s string) string
   482  }
   483  type T1 int
   484  
   485  func (t T1) a(s string) I {
   486  	return t
   487  }
   488  func (T1) b(s string) string {
   489  	return s
   490  }
   491  `)
   492  }
   493  
   494  func TestChanRecvIssue789(t *testing.T) {
   495  	gopClTest(t, `
   496  func foo(ch chan int) (int, bool) {
   497  	x, ok := (<-ch)
   498  	return x, ok
   499  }
   500  `, `package main
   501  
   502  func foo(ch chan int) (int, bool) {
   503  	x, ok := <-ch
   504  	return x, ok
   505  }
   506  `)
   507  }
   508  
   509  func TestNamedChanCloseIssue790(t *testing.T) {
   510  	gopClTest(t, `
   511  type XChan chan int
   512  
   513  func foo(ch XChan) {
   514  	close(ch)
   515  }
   516  `, `package main
   517  
   518  type XChan chan int
   519  
   520  func foo(ch XChan) {
   521  	close(ch)
   522  }
   523  `)
   524  }
   525  
   526  func TestUntypedFloatIssue793(t *testing.T) {
   527  	gopClTest(t, `
   528  var a [1e1]int
   529  `, `package main
   530  
   531  var a [10]int
   532  `)
   533  }
   534  
   535  func TestUntypedFloatIssue788(t *testing.T) {
   536  	gopClTest(t, `
   537  func foo(v int) bool {
   538      return v > 1.1e5
   539  }
   540  `, `package main
   541  
   542  func foo(v int) bool {
   543  	return v > 1.1e5
   544  }
   545  `)
   546  }
   547  
   548  func TestSwitchCompositeLitIssue801(t *testing.T) {
   549  	gopClTest(t, `
   550  type T struct {
   551  	X int
   552  }
   553  
   554  switch (T{}) {
   555  case T{1}:
   556  	panic("bad")
   557  }
   558  `, `package main
   559  
   560  type T struct {
   561  	X int
   562  }
   563  
   564  func main() {
   565  	switch (T{}) {
   566  	case T{1}:
   567  		panic("bad")
   568  	}
   569  }
   570  `)
   571  }
   572  
   573  func TestConstIssue800(t *testing.T) {
   574  	gopClTest(t, `
   575  const (
   576  	h0_0, h0_1 = 1.0 / (iota + 1), 1.0 / (iota + 2)
   577  	h1_0, h1_1
   578  )
   579  `, `package main
   580  
   581  const (
   582  	h0_0, h0_1 = 1.0 / (iota + 1), 1.0 / (iota + 2)
   583  	h1_0, h1_1
   584  )
   585  `)
   586  }
   587  
   588  func TestConstIssue805(t *testing.T) {
   589  	gopClTest(t, `
   590  const (
   591  	n1 = +5
   592  	d1 = +3
   593  
   594  	q1 = +1
   595  	r1 = +2
   596  )
   597  
   598  const (
   599  	ret1 = n1/d1 != q1
   600  	ret2 = n1%d1 != r1
   601  	ret3 = n1/d1 != q1 || n1%d1 != r1
   602  )
   603  `, `package main
   604  
   605  const (
   606  	n1 = +5
   607  	d1 = +3
   608  	q1 = +1
   609  	r1 = +2
   610  )
   611  const (
   612  	ret1 = n1/d1 != q1
   613  	ret2 = n1%d1 != r1
   614  	ret3 = false
   615  )
   616  `)
   617  }
   618  
   619  func TestUntypedNilIssue806(t *testing.T) {
   620  	gopClTest(t, `
   621  switch f := func() {}; f {
   622  case nil:
   623  }
   624  `, `package main
   625  
   626  func main() {
   627  	switch f := func() {
   628  	}; f {
   629  	case nil:
   630  	}
   631  }
   632  `)
   633  }
   634  
   635  func TestSwitchIssue807(t *testing.T) {
   636  	gopClTest(t, `
   637  switch {
   638  case interface{}(true):
   639  }
   640  `, `package main
   641  
   642  func main() {
   643  	switch {
   644  	case interface{}(true):
   645  	}
   646  }
   647  `)
   648  }
   649  
   650  func TestUntypedComplexIssue799(t *testing.T) {
   651  	gopClTest(t, `
   652  const ulp1 = imag(1i + 2i / 3 - 5i / 3)
   653  const ulp2 = imag(1i + complex(0, 2) / 3 - 5i / 3)
   654  
   655  func main() {
   656  	const a = (ulp1 == ulp2)
   657  }
   658  `, `package main
   659  
   660  const ulp1 = imag(1i + 2i/3 - 5i/3)
   661  const ulp2 = imag(1i + complex(0, 2)/3 - 5i/3)
   662  
   663  func main() {
   664  	const a = ulp1 == ulp2
   665  }
   666  `)
   667  }
   668  
   669  func TestUnderscoreConstAndVar(t *testing.T) {
   670  	gopClTest(t, `
   671  const (
   672  	c0 = 1 << iota
   673  	_
   674  	_
   675  	_
   676  	c4
   677  )
   678  
   679  func i() int {
   680  	return 23
   681  }
   682  
   683  var (
   684  	_ = i()
   685  	_ = i()
   686  )
   687  `, `package main
   688  
   689  const (
   690  	c0 = 1 << iota
   691  	_
   692  	_
   693  	_
   694  	c4
   695  )
   696  
   697  func i() int {
   698  	return 23
   699  }
   700  
   701  var _ = i()
   702  var _ = i()
   703  `)
   704  }
   705  
   706  func TestUnderscoreFuncAndMethod(t *testing.T) {
   707  	gopClTest(t, `
   708  func _() {
   709  }
   710  
   711  type T struct {
   712  	_, _, _ int
   713  }
   714  
   715  func (T) _() {
   716  }
   717  
   718  func (T) _() {
   719  }
   720  `, `package main
   721  
   722  type T struct {
   723  	_ int
   724  	_ int
   725  	_ int
   726  }
   727  
   728  func (T) _() {
   729  }
   730  func (T) _() {
   731  }
   732  func _() {
   733  }
   734  `)
   735  }
   736  
   737  func TestErrWrapIssue772(t *testing.T) {
   738  	gopClTest(t, `
   739  package main
   740  
   741  func t() (int,int,error){
   742  	return 0, 0, nil
   743  }
   744  
   745  func main() {
   746  	a, b := t()!
   747  	println(a, b)
   748  }`, `package main
   749  
   750  import (
   751  	"fmt"
   752  	"github.com/qiniu/x/errors"
   753  )
   754  
   755  func t() (int, int, error) {
   756  	return 0, 0, nil
   757  }
   758  func main() {
   759  	a, b := func() (_gop_ret int, _gop_ret2 int) {
   760  		var _gop_err error
   761  		_gop_ret, _gop_ret2, _gop_err = t()
   762  		if _gop_err != nil {
   763  			_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
   764  			panic(_gop_err)
   765  		}
   766  		return
   767  	}()
   768  	fmt.Println(a, b)
   769  }
   770  `)
   771  }
   772  
   773  func TestErrWrapIssue778(t *testing.T) {
   774  	gopClTest(t, `
   775  package main
   776  
   777  func t() error {
   778  	return nil
   779  }
   780  
   781  func main() {
   782  	t()!
   783  }`, `package main
   784  
   785  import "github.com/qiniu/x/errors"
   786  
   787  func t() error {
   788  	return nil
   789  }
   790  func main() {
   791  	func() {
   792  		var _gop_err error
   793  		_gop_err = t()
   794  		if _gop_err != nil {
   795  			_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
   796  			panic(_gop_err)
   797  		}
   798  		return
   799  	}()
   800  }
   801  `)
   802  }
   803  
   804  func TestIssue774(t *testing.T) {
   805  	gopClNamedTest(t, "InterfaceTypeAssert", `
   806  package main
   807  
   808  import "fmt"
   809  
   810  func main() {
   811  	var a AA = &A{str: "hello"}
   812  	fmt.Println(a.(*A))
   813  }
   814  
   815  type AA interface {
   816  	String() string
   817  }
   818  
   819  type A struct {
   820  	str string
   821  }
   822  
   823  func (a *A) String() string {
   824  	return a.str
   825  }
   826  `, `package main
   827  
   828  import "fmt"
   829  
   830  type AA interface {
   831  	String() string
   832  }
   833  type A struct {
   834  	str string
   835  }
   836  
   837  func main() {
   838  	var a AA = &A{str: "hello"}
   839  	fmt.Println(a.(*A))
   840  }
   841  func (a *A) String() string {
   842  	return a.str
   843  }
   844  `)
   845  	gopClNamedTest(t, "getInterface", `
   846  package main
   847  
   848  import "fmt"
   849  
   850  func main() {
   851  	a := get()
   852  	fmt.Println(a.(*A))
   853  }
   854  
   855  type AA interface {
   856  	String() string
   857  }
   858  
   859  func get() AA {
   860  	var a AA
   861  	return a
   862  }
   863  
   864  type A struct {
   865  	str string
   866  }
   867  
   868  func (a *A) String() string {
   869  	return a.str
   870  }
   871  `, `package main
   872  
   873  import "fmt"
   874  
   875  type AA interface {
   876  	String() string
   877  }
   878  type A struct {
   879  	str string
   880  }
   881  
   882  func main() {
   883  	a := get()
   884  	fmt.Println(a.(*A))
   885  }
   886  func get() AA {
   887  	var a AA
   888  	return a
   889  }
   890  func (a *A) String() string {
   891  	return a.str
   892  }
   893  `)
   894  }
   895  
   896  func TestBlockStmt(t *testing.T) {
   897  	gopClTest(t, `
   898  package main
   899  
   900  func main() {
   901  	{
   902  		type T int
   903  		t := T(100)
   904  		println(t)
   905  	}
   906  	{
   907  		type T string
   908  		t := "hello"
   909  		println(t)
   910  	}
   911  }
   912  `, `package main
   913  
   914  import "fmt"
   915  
   916  func main() {
   917  	{
   918  		type T int
   919  		t := T(100)
   920  		fmt.Println(t)
   921  	}
   922  	{
   923  		type T string
   924  		t := "hello"
   925  		fmt.Println(t)
   926  	}
   927  }
   928  `)
   929  }
   930  
   931  func TestConstTypeConvIssue792(t *testing.T) {
   932  	gopClTest(t, `
   933  const dots = ". . . " + ". . . . . "
   934  const n = uint(len(dots))
   935  `, `package main
   936  
   937  const dots = ". . . " + ". . . . . "
   938  const n = uint(len(dots))
   939  `)
   940  }
   941  
   942  func TestVarInitTwoValueIssue791(t *testing.T) {
   943  	gopClTest(t, `
   944  var (
   945  	m      = map[string]string{"a": "A"}
   946  	a, ok  = m["a"]
   947  )
   948  `, `package main
   949  
   950  var m = map[string]string{"a": "A"}
   951  var a, ok = m["a"]
   952  `)
   953  }
   954  
   955  func TestVarAfterMain(t *testing.T) {
   956  	gopClTest(t, `
   957  package main
   958  
   959  func main() {
   960  	println(i)
   961  }
   962  
   963  var i int
   964  `, `package main
   965  
   966  import "fmt"
   967  
   968  func main() {
   969  	fmt.Println(i)
   970  }
   971  
   972  var i int
   973  `)
   974  	gopClTest(t, `
   975  package main
   976  
   977  func f(v float64) float64 {
   978  	return v
   979  }
   980  func main() {
   981  	sink = f(100)
   982  }
   983  
   984  var sink float64
   985  `, `package main
   986  
   987  func f(v float64) float64 {
   988  	return v
   989  }
   990  func main() {
   991  	sink = f(100)
   992  }
   993  
   994  var sink float64
   995  `)
   996  }
   997  
   998  func TestVarAfterMain2(t *testing.T) {
   999  	gopClTest(t, `
  1000  package main
  1001  
  1002  func main() {
  1003  	println(i)
  1004  }
  1005  
  1006  var i = 100
  1007  `, `package main
  1008  
  1009  import "fmt"
  1010  
  1011  func main() {
  1012  	fmt.Println(i)
  1013  }
  1014  
  1015  var i = 100
  1016  `)
  1017  }
  1018  
  1019  func TestVarInMain(t *testing.T) {
  1020  	gopClTest(t, `
  1021  package main
  1022  
  1023  func main() {
  1024  	v := []uint64{2, 3, 5}
  1025  	var n = len(v)
  1026  	println(n)
  1027  }`, `package main
  1028  
  1029  import "fmt"
  1030  
  1031  func main() {
  1032  	v := []uint64{2, 3, 5}
  1033  	var n = len(v)
  1034  	fmt.Println(n)
  1035  }
  1036  `)
  1037  }
  1038  
  1039  func TestSelect(t *testing.T) {
  1040  	gopClTest(t, `
  1041  
  1042  func consume(xchg chan int) {
  1043  	select {
  1044  	case c := <-xchg:
  1045  		println(c)
  1046  	case xchg <- 1:
  1047  		println("send ok")
  1048  	default:
  1049  		println(0)
  1050  	}
  1051  }
  1052  `, `package main
  1053  
  1054  import "fmt"
  1055  
  1056  func consume(xchg chan int) {
  1057  	select {
  1058  	case c := <-xchg:
  1059  		fmt.Println(c)
  1060  	case xchg <- 1:
  1061  		fmt.Println("send ok")
  1062  	default:
  1063  		fmt.Println(0)
  1064  	}
  1065  }
  1066  `)
  1067  }
  1068  
  1069  func TestTypeSwitch(t *testing.T) {
  1070  	gopClTest(t, `
  1071  
  1072  func bar(p *interface{}) {
  1073  }
  1074  
  1075  func foo(v interface{}) {
  1076  	switch t := v.(type) {
  1077  	case int, string:
  1078  		bar(&v)
  1079  	case bool:
  1080  		var x bool = t
  1081  	default:
  1082  		bar(nil)
  1083  	}
  1084  }
  1085  `, `package main
  1086  
  1087  func bar(p *interface{}) {
  1088  }
  1089  func foo(v interface{}) {
  1090  	switch t := v.(type) {
  1091  	case int, string:
  1092  		bar(&v)
  1093  	case bool:
  1094  		var x bool = t
  1095  	default:
  1096  		bar(nil)
  1097  	}
  1098  }
  1099  `)
  1100  }
  1101  
  1102  func TestTypeSwitch2(t *testing.T) {
  1103  	gopClTest(t, `
  1104  
  1105  func bar(p *interface{}) {
  1106  }
  1107  
  1108  func foo(v interface{}) {
  1109  	switch bar(nil); v.(type) {
  1110  	case int, string:
  1111  		bar(&v)
  1112  	}
  1113  }
  1114  `, `package main
  1115  
  1116  func bar(p *interface{}) {
  1117  }
  1118  func foo(v interface{}) {
  1119  	switch bar(nil); v.(type) {
  1120  	case int, string:
  1121  		bar(&v)
  1122  	}
  1123  }
  1124  `)
  1125  }
  1126  
  1127  func TestTypeAssert(t *testing.T) {
  1128  	gopClTest(t, `
  1129  
  1130  func foo(v interface{}) {
  1131  	x := v.(int)
  1132  	y, ok := v.(string)
  1133  }
  1134  `, `package main
  1135  
  1136  func foo(v interface{}) {
  1137  	x := v.(int)
  1138  	y, ok := v.(string)
  1139  }
  1140  `)
  1141  }
  1142  
  1143  func TestInterface(t *testing.T) {
  1144  	gopClTest(t, `
  1145  
  1146  type Shape interface {
  1147  	Area() float64
  1148  }
  1149  
  1150  func foo(shape Shape) {
  1151  	shape.Area()
  1152  }
  1153  `, `package main
  1154  
  1155  type Shape interface {
  1156  	Area() float64
  1157  }
  1158  
  1159  func foo(shape Shape) {
  1160  	shape.Area()
  1161  }
  1162  `)
  1163  }
  1164  
  1165  func TestInterfaceEmbedded(t *testing.T) {
  1166  	gopClTest(t, `
  1167  type Shape interface {
  1168  	Area() float64
  1169  }
  1170  
  1171  type Bar interface {
  1172  	Shape
  1173  }
  1174  `, `package main
  1175  
  1176  type Shape interface {
  1177  	Area() float64
  1178  }
  1179  type Bar interface {
  1180  	Shape
  1181  }
  1182  `)
  1183  }
  1184  
  1185  func TestInterfaceExample(t *testing.T) {
  1186  	gopClTest(t, `
  1187  type Shape interface {
  1188  	Area() float64
  1189  }
  1190  
  1191  type Rect struct {
  1192  	x, y, w, h float64
  1193  }
  1194  
  1195  func (p *Rect) Area() float64 {
  1196  	return p.w * p.h
  1197  }
  1198  
  1199  type Circle struct {
  1200  	x, y, r float64
  1201  }
  1202  
  1203  func (p *Circle) Area() float64 {
  1204  	return 3.14 * p.r * p.r
  1205  }
  1206  
  1207  func Area(shapes ...Shape) float64 {
  1208  	s := 0.0
  1209  	for shape <- shapes {
  1210  		s += shape.Area()
  1211  	}
  1212  	return s
  1213  }
  1214  
  1215  func main() {
  1216  	rect := &Rect{0, 0, 2, 5}
  1217  	circle := &Circle{0, 0, 3}
  1218  	println(Area(circle, rect))
  1219  }
  1220  `, `package main
  1221  
  1222  import "fmt"
  1223  
  1224  type Shape interface {
  1225  	Area() float64
  1226  }
  1227  type Rect struct {
  1228  	x float64
  1229  	y float64
  1230  	w float64
  1231  	h float64
  1232  }
  1233  type Circle struct {
  1234  	x float64
  1235  	y float64
  1236  	r float64
  1237  }
  1238  
  1239  func (p *Rect) Area() float64 {
  1240  	return p.w * p.h
  1241  }
  1242  func (p *Circle) Area() float64 {
  1243  	return 3.14 * p.r * p.r
  1244  }
  1245  func Area(shapes ...Shape) float64 {
  1246  	s := 0.0
  1247  	for _, shape := range shapes {
  1248  		s += shape.Area()
  1249  	}
  1250  	return s
  1251  }
  1252  func main() {
  1253  	rect := &Rect{0, 0, 2, 5}
  1254  	circle := &Circle{0, 0, 3}
  1255  	fmt.Println(Area(circle, rect))
  1256  }
  1257  `)
  1258  }
  1259  
  1260  func TestEmbeddField(t *testing.T) {
  1261  	gopClTest(t, `import "math/big"
  1262  
  1263  type BigInt struct {
  1264  	*big.Int
  1265  }`, `package main
  1266  
  1267  import "math/big"
  1268  
  1269  type BigInt struct {
  1270  	*big.Int
  1271  }
  1272  `)
  1273  }
  1274  
  1275  func TestAutoProperty(t *testing.T) {
  1276  	gopClTest(t, `import "github.com/goplus/gop/ast/goptest"
  1277  
  1278  func foo(script string) {
  1279  	doc := goptest.New(script)!
  1280  
  1281  	println(doc.any.funcDecl.name)
  1282  	println(doc.any.importSpec.name)
  1283  }
  1284  `, `package main
  1285  
  1286  import (
  1287  	"fmt"
  1288  	"github.com/goplus/gop/ast/gopq"
  1289  	"github.com/goplus/gop/ast/goptest"
  1290  	"github.com/qiniu/x/errors"
  1291  )
  1292  
  1293  func foo(script string) {
  1294  	doc := func() (_gop_ret gopq.NodeSet) {
  1295  		var _gop_err error
  1296  		_gop_ret, _gop_err = goptest.New(script)
  1297  		if _gop_err != nil {
  1298  			_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
  1299  			panic(_gop_err)
  1300  		}
  1301  		return
  1302  	}()
  1303  	fmt.Println(doc.Any().FuncDecl().Name())
  1304  	fmt.Println(doc.Any().ImportSpec().Name())
  1305  }
  1306  `)
  1307  }
  1308  
  1309  func TestSimplifyAutoProperty(t *testing.T) {
  1310  	gopClTest(t, `import "gop/ast/goptest"
  1311  
  1312  func foo(script string) {
  1313  	doc := goptest.New(script)!
  1314  
  1315  	println(doc.any.funcDecl.name)
  1316  	println(doc.any.importSpec.name)
  1317  }
  1318  `, `package main
  1319  
  1320  import (
  1321  	"fmt"
  1322  	"github.com/goplus/gop/ast/gopq"
  1323  	"github.com/goplus/gop/ast/goptest"
  1324  	"github.com/qiniu/x/errors"
  1325  )
  1326  
  1327  func foo(script string) {
  1328  	doc := func() (_gop_ret gopq.NodeSet) {
  1329  		var _gop_err error
  1330  		_gop_ret, _gop_err = goptest.New(script)
  1331  		if _gop_err != nil {
  1332  			_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
  1333  			panic(_gop_err)
  1334  		}
  1335  		return
  1336  	}()
  1337  	fmt.Println(doc.Any().FuncDecl().Name())
  1338  	fmt.Println(doc.Any().ImportSpec().Name())
  1339  }
  1340  `)
  1341  }
  1342  
  1343  func TestErrWrapBasic(t *testing.T) {
  1344  	gopClTest(t, `
  1345  import "strconv"
  1346  
  1347  func add(x, y string) (int, error) {
  1348  	return strconv.Atoi(x)? + strconv.Atoi(y)?, nil
  1349  }
  1350  `, `package main
  1351  
  1352  import (
  1353  	"github.com/qiniu/x/errors"
  1354  	"strconv"
  1355  )
  1356  
  1357  func add(x string, y string) (int, error) {
  1358  	var _autoGo_1 int
  1359  	{
  1360  		var _gop_err error
  1361  		_autoGo_1, _gop_err = strconv.Atoi(x)
  1362  		if _gop_err != nil {
  1363  			_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(x)", "/foo/bar.gop", 5, "main.add")
  1364  			return 0, _gop_err
  1365  		}
  1366  		goto _autoGo_2
  1367  	_autoGo_2:
  1368  	}
  1369  	var _autoGo_3 int
  1370  	{
  1371  		var _gop_err error
  1372  		_autoGo_3, _gop_err = strconv.Atoi(y)
  1373  		if _gop_err != nil {
  1374  			_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(y)", "/foo/bar.gop", 5, "main.add")
  1375  			return 0, _gop_err
  1376  		}
  1377  		goto _autoGo_4
  1378  	_autoGo_4:
  1379  	}
  1380  	return _autoGo_1 + _autoGo_3, nil
  1381  }
  1382  `)
  1383  }
  1384  
  1385  func TestErrWrapDefVal(t *testing.T) {
  1386  	gopClTest(t, `
  1387  import "strconv"
  1388  
  1389  func addSafe(x, y string) int {
  1390  	return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0
  1391  }
  1392  `, `package main
  1393  
  1394  import "strconv"
  1395  
  1396  func addSafe(x string, y string) int {
  1397  	return func() (_gop_ret int) {
  1398  		var _gop_err error
  1399  		_gop_ret, _gop_err = strconv.Atoi(x)
  1400  		if _gop_err != nil {
  1401  			return 0
  1402  		}
  1403  		return
  1404  	}() + func() (_gop_ret int) {
  1405  		var _gop_err error
  1406  		_gop_ret, _gop_err = strconv.Atoi(y)
  1407  		if _gop_err != nil {
  1408  			return 0
  1409  		}
  1410  		return
  1411  	}()
  1412  }
  1413  `)
  1414  }
  1415  
  1416  func TestErrWrapPanic(t *testing.T) {
  1417  	gopClTest(t, `
  1418  var ret int = println("Hi")!
  1419  `, `package main
  1420  
  1421  import (
  1422  	"fmt"
  1423  	"github.com/qiniu/x/errors"
  1424  )
  1425  
  1426  var ret int = func() (_gop_ret int) {
  1427  	var _gop_err error
  1428  	_gop_ret, _gop_err = fmt.Println("Hi")
  1429  	if _gop_err != nil {
  1430  		_gop_err = errors.NewFrame(_gop_err, "println(\"Hi\")", "/foo/bar.gop", 2, "main.main")
  1431  		panic(_gop_err)
  1432  	}
  1433  	return
  1434  }()
  1435  `)
  1436  }
  1437  
  1438  func TestErrWrapCommand(t *testing.T) {
  1439  	gopClTest(t, `
  1440  func mkdir(name string) error {
  1441  	return nil
  1442  }
  1443  
  1444  mkdir! "foo"
  1445  `, `package main
  1446  
  1447  import "github.com/qiniu/x/errors"
  1448  
  1449  func mkdir(name string) error {
  1450  	return nil
  1451  }
  1452  func main() {
  1453  	func() {
  1454  		var _gop_err error
  1455  		_gop_err = mkdir("foo")
  1456  		if _gop_err != nil {
  1457  			_gop_err = errors.NewFrame(_gop_err, "mkdir \"foo\"", "/foo/bar.gop", 6, "main.main")
  1458  			panic(_gop_err)
  1459  		}
  1460  		return
  1461  	}()
  1462  }
  1463  `)
  1464  }
  1465  
  1466  func TestErrWrapCall(t *testing.T) {
  1467  	gopClTest(t, `
  1468  func foo() (func(), error) {
  1469  	return nil, nil
  1470  }
  1471  
  1472  foo()!()
  1473  `, `package main
  1474  
  1475  import "github.com/qiniu/x/errors"
  1476  
  1477  func foo() (func(), error) {
  1478  	return nil, nil
  1479  }
  1480  func main() {
  1481  	func() (_gop_ret func()) {
  1482  		var _gop_err error
  1483  		_gop_ret, _gop_err = foo()
  1484  		if _gop_err != nil {
  1485  			_gop_err = errors.NewFrame(_gop_err, "foo()", "/foo/bar.gop", 6, "main.main")
  1486  			panic(_gop_err)
  1487  		}
  1488  		return
  1489  	}()()
  1490  }
  1491  `)
  1492  }
  1493  
  1494  func TestMakeAndNew(t *testing.T) {
  1495  	gopClTest(t, `
  1496  var a *int = new(int)
  1497  var b map[string]int = make(map[string]int)
  1498  var c []byte = make([]byte, 0, 2)
  1499  `, `package main
  1500  
  1501  var a *int = new(int)
  1502  var b map[string]int = make(map[string]int)
  1503  var c []byte = make([]byte, 0, 2)
  1504  `)
  1505  }
  1506  
  1507  func TestVarDecl(t *testing.T) {
  1508  	gopClTest(t, `
  1509  var a int
  1510  var x, y = 1, "Hi"
  1511  `, `package main
  1512  
  1513  var a int
  1514  var x, y = 1, "Hi"
  1515  `)
  1516  }
  1517  
  1518  func TestUint128Add(t *testing.T) {
  1519  	gopClTest(t, `
  1520  var x, y uint128
  1521  var z uint128 = x + y
  1522  `, `package main
  1523  
  1524  import "github.com/goplus/gop/builtin/ng"
  1525  
  1526  var x, y ng.Uint128
  1527  var z ng.Uint128 = (ng.Uint128).Gop_Add__1(x, y)
  1528  `)
  1529  }
  1530  
  1531  func TestInt128Add(t *testing.T) {
  1532  	gopClTest(t, `
  1533  var x, y int128
  1534  var z int128 = x + y
  1535  `, `package main
  1536  
  1537  import "github.com/goplus/gop/builtin/ng"
  1538  
  1539  var x, y ng.Int128
  1540  var z ng.Int128 = (ng.Int128).Gop_Add__1(x, y)
  1541  `)
  1542  }
  1543  
  1544  func TestBigIntAdd(t *testing.T) {
  1545  	gopClTest(t, `
  1546  var x, y bigint
  1547  var z bigint = x + y
  1548  `, `package main
  1549  
  1550  import "github.com/goplus/gop/builtin/ng"
  1551  
  1552  var x, y ng.Bigint
  1553  var z ng.Bigint = (ng.Bigint).Gop_Add(x, y)
  1554  `)
  1555  }
  1556  
  1557  func TestBigIntLit(t *testing.T) {
  1558  	gopClTest(t, `
  1559  var x = 1r
  1560  `, `package main
  1561  
  1562  import (
  1563  	"github.com/goplus/gop/builtin/ng"
  1564  	"math/big"
  1565  )
  1566  
  1567  var x = ng.Bigint_Init__1(big.NewInt(1))
  1568  `)
  1569  }
  1570  
  1571  func TestUint128Lit(t *testing.T) {
  1572  	gopClTest(t, `
  1573  var x uint128 = 1
  1574  `, `package main
  1575  
  1576  import "github.com/goplus/gop/builtin/ng"
  1577  
  1578  var x ng.Uint128 = ng.Uint128_Init__0(1)
  1579  `)
  1580  }
  1581  
  1582  func TestInt128Lit(t *testing.T) {
  1583  	gopClTest(t, `
  1584  var x int128 = 1
  1585  `, `package main
  1586  
  1587  import "github.com/goplus/gop/builtin/ng"
  1588  
  1589  var x ng.Int128 = ng.Int128_Init__0(1)
  1590  `)
  1591  }
  1592  
  1593  func TestBigRatLit(t *testing.T) {
  1594  	gopClTest(t, `
  1595  var x = 1/2r
  1596  `, `package main
  1597  
  1598  import (
  1599  	"github.com/goplus/gop/builtin/ng"
  1600  	"math/big"
  1601  )
  1602  
  1603  var x = ng.Bigrat_Init__2(big.NewRat(1, 2))
  1604  `)
  1605  }
  1606  
  1607  func TestBigRatLitAdd(t *testing.T) {
  1608  	gopClTest(t, `
  1609  var x = 3 + 1/2r
  1610  `, `package main
  1611  
  1612  import (
  1613  	"github.com/goplus/gop/builtin/ng"
  1614  	"math/big"
  1615  )
  1616  
  1617  var x = ng.Bigrat_Init__2(big.NewRat(7, 2))
  1618  `)
  1619  }
  1620  
  1621  func TestBigRatAdd(t *testing.T) {
  1622  	gogen.SetDebug(gogen.DbgFlagAll)
  1623  	gopClTest(t, `
  1624  var x = 3 + 1/2r
  1625  var y = x + 100
  1626  var z = 100 + y
  1627  `, `package main
  1628  
  1629  import (
  1630  	"github.com/goplus/gop/builtin/ng"
  1631  	"math/big"
  1632  )
  1633  
  1634  var x = ng.Bigrat_Init__2(big.NewRat(7, 2))
  1635  var y = (ng.Bigrat).Gop_Add(x, ng.Bigrat_Init__0(100))
  1636  var z = (ng.Bigrat).Gop_Add(ng.Bigrat_Init__0(100), y)
  1637  `)
  1638  }
  1639  
  1640  func TestTypeConv(t *testing.T) {
  1641  	gopClTest(t, `
  1642  var a = (*struct{})(nil)
  1643  var b = interface{}(nil)
  1644  var c = (func())(nil)
  1645  var x uint32 = uint32(0)
  1646  var y *uint32 = (*uint32)(nil)
  1647  `, `package main
  1648  
  1649  var a = (*struct {
  1650  })(nil)
  1651  var b = interface{}(nil)
  1652  var c = (func())(nil)
  1653  var x uint32 = uint32(0)
  1654  var y *uint32 = (*uint32)(nil)
  1655  `)
  1656  }
  1657  
  1658  func TestStar(t *testing.T) {
  1659  	gopClTest(t, `
  1660  var x *uint32 = (*uint32)(nil)
  1661  var y uint32 = *x
  1662  `, `package main
  1663  
  1664  var x *uint32 = (*uint32)(nil)
  1665  var y uint32 = *x
  1666  `)
  1667  }
  1668  
  1669  func TestLHS(t *testing.T) {
  1670  	gopClTest(t, `
  1671  type T struct {
  1672  	a int
  1673  }
  1674  
  1675  func foo() *T {
  1676  	return nil
  1677  }
  1678  
  1679  foo().a = 123
  1680  `, `package main
  1681  
  1682  type T struct {
  1683  	a int
  1684  }
  1685  
  1686  func foo() *T {
  1687  	return nil
  1688  }
  1689  func main() {
  1690  	foo().a = 123
  1691  }
  1692  `)
  1693  }
  1694  
  1695  func TestSend(t *testing.T) {
  1696  	gopClTest(t, `
  1697  var x chan bool
  1698  x <- true
  1699  `, `package main
  1700  
  1701  var x chan bool
  1702  
  1703  func main() {
  1704  	x <- true
  1705  }
  1706  `)
  1707  }
  1708  
  1709  func TestIncDec(t *testing.T) {
  1710  	gopClTest(t, `
  1711  var x uint32
  1712  x++
  1713  `, `package main
  1714  
  1715  var x uint32
  1716  
  1717  func main() {
  1718  	x++
  1719  }
  1720  `)
  1721  }
  1722  
  1723  func TestAssignOp(t *testing.T) {
  1724  	gopClTest(t, `
  1725  var x uint32
  1726  x += 3
  1727  `, `package main
  1728  
  1729  var x uint32
  1730  
  1731  func main() {
  1732  	x += 3
  1733  }
  1734  `)
  1735  }
  1736  
  1737  func TestBigIntAssignOp(t *testing.T) {
  1738  	gopClTest(t, `
  1739  var x bigint
  1740  x += 3
  1741  `, `package main
  1742  
  1743  import "github.com/goplus/gop/builtin/ng"
  1744  
  1745  var x ng.Bigint
  1746  
  1747  func main() {
  1748  	x.Gop_AddAssign(ng.Bigint_Init__0(3))
  1749  }
  1750  `)
  1751  }
  1752  
  1753  func TestBigIntAssignOp2(t *testing.T) {
  1754  	gopClTest(t, `
  1755  x := 3r
  1756  x *= 2
  1757  `, `package main
  1758  
  1759  import (
  1760  	"github.com/goplus/gop/builtin/ng"
  1761  	"math/big"
  1762  )
  1763  
  1764  func main() {
  1765  	x := ng.Bigint_Init__1(big.NewInt(3))
  1766  	x.Gop_MulAssign(ng.Bigint_Init__0(2))
  1767  }
  1768  `)
  1769  }
  1770  
  1771  func TestBigIntAssignOp3(t *testing.T) {
  1772  	gopClTest(t, `
  1773  x := 3r
  1774  x *= 2r
  1775  `, `package main
  1776  
  1777  import (
  1778  	"github.com/goplus/gop/builtin/ng"
  1779  	"math/big"
  1780  )
  1781  
  1782  func main() {
  1783  	x := ng.Bigint_Init__1(big.NewInt(3))
  1784  	x.Gop_MulAssign(ng.Bigint_Init__1(big.NewInt(2)))
  1785  }
  1786  `)
  1787  }
  1788  
  1789  func TestCompositeLit(t *testing.T) {
  1790  	gopClTest(t, `
  1791  x := []float64{1, 3.4, 5}
  1792  y := map[string]int{"Hello": 1, "Go+": 5}
  1793  z := [...]int{1, 3, 5}
  1794  a := {"Hello": 1, "Go+": 5.1}
  1795  `, `package main
  1796  
  1797  func main() {
  1798  	x := []float64{1, 3.4, 5}
  1799  	y := map[string]int{"Hello": 1, "Go+": 5}
  1800  	z := [...]int{1, 3, 5}
  1801  	a := map[string]float64{"Hello": 1, "Go+": 5.1}
  1802  }
  1803  `)
  1804  }
  1805  
  1806  func TestCompositeLit2(t *testing.T) {
  1807  	gopClTest(t, `
  1808  type foo struct {
  1809  	A int
  1810  }
  1811  
  1812  x := []*struct{a int}{
  1813  	{1}, {3}, {5},
  1814  }
  1815  y := map[foo]struct{a string}{
  1816  	{1}: {"Hi"},
  1817  }
  1818  z := [...]foo{
  1819  	{1}, {3}, {5},
  1820  }
  1821  `, `package main
  1822  
  1823  type foo struct {
  1824  	A int
  1825  }
  1826  
  1827  func main() {
  1828  	x := []*struct {
  1829  		a int
  1830  	}{&struct {
  1831  		a int
  1832  	}{1}, &struct {
  1833  		a int
  1834  	}{3}, &struct {
  1835  		a int
  1836  	}{5}}
  1837  	y := map[foo]struct {
  1838  		a string
  1839  	}{foo{1}: struct {
  1840  		a string
  1841  	}{"Hi"}}
  1842  	z := [...]foo{foo{1}, foo{3}, foo{5}}
  1843  }
  1844  `)
  1845  }
  1846  
  1847  // deduce struct type as parameters of a function call
  1848  func TestCompositeLit3(t *testing.T) {
  1849  	gopClTest(t, `
  1850  type Config struct {
  1851  	A int
  1852  }
  1853  
  1854  func foo(conf *Config) {
  1855  }
  1856  
  1857  func bar(conf ...Config) {
  1858  }
  1859  
  1860  foo({A: 1})
  1861  bar({A: 2})
  1862  foo({})
  1863  bar({})
  1864  `, `package main
  1865  
  1866  type Config struct {
  1867  	A int
  1868  }
  1869  
  1870  func foo(conf *Config) {
  1871  }
  1872  func bar(conf ...Config) {
  1873  }
  1874  func main() {
  1875  	foo(&Config{A: 1})
  1876  	bar(Config{A: 2})
  1877  	foo(&Config{})
  1878  	bar(Config{})
  1879  }
  1880  `)
  1881  }
  1882  
  1883  // deduce struct type as results of a function call
  1884  func TestCompositeLit4(t *testing.T) {
  1885  	gopClTest(t, `
  1886  type Result struct {
  1887  	A int
  1888  }
  1889  
  1890  func foo() *Result {
  1891  	return {A: 1}
  1892  }
  1893  `, `package main
  1894  
  1895  type Result struct {
  1896  	A int
  1897  }
  1898  
  1899  func foo() *Result {
  1900  	return &Result{A: 1}
  1901  }
  1902  `)
  1903  }
  1904  
  1905  func TestCompositeLit5(t *testing.T) {
  1906  	gopClTest(t, `
  1907  type mymap map[float64]string
  1908  var x = {1:"hello", 2:"world"}
  1909  var y map[float64]string = {1:"hello", 2:"world"}
  1910  var z mymap = {1:"hello", 2:"world"}
  1911  `, `package main
  1912  
  1913  type mymap map[float64]string
  1914  
  1915  var x = map[int]string{1: "hello", 2: "world"}
  1916  var y map[float64]string = map[float64]string{1: "hello", 2: "world"}
  1917  var z mymap = mymap{1: "hello", 2: "world"}
  1918  `)
  1919  }
  1920  
  1921  func TestSliceLit(t *testing.T) {
  1922  	gopClTest(t, `
  1923  x := [1, 3.4, 5]
  1924  y := [1]
  1925  z := []
  1926  `, `package main
  1927  
  1928  func main() {
  1929  	x := []float64{1, 3.4, 5}
  1930  	y := []int{1}
  1931  	z := []interface{}{}
  1932  }
  1933  `)
  1934  	gopClTest(t, `
  1935  type vector []float64
  1936  var x = [1, 2, 3]
  1937  var y []float64 = [1, 2, 3]
  1938  var z vector = [1, 2, 3]
  1939  `, `package main
  1940  
  1941  type vector []float64
  1942  
  1943  var x = []int{1, 2, 3}
  1944  var y []float64 = []float64{1, 2, 3}
  1945  var z vector = vector{1, 2, 3}
  1946  `)
  1947  }
  1948  
  1949  func TestChan(t *testing.T) {
  1950  	gopClTest(t, `
  1951  a := make(chan int, 10)
  1952  a <- 3
  1953  var b int = <-a
  1954  x, ok := <-a
  1955  `, `package main
  1956  
  1957  func main() {
  1958  	a := make(chan int, 10)
  1959  	a <- 3
  1960  	var b int = <-a
  1961  	x, ok := <-a
  1962  }
  1963  `)
  1964  }
  1965  
  1966  func TestKeyValModeLit(t *testing.T) {
  1967  	gopClTest(t, `
  1968  a := [...]float64{1, 3: 3.4, 5}
  1969  b := []float64{2: 1.2, 3, 6: 4.5}
  1970  `, `package main
  1971  
  1972  func main() {
  1973  	a := [...]float64{1, 3: 3.4, 5}
  1974  	b := []float64{2: 1.2, 3, 6: 4.5}
  1975  }
  1976  `)
  1977  }
  1978  
  1979  func TestStructLit(t *testing.T) {
  1980  	gopClTest(t, `
  1981  type foo struct {
  1982  	A int
  1983  	B string "tag1:123"
  1984  }
  1985  
  1986  a := struct {
  1987  	A int
  1988  	B string "tag1:123"
  1989  }{1, "Hello"}
  1990  
  1991  b := foo{1, "Hello"}
  1992  c := foo{B: "Hi"}
  1993  `, `package main
  1994  
  1995  type foo struct {
  1996  	A int
  1997  	B string `+"`tag1:123`"+`
  1998  }
  1999  
  2000  func main() {
  2001  	a := struct {
  2002  		A int
  2003  		B string `+"`tag1:123`"+`
  2004  	}{1, "Hello"}
  2005  	b := foo{1, "Hello"}
  2006  	c := foo{B: "Hi"}
  2007  }
  2008  `)
  2009  }
  2010  
  2011  func TestStructType(t *testing.T) {
  2012  	gopClTest(t, `
  2013  type bar = foo
  2014  
  2015  type foo struct {
  2016  	p *bar
  2017  	A int
  2018  	B string "tag1:123"
  2019  }
  2020  
  2021  func main() {
  2022  	type a struct {
  2023  		p *a
  2024  	}
  2025  	type b = a
  2026  }
  2027  `, `package main
  2028  
  2029  type bar = foo
  2030  type foo struct {
  2031  	p *foo
  2032  	A int
  2033  	B string `+"`tag1:123`"+`
  2034  }
  2035  
  2036  func main() {
  2037  	type a struct {
  2038  		p *a
  2039  	}
  2040  	type b = a
  2041  }
  2042  `)
  2043  }
  2044  
  2045  func TestDeferGo(t *testing.T) {
  2046  	gopClTest(t, `
  2047  go println("Hi")
  2048  defer println("Go+")
  2049  `, `package main
  2050  
  2051  import "fmt"
  2052  
  2053  func main() {
  2054  	go fmt.Println("Hi")
  2055  	defer fmt.Println("Go+")
  2056  }
  2057  `)
  2058  }
  2059  
  2060  func TestFor(t *testing.T) {
  2061  	gopClTest(t, `
  2062  a := [1, 3.4, 5]
  2063  for i := 0; i < 3; i=i+1 {
  2064  	println(i)
  2065  }
  2066  for {
  2067  	println("loop")
  2068  }
  2069  `, `package main
  2070  
  2071  import "fmt"
  2072  
  2073  func main() {
  2074  	a := []float64{1, 3.4, 5}
  2075  	for i := 0; i < 3; i = i + 1 {
  2076  		fmt.Println(i)
  2077  	}
  2078  	for {
  2079  		fmt.Println("loop")
  2080  	}
  2081  }
  2082  `)
  2083  }
  2084  
  2085  func TestRangeStmt(t *testing.T) {
  2086  	gopClTest(t, `
  2087  a := [1, 3.4, 5]
  2088  for _, x := range a {
  2089  	println(x)
  2090  }
  2091  for i, x := range a {
  2092  	println(i, x)
  2093  }
  2094  
  2095  var i int
  2096  var x float64
  2097  for _, x = range a {
  2098  	println(i, x)
  2099  }
  2100  for i, x = range a {
  2101  	println(i, x)
  2102  }
  2103  for range a {
  2104  	println("Hi")
  2105  }
  2106  `, `package main
  2107  
  2108  import "fmt"
  2109  
  2110  func main() {
  2111  	a := []float64{1, 3.4, 5}
  2112  	for _, x := range a {
  2113  		fmt.Println(x)
  2114  	}
  2115  	for i, x := range a {
  2116  		fmt.Println(i, x)
  2117  	}
  2118  	var i int
  2119  	var x float64
  2120  	for _, x = range a {
  2121  		fmt.Println(i, x)
  2122  	}
  2123  	for i, x = range a {
  2124  		fmt.Println(i, x)
  2125  	}
  2126  	for range a {
  2127  		fmt.Println("Hi")
  2128  	}
  2129  }
  2130  `)
  2131  }
  2132  
  2133  func TestRangeStmtUDT(t *testing.T) {
  2134  	gopClTest(t, `
  2135  type foo struct {
  2136  }
  2137  
  2138  func (p *foo) Gop_Enum(c func(key int, val string)) {
  2139  }
  2140  
  2141  for k, v := range new(foo) {
  2142  	println(k, v)
  2143  }
  2144  `, `package main
  2145  
  2146  import "fmt"
  2147  
  2148  type foo struct {
  2149  }
  2150  
  2151  func (p *foo) Gop_Enum(c func(key int, val string)) {
  2152  }
  2153  func main() {
  2154  	new(foo).Gop_Enum(func(k int, v string) {
  2155  		fmt.Println(k, v)
  2156  	})
  2157  }
  2158  `)
  2159  }
  2160  
  2161  func TestForPhraseUDT(t *testing.T) {
  2162  	gopClTest(t, `
  2163  type foo struct {
  2164  }
  2165  
  2166  func (p *foo) Gop_Enum(c func(val string)) {
  2167  }
  2168  
  2169  for v <- new(foo) {
  2170  	println(v)
  2171  }
  2172  `, `package main
  2173  
  2174  import "fmt"
  2175  
  2176  type foo struct {
  2177  }
  2178  
  2179  func (p *foo) Gop_Enum(c func(val string)) {
  2180  }
  2181  func main() {
  2182  	new(foo).Gop_Enum(func(v string) {
  2183  		fmt.Println(v)
  2184  	})
  2185  }
  2186  `)
  2187  }
  2188  
  2189  func TestForPhraseUDT2(t *testing.T) {
  2190  	gopClTest(t, `
  2191  type fooIter struct {
  2192  }
  2193  
  2194  func (p fooIter) Next() (key string, val int, ok bool) {
  2195  	return
  2196  }
  2197  
  2198  type foo struct {
  2199  }
  2200  
  2201  func (p *foo) Gop_Enum() fooIter {
  2202  }
  2203  
  2204  for k, v <- new(foo) {
  2205  	println(k, v)
  2206  }
  2207  `, `package main
  2208  
  2209  import "fmt"
  2210  
  2211  type fooIter struct {
  2212  }
  2213  type foo struct {
  2214  }
  2215  
  2216  func (p fooIter) Next() (key string, val int, ok bool) {
  2217  	return
  2218  }
  2219  func (p *foo) Gop_Enum() fooIter {
  2220  }
  2221  func main() {
  2222  	for _gop_it := new(foo).Gop_Enum(); ; {
  2223  		var _gop_ok bool
  2224  		k, v, _gop_ok := _gop_it.Next()
  2225  		if !_gop_ok {
  2226  			break
  2227  		}
  2228  		fmt.Println(k, v)
  2229  	}
  2230  }
  2231  `)
  2232  }
  2233  
  2234  func TestForPhraseUDT3(t *testing.T) {
  2235  	gopClTest(t, `
  2236  type foo struct {
  2237  }
  2238  
  2239  func (p *foo) Gop_Enum(c func(val string)) {
  2240  }
  2241  
  2242  println([v for v <- new(foo)])
  2243  `, `package main
  2244  
  2245  import "fmt"
  2246  
  2247  type foo struct {
  2248  }
  2249  
  2250  func (p *foo) Gop_Enum(c func(val string)) {
  2251  }
  2252  func main() {
  2253  	fmt.Println(func() (_gop_ret []string) {
  2254  		new(foo).Gop_Enum(func(v string) {
  2255  			_gop_ret = append(_gop_ret, v)
  2256  		})
  2257  		return
  2258  	}())
  2259  }
  2260  `)
  2261  }
  2262  
  2263  func TestForPhraseUDT4(t *testing.T) {
  2264  	gopClTest(t, `
  2265  type fooIter struct {
  2266  	data *foo
  2267  	idx  int
  2268  }
  2269  
  2270  func (p *fooIter) Next() (key int, val string, ok bool) {
  2271  	if p.idx < len(p.data.key) {
  2272  		key, val, ok = p.data.key[p.idx], p.data.val[p.idx], true
  2273  		p.idx++
  2274  	}
  2275  	return
  2276  }
  2277  
  2278  type foo struct {
  2279  	key []int
  2280  	val []string
  2281  }
  2282  
  2283  func newFoo() *foo {
  2284  	return &foo{key: [3, 7], val: ["Hi", "Go+"]}
  2285  }
  2286  
  2287  func (p *foo) Gop_Enum() *fooIter {
  2288  	return &fooIter{data: p}
  2289  }
  2290  
  2291  for k, v <- newFoo() {
  2292  	println(k, v)
  2293  }
  2294  `, `package main
  2295  
  2296  import "fmt"
  2297  
  2298  type fooIter struct {
  2299  	data *foo
  2300  	idx  int
  2301  }
  2302  type foo struct {
  2303  	key []int
  2304  	val []string
  2305  }
  2306  
  2307  func (p *fooIter) Next() (key int, val string, ok bool) {
  2308  	if p.idx < len(p.data.key) {
  2309  		key, val, ok = p.data.key[p.idx], p.data.val[p.idx], true
  2310  		p.idx++
  2311  	}
  2312  	return
  2313  }
  2314  func (p *foo) Gop_Enum() *fooIter {
  2315  	return &fooIter{data: p}
  2316  }
  2317  func newFoo() *foo {
  2318  	return &foo{key: []int{3, 7}, val: []string{"Hi", "Go+"}}
  2319  }
  2320  func main() {
  2321  	for _gop_it := newFoo().Gop_Enum(); ; {
  2322  		var _gop_ok bool
  2323  		k, v, _gop_ok := _gop_it.Next()
  2324  		if !_gop_ok {
  2325  			break
  2326  		}
  2327  		fmt.Println(k, v)
  2328  	}
  2329  }
  2330  `)
  2331  }
  2332  
  2333  func TestForPhrase(t *testing.T) {
  2334  	gopClTest(t, `
  2335  sum := 0
  2336  for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 {
  2337  	sum = sum + x
  2338  }
  2339  for i, x <- [1, 3, 5, 7, 11, 13, 17] {
  2340  	sum = sum + i*x
  2341  }
  2342  println("sum(5,7,11,13,17):", sum)
  2343  `, `package main
  2344  
  2345  import "fmt"
  2346  
  2347  func main() {
  2348  	sum := 0
  2349  	for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
  2350  		if x > 3 {
  2351  			sum = sum + x
  2352  		}
  2353  	}
  2354  	for i, x := range []int{1, 3, 5, 7, 11, 13, 17} {
  2355  		sum = sum + i*x
  2356  	}
  2357  	fmt.Println("sum(5,7,11,13,17):", sum)
  2358  }
  2359  `)
  2360  }
  2361  
  2362  func TestMapComprehension(t *testing.T) {
  2363  	gopClTest(t, `
  2364  y := {x: i for i, x <- ["1", "3", "5", "7", "11"]}
  2365  `, `package main
  2366  
  2367  func main() {
  2368  	y := func() (_gop_ret map[string]int) {
  2369  		_gop_ret = map[string]int{}
  2370  		for i, x := range []string{"1", "3", "5", "7", "11"} {
  2371  			_gop_ret[x] = i
  2372  		}
  2373  		return
  2374  	}()
  2375  }
  2376  `)
  2377  }
  2378  
  2379  func TestMapComprehensionCond(t *testing.T) {
  2380  	gopClTest(t, `
  2381  z := {v: k for k, v <- {"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7}, v > 3}
  2382  `, `package main
  2383  
  2384  func main() {
  2385  	z := func() (_gop_ret map[int]string) {
  2386  		_gop_ret = map[int]string{}
  2387  		for k, v := range map[string]int{"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7} {
  2388  			if v > 3 {
  2389  				_gop_ret[v] = k
  2390  			}
  2391  		}
  2392  		return
  2393  	}()
  2394  }
  2395  `)
  2396  }
  2397  
  2398  func TestMapComprehensionCond2(t *testing.T) {
  2399  	gopClTest(t, `
  2400  z := {t: k for k, v <- {"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7}, t := v; t > 3}
  2401  `, `package main
  2402  
  2403  func main() {
  2404  	z := func() (_gop_ret map[int]string) {
  2405  		_gop_ret = map[int]string{}
  2406  		for k, v := range map[string]int{"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7} {
  2407  			if t := v; t > 3 {
  2408  				_gop_ret[t] = k
  2409  			}
  2410  		}
  2411  		return
  2412  	}()
  2413  }
  2414  `)
  2415  }
  2416  
  2417  func TestExistsComprehension(t *testing.T) {
  2418  	gopClTest(t, `
  2419  hasFive := {for x <- ["1", "3", "5", "7", "11"], x == "5"}
  2420  `, `package main
  2421  
  2422  func main() {
  2423  	hasFive := func() (_gop_ok bool) {
  2424  		for _, x := range []string{"1", "3", "5", "7", "11"} {
  2425  			if x == "5" {
  2426  				return true
  2427  			}
  2428  		}
  2429  		return
  2430  	}()
  2431  }
  2432  `)
  2433  }
  2434  
  2435  func TestSelectComprehension(t *testing.T) {
  2436  	gopClTest(t, `
  2437  y := {i for i, x <- ["1", "3", "5", "7", "11"], x == "5"}
  2438  `, `package main
  2439  
  2440  func main() {
  2441  	y := func() (_gop_ret int) {
  2442  		for i, x := range []string{"1", "3", "5", "7", "11"} {
  2443  			if x == "5" {
  2444  				return i
  2445  			}
  2446  		}
  2447  		return
  2448  	}()
  2449  }
  2450  `)
  2451  }
  2452  
  2453  func TestSelectComprehensionTwoValue(t *testing.T) {
  2454  	gopClTest(t, `
  2455  y, ok := {i for i, x <- ["1", "3", "5", "7", "11"], x == "5"}
  2456  `, `package main
  2457  
  2458  func main() {
  2459  	y, ok := func() (_gop_ret int, _gop_ok bool) {
  2460  		for i, x := range []string{"1", "3", "5", "7", "11"} {
  2461  			if x == "5" {
  2462  				return i, true
  2463  			}
  2464  		}
  2465  		return
  2466  	}()
  2467  }
  2468  `)
  2469  }
  2470  
  2471  func TestSelectComprehensionRetTwoValue(t *testing.T) {
  2472  	gopClTest(t, `
  2473  func foo() (int, bool) {
  2474  	return {i for i, x <- ["1", "3", "5", "7", "11"], x == "5"}
  2475  }
  2476  `, `package main
  2477  
  2478  func foo() (int, bool) {
  2479  	return func() (_gop_ret int, _gop_ok bool) {
  2480  		for i, x := range []string{"1", "3", "5", "7", "11"} {
  2481  			if x == "5" {
  2482  				return i, true
  2483  			}
  2484  		}
  2485  		return
  2486  	}()
  2487  }
  2488  `)
  2489  }
  2490  
  2491  func TestListComprehension(t *testing.T) {
  2492  	gopClTest(t, `
  2493  a := [1, 3.4, 5]
  2494  b := [x*x for x <- a]
  2495  `, `package main
  2496  
  2497  func main() {
  2498  	a := []float64{1, 3.4, 5}
  2499  	b := func() (_gop_ret []float64) {
  2500  		for _, x := range a {
  2501  			_gop_ret = append(_gop_ret, x*x)
  2502  		}
  2503  		return
  2504  	}()
  2505  }
  2506  `)
  2507  }
  2508  
  2509  func TestListComprehensionMultiLevel(t *testing.T) {
  2510  	gopClTest(t, `
  2511  arr := [1, 2, 3, 4.1, 5, 6]
  2512  x := [[a, b] for a <- arr, a < b for b <- arr, b > 2]
  2513  println("x:", x)
  2514  `, `package main
  2515  
  2516  import "fmt"
  2517  
  2518  func main() {
  2519  	arr := []float64{1, 2, 3, 4.1, 5, 6}
  2520  	x := func() (_gop_ret [][]float64) {
  2521  		for _, b := range arr {
  2522  			if b > 2 {
  2523  				for _, a := range arr {
  2524  					if a < b {
  2525  						_gop_ret = append(_gop_ret, []float64{a, b})
  2526  					}
  2527  				}
  2528  			}
  2529  		}
  2530  		return
  2531  	}()
  2532  	fmt.Println("x:", x)
  2533  }
  2534  `)
  2535  }
  2536  
  2537  func TestSliceGet(t *testing.T) {
  2538  	gopClTest(t, `
  2539  a := [1, 3, 5, 7, 9]
  2540  b := a[:3]
  2541  c := a[1:]
  2542  d := a[1:2:3]
  2543  e := "Hello, Go+"[7:]
  2544  `, `package main
  2545  
  2546  func main() {
  2547  	a := []int{1, 3, 5, 7, 9}
  2548  	b := a[:3]
  2549  	c := a[1:]
  2550  	d := a[1:2:3]
  2551  	e := "Hello, Go+"[7:]
  2552  }
  2553  `)
  2554  }
  2555  
  2556  func TestIndexGetTwoValue(t *testing.T) {
  2557  	gopClTest(t, `
  2558  a := {"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7}
  2559  x, ok := a["Hi"]
  2560  y := a["Go+"]
  2561  `, `package main
  2562  
  2563  func main() {
  2564  	a := map[string]int{"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7}
  2565  	x, ok := a["Hi"]
  2566  	y := a["Go+"]
  2567  }
  2568  `)
  2569  }
  2570  
  2571  func TestIndexGet(t *testing.T) {
  2572  	gopClTest(t, `
  2573  a := [1, 3.4, 5]
  2574  b := a[1]
  2575  `, `package main
  2576  
  2577  func main() {
  2578  	a := []float64{1, 3.4, 5}
  2579  	b := a[1]
  2580  }
  2581  `)
  2582  }
  2583  
  2584  func TestIndexRef(t *testing.T) {
  2585  	gopClTest(t, `
  2586  a := [1, 3.4, 5]
  2587  a[1] = 2.1
  2588  `, `package main
  2589  
  2590  func main() {
  2591  	a := []float64{1, 3.4, 5}
  2592  	a[1] = 2.1
  2593  }
  2594  `)
  2595  }
  2596  
  2597  func TestIndexArrayPtrIssue784(t *testing.T) {
  2598  	gopClTest(t, `
  2599  type intArr [2]int
  2600  
  2601  func foo(a *intArr) {
  2602  	a[1] = 10
  2603  }
  2604  `, `package main
  2605  
  2606  type intArr [2]int
  2607  
  2608  func foo(a *intArr) {
  2609  	a[1] = 10
  2610  }
  2611  `)
  2612  }
  2613  
  2614  func TestMemberVal(t *testing.T) {
  2615  	gopClTest(t, `import "strings"
  2616  
  2617  x := strings.NewReplacer("?", "!").Replace("hello, world???")
  2618  println("x:", x)
  2619  `, `package main
  2620  
  2621  import (
  2622  	"fmt"
  2623  	"strings"
  2624  )
  2625  
  2626  func main() {
  2627  	x := strings.NewReplacer("?", "!").Replace("hello, world???")
  2628  	fmt.Println("x:", x)
  2629  }
  2630  `)
  2631  }
  2632  
  2633  func TestNamedPtrMemberIssue786(t *testing.T) {
  2634  	gopClTest(t, `
  2635  type foo struct {
  2636  	req int
  2637  }
  2638  
  2639  type pfoo *foo
  2640  
  2641  func bar(p pfoo) {
  2642  	println(p.req)
  2643  }
  2644  `, `package main
  2645  
  2646  import "fmt"
  2647  
  2648  type foo struct {
  2649  	req int
  2650  }
  2651  type pfoo *foo
  2652  
  2653  func bar(p pfoo) {
  2654  	fmt.Println(p.req)
  2655  }
  2656  `)
  2657  }
  2658  
  2659  func TestMember(t *testing.T) {
  2660  	gopClTest(t, `
  2661  
  2662  import "flag"
  2663  
  2664  a := &struct {
  2665  	A int
  2666  	B string
  2667  }{1, "Hello"}
  2668  
  2669  x := a.A
  2670  a.B = "Hi"
  2671  
  2672  flag.Usage = nil
  2673  `, `package main
  2674  
  2675  import "flag"
  2676  
  2677  func main() {
  2678  	a := &struct {
  2679  		A int
  2680  		B string
  2681  	}{1, "Hello"}
  2682  	x := a.A
  2683  	a.B = "Hi"
  2684  	flag.Usage = nil
  2685  }
  2686  `)
  2687  }
  2688  
  2689  func TestElem(t *testing.T) {
  2690  	gopClTest(t, `
  2691  
  2692  func foo(a *int, b int) {
  2693  	b = *a
  2694  	*a = b
  2695  }
  2696  `, `package main
  2697  
  2698  func foo(a *int, b int) {
  2699  	b = *a
  2700  	*a = b
  2701  }
  2702  `)
  2703  }
  2704  
  2705  func TestNamedPtrIssue797(t *testing.T) {
  2706  	gopClTest(t, `
  2707  type Bar *int
  2708  
  2709  func foo(a Bar) {
  2710  	var b int = *a
  2711  }
  2712  `, `package main
  2713  
  2714  type Bar *int
  2715  
  2716  func foo(a Bar) {
  2717  	var b int = *a
  2718  }
  2719  `)
  2720  }
  2721  
  2722  func TestMethod(t *testing.T) {
  2723  	gopClTest(t, `
  2724  type M int
  2725  
  2726  func (m M) Foo() {
  2727  	println("foo", m)
  2728  }
  2729  
  2730  func (M) Bar() {
  2731  	println("bar")
  2732  }
  2733  `, `package main
  2734  
  2735  import "fmt"
  2736  
  2737  type M int
  2738  
  2739  func (m M) Foo() {
  2740  	fmt.Println("foo", m)
  2741  }
  2742  func (M) Bar() {
  2743  	fmt.Println("bar")
  2744  }
  2745  `)
  2746  }
  2747  
  2748  func TestCmdlineNoEOL(t *testing.T) {
  2749  	gopClTest(t, `println "Hi"`, `package main
  2750  
  2751  import "fmt"
  2752  
  2753  func main() {
  2754  	fmt.Println("Hi")
  2755  }
  2756  `)
  2757  }
  2758  
  2759  func TestImport(t *testing.T) {
  2760  	gopClTest(t, `import "fmt"
  2761  
  2762  func main() {
  2763  	fmt.println "Hi"
  2764  }`, `package main
  2765  
  2766  import "fmt"
  2767  
  2768  func main() {
  2769  	fmt.Println("Hi")
  2770  }
  2771  `)
  2772  }
  2773  
  2774  func TestDotImport(t *testing.T) {
  2775  	gopClTest(t, `import . "math"
  2776  
  2777  var a = round(1.2)
  2778  `, `package main
  2779  
  2780  import "math"
  2781  
  2782  var a = math.Round(1.2)
  2783  `)
  2784  }
  2785  
  2786  func TestLocalImport(t *testing.T) {
  2787  	gopClTest(t, `import "./internal/spx"
  2788  
  2789  var a = spx.TestIntValue
  2790  `, `package main
  2791  
  2792  import "github.com/goplus/gop/cl/internal/spx"
  2793  
  2794  var a = spx.TestIntValue
  2795  `)
  2796  }
  2797  
  2798  func TestImportUnused(t *testing.T) {
  2799  	gopClTest(t, `import "fmt"
  2800  
  2801  func main() {
  2802  }`, `package main
  2803  
  2804  func main() {
  2805  }
  2806  `)
  2807  }
  2808  
  2809  func TestImportForceUsed(t *testing.T) {
  2810  	gopClTest(t, `import _ "fmt"
  2811  
  2812  func main() {
  2813  }`, `package main
  2814  
  2815  import _ "fmt"
  2816  
  2817  func main() {
  2818  }
  2819  `)
  2820  }
  2821  
  2822  func TestAnonymousImport(t *testing.T) {
  2823  	gopClTest(t, `println("Hello")
  2824  printf("Hello Go+\n")
  2825  `, `package main
  2826  
  2827  import "fmt"
  2828  
  2829  func main() {
  2830  	fmt.Println("Hello")
  2831  	fmt.Printf("Hello Go+\n")
  2832  }
  2833  `)
  2834  }
  2835  
  2836  func TestVarAndConst(t *testing.T) {
  2837  	gopClTest(t, `
  2838  const (
  2839  	i = 1
  2840  	x float64 = 1
  2841  )
  2842  var j int = i
  2843  `, `package main
  2844  
  2845  const (
  2846  	i         = 1
  2847  	x float64 = 1
  2848  )
  2849  
  2850  var j int = i
  2851  `)
  2852  }
  2853  
  2854  func TestDeclStmt(t *testing.T) {
  2855  	gopClTest(t, `import "fmt"
  2856  
  2857  func main() {
  2858  	const (
  2859  		i = 1
  2860  		x float64 = 1
  2861  	)
  2862  	var j int = i
  2863  	fmt.Println("Hi")
  2864  }`, `package main
  2865  
  2866  import "fmt"
  2867  
  2868  func main() {
  2869  	const (
  2870  		i         = 1
  2871  		x float64 = 1
  2872  	)
  2873  	var j int = i
  2874  	fmt.Println("Hi")
  2875  }
  2876  `)
  2877  }
  2878  
  2879  func TestIf(t *testing.T) {
  2880  	gopClTest(t, `x := 0
  2881  if t := false; t {
  2882  	x = 3
  2883  } else if !t {
  2884  	x = 5
  2885  } else {
  2886  	x = 7
  2887  }
  2888  println("x:", x)
  2889  `, `package main
  2890  
  2891  import "fmt"
  2892  
  2893  func main() {
  2894  	x := 0
  2895  	if t := false; t {
  2896  		x = 3
  2897  	} else if !t {
  2898  		x = 5
  2899  	} else {
  2900  		x = 7
  2901  	}
  2902  	fmt.Println("x:", x)
  2903  }
  2904  `)
  2905  }
  2906  
  2907  func TestSwitch(t *testing.T) {
  2908  	gopClTest(t, `x := 0
  2909  switch s := "Hello"; s {
  2910  default:
  2911  	x = 7
  2912  case "world", "hi":
  2913  	x = 5
  2914  case "xsw":
  2915  	x = 3
  2916  }
  2917  println("x:", x)
  2918  
  2919  v := "Hello"
  2920  switch {
  2921  case v == "xsw":
  2922  	x = 3
  2923  case v == "hi", v == "world":
  2924  	x = 9
  2925  default:
  2926  	x = 11
  2927  }
  2928  println("x:", x)
  2929  `, `package main
  2930  
  2931  import "fmt"
  2932  
  2933  func main() {
  2934  	x := 0
  2935  	switch s := "Hello"; s {
  2936  	default:
  2937  		x = 7
  2938  	case "world", "hi":
  2939  		x = 5
  2940  	case "xsw":
  2941  		x = 3
  2942  	}
  2943  	fmt.Println("x:", x)
  2944  	v := "Hello"
  2945  	switch {
  2946  	case v == "xsw":
  2947  		x = 3
  2948  	case v == "hi", v == "world":
  2949  		x = 9
  2950  	default:
  2951  		x = 11
  2952  	}
  2953  	fmt.Println("x:", x)
  2954  }
  2955  `)
  2956  }
  2957  
  2958  func TestSwitchFallthrough(t *testing.T) {
  2959  	gopClTest(t, `v := "Hello"
  2960  switch v {
  2961  case "Hello":
  2962  	println(v)
  2963  	fallthrough
  2964  case "hi":
  2965  	println(v)
  2966  	fallthrough
  2967  default:
  2968  	println(v)
  2969  }
  2970  `, `package main
  2971  
  2972  import "fmt"
  2973  
  2974  func main() {
  2975  	v := "Hello"
  2976  	switch v {
  2977  	case "Hello":
  2978  		fmt.Println(v)
  2979  		fallthrough
  2980  	case "hi":
  2981  		fmt.Println(v)
  2982  		fallthrough
  2983  	default:
  2984  		fmt.Println(v)
  2985  	}
  2986  }
  2987  `)
  2988  }
  2989  
  2990  func TestBranchStmt(t *testing.T) {
  2991  	gopClTest(t, `
  2992  	a := [1, 3.4, 5]
  2993  label:
  2994  	for i := 0; i < 3; i=i+1 {
  2995  		println(i)
  2996  		break
  2997  		break label
  2998  		continue
  2999  		continue label
  3000  		goto label
  3001  	}
  3002  `, `package main
  3003  
  3004  import "fmt"
  3005  
  3006  func main() {
  3007  	a := []float64{1, 3.4, 5}
  3008  label:
  3009  	for i := 0; i < 3; i = i + 1 {
  3010  		fmt.Println(i)
  3011  		break
  3012  		break label
  3013  		continue
  3014  		continue label
  3015  		goto label
  3016  	}
  3017  }
  3018  `)
  3019  }
  3020  
  3021  func TestReturn(t *testing.T) {
  3022  	gopClTest(t, `
  3023  func foo(format string, args ...interface{}) (int, error) {
  3024  	return printf(format, args...)
  3025  }
  3026  
  3027  func main() {
  3028  }
  3029  `, `package main
  3030  
  3031  import "fmt"
  3032  
  3033  func foo(format string, args ...interface{}) (int, error) {
  3034  	return fmt.Printf(format, args...)
  3035  }
  3036  func main() {
  3037  }
  3038  `)
  3039  }
  3040  
  3041  func TestReturnExpr(t *testing.T) {
  3042  	gopClTest(t, `
  3043  func foo(format string, args ...interface{}) (int, error) {
  3044  	return 0, nil
  3045  }
  3046  
  3047  func main() {
  3048  }
  3049  `, `package main
  3050  
  3051  func foo(format string, args ...interface{}) (int, error) {
  3052  	return 0, nil
  3053  }
  3054  func main() {
  3055  }
  3056  `)
  3057  }
  3058  
  3059  func TestClosure(t *testing.T) {
  3060  	gopClTest(t, `import "fmt"
  3061  
  3062  func(v string) {
  3063  	fmt.Println(v)
  3064  }("Hello")
  3065  `, `package main
  3066  
  3067  import "fmt"
  3068  
  3069  func main() {
  3070  	func(v string) {
  3071  		fmt.Println(v)
  3072  	}("Hello")
  3073  }
  3074  `)
  3075  }
  3076  
  3077  func TestFunc(t *testing.T) {
  3078  	gopClTest(t, `func foo(format string, a [10]int, args ...interface{}) {
  3079  }
  3080  
  3081  func main() {
  3082  }`, `package main
  3083  
  3084  func foo(format string, a [10]int, args ...interface{}) {
  3085  }
  3086  func main() {
  3087  }
  3088  `)
  3089  }
  3090  
  3091  func TestLambdaExpr(t *testing.T) {
  3092  	gopClTest(t, `
  3093  func Map(c []float64, t func(float64) float64) {
  3094  	// ...
  3095  }
  3096  
  3097  func Map2(c []float64, t func(float64) (float64, float64)) {
  3098  	// ...
  3099  }
  3100  
  3101  Map([1.2, 3.5, 6], x => x * x)
  3102  Map2([1.2, 3.5, 6], x => (x * x, x + x))
  3103  `, `package main
  3104  
  3105  func Map(c []float64, t func(float64) float64) {
  3106  }
  3107  func Map2(c []float64, t func(float64) (float64, float64)) {
  3108  }
  3109  func main() {
  3110  	Map([]float64{1.2, 3.5, 6}, func(x float64) float64 {
  3111  		return x * x
  3112  	})
  3113  	Map2([]float64{1.2, 3.5, 6}, func(x float64) (float64, float64) {
  3114  		return x * x, x + x
  3115  	})
  3116  }
  3117  `)
  3118  	gopClTest(t, `type Foo struct {
  3119  	Plot func(x float64) (float64, float64)
  3120  }
  3121  foo := &Foo{
  3122  	Plot: x => (x * 2, x * x),
  3123  }`, `package main
  3124  
  3125  type Foo struct {
  3126  	Plot func(x float64) (float64, float64)
  3127  }
  3128  
  3129  func main() {
  3130  	foo := &Foo{Plot: func(x float64) (float64, float64) {
  3131  		return x * 2, x * x
  3132  	}}
  3133  }
  3134  `)
  3135  	gopClTest(t, `
  3136  type Fn func(x float64) (float64, float64)
  3137  type Foo struct {
  3138  	Plot Fn
  3139  }
  3140  foo := &Foo{
  3141  	Plot: x => (x * 2, x * x),
  3142  }`, `package main
  3143  
  3144  type Fn func(x float64) (float64, float64)
  3145  type Foo struct {
  3146  	Plot Fn
  3147  }
  3148  
  3149  func main() {
  3150  	foo := &Foo{Plot: func(x float64) (float64, float64) {
  3151  		return x * 2, x * x
  3152  	}}
  3153  }
  3154  `)
  3155  	gopClTest(t, `
  3156  type Fn func() (int, error)
  3157  func Do(fn Fn) {
  3158  }
  3159  
  3160  Do => (100, nil)
  3161  `, `package main
  3162  
  3163  type Fn func() (int, error)
  3164  
  3165  func Do(fn Fn) {
  3166  }
  3167  func main() {
  3168  	Do(func() (int, error) {
  3169  		return 100, nil
  3170  	})
  3171  }
  3172  `)
  3173  	gopClTest(t, `
  3174  var fn func(int) (int,error) = x => (x*x, nil)
  3175  `, `package main
  3176  
  3177  var fn func(int) (int, error) = func(x int) (int, error) {
  3178  	return x * x, nil
  3179  }
  3180  `)
  3181  	gopClTest(t, `
  3182  var fn func(int) (int,error)
  3183  fn = x => (x*x, nil)
  3184  `, `package main
  3185  
  3186  var fn func(int) (int, error)
  3187  
  3188  func main() {
  3189  	fn = func(x int) (int, error) {
  3190  		return x * x, nil
  3191  	}
  3192  }
  3193  `)
  3194  }
  3195  
  3196  func TestLambdaExpr2(t *testing.T) {
  3197  	gopClTest(t, `
  3198  func Do(func()) {
  3199  	// ...
  3200  }
  3201  
  3202  Do => {
  3203  	println "Hi"
  3204  }
  3205  `, `package main
  3206  
  3207  import "fmt"
  3208  
  3209  func Do(func()) {
  3210  }
  3211  func main() {
  3212  	Do(func() {
  3213  		fmt.Println("Hi")
  3214  	})
  3215  }
  3216  `)
  3217  	gopClTest(t, `
  3218  func Do(fn func() (int, error)) {
  3219  	// ...
  3220  }
  3221  
  3222  Do => {
  3223  	return 100, nil
  3224  }
  3225  `, `package main
  3226  
  3227  func Do(fn func() (int, error)) {
  3228  }
  3229  func main() {
  3230  	Do(func() (int, error) {
  3231  		return 100, nil
  3232  	})
  3233  }
  3234  `)
  3235  	gopClTest(t, `type Foo struct {
  3236  	Plot func(x float64) (float64, float64)
  3237  }
  3238  foo := &Foo{
  3239  	Plot: x => {
  3240  		return x * 2, x * x
  3241  	},
  3242  }`, `package main
  3243  
  3244  type Foo struct {
  3245  	Plot func(x float64) (float64, float64)
  3246  }
  3247  
  3248  func main() {
  3249  	foo := &Foo{Plot: func(x float64) (float64, float64) {
  3250  		return x * 2, x * x
  3251  	}}
  3252  }
  3253  `)
  3254  	gopClTest(t, `
  3255  type Fn func(x float64) (float64, float64)
  3256  type Foo struct {
  3257  	Plot Fn
  3258  }
  3259  foo := &Foo{
  3260  	Plot: x => {
  3261  		return x * 2, x * x
  3262  	},
  3263  }`, `package main
  3264  
  3265  type Fn func(x float64) (float64, float64)
  3266  type Foo struct {
  3267  	Plot Fn
  3268  }
  3269  
  3270  func main() {
  3271  	foo := &Foo{Plot: func(x float64) (float64, float64) {
  3272  		return x * 2, x * x
  3273  	}}
  3274  }
  3275  `)
  3276  
  3277  	gopClTest(t, `
  3278  type Fn func() (int, error)
  3279  func Do(fn Fn) {
  3280  }
  3281  
  3282  Do => {
  3283  	return 100, nil
  3284  }
  3285  `, `package main
  3286  
  3287  type Fn func() (int, error)
  3288  
  3289  func Do(fn Fn) {
  3290  }
  3291  func main() {
  3292  	Do(func() (int, error) {
  3293  		return 100, nil
  3294  	})
  3295  }
  3296  `)
  3297  	gopClTest(t, `
  3298  var fn func(int) (int,error) = x => {
  3299  	return x * x, nil
  3300  }
  3301  `, `package main
  3302  
  3303  var fn func(int) (int, error) = func(x int) (int, error) {
  3304  	return x * x, nil
  3305  }
  3306  `)
  3307  	gopClTest(t, `
  3308  var fn func(int) (int,error)
  3309  fn = x => {
  3310  	return x * x, nil
  3311  }
  3312  `, `package main
  3313  
  3314  var fn func(int) (int, error)
  3315  
  3316  func main() {
  3317  	fn = func(x int) (int, error) {
  3318  		return x * x, nil
  3319  	}
  3320  }
  3321  `)
  3322  }
  3323  
  3324  func TestLambdaExpr3(t *testing.T) {
  3325  	gopClTest(t, `
  3326  func intSeq() func() int {
  3327  	i := 0
  3328  	return => {
  3329  		i++
  3330  		return i
  3331  	}
  3332  }
  3333  `, `package main
  3334  
  3335  func intSeq() func() int {
  3336  	i := 0
  3337  	return func() int {
  3338  		i++
  3339  		return i
  3340  	}
  3341  }
  3342  `)
  3343  	gopClTest(t, `
  3344  func intDouble() func(int) int {
  3345  	return i => i*2
  3346  }
  3347  `, `package main
  3348  
  3349  func intDouble() func(int) int {
  3350  	return func(i int) int {
  3351  		return i * 2
  3352  	}
  3353  }
  3354  `)
  3355  }
  3356  
  3357  func TestUnnamedMainFunc(t *testing.T) {
  3358  	gopClTest(t, `i := 1`, `package main
  3359  
  3360  func main() {
  3361  	i := 1
  3362  }
  3363  `)
  3364  }
  3365  
  3366  func TestFuncAsParam(t *testing.T) {
  3367  	gopClTest(t, `import "fmt"
  3368  
  3369  func bar(foo func(string, ...interface{}) (int, error)) {
  3370  	foo("Hello, %v!\n", "Go+")
  3371  }
  3372  
  3373  bar(fmt.Printf)
  3374  `, `package main
  3375  
  3376  import "fmt"
  3377  
  3378  func bar(foo func(string, ...interface{}) (int, error)) {
  3379  	foo("Hello, %v!\n", "Go+")
  3380  }
  3381  func main() {
  3382  	bar(fmt.Printf)
  3383  }
  3384  `)
  3385  }
  3386  
  3387  func TestFuncAsParam2(t *testing.T) {
  3388  	gopClTest(t, `import (
  3389  	"fmt"
  3390  	"strings"
  3391  )
  3392  
  3393  func foo(x string) string {
  3394  	return strings.NewReplacer("?", "!").Replace(x)
  3395  }
  3396  
  3397  func printf(format string, args ...interface{}) (n int, err error) {
  3398  	n, err = fmt.Printf(format, args...)
  3399  	return
  3400  }
  3401  
  3402  func bar(foo func(string, ...interface{}) (int, error)) {
  3403  	foo("Hello, %v!\n", "Go+")
  3404  }
  3405  
  3406  bar(printf)
  3407  fmt.Println(foo("Hello, world???"))
  3408  fmt.Println(printf("Hello, %v\n", "Go+"))
  3409  `, `package main
  3410  
  3411  import (
  3412  	"fmt"
  3413  	"strings"
  3414  )
  3415  
  3416  func foo(x string) string {
  3417  	return strings.NewReplacer("?", "!").Replace(x)
  3418  }
  3419  func printf(format string, args ...interface{}) (n int, err error) {
  3420  	n, err = fmt.Printf(format, args...)
  3421  	return
  3422  }
  3423  func bar(foo func(string, ...interface{}) (int, error)) {
  3424  	foo("Hello, %v!\n", "Go+")
  3425  }
  3426  func main() {
  3427  	bar(printf)
  3428  	fmt.Println(foo("Hello, world???"))
  3429  	fmt.Println(printf("Hello, %v\n", "Go+"))
  3430  }
  3431  `)
  3432  }
  3433  
  3434  func TestFuncCall(t *testing.T) {
  3435  	gopClTest(t, `import "fmt"
  3436  
  3437  fmt.Println("Hello")`, `package main
  3438  
  3439  import "fmt"
  3440  
  3441  func main() {
  3442  	fmt.Println("Hello")
  3443  }
  3444  `)
  3445  }
  3446  
  3447  func TestFuncCallEllipsis(t *testing.T) {
  3448  	gopClTest(t, `import "fmt"
  3449  
  3450  func foo(args ...interface{}) {
  3451  	fmt.Println(args...)
  3452  }
  3453  
  3454  func main() {
  3455  }`, `package main
  3456  
  3457  import "fmt"
  3458  
  3459  func foo(args ...interface{}) {
  3460  	fmt.Println(args...)
  3461  }
  3462  func main() {
  3463  }
  3464  `)
  3465  }
  3466  
  3467  func TestFuncCallCodeOrder(t *testing.T) {
  3468  	gopClTest(t, `import "fmt"
  3469  
  3470  func main() {
  3471  	foo("Hello", 123)
  3472  }
  3473  
  3474  func foo(args ...interface{}) {
  3475  	fmt.Println(args...)
  3476  }
  3477  `, `package main
  3478  
  3479  import "fmt"
  3480  
  3481  func main() {
  3482  	foo("Hello", 123)
  3483  }
  3484  func foo(args ...interface{}) {
  3485  	fmt.Println(args...)
  3486  }
  3487  `)
  3488  }
  3489  
  3490  func TestInterfaceMethods(t *testing.T) {
  3491  	gopClTest(t, `package main
  3492  
  3493  func foo(v ...interface { Bar() }) {
  3494  }
  3495  
  3496  func main() {
  3497  }`, `package main
  3498  
  3499  func foo(v ...interface {
  3500  	Bar()
  3501  }) {
  3502  }
  3503  func main() {
  3504  }
  3505  `)
  3506  }
  3507  
  3508  func TestAssignUnderscore(t *testing.T) {
  3509  	gopClTest(t, `import log "fmt"
  3510  
  3511  _, err := log.Println("Hello")
  3512  `, `package main
  3513  
  3514  import "fmt"
  3515  
  3516  func main() {
  3517  	_, err := fmt.Println("Hello")
  3518  }
  3519  `)
  3520  }
  3521  
  3522  func TestOperator(t *testing.T) {
  3523  	gopClTest(t, `
  3524  a := "Hi"
  3525  b := a + "!"
  3526  c := 13
  3527  d := -c
  3528  `, `package main
  3529  
  3530  func main() {
  3531  	a := "Hi"
  3532  	b := a + "!"
  3533  	c := 13
  3534  	d := -c
  3535  }
  3536  `)
  3537  }
  3538  
  3539  var (
  3540  	autogen sync.Mutex
  3541  )
  3542  
  3543  func removeAutogenFiles() {
  3544  	os.Remove("./internal/gop-in-go/foo/gop_autogen.go")
  3545  	os.Remove("./internal/gop-in-go/foo/gop_autogen_test.go")
  3546  	os.Remove("./internal/gop-in-go/foo/gop_autogen2_test.go")
  3547  }
  3548  
  3549  func TestImportGopPkg(t *testing.T) {
  3550  	autogen.Lock()
  3551  	defer autogen.Unlock()
  3552  
  3553  	removeAutogenFiles()
  3554  	gopClTest(t, `import "github.com/goplus/gop/cl/internal/gop-in-go/foo"
  3555  
  3556  rmap := foo.ReverseMap(map[string]int{"Hi": 1, "Hello": 2})
  3557  println(rmap)
  3558  `, `package main
  3559  
  3560  import (
  3561  	"fmt"
  3562  	"github.com/goplus/gop/cl/internal/gop-in-go/foo"
  3563  )
  3564  
  3565  func main() {
  3566  	rmap := foo.ReverseMap(map[string]int{"Hi": 1, "Hello": 2})
  3567  	fmt.Println(rmap)
  3568  }
  3569  `)
  3570  }
  3571  
  3572  func TestCallDep(t *testing.T) {
  3573  	for i := 0; i < 2; i++ {
  3574  		gopClTest(t, `
  3575  import (
  3576  	"reflect"
  3577  	"testing"
  3578  )
  3579  
  3580  func TestNew(t *testing.T) {
  3581  	ret := New()
  3582  	expected := Result{}
  3583  	if reflect.DeepEqual(ret, expected) {
  3584  		t.Fatal("Test failed:", ret, expected)
  3585  	}
  3586  }
  3587  
  3588  type Repo struct {
  3589  	Title string
  3590  }
  3591  
  3592  func newRepo() Repo {
  3593  	return {Title: "Hi"}
  3594  }
  3595  
  3596  type Result struct {
  3597  	Repo Repo
  3598  }
  3599  
  3600  func New() Result {
  3601  	repo := newRepo()
  3602  	return {Repo: repo}
  3603  }
  3604  `, `package main
  3605  
  3606  import (
  3607  	"reflect"
  3608  	"testing"
  3609  )
  3610  
  3611  type Repo struct {
  3612  	Title string
  3613  }
  3614  type Result struct {
  3615  	Repo Repo
  3616  }
  3617  
  3618  func TestNew(t *testing.T) {
  3619  	ret := New()
  3620  	expected := Result{}
  3621  	if reflect.DeepEqual(ret, expected) {
  3622  		t.Fatal("Test failed:", ret, expected)
  3623  	}
  3624  }
  3625  func New() Result {
  3626  	repo := newRepo()
  3627  	return Result{Repo: repo}
  3628  }
  3629  func newRepo() Repo {
  3630  	return Repo{Title: "Hi"}
  3631  }
  3632  `)
  3633  	}
  3634  }
  3635  
  3636  func TestGoFuncInstr(t *testing.T) {
  3637  	gopClTest(t, `package main
  3638  
  3639  //go:noinline
  3640  //go:uintptrescapes
  3641  func test(s string, p, q uintptr, rest ...uintptr) int {
  3642  }`, `package main
  3643  //go:noinline
  3644  //go:uintptrescapes
  3645  func test(s string, p uintptr, q uintptr, rest ...uintptr) int {
  3646  }
  3647  `)
  3648  }
  3649  
  3650  func TestGoTypeInstr(t *testing.T) {
  3651  	gopClTest(t, `package main
  3652  
  3653  //go:notinheap
  3654  type S struct{ x int }
  3655  `, `package main
  3656  //go:notinheap
  3657  type S struct {
  3658  	x int
  3659  }
  3660  `)
  3661  }
  3662  
  3663  func TestNoEntrypoint(t *testing.T) {
  3664  	gopClTest(t, `println("init")
  3665  `, `package main
  3666  
  3667  import "fmt"
  3668  
  3669  func main() {
  3670  	fmt.Println("init")
  3671  }
  3672  `)
  3673  	gopClTestEx(t, gblConf, "bar", `package bar
  3674  println("init")
  3675  `, `package bar
  3676  
  3677  import "fmt"
  3678  
  3679  func init() {
  3680  	fmt.Println("init")
  3681  }
  3682  `)
  3683  }
  3684  
  3685  func TestParentExpr(t *testing.T) {
  3686  	gopClTest(t, `var t1 *(int)
  3687  var t2 chan (int)
  3688  `, `package main
  3689  
  3690  var t1 *int
  3691  var t2 chan int
  3692  `)
  3693  }
  3694  
  3695  func TestCommandStyle(t *testing.T) {
  3696  	gopClTest(t, `
  3697  println []
  3698  println {}
  3699  `, `package main
  3700  
  3701  import "fmt"
  3702  
  3703  func main() {
  3704  	fmt.Println([]interface{}{})
  3705  	fmt.Println(map[string]interface{}{})
  3706  }
  3707  `)
  3708  }
  3709  
  3710  func TestTypeLoader(t *testing.T) {
  3711  	gopClTest(t, `import "fmt"
  3712  
  3713  func (p *Point) String() string {
  3714  	return fmt.Sprintf("%v-%v",p.X,p.Y)
  3715  }
  3716  
  3717  type Point struct {
  3718  	X int
  3719  	Y int
  3720  }
  3721  `, `package main
  3722  
  3723  import "fmt"
  3724  
  3725  type Point struct {
  3726  	X int
  3727  	Y int
  3728  }
  3729  
  3730  func (p *Point) String() string {
  3731  	return fmt.Sprintf("%v-%v", p.X, p.Y)
  3732  }
  3733  `)
  3734  }
  3735  
  3736  func TestCallPrintln(t *testing.T) {
  3737  	gopClTest(t, `
  3738  print
  3739  print "hello"
  3740  print("hello")
  3741  println
  3742  println "hello"
  3743  println("hello")
  3744  `, `package main
  3745  
  3746  import "fmt"
  3747  
  3748  func main() {
  3749  	fmt.Print()
  3750  	fmt.Print("hello")
  3751  	fmt.Print("hello")
  3752  	fmt.Println()
  3753  	fmt.Println("hello")
  3754  	fmt.Println("hello")
  3755  }
  3756  `)
  3757  }
  3758  
  3759  func TestAnyAlias(t *testing.T) {
  3760  	gopClTest(t, `
  3761  var a any = 100
  3762  println(a)
  3763  `, `package main
  3764  
  3765  import "fmt"
  3766  
  3767  var a interface{} = 100
  3768  
  3769  func main() {
  3770  	fmt.Println(a)
  3771  }
  3772  `)
  3773  }
  3774  
  3775  func TestMainEntry(t *testing.T) {
  3776  	conf := *gblConf
  3777  	conf.NoAutoGenMain = false
  3778  
  3779  	gopClTestEx(t, &conf, "main", `
  3780  `, `package main
  3781  
  3782  func main() {
  3783  }
  3784  `)
  3785  	gopClTestEx(t, &conf, "main", `
  3786  func test() {
  3787  	println "hello"
  3788  }
  3789  `, `package main
  3790  
  3791  import "fmt"
  3792  
  3793  func test() {
  3794  	fmt.Println("hello")
  3795  }
  3796  func main() {
  3797  }
  3798  `)
  3799  
  3800  	gopClTestEx(t, &conf, "main", `
  3801  func main() {
  3802  	println "hello"
  3803  }
  3804  `, `package main
  3805  
  3806  import "fmt"
  3807  
  3808  func main() {
  3809  	fmt.Println("hello")
  3810  }
  3811  `)
  3812  }
  3813  
  3814  func TestCommandNotExpr(t *testing.T) {
  3815  	gopClTest(t, `
  3816  println !true
  3817  `, `package main
  3818  
  3819  import "fmt"
  3820  
  3821  func main() {
  3822  	fmt.Println(false)
  3823  }
  3824  `)
  3825  	gopClTest(t, `
  3826  a := true
  3827  println !a
  3828  `, `package main
  3829  
  3830  import "fmt"
  3831  
  3832  func main() {
  3833  	a := true
  3834  	fmt.Println(!a)
  3835  }
  3836  `)
  3837  	gopClTest(t, `
  3838  println !func() bool { return true }()
  3839  `, `package main
  3840  
  3841  import "fmt"
  3842  
  3843  func main() {
  3844  	fmt.Println(!func() bool {
  3845  		return true
  3846  	}())
  3847  }
  3848  `)
  3849  }
  3850  
  3851  func TestCommentLine(t *testing.T) {
  3852  	gopClTestEx(t, gblConfLine, "main", `
  3853  type Point struct {
  3854  	x int
  3855  	y int
  3856  }
  3857  
  3858  func (pt *Point) Test() {
  3859  	println(pt.x, pt.y)
  3860  }
  3861  
  3862  // testPoint is test point
  3863  func testPoint() {
  3864  	var pt Point
  3865  	pt.Test()
  3866  }
  3867  
  3868  println "hello"
  3869  testPoint()
  3870  `, `package main
  3871  
  3872  import "fmt"
  3873  
  3874  type Point struct {
  3875  	x int
  3876  	y int
  3877  }
  3878  //line /foo/bar.gop:7:1
  3879  func (pt *Point) Test() {
  3880  //line /foo/bar.gop:8:1
  3881  	fmt.Println(pt.x, pt.y)
  3882  }
  3883  //line /foo/bar.gop:11:1
  3884  // testPoint is test point
  3885  func testPoint() {
  3886  //line /foo/bar.gop:13:1
  3887  	var pt Point
  3888  //line /foo/bar.gop:14:1
  3889  	pt.Test()
  3890  }
  3891  //line /foo/bar.gop:17
  3892  func main() {
  3893  //line /foo/bar.gop:17:1
  3894  	fmt.Println("hello")
  3895  //line /foo/bar.gop:18:1
  3896  	testPoint()
  3897  }
  3898  `)
  3899  }
  3900  
  3901  func TestCommentLineRoot(t *testing.T) {
  3902  	conf := *gblConf
  3903  	conf.NoFileLine = false
  3904  	conf.RelativeBase = "/foo/root"
  3905  	var src = `
  3906  type Point struct {
  3907  	x int
  3908  	y int
  3909  }
  3910  
  3911  func (pt *Point) Test() {
  3912  	println(pt.x, pt.y)
  3913  }
  3914  
  3915  // testPoint is test point
  3916  func testPoint() {
  3917  	var pt Point
  3918  	pt.Test()
  3919  }
  3920  
  3921  println "hello"
  3922  testPoint()
  3923  `
  3924  	var expected = `package main
  3925  
  3926  import "fmt"
  3927  
  3928  type Point struct {
  3929  	x int
  3930  	y int
  3931  }
  3932  //line ../bar.gop:7:1
  3933  func (pt *Point) Test() {
  3934  //line ../bar.gop:8:1
  3935  	fmt.Println(pt.x, pt.y)
  3936  }
  3937  //line ../bar.gop:11:1
  3938  // testPoint is test point
  3939  func testPoint() {
  3940  //line ../bar.gop:13:1
  3941  	var pt Point
  3942  //line ../bar.gop:14:1
  3943  	pt.Test()
  3944  }
  3945  //line ../bar.gop:17
  3946  func main() {
  3947  //line ../bar.gop:17:1
  3948  	fmt.Println("hello")
  3949  //line ../bar.gop:18:1
  3950  	testPoint()
  3951  }
  3952  `
  3953  	gopClTestEx(t, &conf, "main", src, expected)
  3954  }
  3955  
  3956  func TestRangeScope(t *testing.T) {
  3957  	gopClTest(t, `
  3958  ar := []int{100, 200}
  3959  for k, v := range ar {
  3960  	println(k, v, ar)
  3961  	var k, v, ar int
  3962  	println(ar, k, v)
  3963  }
  3964  `, `package main
  3965  
  3966  import "fmt"
  3967  
  3968  func main() {
  3969  	ar := []int{100, 200}
  3970  	for k, v := range ar {
  3971  		fmt.Println(k, v, ar)
  3972  		var k, v, ar int
  3973  		fmt.Println(ar, k, v)
  3974  	}
  3975  }
  3976  `)
  3977  }
  3978  
  3979  func TestSelectScope(t *testing.T) {
  3980  	gopClTest(t, `
  3981  c1 := make(chan int)
  3982  c2 := make(chan int)
  3983  go func() {
  3984  	c1 <- 100
  3985  }()
  3986  select {
  3987  case i := <-c1:
  3988  	println i
  3989  case i := <-c2:
  3990  	println i
  3991  }
  3992  `, `package main
  3993  
  3994  import "fmt"
  3995  
  3996  func main() {
  3997  	c1 := make(chan int)
  3998  	c2 := make(chan int)
  3999  	go func() {
  4000  		c1 <- 100
  4001  	}()
  4002  	select {
  4003  	case i := <-c1:
  4004  		fmt.Println(i)
  4005  	case i := <-c2:
  4006  		fmt.Println(i)
  4007  	}
  4008  }
  4009  `)
  4010  }
  4011  
  4012  func TestCommentVar(t *testing.T) {
  4013  	gopClTestEx(t, gblConfLine, "main", `
  4014  // doc a line2
  4015  var a int
  4016  println a
  4017  
  4018  // doc b line6
  4019  var b int
  4020  println b
  4021  
  4022  var c int
  4023  println c
  4024  `, `package main
  4025  
  4026  import "fmt"
  4027  // doc a line2
  4028  var a int
  4029  //line /foo/bar.gop:4
  4030  func main() {
  4031  //line /foo/bar.gop:4:1
  4032  	fmt.Println(a)
  4033  //line /foo/bar.gop:6:1
  4034  	// doc b line6
  4035  	var b int
  4036  //line /foo/bar.gop:8:1
  4037  	fmt.Println(b)
  4038  //line /foo/bar.gop:10:1
  4039  	var c int
  4040  //line /foo/bar.gop:11:1
  4041  	fmt.Println(c)
  4042  }
  4043  `)
  4044  
  4045  	gopClTestEx(t, gblConfLine, "main", `
  4046  func demo() {
  4047  	// doc a line3
  4048  	var a int
  4049  	println a
  4050  	
  4051  	// doc b line7
  4052  	var b int
  4053  	println b
  4054  	
  4055  	var c int
  4056  	println c
  4057  }
  4058  `, `package main
  4059  
  4060  import "fmt"
  4061  //line /foo/bar.gop:2:1
  4062  func demo() {
  4063  //line /foo/bar.gop:3:1
  4064  	// doc a line3
  4065  	var a int
  4066  //line /foo/bar.gop:5:1
  4067  	fmt.Println(a)
  4068  //line /foo/bar.gop:7:1
  4069  	// doc b line7
  4070  	var b int
  4071  //line /foo/bar.gop:9:1
  4072  	fmt.Println(b)
  4073  //line /foo/bar.gop:11:1
  4074  	var c int
  4075  //line /foo/bar.gop:12:1
  4076  	fmt.Println(c)
  4077  }
  4078  `)
  4079  }
  4080  
  4081  func TestForPhraseScope(t *testing.T) {
  4082  	gopClTest(t, `sum := 0
  4083  for x <- [1, 3, 5, 7, 11, 13, 17] {
  4084  	sum = sum + x
  4085  	println x
  4086  	x := 200
  4087  	println x
  4088  }`, `package main
  4089  
  4090  import "fmt"
  4091  
  4092  func main() {
  4093  	sum := 0
  4094  	for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
  4095  		sum = sum + x
  4096  		fmt.Println(x)
  4097  		x := 200
  4098  		fmt.Println(x)
  4099  	}
  4100  }
  4101  `)
  4102  	gopClTest(t, `sum := 0
  4103  for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 {
  4104  	sum = sum + x
  4105  	println x
  4106  	x := 200
  4107  	println x
  4108  }`, `package main
  4109  
  4110  import "fmt"
  4111  
  4112  func main() {
  4113  	sum := 0
  4114  	for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
  4115  		if x > 3 {
  4116  			sum = sum + x
  4117  			fmt.Println(x)
  4118  			x := 200
  4119  			fmt.Println(x)
  4120  		}
  4121  	}
  4122  }
  4123  `)
  4124  }
  4125  
  4126  func TestAddress(t *testing.T) {
  4127  	gopClTest(t, `
  4128  type foo struct{ c int }
  4129  
  4130  func (f foo) ptr() *foo { return &f }
  4131  func (f foo) clone() foo { return f }
  4132  
  4133  type nested struct {
  4134  	f foo
  4135  	a [2]foo
  4136  	s []foo
  4137  }
  4138  
  4139  func _() {
  4140  	getNested := func() nested { return nested{} }
  4141  
  4142  	_ = getNested().f.c
  4143  	_ = getNested().a[0].c
  4144  	_ = getNested().s[0].c
  4145  	_ = getNested().f.ptr().c
  4146  	_ = getNested().f.clone().c
  4147  	_ = getNested().f.clone().ptr().c
  4148  }
  4149  `, `package main
  4150  
  4151  type foo struct {
  4152  	c int
  4153  }
  4154  type nested struct {
  4155  	f foo
  4156  	a [2]foo
  4157  	s []foo
  4158  }
  4159  
  4160  func (f foo) ptr() *foo {
  4161  	return &f
  4162  }
  4163  func (f foo) clone() foo {
  4164  	return f
  4165  }
  4166  func _() {
  4167  	getNested := func() nested {
  4168  		return nested{}
  4169  	}
  4170  	_ = getNested().f.c
  4171  	_ = getNested().a[0].c
  4172  	_ = getNested().s[0].c
  4173  	_ = getNested().f.ptr().c
  4174  	_ = getNested().f.clone().c
  4175  	_ = getNested().f.clone().ptr().c
  4176  }
  4177  `)
  4178  }