"use strict";

require('colors');
var _ = require('lodash');
var Verifier = require('./lib');
var async = require('async');

var inspect = function (value) {
	return '\n' + require('util').inspect(value, {depth: null, colors: true}) + '\n';
};

var tester = function (cases) {
	return function (test) {
		async.each(cases, function (testCase, done) {
			var verifier;
			try {
				verifier = new Verifier(testCase.rules);
			} catch (e) {
				if (testCase.error) {
					if (typeof testCase.error === 'string') {
						var r = e.message.indexOf('#' + testCase.error + ': ') === 0;

						if (!r) {
							console.error(('must be error! #'+testCase.error + ' -> given "' + e.message + '"').red);
						}

						test.ok(r);
					}
					test.ok(true);
					done();
					return;
				}

				done(e);
				return;
			}

			if (testCase.error) {
				console.log('must be error! ' + testCase.error);
				done('must be error!');
				return;
			}

			verifier.verify(testCase.value, function (err) {
				var isValid = !err;
				if (err) {
					if (err instanceof Verifier.Rule.ValidationError) {
						if (testCase.expect) {
							console.log('validation-error >> ', inspect(err));
						}

						test.ok(!testCase.expect);

						if (testCase.verr) {
							_.each(testCase.verr, function (v, k) {
								test.ok(_.isEqual(err[k], v), 'must be equal:'+inspect(v)+'<<and>>'+inspect(err[k]));
							});
						}

						done();
						return;
					}

					console.log('unexpected error Error('.red, err instanceof Error, ') \n>>'.red, err, '\n', inspect(testCase).cyan);
					done(err);
					return;
				}

				test.ok(isValid);
				done(err);
			});
		}, test.done);
	};
};

module.exports = tester;
@@ -296,16 +296,135 @@
 test) %7B%0A
+%09%09var testCases = _.map(cases, function (testCase, i) %7B%0A%09%09%09return %7B%0A%09%09%09%09testCase: testCase,%0A%09%09%09%09number: i%0A%09%09%09%7D;%0A%09%09%7D);%0A%0A
 %09%09async.
@@ -428,17 +428,21 @@
 nc.each(
-c
+testC
 ases, fu
@@ -457,16 +457,19 @@
 testCase
+Obj
 , done) 
@@ -470,16 +470,92 @@
 done) %7B%0A
+%09%09%09var testCase = testCaseObj.testCase;%0A%09%09%09var number = testCaseObj.number;%0A
 %09%09%09var v
@@ -813,16 +813,37 @@
 e.error(
+'%3E%3E%3E #'+number+': ', 
 ('must b
@@ -1069,16 +1069,37 @@
 ole.log(
+'%3E%3E%3E #'+number+': ', 
 'must be
@@ -1369,16 +1369,37 @@
 ole.log(
+'%3E%3E%3E #'+number+': ', 
 'validat
@@ -1585,16 +1585,33 @@
 %5D, v), '
+%3E%3E%3E #'+number+': 
 must be 
@@ -1615,16 +1615,26 @@
 be equal
+ (expect:)
 :'+inspe
@@ -1646,11 +1646,16 @@
 +'%3C%3C
-and
+ given: 
 %3E%3E'+
@@ -1724,24 +1724,24 @@
 rn;%0A%09%09%09%09%09%7D%0A%0A
-
 %09%09%09%09%09console
@@ -1745,16 +1745,37 @@
 ole.log(
+'%3E%3E%3E #'+number+': ', 
 'unexpec

add tracing counter in tester