This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
HIBitcoinFormatServiceSpec.m
213 lines (182 loc) · 7.6 KB
/
HIBitcoinFormatServiceSpec.m
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#import "HIBitcoinFormatService.h"
@interface NSObject()
- (void)receiveNotification:(NSNotification *)notification;
@end
SPEC_BEGIN(HIBitcoinFormatServiceSpec)
describe(@"Formatting", ^{
__block HIBitcoinFormatService *service;
beforeEach(^{
service = [HIBitcoinFormatService new];
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
});
context(@"preferred format is BTC", ^{
beforeEach(^{
service.preferredFormat = @"BTC";
});
it(@"formats using the preferred format", ^{
NSString *string = [service stringForBitcoin:60000000];
assertThat(string, equalTo(@"0.60"));
});
it(@"adds the correct unit", ^{
NSString *string = [service stringWithUnitForBitcoin:60000000];
assertThat(string, equalTo(@"0.60 BTC"));
});
});
context(@"preferred format is mBTC", ^{
beforeEach(^{
service.preferredFormat = @"mBTC";
});
it(@"formats using the preferred format", ^{
NSString *string = [service stringForBitcoin:160000];
assertThat(string, equalTo(@"1.6"));
});
it(@"adds the correct unit", ^{
NSString *string = [service stringWithUnitForBitcoin:160000];
assertThat(string, equalTo(@"1.6 mBTC"));
});
});
context(@"preferred format is bits", ^{
beforeEach(^{
service.preferredFormat = @"bits";
});
it(@"formats using the preferred format", ^{
NSString *string = [service stringForBitcoin:160];
assertThat(string, equalTo(@"1.6"));
});
it(@"adds the correct unit", ^{
NSString *string = [service stringWithUnitForBitcoin:160];
assertThat(string, equalTo(@"1.6 bits"));
});
});
context(@"formatting as BTC", ^{
it(@"adds the minimum number of decimal places", ^{
NSString *string = [service stringForBitcoin:60000000 withFormat:@"BTC"];
assertThat(string, equalTo(@"0.60"));
});
it(@"keeps all required decimal places", ^{
NSString *string = [service stringForBitcoin:123456789 withFormat:@"BTC"];
assertThat(string, equalTo(@"1.23456789"));
});
it(@"formats negative values correcly", ^{
NSString *string = [service stringForBitcoin:-60000000 withFormat:@"BTC"];
assertThat(string, equalTo(@"-0.60"));
});
});
context(@"formatting as mBTC", ^{
it(@"formats value correcly", ^{
NSString *string = [service stringForBitcoin:160000 withFormat:@"mBTC"];
assertThat(string, equalTo(@"1.6"));
});
});
context(@"formatting as bits", ^{
it(@"formats value correcly", ^{
NSString *string = [service stringForBitcoin:160 withFormat:@"bits"];
assertThat(string, equalTo(@"1.6"));
});
});
context(@"using a different locale", ^{
it(@"formats value correcly", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSString *string = [service stringForBitcoin:100060000000 withFormat:@"BTC"];
assertThat(string, equalTo(@"1.000,60"));
});
});
});
describe(@"Parsing", ^{
__block HIBitcoinFormatService *service;
beforeEach(^{
service = [HIBitcoinFormatService new];
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
});
it(@"should parse BTC value correctly", ^{
satoshi_t amount = [service parseString:@"0.6" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(60000000));
});
it(@"should parse mBTC value correctly", ^{
satoshi_t amount = [service parseString:@"0.6" withFormat:@"mBTC" error:NULL];
assertThat(@(amount), equalToLongLong(60000));
});
it(@"should parse bits value correctly", ^{
satoshi_t amount = [service parseString:@"0.6" withFormat:@"bits" error:NULL];
assertThat(@(amount), equalToLongLong(60));
});
it(@"should parse string without leading zero correctly", ^{
satoshi_t amount = [service parseString:@".60" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(60000000));
});
it(@"should parse string with thousands separator correctly", ^{
satoshi_t amount = [service parseString:@"1,000.6" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(100060000000));
});
it(@"should parse negative string correctly", ^{
satoshi_t amount = [service parseString:@"-0.6" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(-60000000));
});
it(@"should round BTC value correctly", ^{
satoshi_t amount = [service parseString:@"81.1" withFormat:@"mBTC" error:NULL];
assertThat(@(amount), equalToLongLong(8110000));
});
context(@"using a different locale", ^{
it(@"should parse value correctly", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
satoshi_t amount = [service parseString:@"1.000,6" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(100060000000));
});
it(@"should parse value with spaces correctly", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"];
satoshi_t amount = [service parseString:@"1 000,6" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(100060000000));
});
});
context(@"parsing illegal string", ^{
context(@"error variable is set", ^{
it(@"should return 0", ^{
NSError *error;
satoshi_t amount = [service parseString:@"xx" withFormat:@"BTC" error:&error];
assertThat(@(amount), equalToLongLong(0));
});
it(@"should set error", ^{
NSError *error;
[service parseString:@"xx" withFormat:@"BTC" error:&error];
assertThat(error, notNilValue());
});
});
context(@"error variable is NULL", ^{
it(@"should return 0", ^{
satoshi_t amount = [service parseString:@"xx" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(0));
});
});
});
context(@"when string is empty", ^{
it(@"should return 0", ^{
satoshi_t amount = [service parseString:@"" withFormat:@"BTC" error:NULL];
assertThat(@(amount), equalToLongLong(0));
});
});
});
describe(@"Observing", ^{
__block HIBitcoinFormatService *service;
beforeEach(^{
service = [HIBitcoinFormatService new];
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
});
context(@"preferred format is being observed", ^{
service.preferredFormat = @"BTC";
id mock = [KWMock mock];
beforeAll(^{
[[NSNotificationCenter defaultCenter] addObserver:mock
selector:@selector(receiveNotification:)
name:HIPreferredFormatChangeNotification
object:nil];
});
afterAll(^{
[[NSNotificationCenter defaultCenter] removeObserver:mock];
});
it(@"should notify the observer the preferred format changes", ^{
[[[mock should] receive] receiveNotification:nil];
service.preferredFormat = @"mBTC";
});
});
});
SPEC_END