github.com/HaHadaxigua/yaegi@v1.0.1/interp/interp_test.go (about)

     1  package interp
     2  
     3  import (
     4  	"go/constant"
     5  	"log"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/traefik/yaegi/stdlib"
    10  )
    11  
    12  func init() { log.SetFlags(log.Lshortfile) }
    13  
    14  func TestIsNatural(t *testing.T) {
    15  	tests := []struct {
    16  		desc     string
    17  		n        *node
    18  		expected bool
    19  	}{
    20  		{
    21  			desc: "positive uint var",
    22  			n: &node{
    23  				typ: &itype{
    24  					rtype: func() reflect.Type {
    25  						var a uint = 3
    26  						return reflect.TypeOf(a)
    27  					}(),
    28  				},
    29  				rval: func() reflect.Value {
    30  					var a uint = 3
    31  					return reflect.ValueOf(a)
    32  				}(),
    33  			},
    34  			expected: true,
    35  		},
    36  		{
    37  			desc: "positive untyped var",
    38  			n: &node{
    39  				typ: &itype{
    40  					rtype: func() reflect.Type {
    41  						a := 3
    42  						return reflect.TypeOf(a)
    43  					}(),
    44  				},
    45  				rval: func() reflect.Value {
    46  					a := 3
    47  					return reflect.ValueOf(a)
    48  				}(),
    49  			},
    50  			expected: true,
    51  		},
    52  		{
    53  			desc: "positive int var",
    54  			n: &node{
    55  				typ: &itype{
    56  					rtype: func() reflect.Type {
    57  						var a int = 3
    58  						return reflect.TypeOf(a)
    59  					}(),
    60  				},
    61  				rval: func() reflect.Value {
    62  					var a int = 3
    63  					return reflect.ValueOf(a)
    64  				}(),
    65  			},
    66  			expected: true,
    67  		},
    68  		{
    69  			desc: "positive float var, null decimal",
    70  			n: &node{
    71  				typ: &itype{
    72  					rtype: func() reflect.Type {
    73  						var a float64 = 3.0
    74  						return reflect.TypeOf(a)
    75  					}(),
    76  				},
    77  				rval: func() reflect.Value {
    78  					var a float64 = 3.0
    79  					return reflect.ValueOf(a)
    80  				}(),
    81  			},
    82  			expected: true,
    83  		},
    84  		{
    85  			desc: "positive float var, with decimal",
    86  			n: &node{
    87  				typ: &itype{
    88  					rtype: func() reflect.Type {
    89  						var a float64 = 3.14
    90  						return reflect.TypeOf(a)
    91  					}(),
    92  				},
    93  				rval: func() reflect.Value {
    94  					var a float64 = 3.14
    95  					return reflect.ValueOf(a)
    96  				}(),
    97  			},
    98  			expected: false,
    99  		},
   100  		{
   101  			desc: "negative int var",
   102  			n: &node{
   103  				typ: &itype{
   104  					rtype: func() reflect.Type {
   105  						var a int = -3
   106  						return reflect.TypeOf(a)
   107  					}(),
   108  				},
   109  				rval: func() reflect.Value {
   110  					var a int = -3
   111  					return reflect.ValueOf(a)
   112  				}(),
   113  			},
   114  			expected: false,
   115  		},
   116  		{
   117  			desc: "positive typed const",
   118  			n: &node{
   119  				typ: &itype{
   120  					rtype: func() reflect.Type {
   121  						const a uint = 3
   122  						return reflect.TypeOf(a)
   123  					}(),
   124  				},
   125  				rval: func() reflect.Value {
   126  					const a uint = 3
   127  					return reflect.ValueOf(a)
   128  				}(),
   129  			},
   130  			expected: true,
   131  		},
   132  		{
   133  			desc: "positive untyped const",
   134  			n: &node{
   135  				typ: &itype{
   136  					rtype: func() reflect.Type {
   137  						const a = 3
   138  						return reflect.TypeOf(a)
   139  					}(),
   140  				},
   141  				rval: func() reflect.Value {
   142  					const a = 3
   143  					return reflect.ValueOf(a)
   144  				}(),
   145  			},
   146  			expected: true,
   147  		},
   148  		{
   149  			desc: "positive untyped const (iota)",
   150  			n: &node{
   151  				typ: &itype{
   152  					rtype: func() reflect.Type {
   153  						const (
   154  							zero = iota
   155  							a
   156  						)
   157  						return reflect.TypeOf(a)
   158  					}(),
   159  				},
   160  				rval: func() reflect.Value {
   161  					const (
   162  						zero = iota
   163  						a
   164  					)
   165  					return reflect.ValueOf(a)
   166  				}(),
   167  			},
   168  			expected: true,
   169  		},
   170  		{
   171  			desc: "negative const",
   172  			n: &node{
   173  				typ: &itype{
   174  					rtype: func() reflect.Type {
   175  						const a = -3
   176  						return reflect.TypeOf(a)
   177  					}(),
   178  				},
   179  				rval: func() reflect.Value {
   180  					const a = -3
   181  					return reflect.ValueOf(a)
   182  				}(),
   183  			},
   184  			expected: false,
   185  		},
   186  	}
   187  	for _, test := range tests {
   188  		got := test.n.isNatural()
   189  		if test.expected != got {
   190  			t.Fatalf("%s: got %v, wanted %v", test.desc, got, test.expected)
   191  		}
   192  	}
   193  }
   194  
   195  func TestGlobals(t *testing.T) {
   196  	i := New(Options{})
   197  	if err := i.Use(stdlib.Symbols); err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	if _, err := i.Eval("var a = 1"); err != nil {
   201  		t.Fatal(err)
   202  	}
   203  	if _, err := i.Eval("b := 2"); err != nil {
   204  		t.Fatal(err)
   205  	}
   206  	if _, err := i.Eval("const c = 3"); err != nil {
   207  		t.Fatal(err)
   208  	}
   209  
   210  	g := i.Globals()
   211  	a := g["a"]
   212  	if !a.IsValid() {
   213  		t.Fatal("a not found")
   214  	}
   215  	if a := a.Interface(); a != 1 {
   216  		t.Fatalf("wrong a: want (%[1]T) %[1]v, have (%[2]T) %[2]v", 1, a)
   217  	}
   218  	b := g["b"]
   219  	if !b.IsValid() {
   220  		t.Fatal("b not found")
   221  	}
   222  	if b := b.Interface(); b != 2 {
   223  		t.Fatalf("wrong b: want (%[1]T) %[1]v, have (%[2]T) %[2]v", 2, b)
   224  	}
   225  	c := g["c"]
   226  	if !c.IsValid() {
   227  		t.Fatal("c not found")
   228  	}
   229  	if cc, ok := c.Interface().(constant.Value); ok && constant.MakeInt64(3) != cc {
   230  		t.Fatalf("wrong c: want (%[1]T) %[1]v, have (%[2]T) %[2]v", constant.MakeInt64(3), cc)
   231  	}
   232  }