Completed
Push — master ( b8d05c...c84308 )
by wiese
269:29 queued 204:24
created

test_integer_currency.js ➔ ... ➔ inputs.map   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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 4
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
		[ '12', 1200 ]
33
	];
34
	inputsAndExpectedOutputs.map( function ( io ) {
35
		t.equal( parser.parse( io[0] ), io[1] );
36
	});
37
	t.end();
38
} );
39
40
test( 'German locale - parsing invalid strings', function ( t ) {
41
	var parser = IntegerCurrency.createCurrencyParser( 'de' );
42
	var inputs = [
43
		'',
44
		',01',
45
		'A,1',
46
		'1,*',
47
		'CAFFE',
48
		'1.2',
49
		'1,2,3',
50
		'13,33337'
51
	];
52
	inputs.map( function ( invalidInput ) {
53
		t.throws( function() { parser.parse( invalidInput ); },
54
			'"' + invalidInput + '" should throw exception' );
55
	});
56
	t.end();
57
} );
58
59
test( 'English locale - formatting an integer ', function ( t ) {
60
	var formatter = IntegerCurrency.createCurrencyFormatter( 'en' );
61
	var inputsAndExpectedOutputs = [
62
		[ 0, '0.00' ],
63
		[ 1, '0.01' ],
64
		[ 99, '0.99' ],
65
		[ 100, '1.00' ],
66
		[ 1000, '10.00' ],
67
		[ 1337, '13.37' ]
68
	];
69
	inputsAndExpectedOutputs.map( function ( io ) {
70
		t.equal( formatter.format( io[0] ), io[1] );
71
	});
72
	t.end();
73
} );
74
75
test( 'English locale - parsing valid strings', function ( t ) {
76
	var parser = IntegerCurrency.createCurrencyParser( 'en' );
77
	var inputsAndExpectedOutputs = [
78
		[ '0.00', 0 ],
79
		[ '0.01', 1 ],
80
		[ '0.99', 99 ],
81
		[ '1.00', 100 ],
82
		[ '10.00', 1000 ],
83
		[ '13.37', 1337 ],
84
		[ '12', 1200 ]
85
	];
86
	inputsAndExpectedOutputs.map( function ( io ) {
87
		t.equal( parser.parse( io[0] ), io[1] );
88
	});
89
	t.end();
90
} );
91
92
test( 'English locale - parsing invalid strings', function ( t ) {
93
	var parser = IntegerCurrency.createCurrencyParser( 'en' );
94
	var inputs = [
95
		'',
96
		'.01',
97
		'A.1',
98
		'1.*',
99
		'CAFFE',
100
		'1,2',
101
		'1.2.3',
102
		'13.33337'
103
	];
104
	inputs.map( function ( invalidInput ) {
105
		t.throws( function() { parser.parse( invalidInput ); },
106
			'"' + invalidInput + '" should throw exception' );
107
	});
108
	t.end();
109
} );
110
111
test( 'Other locales than German and english throw and error', function ( t ) {
112
	t.throws( function () { IntegerCurrency.createCurrencyFormatter( 'fr' ); }, 'Unsupported locale' );
113
	t.throws( function () { IntegerCurrency.createCurrencyParser( 'fr' ); }, 'Unsupported locale' );
114
	t.end();
115
} );