github.com/goplus/igop@v0.25.0/repl_test.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package igop_test
    18  
    19  import (
    20  	"fmt"
    21  	"go/token"
    22  	"testing"
    23  
    24  	"github.com/goplus/igop"
    25  	_ "github.com/goplus/igop/pkg/fmt"
    26  )
    27  
    28  func TestReplExpr(t *testing.T) {
    29  	ctx := igop.NewContext(0)
    30  	repl := igop.NewRepl(ctx)
    31  	list := []string{
    32  		`a := 1`,
    33  		`b := 2`,
    34  		`a+b`,
    35  	}
    36  	result := []string{
    37  		`[]`,
    38  		`[]`,
    39  		`[3 int]`,
    40  	}
    41  	for i, expr := range list {
    42  		_, v, err := repl.Eval(expr)
    43  		if err != nil {
    44  			t.Fatal(err)
    45  		}
    46  		if fmt.Sprint(v) != result[i] {
    47  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
    48  		}
    49  	}
    50  }
    51  
    52  func TestReplImports(t *testing.T) {
    53  	ctx := igop.NewContext(0)
    54  	repl := igop.NewRepl(ctx)
    55  	list := []string{
    56  		`a := 1`,
    57  		`b := 2`,
    58  		`import "fmt"`,
    59  		`c := fmt.Sprintf("%v-%v",a,b)`,
    60  		`c`,
    61  	}
    62  	result := []string{
    63  		`[]`,
    64  		`[]`,
    65  		`[]`,
    66  		`[]`,
    67  		`[1-2 string]`,
    68  	}
    69  	for i, expr := range list {
    70  		_, v, err := repl.Eval(expr)
    71  		if err != nil {
    72  			t.Fatal(err)
    73  		}
    74  		if fmt.Sprint(v) != result[i] {
    75  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
    76  		}
    77  	}
    78  }
    79  
    80  func TestReplClosure(t *testing.T) {
    81  	ctx := igop.NewContext(0)
    82  	repl := igop.NewRepl(ctx)
    83  	list := []string{
    84  		`a := 1`,
    85  		`b := 2`,
    86  		`fn := func() int { return a }`,
    87  		`d := fn()+b`,
    88  		`d`,
    89  	}
    90  	result := []string{
    91  		`[]`,
    92  		`[]`,
    93  		`[]`,
    94  		`[]`,
    95  		`[3 int]`,
    96  	}
    97  	for i, expr := range list {
    98  		_, v, err := repl.Eval(expr)
    99  		if err != nil {
   100  			t.Fatal(err)
   101  		}
   102  		if result[i] == "-" {
   103  			continue
   104  		}
   105  		if fmt.Sprint(v) != result[i] {
   106  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   107  		}
   108  	}
   109  }
   110  
   111  func TestReplVar(t *testing.T) {
   112  	ctx := igop.NewContext(0)
   113  	repl := igop.NewRepl(ctx)
   114  	list := []string{
   115  		`a := 1`,
   116  		`b := 2`,
   117  		`var c = 100`,
   118  		`fn := func() int { return a+c }`,
   119  		`d := fn()+b`,
   120  		`d`,
   121  		`c`,
   122  	}
   123  	result := []string{
   124  		`[]`,
   125  		`[]`,
   126  		`[]`,
   127  		`[]`,
   128  		`[]`,
   129  		`[103 int]`,
   130  		`[100 int]`,
   131  	}
   132  	for i, expr := range list {
   133  		_, v, err := repl.Eval(expr)
   134  		if err != nil {
   135  			t.Fatal(err)
   136  		}
   137  		if result[i] == "-" {
   138  			continue
   139  		}
   140  		if fmt.Sprint(v) != result[i] {
   141  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   142  		}
   143  	}
   144  }
   145  
   146  func TestReplType(t *testing.T) {
   147  	ctx := igop.NewContext(0)
   148  	repl := igop.NewRepl(ctx)
   149  	list := []string{
   150  		`type T struct {
   151  	X int
   152  	Y int
   153  }`,
   154  		`v1 := &T{10,20}`,
   155  		`import "fmt"`,
   156  		`v1`,
   157  		`func (t *T) String() string {
   158  	return fmt.Sprintf("%v-%v",t.X,t.Y)
   159  }`,
   160  		`v2 := &T{10,20}`,
   161  		`v2`,
   162  	}
   163  	result := []string{
   164  		`-`,
   165  		`-`,
   166  		`-`,
   167  		`[&{10 20} *main.T]`,
   168  		`-`,
   169  		`-`,
   170  		`[10-20 *main.T]`,
   171  	}
   172  	for i, expr := range list {
   173  		_, v, err := repl.Eval(expr)
   174  		if err != nil {
   175  			t.Fatal(err)
   176  		}
   177  		if result[i] == "-" {
   178  			continue
   179  		}
   180  		if fmt.Sprint(v) != result[i] {
   181  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   182  		}
   183  	}
   184  }
   185  
   186  func TestReplFunc(t *testing.T) {
   187  	ctx := igop.NewContext(0)
   188  	repl := igop.NewRepl(ctx)
   189  	list := []string{
   190  		`a := "hello"`,
   191  		`import "fmt"`,
   192  		`a`,
   193  		`fmt.Println(a)`,
   194  		`s := fmt.Sprint(a)`,
   195  		`s`,
   196  		`fmt.Println(s)`,
   197  	}
   198  	result := []string{
   199  		`[]`,
   200  		`-`,
   201  		`[hello string]`,
   202  		`[6 int <nil> error]`,
   203  		`-`,
   204  		`[hello string]`,
   205  		`[6 int <nil> error]`,
   206  	}
   207  	for i, expr := range list {
   208  		_, v, err := repl.Eval(expr)
   209  		if err != nil {
   210  			t.Fatal(err)
   211  		}
   212  		if result[i] == "-" {
   213  			continue
   214  		}
   215  		if fmt.Sprint(v) != result[i] {
   216  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   217  		}
   218  	}
   219  }
   220  
   221  func TestReplTok(t *testing.T) {
   222  	ctx := igop.NewContext(0)
   223  	repl := igop.NewRepl(ctx)
   224  	list := []string{
   225  		`a := "hello"`,
   226  		`import "fmt"`,
   227  		`var c int`,
   228  		`const d = 100`,
   229  		`func test(v int) {}`,
   230  		`fmt.Println(a)`,
   231  		`type T int`,
   232  	}
   233  	toks := []token.Token{
   234  		token.IDENT,
   235  		token.IMPORT,
   236  		token.VAR,
   237  		token.CONST,
   238  		token.FUNC,
   239  		token.IDENT,
   240  		token.TYPE,
   241  	}
   242  	for i, expr := range list {
   243  		tok, _, err := repl.Eval(expr)
   244  		if err != nil {
   245  			t.Fatal(err)
   246  		}
   247  		if tok != toks[i] {
   248  			t.Fatalf("expr:%v token:%v", expr, tok)
   249  		}
   250  	}
   251  }
   252  
   253  func TestReplInit(t *testing.T) {
   254  	ctx := igop.NewContext(0)
   255  	repl := igop.NewRepl(ctx)
   256  	list := []string{
   257  		`var i int = 10`,
   258  		`var j int = 20`,
   259  		`func init() { i++ }`,
   260  		`func init() { j++ }`,
   261  		`var c int`,
   262  		`i`,
   263  		`j`,
   264  	}
   265  	result := []string{
   266  		`-`,
   267  		`-`,
   268  		`-`,
   269  		`-`,
   270  		`-`,
   271  		`[11 int]`,
   272  		`[21 int]`,
   273  	}
   274  	for i, expr := range list {
   275  		_, v, err := repl.Eval(expr)
   276  		if err != nil {
   277  			t.Fatal(err)
   278  		}
   279  		if result[i] == "-" {
   280  			continue
   281  		}
   282  		if fmt.Sprint(v) != result[i] {
   283  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   284  		}
   285  	}
   286  }
   287  
   288  func TestReplFunLit(t *testing.T) {
   289  	ctx := igop.NewContext(0)
   290  	repl := igop.NewRepl(ctx)
   291  	list := []string{
   292  		`var a int`,
   293  		`func(x int){ a = x }(100)`,
   294  		`a`,
   295  	}
   296  	result := []string{
   297  		`-`,
   298  		`-`,
   299  		`[100 int]`,
   300  	}
   301  	for i, expr := range list {
   302  		_, v, err := repl.Eval(expr)
   303  		if err != nil {
   304  			t.Fatal(err)
   305  		}
   306  		if result[i] == "-" {
   307  			continue
   308  		}
   309  		if fmt.Sprint(v) != result[i] {
   310  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   311  		}
   312  	}
   313  }
   314  
   315  func TestReplFor(t *testing.T) {
   316  	ctx := igop.NewContext(0)
   317  	repl := igop.NewRepl(ctx)
   318  	list := []string{
   319  		`import "fmt"`,
   320  		`for i := 0; i < 3; i++ { fmt.Println(i) }`,
   321  		`fmt.Println(100)`,
   322  	}
   323  	for _, expr := range list {
   324  		_, _, err := repl.Eval(expr)
   325  		if err != nil {
   326  			t.Fatal(err)
   327  		}
   328  	}
   329  }
   330  
   331  func TestReplIf(t *testing.T) {
   332  	ctx := igop.NewContext(0)
   333  	repl := igop.NewRepl(ctx)
   334  	list := []string{
   335  		`import "fmt"`,
   336  		`a := 2`,
   337  		`if a == 1 { fmt.Println(1) } else { fmt.Println(a) }`,
   338  		`fmt.Println(100)`,
   339  	}
   340  	for _, expr := range list {
   341  		_, _, err := repl.Eval(expr)
   342  		if err != nil {
   343  			t.Fatal(err)
   344  		}
   345  	}
   346  }
   347  
   348  func TestReplSwitch(t *testing.T) {
   349  	ctx := igop.NewContext(0)
   350  	repl := igop.NewRepl(ctx)
   351  	list := []string{
   352  		`import "fmt"`,
   353  		`a := 2`,
   354  		`switch a {
   355  		case 1: 
   356  			if a == 1 { fmt.Println(1) }
   357  		default: 
   358  			fmt.Println(a)
   359  		}`,
   360  		`fmt.Println(100)`,
   361  	}
   362  	for _, expr := range list {
   363  		_, _, err := repl.Eval(expr)
   364  		if err != nil {
   365  			t.Fatal(err)
   366  		}
   367  	}
   368  }
   369  
   370  func TestReplSelect(t *testing.T) {
   371  	ctx := igop.NewContext(0)
   372  	repl := igop.NewRepl(ctx)
   373  	list := []string{
   374  		`import "fmt"`,
   375  		`a := 1`,
   376  		`ch := make(chan int)`,
   377  		`select {
   378  		case ch <- 1:
   379  			if a == 1 { fmt.Println(1) }
   380  			fmt.Println(1)
   381  		default:
   382  			fmt.Println(ch)
   383  		}`,
   384  		`fmt.Println(100)`,
   385  	}
   386  	for _, expr := range list {
   387  		_, _, err := repl.Eval(expr)
   388  		if err != nil {
   389  			t.Fatal(err)
   390  		}
   391  	}
   392  }
   393  
   394  func TestReplLiteral(t *testing.T) {
   395  	ctx := igop.NewContext(0)
   396  	repl := igop.NewRepl(ctx)
   397  	list := []string{
   398  		`100`,
   399  		`100.0*200`,
   400  		`(100*200)`,
   401  		`map[int]string{1:"100"}`,
   402  		`1e100<<2`,
   403  		`+100*2`,
   404  		`-100/2`,
   405  		`^100`,
   406  		`"hello"+"world"`,
   407  	}
   408  	result := []string{
   409  		`[100 int]`,
   410  		`[20000 float64]`,
   411  		`[20000 int]`,
   412  		`[map[1:100] map[int]string]`,
   413  		`[40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 untyped int]`,
   414  		`[200 int]`,
   415  		`[-50 int]`,
   416  		`[-101 int]`,
   417  		`[helloworld string]`,
   418  	}
   419  	for i, expr := range list {
   420  		_, v, err := repl.Eval(expr)
   421  		if err != nil {
   422  			t.Fatal(err)
   423  		}
   424  		if fmt.Sprint(v) != result[i] {
   425  			t.Fatalf("expr:%v dump:%v src:%v", expr, v, repl.Source())
   426  		}
   427  	}
   428  }