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
/
HICurrencyFormatServiceSpec.m
144 lines (120 loc) · 5.16 KB
/
HICurrencyFormatServiceSpec.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
#import "HICurrencyFormatService.h"
SPEC_BEGIN(HICurrencyFormatServiceSpec)
describe(@"Formatting", ^{
__block HICurrencyFormatService *service;
beforeEach(^{
service = [HICurrencyFormatService new];
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
});
it(@"has user locale", ^{
assertThat([HICurrencyFormatService new].locale, is([NSLocale currentLocale]));
});
it(@"formats value", ^{
NSString *string = [service stringForValue:@1060.5 inCurrency:@"USD"];
assertThat(string, equalTo(@"1,060.50"));
});
it(@"rounds the value", ^{
NSString *string = [service stringForValue:@1.503 inCurrency:@"USD"];
assertThat(string, equalTo(@"1.50"));
});
it(@"formats negative values", ^{
NSString *string = [service stringWithUnitForValue:@-1060.5 inCurrency:@"USD"];
assertThat(string, equalTo(@"-1,060.50"));
});
context(@"using a different locale", ^{
it(@"formats value", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSString *string = [service stringForValue:@1060.5 inCurrency:@"USD"];
assertThat(string, equalTo(@"1.060,50"));
});
});
context(@"formatting a currency with three decimal places", ^{
it(@"formats value", ^{
NSString *string = [service stringForValue:@1060.5 inCurrency:@"TND"];
assertThat(string, equalTo(@"1,060.500"));
});
});
context(@"formatting with unit", ^{
it(@"prepend unit", ^{
NSString *string = [service stringWithUnitForValue:@1060.5 inCurrency:@"USD"];
assertThat(string, equalTo(@"$1,060.50"));
});
it(@"appends unit for some locales", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSString *string = [service stringWithUnitForValue:@1060.5 inCurrency:@"EUR"];
assertThat(string, equalTo(@"1.060,50\u00A0€"));
});
it(@"prepends unit if it doesn't match locale's currency", ^{
NSString *string = [service stringWithUnitForValue:@1060.5 inCurrency:@"PLN"];
assertThat(string, equalTo(@"zł1,060.50"));
});
});
});
describe(@"Parsing", ^{
__block HICurrencyFormatService *service;
beforeEach(^{
service = [HICurrencyFormatService new];
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
});
it(@"should parse value correctly", ^{
NSNumber *amount = [service parseString:@"1,000.6" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
it(@"should parse value without leading zero correctly", ^{
NSNumber *amount = [service parseString:@".60" error:NULL];
assertThat(amount, equalTo(@.6));
});
context(@"parsing with unit", ^{
it(@"should parse value with unit", ^{
NSNumber *amount = [service parseString:@"$1,000.6" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
it(@"should parse value with trailing unit", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSNumber *amount = [service parseString:@"1.000,6\u00A0€" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
it(@"should parse value with currency if it doesn't matches the locale's currency", ^{
NSNumber *amount = [service parseString:@"zł1,000.60" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
it(@"should parse value with a dollar in Thai locale", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"th_TH"];
NSNumber *amount = [service parseString:@"$1,000.60" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
});
context(@"using a different locale", ^{
it(@"should parse value correctly", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSNumber *amount = [service parseString:@"1.000,6" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
it(@"should parse value with spaces correctly", ^{
service.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"];
NSNumber *amount = [service parseString:@"1 000,6" error:NULL];
assertThat(amount, equalTo(@1000.6));
});
});
context(@"parsing illegal string", ^{
context(@"error variable is set", ^{
it(@"should return nil", ^{
NSError *error;
NSNumber *amount = [service parseString:@"xx" error:&error];
assertThat(amount, nilValue());
});
it(@"should set error", ^{
NSError *error;
[service parseString:@"xx" error:&error];
assertThat(error, notNilValue());
});
});
context(@"error variable is NULL", ^{
it(@"should return 0", ^{
NSNumber *amount = [service parseString:@"xx" error:NULL];
assertThat(amount, nilValue());
});
});
});
});
SPEC_END