Completed
Push — master ( 99c7c6...598cbd )
by wiese
369:15 queued 304:17
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
'use strict';
2
3
var test = require( 'tape' ),
4
	IntegerCurrency = require( '../lib/integer_currency' )
5
;
6
7
test( 'German locale - formatting an integer', function ( t ) {
8
	var formatter = IntegerCurrency.createCurrencyFormatter( 'de' );
9
	var inputsAndExpectedOutputs = [
10
		[ 0, '0,00' ],
11
		[ 1, '0,01' ],
12
		[ 99, '0,99' ],
13
		[ 100, '1,00' ],
14
		[ 1000, '10,00' ],
15
		[ 1337, '13,37' ]
16
	];
17
	inputsAndExpectedOutputs.map( function ( io ) {
18
		t.equal( formatter.format( io[0] ), io[1] );
19
	});
20
	t.end();
21
} );
22
23
test( 'German locale - parsing valid strings', function ( t ) {
24
	var parser = IntegerCurrency.createCurrencyParser( 'de' );
25
	var inputsAndExpectedOutputs = [
26
		[ '0,00', 0 ],
27
		[ '0,01', 1 ],
28
		[ '0,99', 99 ],
29
		[ '1,00', 100 ],
30
		[ '10,00', 1000 ],
31
		[ '13,37', 1337 ],
32
33
		// long decimal values should be truncated
34
		[ '13,3373', 1333 ],
35
		[ '1,989', 198 ],
36
		[ '1,991', 199 ],
37
		[ '1,999', 199 ],
38
		[ '17,995', 1799 ],
39
40
		// Values with less than 2 decimal points should be valid
41
		[ '12', 1200 ],
42
		[ '12,9', 1290 ]
43
	];
44
	inputsAndExpectedOutputs.map( function ( io ) {
45
		t.equal( parser.parse( io[0] ), io[1] );
46
	});
47
	t.end();
48
} );
49
50
test( 'German locale - parsing invalid strings', function ( t ) {
51
	var parser = IntegerCurrency.createCurrencyParser( 'de' );
52
	var inputs = [
53
		'',
54
		',01',
55
		'A,1',
56
		'1,*',
57
		'CAFFE',
58
		'1.2',
59
		'1,2,3'
60
	];
61
	inputs.map( function ( invalidInput ) {
62
		t.throws( function() { parser.parse( invalidInput ); },
63
			'"' + invalidInput + '" should throw exception' );
64
	});
65
	t.end();
66
} );
67
68
test( 'English locale - formatting an integer ', function ( t ) {
69
	var formatter = IntegerCurrency.createCurrencyFormatter( 'en' );
70
	var inputsAndExpectedOutputs = [
71
		[ 0, '0.00' ],
72
		[ 1, '0.01' ],
73
		[ 99, '0.99' ],
74
		[ 100, '1.00' ],
75
		[ 1000, '10.00' ],
76
		[ 1337, '13.37' ]
77
	];
78
	inputsAndExpectedOutputs.map( function ( io ) {
79
		t.equal( formatter.format( io[0] ), io[1] );
80
	});
81
	t.end();
82
} );
83
84
test( 'English locale - parsing valid strings', function ( t ) {
85
	var parser = IntegerCurrency.createCurrencyParser( 'en' );
86
	var inputsAndExpectedOutputs = [
87
		[ '0.00', 0 ],
88
		[ '0.01', 1 ],
89
		[ '0.99', 99 ],
90
		[ '1.00', 100 ],
91
		[ '10.00', 1000 ],
92
		[ '13.37', 1337 ],
93
94
		// long decimal values should be truncated
95
		[ '13.3373', 1333 ],
96
		[ '1.989', 198 ],
97
		[ '1.991', 199 ],
98
		[ '1.999', 199 ],
99
		[ '17.995', 1799 ],
100
101
		// Values with less than 2 decimal points should be valid
102
		[ '12', 1200 ],
103
		[ '12.9', 1290 ]
104
	];
105
	inputsAndExpectedOutputs.map( function ( io ) {
106
		t.equal( parser.parse( io[0] ), io[1] );
107
	});
108
	t.end();
109
} );
110
111
test( 'English locale - parsing invalid strings', function ( t ) {
112
	var parser = IntegerCurrency.createCurrencyParser( 'en' );
113
	var inputs = [
114
		'',
115
		'.01',
116
		'A.1',
117
		'1.*',
118
		'CAFFE',
119
		'1,2',
120
		'1.2.3'
121
	];
122
	inputs.map( function ( invalidInput ) {
123
		t.throws( function() { parser.parse( invalidInput ); },
124
			'"' + invalidInput + '" should throw exception' );
125
	});
126
	t.end();
127
} );
128
129
test( 'Other locales than German and english throw and error', function ( t ) {
130
	t.throws( function () { IntegerCurrency.createCurrencyFormatter( 'fr' ); }, 'Unsupported locale' );
131
	t.throws( function () { IntegerCurrency.createCurrencyParser( 'fr' ); }, 'Unsupported locale' );
132
	t.end();
133
} );
134
135
test( 'German locale without decimals - valid strings', function ( t ) {
136
	var parser = IntegerCurrency.createCurrencyParser( 'de', false );
137
	var inputsAndExpectedOutputs = [
138
		[ '0,00', 0 ],
139
		[ '0,01', 0 ],
140
		[ '0,99', 0 ],
141
		[ '1,00', 100 ],
142
		[ '10,00', 1000 ],
143
		[ '13,37', 1300 ],
144
145
		// long decimal values should be truncated
146
		[ '13,3373', 1300 ],
147
		[ '1,989', 100 ],
148
		[ '1,991', 100 ],
149
		[ '1,999', 100 ],
150
		[ '17,995', 1700 ],
151
152
		// Values with less than 2 decimal points should be valid
153
		[ '12', 1200 ],
154
		[ '12,9', 1200 ]
155
	];
156
	inputsAndExpectedOutputs.map( function ( io ) {
157
		t.equal( parser.parse( io[0] ), io[1] );
158
	});
159
	t.end();
160
} );
161
162
163
test( 'German locale without decimals - parsing invalid strings', function ( t ) {
164
	var parser = IntegerCurrency.createCurrencyParser( 'de', false );
165
	var inputs = [
166
		'',
167
		',01',
168
		'A,1',
169
		'CAFFE',
170
		'1.2',
171
		'1,2,3'
172
		// '1,*' could be a candidate but we ignore digits so allowing it also make sense
173
	];
174
	inputs.map( function ( invalidInput ) {
175
		t.throws(
176
			function() { parser.parse( invalidInput ); },
177
			'"' + invalidInput + '" should throw exception'
178
		);
179
	});
180
	t.end();
181
} );
182