src/codingame/parsers/failexpected.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 41
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 41
rs 10
c 1
b 0
f 0
wmc 5
mnd 2
bc 5
fnc 1
bpm 5
cpm 5
noi 3

1 Function

Rating   Name   Duplication   Size   Complexity  
B failexpected.js ➔ ??? 0 22 5
1
/**
2
 * @file Module 'codingame/parsers/failexpected'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse failed response from Codingame when test found a value different from the expected.
7
 *
8
 * The typical response in this case if of the following form.
9
 * ```json
10
 * {
11
 * 	"success": {
12
 * 		"output": '43',
13
 * 		"comparison": {
14
 * 			"success": false,
15
 * 			"found": '43',
16
 * 			"expected": '42'
17
 * 		}
18
 * 	}
19
 * }
20
 * ```
21
 * @module codingame/parsers/failexpected
22
 */
23
import CodingameError from '../error.js';
24
25
let name = `fail-expected`;
26
27
/**
28
 * Attempt to parse the body of a successful request to Codingame test API.
29
 * This function will try to map a response for a failed test because of a unexpected result for the test.
30
 *
31
 * @function parse
32
 * @param {Object} body Body of the response
33
 * @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful
34
 * @throws {Error} Throw is parsing failed
35
 * @instance
36
 */
37
let parse = (body) => {
38
	try {
39
		let {
40
			"output": output,
41
			"comparison": {
42
				"success": success,
43
				"expected": expected,
44
				"found": found
45
			}
46
		} = body;
47
		if (!success && expected !== undefined && found !== undefined) {
0 ignored issues
show
Comprehensibility Bug Compatibility introduced by
Using found !== undefined to check if a variable is declared may throw an Error. Consider using typeof {name} === "undefined"instead.
Loading history...
Best Practice introduced by
If you intend to check if the variable success is declared in the current environment, consider using typeof success === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility Bug Compatibility introduced by
Using expected !== undefined to check if a variable is declared may throw an Error. Consider using typeof {name} === "undefined"instead.
Loading history...
48
			let error = new CodingameError(`Expected <${body.comparison.expected}> but found <${body.comparison.found}>`);
49
			return Promise.reject(error);
50
		} else {
51
			throw `Success value should be false when expected and found properties are provided`;
52
		}
53
	} catch (error) {
54
		let message = `The body is not of response type '${name}'\n`;
55
		message += error.message;
56
		throw new Error(message);
57
	}
58
};
59
60
export default {
61
	"name": name,
62
	"parse": parse
63
};
64