github.com/sandwich-go/boost@v1.3.29/misc/annotation/annotation_test.go (about)

     1  package annotation
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/smartystreets/goconvey/convey"
     7  )
     8  
     9  func TestAnnotation(t *testing.T) {
    10  	Convey("different input for Filter", t, func() {
    11  		for _, test := range []struct {
    12  			err      bool
    13  			name     string
    14  			in       []string
    15  			validate func(as []Annotation)
    16  		}{
    17  			// 非注解
    18  			{in: []string{`// wvdwadbvb`}, validate: func(as []Annotation) {
    19  				So(len(as), ShouldEqual, 0)
    20  			}},
    21  			{in: []string{`// annotation@X( a = "A" }`}, name: "X", err: true},
    22  			{in: []string{`// annotation@X( a = A B C D  )`}, name: "X", validate: func(as []Annotation) {
    23  				So(len(as), ShouldEqual, 1)
    24  				So(as[0].String("a"), ShouldEqual, "")
    25  			}},
    26  			{in: []string{`// annotation@X( a = false  )`}, name: "X", validate: func(as []Annotation) {
    27  				So(len(as), ShouldEqual, 1)
    28  				So(as[0].String("a"), ShouldEqual, "")
    29  			}},
    30  			{in: []string{`// annotation@X( a = "A" ) some words`}, name: "X", validate: func(as []Annotation) {
    31  				So(len(as), ShouldEqual, 1)
    32  				So(as[0].String("a"), ShouldEqual, "A")
    33  			}},
    34  			{in: []string{`// annotation@X( a = "A" ) `}, name: "X", validate: func(as []Annotation) {
    35  				So(len(as), ShouldEqual, 1)
    36  				So(as[0].String("a"), ShouldEqual, "A")
    37  			}},
    38  			{in: []string{`// annotation@X( a = "A" )`, `annotation@X( b = "B" )`}, name: "X", validate: func(as []Annotation) {
    39  				So(len(as), ShouldEqual, 2)
    40  				So(as[0].String("a"), ShouldEqual, "A")
    41  				So(as[1].String("b"), ShouldEqual, "B")
    42  			}},
    43  			{in: []string{`// annotation@X( a = "A" )`, `annotation@X( b = "B" )`}, name: "*", validate: func(as []Annotation) {
    44  				So(len(as), ShouldEqual, 2)
    45  				So(as[0].String("a"), ShouldEqual, "A")
    46  				So(as[1].String("b"), ShouldEqual, "B")
    47  			}},
    48  			{in: []string{`// annotation@SomethingElse( aggregate = "@A@")`, `// annotation@Event( aggregate = "@A@")`}, name: "Event", validate: func(as []Annotation) {
    49  				So(len(as), ShouldEqual, 1)
    50  				So(as[0].String("aggregate"), ShouldEqual, "@A@")
    51  			}},
    52  			{in: []string{`// annotation@Doit( a="/A/", b="/B" )`}, name: "Doit", validate: func(as []Annotation) {
    53  				So(len(as), ShouldEqual, 1)
    54  				So(as[0].String("a"), ShouldEqual, "/A/")
    55  				So(as[0].String("b"), ShouldEqual, "/B")
    56  			}},
    57  			{in: []string{`// annotation@SomethingElse( aggregate = "A")`, `// annotation@Event( aggregate = "@A@")`}, name: "Event", validate: func(as []Annotation) {
    58  				So(len(as), ShouldEqual, 1)
    59  				So(as[0].String("aggregate"), ShouldEqual, "@A@")
    60  			}},
    61  		} {
    62  			r := New(WithDescriptors(Descriptor{Name: test.name}))
    63  			ann, err := r.ResolveMany(test.in...)
    64  			if test.err {
    65  				So(err, ShouldNotBeNil)
    66  			} else {
    67  				test.validate(ann)
    68  			}
    69  		}
    70  	})
    71  
    72  	Convey("annotation", t, func() {
    73  		for _, test := range []struct {
    74  			// input
    75  			name      string
    76  			in        string
    77  			validator func(ann Annotation) bool
    78  			validate  func(ann Annotation)
    79  			err       error
    80  		}{
    81  			{name: "a", in: `// annotation@a( AK="av" )`, err: ErrNoAnnotation},
    82  			{name: "a", in: `// ann@a( ak="av" )`, validate: func(ann Annotation) {
    83  				So(ann.Name(), ShouldEqual, "a")
    84  				So(ann.Line(), ShouldEqual, `// ann@a( ak="av" )`)
    85  				So(ann.Contains("ak"), ShouldBeTrue)
    86  				So(ann.Contains("AK"), ShouldBeFalse)
    87  				So(ann.String("ak"), ShouldEqual, "av")
    88  				_, err := ann.Int64("ak")
    89  				So(err, ShouldNotBeNil)
    90  			}},
    91  			{name: "a", in: `// ann@a( AK="av" )`, validate: func(ann Annotation) {
    92  				So(ann.String("ak"), ShouldEqual, "")
    93  				So(ann.String("AK"), ShouldEqual, "av")
    94  			}},
    95  			{name: "a", in: `// ann@a( AK="av" )`, validator: func(ann Annotation) bool { return ann.String("AK") != "av" }, err: ErrNoAnnotation},
    96  			{name: "a", in: `// ann@a( AK=127, AV=128, AF=0.061, AB=1, INVALID="AAAAA" )`, validate: func(ann Annotation) {
    97  				var check = func(index int, expected, defaultVal interface{}, f func() (interface{}, error)) {
    98  					val, err := f()
    99  					switch index {
   100  					case 0:
   101  						So(err, ShouldBeNil)
   102  						So(val, ShouldEqual, expected)
   103  					case 1:
   104  						So(err, ShouldBeNil)
   105  						So(val, ShouldEqual, defaultVal)
   106  					case 2:
   107  						So(err, ShouldNotBeNil)
   108  					}
   109  				}
   110  				for i, key := range []string{"AK", "no_exists", "INVALID"} {
   111  					for _, defaultVal := range []interface{}{int8(126), int16(128), int32(126), int64(126), 126, uint8(126), uint16(128), uint32(126), uint64(126)} {
   112  						switch vv := defaultVal.(type) {
   113  						case int8:
   114  							check(i, int8(127), vv, func() (interface{}, error) { return ann.Int8(key, vv) })
   115  						case int16:
   116  							check(i, int16(127), vv, func() (interface{}, error) { return ann.Int16(key, vv) })
   117  						case int32:
   118  							check(i, int32(127), vv, func() (interface{}, error) { return ann.Int32(key, vv) })
   119  						case int64:
   120  							check(i, int64(127), vv, func() (interface{}, error) { return ann.Int64(key, vv) })
   121  						case int:
   122  							check(i, 127, vv, func() (interface{}, error) { return ann.Int(key, vv) })
   123  						case uint8:
   124  							check(i, uint8(127), vv, func() (interface{}, error) { return ann.Uint8(key, vv) })
   125  						case uint16:
   126  							check(i, uint16(127), vv, func() (interface{}, error) { return ann.Uint16(key, vv) })
   127  						case uint32:
   128  							check(i, uint32(127), vv, func() (interface{}, error) { return ann.Uint32(key, vv) })
   129  						case uint64:
   130  							check(i, uint64(127), vv, func() (interface{}, error) { return ann.Uint64(key, vv) })
   131  						}
   132  					}
   133  				}
   134  
   135  				for i, key := range []string{"AF", "no_exists", "INVALID"} {
   136  					for _, defaultVal := range []interface{}{float32(0.051), 0.051} {
   137  						switch vv := defaultVal.(type) {
   138  						case float32:
   139  							check(i, float32(0.061), vv, func() (interface{}, error) { return ann.Float32(key, vv) })
   140  						case float64:
   141  							check(i, 0.061, vv, func() (interface{}, error) { return ann.Float64(key, vv) })
   142  						}
   143  					}
   144  				}
   145  
   146  				for i, key := range []string{"AB", "no_exists"} {
   147  					for _, defaultVal := range []interface{}{true} {
   148  						switch vv := defaultVal.(type) {
   149  						case bool:
   150  							check(i, true, vv, func() (interface{}, error) { return ann.Bool(key, vv) })
   151  						}
   152  					}
   153  				}
   154  				_, err := ann.Int8("AV")
   155  				So(err, ShouldNotBeNil)
   156  			}},
   157  		} {
   158  			r := New(WithMagicPrefix("ann@"), WithDescriptors(Descriptor{Name: test.name, Validator: test.validator}), WithLowerKey(false))
   159  			ann, err := r.Resolve(test.in)
   160  			if test.err != nil {
   161  				So(err, ShouldNotBeNil)
   162  				So(err.Error(), ShouldEqual, test.err.Error())
   163  			} else {
   164  				So(err, ShouldBeNil)
   165  				test.validate(ann)
   166  			}
   167  		}
   168  	})
   169  
   170  	Convey("resolve", t, func() {
   171  		ann, err := Resolve(`// wvdwadbvb`)
   172  		So(ann, ShouldBeNil)
   173  		So(err, ShouldNotBeNil)
   174  		So(err, ShouldEqual, ErrNoAnnotation)
   175  
   176  		ann, err = Resolve(`// annotation@X( A = "A" )`)
   177  		So(err, ShouldBeNil)
   178  		So(ann, ShouldNotBeNil)
   179  		So(ann.String("a"), ShouldEqual, "A")
   180  		So(ann.String("A"), ShouldEqual, "")
   181  	})
   182  
   183  	Convey("resolve with name", t, func() {
   184  		ann, err := ResolveWithName("Y", `// annotation@X( A = "A" )`)
   185  		So(ann, ShouldBeNil)
   186  		So(err, ShouldNotBeNil)
   187  		So(err, ShouldEqual, ErrNoAnnotation)
   188  
   189  		ann, err = ResolveWithName("X", `// annotation@X( A = "A" }`)
   190  		So(err, ShouldNotBeNil)
   191  
   192  		ann, err = ResolveWithName("X", `// annotation@X( A = "A" )`, `// annotation@X( B = "B" )`)
   193  		So(err, ShouldBeNil)
   194  		So(ann, ShouldNotBeNil)
   195  		So(ann.String("a"), ShouldEqual, "A")
   196  		So(ann.String("b"), ShouldBeEmpty)
   197  	})
   198  
   199  	Convey("resolve many", t, func() {
   200  		ann, err := ResolveMany(`// annotation@X( A = "A" }`, `// annotation@X( A = "A" )`)
   201  		So(err, ShouldNotBeNil)
   202  		So(len(ann), ShouldEqual, 0)
   203  
   204  		ann, err = ResolveMany(`// wvdwadbvb`, `// annotation@X( A = "A" )`)
   205  		So(err, ShouldBeNil)
   206  		So(ann, ShouldNotBeNil)
   207  		So(len(ann), ShouldEqual, 1)
   208  		So(ann[0].String("a"), ShouldEqual, "A")
   209  	})
   210  
   211  	Convey("resolve with name", t, func() {
   212  		ann, err := ResolveWithName("Y", `// annotation@X( A = "A" )`)
   213  		So(ann, ShouldBeNil)
   214  		So(err, ShouldNotBeNil)
   215  		So(err, ShouldEqual, ErrNoAnnotation)
   216  
   217  		ann, err = ResolveWithName("X", `// annotation@X( A = "A" )`)
   218  		So(err, ShouldBeNil)
   219  		So(ann, ShouldNotBeNil)
   220  		So(ann.String("a"), ShouldEqual, "A")
   221  	})
   222  
   223  	Convey("resolve no duplicate", t, func() {
   224  		ann, err := ResolveNoDuplicate(`// annotation@X( A = "A") `, `// annotation@X( a = "a" )`)
   225  		So(ann, ShouldBeNil)
   226  		So(err, ShouldNotBeNil)
   227  		So(err.Error(), ShouldContainSubstring, "got duplicate annotation")
   228  
   229  		ann, err = ResolveNoDuplicate(`// annotation@X( A = "A") `, `// wvdwadbvb`)
   230  		So(err, ShouldBeNil)
   231  		So(ann, ShouldNotBeNil)
   232  		So(len(ann), ShouldEqual, 1)
   233  		So(ann[0].Name(), ShouldEqual, "X")
   234  		So(ann[0].String("a"), ShouldEqual, "A")
   235  
   236  		ann, err = ResolveNoDuplicate(`// annotation@X( A = "A") `, `// annotation@Y( a = "a" )`, `// wvdwadbvb`)
   237  		So(err, ShouldBeNil)
   238  		So(ann, ShouldNotBeNil)
   239  		So(len(ann), ShouldEqual, 2)
   240  		So(ann[0].Name(), ShouldEqual, "X")
   241  		So(ann[0].String("a"), ShouldEqual, "A")
   242  		So(ann[1].Name(), ShouldEqual, "Y")
   243  		So(ann[1].String("a"), ShouldEqual, "a")
   244  	})
   245  }