-
-
Notifications
You must be signed in to change notification settings - Fork 810
/
Copy pathtest.sync.js
172 lines (126 loc) · 3.81 KB
/
test.sync.js
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
'use strict';
// MODULES //
var tape = require( 'tape' );
var join = require( 'path' ).join;
var isBrowser = require( '@stdlib/utils/is-browser' );
var readJSON = require( './../lib/sync.js' );
// FIXME //
// Don't run tests in the browser...for now...
var opts = {
'skip': isBrowser
};
// FIXTURES //
var goodJSON = join( __dirname, 'fixtures', 'good.json' );
var badJSON = join( __dirname, 'fixtures', 'bad.json.txt' );
var bomJSON = join( __dirname, 'fixtures', 'bom.json.txt' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof readJSON, 'function', 'main export is a function' );
t.end();
});
tape( 'the function throws an error if provided an options argument which is neither a string or object', function test( t ) {
var values;
var i;
values = [
5,
NaN,
true,
null,
undefined,
[],
function noop() {}
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] );
}
t.end();
function badValue( value ) {
return function badValue() {
readJSON( goodJSON, value );
};
}
});
tape( 'if the function encounters an error when attempting to read a file, the function returns the error', opts, function test( t ) {
var out = readJSON( 'beepboopbapbop' );
t.equal( out instanceof Error, true, 'returns an error' );
t.end();
});
tape( 'if the function encounters an error when attempting to read a file, the function returns the error (options)', opts, function test( t ) {
var out;
out = readJSON( 'beepboopbapbop', 'utf8' );
t.equal( out instanceof Error, true, 'returns an error' );
out = readJSON( 'beepboopbapbop', {
'encoding': 'utf8'
});
t.equal( out instanceof Error, true, 'returns an error' );
t.end();
});
tape( 'if the function encounters an error when attempting to parse file contents as JSON, the function returns the error', opts, function test( t ) {
var out = readJSON( badJSON );
t.equal( out instanceof Error, true, 'returns an error' );
t.end();
});
tape( 'if the function encounters an error when attempting to parse file contents as JSON, the function returns the error (options)', opts, function test( t ) {
var out;
out = readJSON( badJSON, 'utf8' );
t.equal( out instanceof Error, true, 'returns an error' );
out = readJSON( badJSON, {
'encoding': 'utf8'
});
t.equal( out instanceof Error, true, 'returns an error' );
t.end();
});
tape( 'the function reads a file as JSON', opts, function test( t ) {
var expected;
var actual;
expected = require( goodJSON );
actual = readJSON( goodJSON );
t.deepEqual( actual, expected, 'returns a file as JSON' );
t.end();
});
tape( 'the function reads a file as JSON using provided options', opts, function test( t ) {
var expected;
var actual;
// String options:
expected = require( goodJSON );
actual = readJSON( goodJSON, 'utf8' );
t.deepEqual( expected, actual, 'returns a file as JSON' );
// Object options:
actual = readJSON( goodJSON, {
'encoding': 'utf8'
});
t.deepEqual( actual, expected, 'returns a file as JSON' );
t.end();
});
tape( 'the function supports reading a UTF-8 encoded file having a byte order mark (BOM) as JSON', opts, function test( t ) {
var expected;
var actual;
expected = require( goodJSON );
actual = readJSON( bomJSON, {
'encoding': 'utf8'
});
t.deepEqual( actual, expected, 'returns a file as JSON' );
t.end();
});
tape( 'the function supports providing a JSON reviver', opts, function test( t ) {
var expected;
var actual;
expected = {
'beep': 'boop',
'bop': [ 1, 2, 3 ],
'bool': true,
'bap': 'woot'
};
actual = readJSON( goodJSON, {
'reviver': reviver
});
t.deepEqual( actual, expected, 'deep equal' );
t.end();
function reviver( key, value ) {
if ( key === 'bap' ) {
return 'woot';
}
return value;
}
});