@@ -16,11 +16,110 @@ package attribute
1616import (
1717 "fmt"
1818 "reflect"
19+ "strings"
1920 "testing"
2021
2122 "github.com/stretchr/testify/assert"
2223)
2324
25+ func TestDictionaryNamedTypeChecker (t * testing.T ) {
26+ a := assert .New (t )
27+ name := "any_attr_name"
28+
29+ assertFunc := func (d Dictionary , value interface {}, ok bool ) {
30+ err := d .Validate (map [string ]interface {}{name : value })
31+ if ok {
32+ a .NoError (err )
33+ } else {
34+ a .Error (err )
35+ }
36+ }
37+
38+ boolDictWithoutChecker := Dictionary {}.Bool (name , false , "" , nil )
39+ assertFunc (boolDictWithoutChecker , "abc" , false )
40+ assertFunc (boolDictWithoutChecker , false , true )
41+ assertFunc (boolDictWithoutChecker , true , true )
42+ boolDictWithChecker := Dictionary {}.Bool (name , nil , "" , func (v bool ) error {
43+ if v {
44+ return fmt .Errorf ("attribute %s must be false" , name )
45+ }
46+ return nil
47+ })
48+ assertFunc (boolDictWithChecker , "abc" , false )
49+ assertFunc (boolDictWithChecker , false , true )
50+ assertFunc (boolDictWithChecker , true , false )
51+
52+ intDictWithoutChecker := Dictionary {}.Int (name , 0 , "" , nil )
53+ assertFunc (intDictWithoutChecker , "abc" , false )
54+ assertFunc (intDictWithoutChecker , 3 , true )
55+ intDictWithChecker := Dictionary {}.Int (name , 0 , "" , func (v int ) error {
56+ if v == 3 {
57+ return fmt .Errorf ("attribute %s cannot be 3" , name )
58+ }
59+ return nil
60+ })
61+ assertFunc (intDictWithChecker , "abc" , false )
62+ assertFunc (intDictWithChecker , 3 , false )
63+ assertFunc (intDictWithChecker , 0 , true )
64+
65+ floatDictWithoutChecker := Dictionary {}.Float (name , nil , "" , nil )
66+ assertFunc (floatDictWithoutChecker , "abc" , false )
67+ assertFunc (floatDictWithoutChecker , float32 (- 1.5 ), true )
68+ floatDictWithChecker := Dictionary {}.Float (name , float32 (0 ), "" , func (v float32 ) error {
69+ if v <= float32 (- 1.0 ) {
70+ return fmt .Errorf ("attribute %s must larger than -1.0" , name )
71+ }
72+ return nil
73+ })
74+ assertFunc (floatDictWithChecker , "abc" , false )
75+ assertFunc (floatDictWithChecker , float32 (- 2.0 ), false )
76+ assertFunc (floatDictWithChecker , float32 (7.5 ), true )
77+
78+ stringDictWithoutChecker := Dictionary {}.String (name , "" , "" , nil )
79+ assertFunc (stringDictWithoutChecker , 1 , false )
80+ assertFunc (stringDictWithoutChecker , "abc" , true )
81+ stringDictWithChecker := Dictionary {}.String (name , nil , "" , func (v string ) error {
82+ if ! strings .HasPrefix (v , "valid" ) {
83+ return fmt .Errorf ("attribute %s must have prefix valid" , name )
84+ }
85+ return nil
86+ })
87+ assertFunc (stringDictWithChecker , 1 , false )
88+ assertFunc (stringDictWithChecker , "invalidString" , false )
89+ assertFunc (stringDictWithChecker , "validString" , true )
90+
91+ intListDictWithoutChecker := Dictionary {}.IntList (name , []int {}, "" , nil )
92+ assertFunc (intListDictWithoutChecker , "abc" , false )
93+ assertFunc (intListDictWithoutChecker , []int {1 }, true )
94+ intListDictWithChecker := Dictionary {}.IntList (name , []int {}, "" , func (v []int ) error {
95+ if len (v ) > 2 {
96+ return fmt .Errorf ("length of attribute %s must be less than or equal to 2" , name )
97+ }
98+ return nil
99+ })
100+ assertFunc (intListDictWithChecker , "abc" , false )
101+ assertFunc (intListDictWithChecker , []int {1 , 2 , 3 }, false )
102+ assertFunc (intListDictWithChecker , []int {1 , 2 }, true )
103+
104+ unknownTypeDictWithoutChecker := Dictionary {}.Unknown (name , nil , "" , nil )
105+ assertFunc (unknownTypeDictWithoutChecker , 1 , true )
106+ assertFunc (unknownTypeDictWithoutChecker , float32 (- 0.5 ), true )
107+ assertFunc (unknownTypeDictWithoutChecker , "abc" , true )
108+
109+ unknownTypeDictWithChecker := Dictionary {}.Unknown (name , 1 , "" , func (v interface {}) error {
110+ if _ , ok := v .(int ); ok {
111+ return nil
112+ }
113+ if _ , ok := v .(string ); ok {
114+ return nil
115+ }
116+ return fmt .Errorf ("attribute %s must be of type int or string" , name )
117+ })
118+ assertFunc (unknownTypeDictWithChecker , 1 , true )
119+ assertFunc (unknownTypeDictWithChecker , "abc" , true )
120+ assertFunc (unknownTypeDictWithChecker , float32 (1.5 ), false )
121+ }
122+
24123func TestDictionaryValidate (t * testing.T ) {
25124 a := assert .New (t )
26125
0 commit comments