Skip to content

Commit afdc17d

Browse files
authored
feat: add test
* test:修改测试用例 * fix:修复#6,并且提交基础类型测试
1 parent 42a8d46 commit afdc17d

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ module github.com/songzhibin97/go-ognl
22

33
go 1.16
44

5-
require github.com/stretchr/testify v1.8.0
5+
require (
6+
github.com/google/go-cmp v0.5.9 // indirect
7+
github.com/pkg/errors v0.9.1 // indirect
8+
github.com/smartystreets/goconvey v1.7.2
9+
github.com/stretchr/testify v1.8.0
10+
gotest.tools v2.2.0+incompatible
11+
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
5+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
7+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
8+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
9+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
10+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
11+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
412
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
513
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14+
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
15+
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
16+
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
17+
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
618
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
719
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
820
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
921
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
1022
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
23+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
24+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
25+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
26+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
27+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
1128
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1229
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1330
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1431
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1532
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33+
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
34+
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

ognl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ const (
6161

6262
func (t Type) String() string {
6363
switch t {
64+
case Func:
65+
return "func"
66+
case Chan:
67+
return "chan"
6468
case Pointer:
6569
return "pointer"
6670
case Interface:

ognl_test.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package ognl
22

33
import (
4+
"fmt"
5+
"reflect"
46
"testing"
7+
"unsafe"
58

69
"github.com/stretchr/testify/assert"
710
)
@@ -255,3 +258,153 @@ func TestRepetition2(t *testing.T) {
255258
vv := Get(r, "repetition").Value()
256259
assert.Equal(t, vv, "r2")
257260
}
261+
262+
// TestBaseGet 测试基础类型
263+
func TestBaseType(t *testing.T) {
264+
p := 1
265+
mock := struct {
266+
Bool_t bool
267+
bool_t bool
268+
Int_t int
269+
int_t int
270+
Int8_t int8
271+
int8_t int8
272+
Int16_t int16
273+
int16_t int16
274+
Int32_t int32
275+
int32_t int32
276+
Int64_t int64
277+
int64_t int64
278+
Uint_t uint
279+
uint_t uint
280+
Uint8_t uint8
281+
uint8_t uint8
282+
Uint16_t uint16
283+
uint16_t uint16
284+
Uint32_t uint32
285+
uint32_t uint32
286+
Uint64_t uint64
287+
uint64_t uint64
288+
Uintptr_t uintptr
289+
uintptr_t uintptr
290+
Float32_t float32
291+
float32_t float32
292+
Float64_t float64
293+
float64_t float64
294+
Complex64_t complex64
295+
complex64_t complex64
296+
Complex128_t complex128
297+
complex128_t complex128
298+
Array_t [1]int
299+
array_t [1]int
300+
Chan_t chan string
301+
chan_t chan string
302+
Func_t func()
303+
func_t func()
304+
Interface_t interface{}
305+
interface_t interface{}
306+
Map_t map[string]interface{}
307+
map_t map[string]interface{}
308+
Pointer_t *int
309+
pointer_t *int
310+
Slice_t []interface{}
311+
slice_t []interface{}
312+
String_t string
313+
string_t string
314+
Struct_t struct{}
315+
struct_t struct{}
316+
UnsafePointer_t unsafe.Pointer
317+
unsafePointer_t unsafe.Pointer
318+
Byte_t byte
319+
byte_t byte
320+
Rune_t rune
321+
rune_t rune
322+
}{
323+
Bool_t: false,
324+
bool_t: true,
325+
Int_t: 13,
326+
int_t: 14,
327+
Int8_t: 15,
328+
int8_t: 16,
329+
Int16_t: 17,
330+
int16_t: 18,
331+
Int32_t: 19,
332+
int32_t: 20,
333+
Int64_t: 21,
334+
int64_t: 22,
335+
Uint_t: 3,
336+
uint_t: 4,
337+
Uint8_t: 5,
338+
uint8_t: 6,
339+
Uint16_t: 7,
340+
uint16_t: 8,
341+
Uint32_t: 9,
342+
uint32_t: 10,
343+
Uint64_t: 11,
344+
uint64_t: 12,
345+
Uintptr_t: 31,
346+
uintptr_t: 32,
347+
Float32_t: 23,
348+
float32_t: 23.1,
349+
Float64_t: 24,
350+
float64_t: 24.1,
351+
Complex64_t: 25,
352+
complex64_t: 25.1,
353+
Complex128_t: 26,
354+
complex128_t: 26.1,
355+
Array_t: [1]int{1},
356+
array_t: [1]int{1},
357+
Chan_t: make(chan string, 5),
358+
chan_t: make(chan string, 5),
359+
Func_t: func() { fmt.Println("666") },
360+
func_t: func() { fmt.Println("666") },
361+
Interface_t: `1`,
362+
interface_t: `2`,
363+
Map_t: map[string]interface{}{"1": 1},
364+
map_t: map[string]interface{}{"1": 1},
365+
Pointer_t: nil,
366+
pointer_t: nil,
367+
Slice_t: []interface{}{1, "2", "3"},
368+
slice_t: []interface{}{1, "2", "3"},
369+
String_t: "1",
370+
string_t: "2",
371+
Struct_t: struct{}{},
372+
struct_t: struct{}{},
373+
UnsafePointer_t: unsafe.Pointer(&p),
374+
unsafePointer_t: unsafe.Pointer(&p),
375+
Byte_t: byte(27),
376+
byte_t: byte(28),
377+
Rune_t: rune(29),
378+
rune_t: rune(30),
379+
}
380+
381+
mokeValue := reflect.ValueOf(mock)
382+
mokeType := reflect.TypeOf(mock)
383+
mokeField := mokeValue.NumField()
384+
385+
for i := 0; i < mokeField; i++ {
386+
structField := mokeType.Field(i)
387+
388+
cp := reflect.New(mokeValue.Type()).Elem()
389+
cp.Set(mokeValue)
390+
rv := cp.FieldByName(structField.Name)
391+
res := reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Interface()
392+
393+
// 获取基本数据value
394+
value := Get(mock, structField.Name).Value()
395+
if rv.Kind() == reflect.Func {
396+
v1 := reflect.ValueOf(res)
397+
v2 := reflect.ValueOf(value)
398+
if !(v1.Pointer() == v2.Pointer()) {
399+
t.Fatalf("%s: incorrect response; want: %d, got: %d", structField.Name, v1.Pointer(), v2.Pointer())
400+
}
401+
} else {
402+
assert.Equal(t, res, value)
403+
}
404+
405+
// 获取基本数据Type
406+
Get(mock, structField.Name).Type()
407+
tp := Get(mock, structField.Name).Type()
408+
assert.Equal(t, int(rv.Kind()), int(tp))
409+
}
410+
}

0 commit comments

Comments
 (0)