@@ -11,61 +11,75 @@ import (
1111 . "github.com/smartystreets/goconvey/convey"
1212)
1313
14- // TODO: Fix tests
1514func TestKeys (t * testing.T ) {
1615 Convey ("Keys(obj, false) should return 'a', 'c', 'b' in any order" , t , func () {
17- //obj := runtime.NewObjectWith(
18- // runtime.NewObjectProperty("a", runtime.NewInt(0)),
19- // runtime.NewObjectProperty("b", runtime.NewInt(1)),
20- // runtime.NewObjectProperty("c", runtime.NewInt(2)),
21- //)
22- //
23- //keys, err := objects.Keys(context.Background(), obj)
24- //keysArray := keys.(*runtime.Array)
25- //
26- //So(err, ShouldEqual, nil)
27- //So(keysArray.Type().Equals(types.Array), ShouldBeTrue)
28- //So(keysArray.Length(), ShouldEqual, 3)
29- //
30- //for _, k := range []string{"b", "a", "c"} {
31- // iof := keysArray.IndexOf(runtime.NewString(k))
32- // So(iof, ShouldNotEqual, -1)
33- //}
16+ obj := runtime .NewObjectWith (
17+ runtime .NewObjectProperty ("a" , runtime .NewInt (0 )),
18+ runtime .NewObjectProperty ("b" , runtime .NewInt (1 )),
19+ runtime .NewObjectProperty ("c" , runtime .NewInt (2 )),
20+ )
21+
22+ keys , err := objects .Keys (context .Background (), obj )
23+ keysArray := keys .(* runtime.Array )
24+
25+ So (err , ShouldEqual , nil )
26+ actualLength , _ := keysArray .Length (context .Background ())
27+ So (actualLength , ShouldEqual , 3 )
28+
29+ // Check that all expected keys are present (order doesn't matter)
30+ keyStrings := make ([]string , 0 , 3 )
31+ keysArray .ForEach (context .Background (), func (ctx context.Context , val runtime.Value , idx runtime.Int ) (runtime.Boolean , error ) {
32+ keyStrings = append (keyStrings , val .String ())
33+ return true , nil
34+ })
35+
36+ for _ , expectedKey := range []string {"a" , "b" , "c" } {
37+ found := false
38+ for _ , actualKey := range keyStrings {
39+ if actualKey == expectedKey {
40+ found = true
41+ break
42+ }
43+ }
44+ So (found , ShouldBeTrue )
45+ }
3446 })
3547
36- Convey ("Keys(obj, false) should return ['a', 'b', 'c']" , t , func () {
37- //obj := runtime.NewObjectWith(
38- // runtime.NewObjectProperty("b", runtime.NewInt(0)),
39- // runtime.NewObjectProperty("a", runtime.NewInt(1)),
40- // runtime.NewObjectProperty("c", runtime.NewInt(3)),
41- //)
42- //
43- //keys, err := objects.Keys(context.Background(), obj, runtime.NewBoolean(true))
44- //keysArray := keys.(*runtime.Array)
45- //
46- //So(err, ShouldEqual, nil)
47- //
48- //for idx, key := range []string{"a", "b", "c"} {
49- // So(keysArray.Get(runtime.NewInt(idx)), ShouldEqual, runtime.NewString(key))
50- //}
48+ Convey ("Keys(obj, true) should return ['a', 'b', 'c'] in sorted order" , t , func () {
49+ obj := runtime .NewObjectWith (
50+ runtime .NewObjectProperty ("b" , runtime .NewInt (0 )),
51+ runtime .NewObjectProperty ("a" , runtime .NewInt (1 )),
52+ runtime .NewObjectProperty ("c" , runtime .NewInt (3 )),
53+ )
54+
55+ keys , err := objects .Keys (context .Background (), obj , runtime .NewBoolean (true ))
56+ keysArray := keys .(* runtime.Array )
57+
58+ So (err , ShouldEqual , nil )
59+
60+ expectedKeys := []string {"a" , "b" , "c" }
61+ for idx , key := range expectedKeys {
62+ actualKey , _ := keysArray .Get (context .Background (), runtime .NewInt (idx ))
63+ So (actualKey .String (), ShouldEqual , key )
64+ }
5165 })
5266
5367 Convey ("When there are no keys" , t , func () {
54- // obj := runtime.NewObject()
55- //
56- // keys, err := objects.Keys(context.Background(), obj, runtime.NewBoolean(true))
57- // keysArray := keys.(*runtime.Array)
58- //
59- // So(err, ShouldEqual, nil)
60- //So( keysArray.Length(), ShouldEqual, runtime.NewInt(0 ))
61- // So(int(keysArray.Length()) , ShouldEqual, 0)
62- //
63- // keys, err = objects.Keys(context.Background(), obj, runtime.NewBoolean(false))
64- // keysArray = keys.(*runtime.Array)
65- //
66- // So(err, ShouldEqual, nil)
67- //So( keysArray.Length(), ShouldEqual, runtime.NewInt(0 ))
68- // So(int(keysArray.Length()) , ShouldEqual, 0)
68+ obj := runtime .NewObject ()
69+
70+ keys , err := objects .Keys (context .Background (), obj , runtime .NewBoolean (true ))
71+ keysArray := keys .(* runtime.Array )
72+
73+ So (err , ShouldEqual , nil )
74+ actualLength , _ := keysArray .Length (context . Background ( ))
75+ So (actualLength , ShouldEqual , 0 )
76+
77+ keys , err = objects .Keys (context .Background (), obj , runtime .NewBoolean (false ))
78+ keysArray = keys .(* runtime.Array )
79+
80+ So (err , ShouldEqual , nil )
81+ actualLength , _ = keysArray .Length (context . Background ( ))
82+ So (actualLength , ShouldEqual , 0 )
6983 })
7084
7185 Convey ("When not enough arguments" , t , func () {
@@ -90,4 +104,28 @@ func TestKeys(t *testing.T) {
90104 So (err , ShouldBeError )
91105 })
92106
107+ Convey ("When object has special character keys" , t , func () {
108+ obj := runtime .NewObjectWith (
109+ runtime .NewObjectProperty ("key with spaces" , runtime .NewInt (1 )),
110+ runtime .NewObjectProperty ("key_with_underscores" , runtime .NewInt (2 )),
111+ runtime .NewObjectProperty ("key-with-dashes" , runtime .NewInt (3 )),
112+ runtime .NewObjectProperty ("key.with.dots" , runtime .NewInt (4 )),
113+ )
114+
115+ keys , err := objects .Keys (context .Background (), obj , runtime .NewBoolean (true ))
116+ keysArray := keys .(* runtime.Array )
117+
118+ So (err , ShouldEqual , nil )
119+ actualLength , _ := keysArray .Length (context .Background ())
120+ So (actualLength , ShouldEqual , 4 )
121+
122+ // Check sorted order
123+ expectedKeys := []string {"key with spaces" , "key-with-dashes" , "key.with.dots" , "key_with_underscores" }
124+ for idx , expectedKey := range expectedKeys {
125+ actualKey , _ := keysArray .Get (context .Background (), runtime .NewInt (idx ))
126+ So (actualKey .String (), ShouldEqual , expectedKey )
127+ }
128+ })
93129}
130+
131+
0 commit comments