github.com/switchupcb/yaegi@v0.10.2/interp/interp_test.go (about) 1 package interp 2 3 import ( 4 "log" 5 "reflect" 6 "testing" 7 ) 8 9 func init() { log.SetFlags(log.Lshortfile) } 10 11 func TestIsNatural(t *testing.T) { 12 tests := []struct { 13 desc string 14 n *node 15 expected bool 16 }{ 17 { 18 desc: "positive uint var", 19 n: &node{ 20 typ: &itype{ 21 rtype: func() reflect.Type { 22 var x uint = 3 23 return reflect.TypeOf(x) 24 }(), 25 }, 26 rval: func() reflect.Value { 27 var x uint = 3 28 return reflect.ValueOf(x) 29 }(), 30 }, 31 expected: true, 32 }, 33 { 34 desc: "positive untyped var", 35 n: &node{ 36 typ: &itype{ 37 rtype: func() reflect.Type { 38 x := 3 39 return reflect.TypeOf(x) 40 }(), 41 }, 42 rval: func() reflect.Value { 43 x := 3 44 return reflect.ValueOf(x) 45 }(), 46 }, 47 expected: true, 48 }, 49 { 50 desc: "positive int var", 51 n: &node{ 52 typ: &itype{ 53 rtype: func() reflect.Type { 54 var x int = 3 55 return reflect.TypeOf(x) 56 }(), 57 }, 58 rval: func() reflect.Value { 59 var x int = 3 60 return reflect.ValueOf(x) 61 }(), 62 }, 63 expected: true, 64 }, 65 { 66 desc: "positive float var, null decimal", 67 n: &node{ 68 typ: &itype{ 69 rtype: func() reflect.Type { 70 var x float64 = 3.0 71 return reflect.TypeOf(x) 72 }(), 73 }, 74 rval: func() reflect.Value { 75 var x float64 = 3.0 76 return reflect.ValueOf(x) 77 }(), 78 }, 79 expected: true, 80 }, 81 { 82 desc: "positive float var, with decimal", 83 n: &node{ 84 typ: &itype{ 85 rtype: func() reflect.Type { 86 var x float64 = 3.14 87 return reflect.TypeOf(x) 88 }(), 89 }, 90 rval: func() reflect.Value { 91 var x float64 = 3.14 92 return reflect.ValueOf(x) 93 }(), 94 }, 95 expected: false, 96 }, 97 { 98 desc: "negative int var", 99 n: &node{ 100 typ: &itype{ 101 rtype: func() reflect.Type { 102 var x int = -3 103 return reflect.TypeOf(x) 104 }(), 105 }, 106 rval: func() reflect.Value { 107 var x int = -3 108 return reflect.ValueOf(x) 109 }(), 110 }, 111 expected: false, 112 }, 113 { 114 desc: "positive typed const", 115 n: &node{ 116 typ: &itype{ 117 rtype: func() reflect.Type { 118 const a uint = 3 119 return reflect.TypeOf(a) 120 }(), 121 }, 122 rval: func() reflect.Value { 123 const a uint = 3 124 return reflect.ValueOf(a) 125 }(), 126 }, 127 expected: true, 128 }, 129 { 130 desc: "positive untyped const", 131 n: &node{ 132 typ: &itype{ 133 rtype: func() reflect.Type { 134 const a = 3 135 return reflect.TypeOf(a) 136 }(), 137 }, 138 rval: func() reflect.Value { 139 const a = 3 140 return reflect.ValueOf(a) 141 }(), 142 }, 143 expected: true, 144 }, 145 { 146 desc: "positive untyped const (iota)", 147 n: &node{ 148 typ: &itype{ 149 rtype: func() reflect.Type { 150 const ( 151 zero = iota 152 a 153 ) 154 return reflect.TypeOf(a) 155 }(), 156 }, 157 rval: func() reflect.Value { 158 const ( 159 zero = iota 160 a 161 ) 162 return reflect.ValueOf(a) 163 }(), 164 }, 165 expected: true, 166 }, 167 { 168 desc: "negative const", 169 n: &node{ 170 typ: &itype{ 171 rtype: func() reflect.Type { 172 const a = -3 173 return reflect.TypeOf(a) 174 }(), 175 }, 176 rval: func() reflect.Value { 177 const a = -3 178 return reflect.ValueOf(a) 179 }(), 180 }, 181 expected: false, 182 }, 183 } 184 for _, test := range tests { 185 got := test.n.isNatural() 186 if test.expected != got { 187 t.Fatalf("%s: got %v, wanted %v", test.desc, got, test.expected) 188 } 189 } 190 }