Skip to content

Commit b23053f

Browse files
committed
chapter 03: copy code from chapter 02 to the chapter 03 folder in order to continue with the book
1 parent 4a2856b commit b23053f

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ArgumentError extends Error {}
2+
3+
module.exports = ArgumentError;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const ArgumentError = require('./ArgumentError');
2+
3+
function logAnalyzer() {
4+
/**
5+
* @type {boolean}
6+
*/
7+
let wasLastFileNameValid;
8+
9+
/**
10+
* @return {boolean}
11+
*/
12+
function getWasLastFileNameValid() {
13+
return wasLastFileNameValid;
14+
}
15+
16+
/**
17+
* @param {string} fileName
18+
* @return {boolean}
19+
*/
20+
function isValidLogFileName(fileName) {
21+
wasLastFileNameValid = false;
22+
23+
if (fileName === '') {
24+
throw new ArgumentError('filename has to be provided');
25+
}
26+
27+
if (!fileName.toUpperCase().endsWith('.SLF')) {
28+
return false;
29+
}
30+
31+
wasLastFileNameValid = true;
32+
return true;
33+
}
34+
35+
return {
36+
getWasLastFileNameValid,
37+
isValidLogFileName,
38+
};
39+
}
40+
41+
module.exports = logAnalyzer;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const logAnalyzer = require('./logAnalyzer');
2+
3+
let logAnalyzerInstance;
4+
beforeEach(() => {
5+
logAnalyzerInstance = logAnalyzer();
6+
});
7+
8+
describe.each([
9+
['johndoe.js', false],
10+
['johndoe.slf', true],
11+
['johndoe.SLF', true],
12+
])('isValidLogFileName("%s"))', (fileName, expected) => {
13+
it(`bad extension returns ${expected}`, () => {
14+
const result = logAnalyzerInstance.isValidLogFileName(fileName);
15+
expect(result).toBe(expected);
16+
});
17+
});
18+
19+
describe('isValidLogFileName', () => {
20+
it('empty filename throws error', () => {
21+
function emptyLogFileName() {
22+
logAnalyzerInstance.isValidLogFileName('');
23+
}
24+
25+
expect(emptyLogFileName).toThrow('filename has to be provided');
26+
});
27+
28+
/**
29+
* an example of state-based testing
30+
*/
31+
it.each`
32+
fileName | expected
33+
${'johndoe.foo'} | ${false}
34+
${'johndoe.slf'} | ${true}
35+
`(
36+
'when called there changes wasLastFileNameValid that returns $expected',
37+
({ fileName, expected }) => {
38+
console.log(fileName);
39+
logAnalyzerInstance.isValidLogFileName(fileName);
40+
const result = logAnalyzerInstance.getWasLastFileNameValid();
41+
42+
expect(result).toBe(expected);
43+
}
44+
);
45+
});

0 commit comments

Comments
 (0)