summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/DevAPISpecs/NSMutableDictionary+StoreSpec.m
blob: 459e14ac6fea3df0bdc2c05ec7b999b798a9b12a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>

#import "NSMutableDictionary+Store.h"

@interface TestObject : NSObject
@property (nonatomic, strong, nullable) NSDictionary *json;
- (instancetype)initWithDictionary:(NSDictionary *)json;
@end

@implementation TestObject
- (instancetype)initWithDictionary:(NSDictionary *)json {
    self = [super init];
    if (self) {
        self.json = json;
    }
    return self;
}
- (BOOL)isEqual:(id)object {
    if (![object isKindOfClass:[TestObject class]]) {
        return false;
    }
    TestObject *anotherObject = (TestObject *)object;
    return [self.json isEqual:anotherObject.json];
}
@end

QuickSpecBegin(NSMutableDictionary_StoreSpec)

describe(@"A mutable dictionary with the storing category imported", ^{
    __block NSMutableDictionary *testDictionary = nil;
    __block NSString *testKey = @"Key";

    beforeEach(^{
        testDictionary = [[NSMutableDictionary alloc] init];
    });

    describe(@"retrieving a string", ^{
        __block NSString *testObject = @"Object";

        beforeEach(^{
            [testDictionary sdl_setObject:testObject forName:testKey];
        });

        it(@"should return string when called correctly", ^{
            expect([testDictionary sdl_objectForName:testKey ofClass:[NSString class]]).to(equal(testObject));
        });
        it(@"should raise an exception when called with wrong class type", ^{
            expectAction(^{
                expect([testDictionary sdl_objectForName:testKey ofClass:[NSNumber class]]).to(beNil());
            }).to(raiseException());
        });
    });

    describe(@"retrieving a single object", ^{
        __block NSString *testObject = @"Object";

        beforeEach(^{
            [testDictionary sdl_setObject:testObject forName:testKey];
        });

        it(@"should return nil and raise an exception when retrieved as an array", ^{
            expectAction(^{
                expect([testDictionary sdl_objectsForName:testKey ofClass:[NSString class] error:nil]).to(beNil());
            }).to(raiseException());
        });
    });

    describe(@"Retrieving a custom JSON object", ^{
        __block TestObject *testObject = nil;
        beforeEach(^{
            NSDictionary *testJson = @{};
            testObject = [[TestObject alloc] initWithDictionary:testJson];
            [testDictionary sdl_setObject:testJson forName:testKey];
        });

        it(@"should return the object correctly when retrieved correctly", ^{
            expect([testDictionary sdl_objectForName:testKey ofClass:[TestObject class]]).to(equal(testObject));
        });
        it(@"should raise an exception when retrieved as an NSNumber", ^{
            expectAction(^{
                expect([testDictionary sdl_objectForName:testKey ofClass:[NSNumber class]]).to(beNil());
            }).to(raiseException());
        });
    });

    describe(@"Retrieving an array of one string", ^{
        __block NSString *testObject = @"Object";
        __block NSArray<NSString *> *testObjectArray = @[testObject];

        beforeEach(^{
            [testDictionary sdl_setObject:testObjectArray forName:testKey];
        });

        it(@"should raise an exception when retrieved as an NSString", ^{
            expectAction(^{
                expect([testDictionary sdl_objectForName:testKey ofClass:[NSString class]]).to(equal(testObject));
            }).to(raiseException());
        });

        it(@"should return correctly when retrieved correctly", ^{
            expect([testDictionary sdl_objectsForName:testKey ofClass:[NSString class] error:nil]).to(equal(testObjectArray));
        });
        it(@"should raise an exception when retrieved as an NSNumber", ^{
            expectAction(^{
                expect([testDictionary sdl_objectForName:testKey ofClass:[NSNumber class] error:nil]).to(beNil());
                expect([testDictionary sdl_objectsForName:testKey ofClass:[NSNumber class] error:nil]).to(beNil());
            }).to(raiseException());
        });
    });

    describe(@"retrieving an array of custom objects", ^{
        __block NSArray<TestObject *> *testObjectArray = nil;

        beforeEach(^{
            NSDictionary *testJson = @{};
            TestObject *testObject = [[TestObject alloc] initWithDictionary:testJson];
            NSArray<NSDictionary *> *testJsonArray = @[testJson];
            testObjectArray = @[testObject];
            [testDictionary sdl_setObject:testJsonArray forName:testKey];
        });

        it(@"should return correctly when retrieved correctly", ^{
            expect([testDictionary sdl_objectsForName:testKey ofClass:[TestObject class] error:nil]).to(equal(testObjectArray));
        });
        it(@"should raise an exception when retrieved as an NSNumber", ^{
            expectAction(^{
                expect([testDictionary sdl_objectsForName:testKey ofClass:[NSNumber class] error:nil]).to(beNil());
            }).to(raiseException());
        });
    });

    describe(@"Retrieving an SDLEnum", ^{
        __block SDLEnum testObject = @"Object";

        beforeEach(^{
            [testDictionary sdl_setObject:testObject forName:testKey];
        });

        it(@"should return correctly when retrieved correctly", ^{
            expect([testDictionary sdl_enumForName:testKey error:nil]).to(equal(testObject));
        });
        it(@"should raise an exception when retrieved as an array", ^{
            expectAction(^{
                expect([testDictionary sdl_enumsForName:testKey error:nil]).to(beNil());
            }).to(raiseException());
        });
    });

    describe(@"retrieving an array of SDLEnums", ^{
        __block NSArray<SDLEnum> *testObjectArray = nil;

        beforeEach(^{
            __block SDLEnum testObject = @"Object";
            testObjectArray = @[testObject];
            [testDictionary sdl_setObject:testObjectArray forName:testKey];
        });

        it(@"should return an array of SDLEnum when retrieved correctly", ^{
            expect([testDictionary sdl_enumsForName:testKey error:nil]).to(equal(testObjectArray));
        });
        it(@"should raise an exception when retrieved as an NSNumber", ^{
            expectAction(^{
                expect([testDictionary sdl_objectForName:testKey ofClass:[NSNumber class]]).to(beNil());
            }).to(raiseException());
        });
    });
});

QuickSpecEnd