codingame-connector
Version:
An interface for Codingame website
75 lines (65 loc) • 2.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _promise = require("babel-runtime/core-js/promise");
var _promise2 = _interopRequireDefault(_promise);
var _error = require("../error.js");
var _error2 = _interopRequireDefault(_error);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var name = "fail-expected";
/**
* Attempt to parse the body of a successful request to Codingame test API.
* This function will try to map a response for a failed test because of a unexpected result for the test.
*
* @function parse
* @param {Object} body Body of the response
* @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful
* @throws {Error} Throw is parsing failed
* @instance
*/
/**
* @file Module 'codingame/parsers/failexpected'
* @author woshilapin <woshilapin@tuziwo.info>
*/
/**
* Parse failed response from Codingame when test found a value different from the expected.
*
* The typical response in this case if of the following form.
* ```json
* {
* "success": {
* "output": '43',
* "comparison": {
* "success": false,
* "found": '43',
* "expected": '42'
* }
* }
* }
* ```
* @module codingame/parsers/failexpected
*/
var parse = function parse(body) {
try {
var output = body["output"],
_body$comparison = body["comparison"],
success = _body$comparison["success"],
expected = _body$comparison["expected"],
found = _body$comparison["found"];
if (!success && expected !== undefined && found !== undefined) {
var error = new _error2.default("Expected <" + body.comparison.expected + "> but found <" + body.comparison.found + ">");
return _promise2.default.reject(error);
} else {
throw "Success value should be false when expected and found properties are provided";
}
} catch (error) {
var message = "The body is not of response type '" + name + "'\n";
message += error.message;
throw new Error(message);
}
};
exports.default = {
"name": name,
"parse": parse
};
;