| 39 | } |
| 40 | |
| 41 | func TestStructMapGet(t *testing.T) { |
| 42 | type testCase struct { |
| 43 | actual interface{} |
| 44 | expected map[string]interface{} |
| 45 | } |
| 46 | |
| 47 | testCases := map[string]testCase{ |
| 48 | "Empty Struct": { |
| 49 | actual: struct{}{}, |
| 50 | expected: map[string]interface{}{ |
| 51 | "Size": 0, |
| 52 | "Keys": []string{}, |
| 53 | }, |
| 54 | }, |
| 55 | "Non-Empty Struct": { |
| 56 | actual: struct { |
| 57 | A string |
| 58 | B string |
| 59 | }{A: "a", B: "b"}, |
| 60 | expected: map[string]interface{}{ |
| 61 | "Size": 2, |
| 62 | "Keys": []string{"A", "B"}, |
| 63 | "A": "a", |
| 64 | "B": "b", |
| 65 | }, |
| 66 | }, |
| 67 | "Non-Empty Struct with Private Fields": { |
| 68 | actual: struct { |
| 69 | A string |
| 70 | B string |
| 71 | c string |
| 72 | }{A: "a", B: "b"}, |
| 73 | expected: map[string]interface{}{ |
| 74 | "Size": 2, |
| 75 | "Keys": []string{"A", "B"}, |
| 76 | "A": "a", |
| 77 | "B": "b", |
| 78 | }, |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | for name, c := range testCases { |
| 83 | t.Run(name, func(t *testing.T) { |
| 84 | sMap, err := NewStructMap(c.actual) |
| 85 | if err != nil { |
| 86 | t.Errorf("could not create struct map: %v", err) |
| 87 | } |
| 88 | |
| 89 | if sMap.Size() != c.expected["Size"] { |
| 90 | t.Errorf("Expected size of %v but received %v", c.expected["Size"], sMap.Size()) |
| 91 | } |
| 92 | |
| 93 | if !reflect.DeepEqual(sMap.Keys(), c.expected["Keys"]) { |
| 94 | t.Errorf("Expected keys %v but received %v", c.expected["Keys"], sMap.Keys()) |
| 95 | } |
| 96 | |
| 97 | for _, k := range sMap.Keys() { |
| 98 | val, ok := sMap.Get(k) |