{"size": 3118, "ext": "js", "max_stars_repo_path": "things-happened-db/src/test/utils/defaultspecs.js", "max_stars_repo_name": "knurtsysteme/things-happened-db", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "things-happened-db/src/test/utils/defaultspecs.js", "max_issues_repo_name": "knurtsysteme/things-happened-db", "max_issues_repo_issues_event_min_datetime": "2015-02-22T09:34:24.000Z", "max_issues_repo_issues_event_max_datetime": "2015-02-22T09:34:24.000Z", "max_forks_repo_path": "things-happened-db/src/test/utils/defaultspecs.js", "max_forks_repo_name": "knurtsysteme/things-happened-db", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 26.8793103448, "max_line_length": 84, "alphanum_fraction": 0.5641436818}
var assert = require('assert');

// assertions free helper
var helper = {};
/**
 * subject to test. may is an object or an array of objects.
 */
helper.subject = function(subject) {
  return {
    /**
     * key of subject ...
     */
    key : function(key) {
      return {
        /**
         * return true, if the key exists in subject
         */
        exists : function() {
          var result = false;
          if (Array.isArray(subject)) {
            subject.forEach(function(element) {
              if (typeof element[key] != 'undefined')
                result = true;
            });
          } else {
            if (typeof subject[key] != 'undefined')
              result = true;
          }
          return result;
        },
        isNull : function() {
          return subject[key] == null
        },
        /**
         * return true, if the key equals the given value.
         */
        hasValue : function(value) {
          var objectHasValue = function(object) {
            return helper.subject(object).key(key).exists() && object[key] == value;
          }
          var result = false;
          if (Array.isArray(subject)) {
            for (var i = 0; i < subject.length; i++) {
              if (objectHasValue(subject[i])) {
                result = true;
                break;
              }
            }
          } else {
            result = objectHasValue(subject);
          }
          return result;
        }
      }
    }
  }
};

var assertResponse = {};
assertResponse.hasStatus = function(code) {
  return function(response) {
    assert.equal(response.statusCode, code);
  };
};
assertResponse.hasStatus200 = function() {
  return assertResponse.hasStatus(200);
};
assertResponse.json = {};
assertResponse.json.isOk = function() {
  return assertResponse.json.key('_ok').hasValue(1);
};
assertResponse.json.hasLength = function(length) {
  return function(response) {
    var json = JSON.parse(response.body);
    assert.isArray(json);
    assert.equal(json.length, length);
  }
};
assertResponse.json.key = function(key) {
  var result = {};
  result.exists = function() {
    return function(response) {
      var json = JSON.parse(response.body);
      assert.isTrue(helper.subject(json).key(key).exists());
    };
  };
  result.isNull = function() {
    return function(response) {
      var json = JSON.parse(response.body);
      assert.isTrue(helper.subject(json).key(key).isNull());
    }
  },
  result.hasValue = function(value) {
    return function(response) {
      var json = JSON.parse(response.body);
      assert.isTrue(helper.subject(json).key(key).hasValue(value));
    }
  };
  result.not = {};
  result.not.exists = function() {
    return function(response) {
      var json = JSON.parse(response.body);
      assert.isFalse(helper.subject(json).key(key).exists());
    }
  };
  result.not.hasValue = function(value) {
    return function(response) {
      var json = JSON.parse(response.body);
      assert.isFalse(helper.subject(json).key(key).hasValue(value));
    }
  };
  return result;
};

exports.assertResponse = assertResponse;
