<commit_before>/*
Copyright (c) 2014 <NAME>
Copyright (c) 2014 <NAME>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

var _ = require('lodash');
var assert = require('assert');
var utils = require('./utils');
var events = require('events');
var PREFIX = 'deride';

var Expectations = function(obj, method) {
	var timesCalled = 0;
	var calledWithArgs = {};

	function checkArg(expected, values) {
		return _.any(_.where(values, expected));
	}

	function checkArgs(expectedArgs, callArgs, evaluator) {
		var values = _.values(callArgs);
		var argResults = [];
		for (var argIndex = 0; argIndex < expectedArgs.length; argIndex++) {
			var expected = expectedArgs[argIndex];
			var foundArg = checkArg(expected, values);
			argResults.push(foundArg);
		}
		return evaluator(argResults);
	}

	function checkAnyArgs(expectedArgs, callArgs) {
		return checkArgs(expectedArgs, callArgs, _.any);
	}

	function withArgs() {
		var args = _.values(arguments);
		assertArgsWithEvaluator(calledWithArgs, args, _.all);
		return self.called;
	}

	function withSingleArg(arg) {
		var args = [arg];
		assertArgsWithEvaluator(calledWithArgs, args, _.any);
		return self.called;
	}

	function assertArgsWithEvaluator(argsToCheck, args, evaluator) {
		var callResults = [];
		_.forEach(argsToCheck, function(value) {
			var argResult = checkArgs(args, value, evaluator);
			callResults.push(argResult);
		});
		var result = _.any(callResults);
		assert(result, 'Expected ' + method + ' to be called with: ' + args.join(', '));
	}

	function withArg(args) {
		return function(arg) {
			assert(checkAnyArgs([arg], args));
		};
	}

	function invocation(index) {
		if (!(index.toString() in calledWithArgs)) {
			throw new Error('invocation out of range');
		}
		var arg = calledWithArgs[index.toString()];
		return {
			withArg: withArg(arg)
		};
	}

	function times(number, err) {
		if (!err) {
			err = 'Expected ' + method + ' to be called ' + utils.humanise(number) + ' but was ' + timesCalled;
		}
		assert.equal(timesCalled, number, err);
	}

	function never(err) {
		times(0, err);
	}

	function calledOnce(err) {
		times(1, err);
	}

	function calledTwice(err) {
		times(2, err);
	}

	function reset() {
		timesCalled = 0;
		calledWithArgs = {};
	}

	function call() {
		calledWithArgs[timesCalled++] = arguments;
	}

	function negate(func) {
		return function() {
			var args = _.values(arguments);
			try {
				func.call(null, args);
			} catch (err) {
				return self;
			}
			assert(false);
		};
	}

	function addNotMethods(obj) {
		var methods = {};
		var calledMethods = _.omit(utils.methods(obj.called), 'reset');
		_.each(calledMethods, function(method) {
			methods[method] = negate(obj.called[method]);
		});
		obj.called.not = methods;
		return obj;
	}

	var self = {
		called: {
			times: times,
			never: never,
			once: calledOnce,
			twice: calledTwice,
			reset: reset,
			withArgs: withArgs,
			withArg: withSingleArg
		},
		invocation: invocation,
		call: call
	};

	return (function() {
		return Object.freeze(addNotMethods(self));
	}());
};

var Setup = function(obj, method, emitter) {
	var debug = require('debug')(PREFIX);
	var Promises = require('when');
	var originalMethod = obj[method];
	var callToInvoke = normalCall;
	var callToInvokeOnArguments = {};
	var argumentsPredicate;

	function call() {
		var key = serializeArgs(arguments);
		var callBasedOnArgs = callToInvokeOnArguments[key];
		if (callBasedOnArgs !== null && callBasedOnArgs !== undefined) {
			return callBasedOnArgs.apply(self, arguments);
		} else {
			return callToInvoke.apply(self, arguments);
		}
	}

	function toCallbackWith(args) {
		var func = function() {
			debug('toCallbackWith', args);
			for (var i = 0; i < arguments.length; i++) {
				var argType = typeof(arguments[i]);
				if (argType === 'function') {
					arguments[i].apply(null, args);
				}
			}<commit_msg>Add setup and method to debug prefix<commit_after>/*
Copyright (c) 2014 <NAME>
Copyright (c) 2014 <NAME>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

var _ = require('lodash');
var assert = require('assert');
var utils = require('./utils');
var events = require('events');
var PREFIX = 'deride';

var Expectations = function(obj, method) {
	var timesCalled = 0;
	var calledWithArgs = {};

	function checkArg(expected, values) {
		return _.any(_.where(values, expected));
	}

	function checkArgs(expectedArgs, callArgs, evaluator) {
		var values = _.values(callArgs);
		var argResults = [];
		for (var argIndex = 0; argIndex < expectedArgs.length; argIndex++) {
			var expected = expectedArgs[argIndex];
			var foundArg = checkArg(expected, values);
			argResults.push(foundArg);
		}
		return evaluator(argResults);
	}

	function checkAnyArgs(expectedArgs, callArgs) {
		return checkArgs(expectedArgs, callArgs, _.any);
	}

	function withArgs() {
		var args = _.values(arguments);
		assertArgsWithEvaluator(calledWithArgs, args, _.all);
		return self.called;
	}

	function withSingleArg(arg) {
		var args = [arg];
		assertArgsWithEvaluator(calledWithArgs, args, _.any);
		return self.called;
	}

	function assertArgsWithEvaluator(argsToCheck, args, evaluator) {
		var callResults = [];
		_.forEach(argsToCheck, function(value) {
			var argResult = checkArgs(args, value, evaluator);
			callResults.push(argResult);
		});
		var result = _.any(callResults);
		assert(result, 'Expected ' + method + ' to be called with: ' + args.join(', '));
	}

	function withArg(args) {
		return function(arg) {
			assert(checkAnyArgs([arg], args));
		};
	}

	function invocation(index) {
		if (!(index.toString() in calledWithArgs)) {
			throw new Error('invocation out of range');
		}
		var arg = calledWithArgs[index.toString()];
		return {
			withArg: withArg(arg)
		};
	}

	function times(number, err) {
		if (!err) {
			err = 'Expected ' + method + ' to be called ' + utils.humanise(number) + ' but was ' + timesCalled;
		}
		assert.equal(timesCalled, number, err);
	}

	function never(err) {
		times(0, err);
	}

	function calledOnce(err) {
		times(1, err);
	}

	function calledTwice(err) {
		times(2, err);
	}

	function reset() {
		timesCalled = 0;
		calledWithArgs = {};
	}

	function call() {
		calledWithArgs[timesCalled++] = arguments;
	}

	function negate(func) {
		return function() {
			var args = _.values(arguments);
			try {
				func.call(null, args);
			} catch (err) {
				return self;
			}
			assert(false);
		};
	}

	function addNotMethods(obj) {
		var methods = {};
		var calledMethods = _.omit(utils.methods(obj.called), 'reset');
		_.each(calledMethods, function(method) {
			methods[method] = negate(obj.called[method]);
		});
		obj.called.not = methods;
		return obj;
	}

	var self = {
		called: {
			times: times,
			never: never,
			once: calledOnce,
			twice: calledTwice,
			reset: reset,
			withArgs: withArgs,
			withArg: withSingleArg
		},
		invocation: invocation,
		call: call
	};

	return (function() {
		return Object.freeze(addNotMethods(self));
	}());
};

var Setup = function(obj, method, emitter) {
	var debug = require('debug')(PREFIX + ':setup:'+ method);
	var Promises = require('when');
	var originalMethod = obj[method];
	var callToInvoke = normalCall;
	var callToInvokeOnArguments = {};
	var argumentsPredicate;

	function call() {
		var key = serializeArgs(arguments);
		var callBasedOnArgs = callToInvokeOnArguments[key];
		if (callBasedOnArgs !== null && callBasedOnArgs !== undefined) {
			return callBasedOnArgs.apply(self, arguments);
		} else {
			return callToInvoke.apply(self, arguments);
		}
	}

	function toCallbackWith(args) {
		var func = function() {
			debug('toCallbackWith', args);
			for (var i = 0; i < arguments.length; i++) {
				var argType = typeof(arguments[i]);
				if (argType === 'function') {
					arguments[i].apply(null, args);
				}
			}
{'fraction_non_alphanumeric': 0.12242386957859121, 'fraction_numerical': 0.0030759766225776685, 'mean_word_length': 4.199360341151386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2702168', 'n_tokens_mistral': 3354, 'n_tokens_neox': 3005, 'n_words': 932}