"use strict";

var _ = require('lodash');
var tester = require('./_lib/tester');
var Verifier = require('./_lib/lib');

exports.flow = {
	standard_call: tester([
		{ rules: 'type string',                       value: 4,      verr: { rule: 'type', params: 'string', index: null } },
		{ rules: 'type string',                       value: "",     expect: true },
		{ rules: ['type string'],                     value: 4,      verr: { rule: 'type', params: 'string', index: null } },
		{ rules: ['type string'],                     value: "",     expect: true },
		{ rules: {type: 'string'},                    value: 4,      verr: { rule: 'type', params: 'string', index: null } },
		{ rules: {type: 'string'},                    value: "",     expect: true },
		{ rules: [{type: 'string'}],                  value: 4,      verr: { rule: 'type', params: 'string', index: null } },
		{ rules: [{type: 'string'}],                  value: "",     expect: true },
		{ rules: ['type string', 'max_length 2'],     value: 4,      verr: { rule: 'type', params: 'string', index: null } },
		{ rules: ['type string', 'max_length 2'],     value: "1",    expect: true },
		{ rules: [{type: 'string'}, {max_length: 2}], value: 4,      verr: { rule: 'type', params: 'string', index: null } },
		{ rules: [{type: 'string'}, {max_length: 2}], value: "1",    expect: true }
	]),

	'create by function': function (test) {
		var verifier1 = new Verifier('type string');
		var verifier2 = Verifier('type string'); // jshint ignore : line

		test.ok(_.isEqual(verifier1, verifier2));
		test.done();
	},

	'throws error if rules is incorrect': function (test) {

		test.throws(function () {
			var verifier = new Verifier();
		});

		test.throws(function () {
			var verifier = new Verifier(123);
		});

		test.throws(function () {
			var verifier = new Verifier('');
		});

		test.doesNotThrow(function () {
			var verifier = Verifier([]); // jshint ignore : line
		});

		test.throws(function () {
			var CustomRule = new Verifier.Rule.extend();
		});

		test.throws(function () {
			var CustomRule = new Verifier.Rule.extend( { name: 'some' } );
		});

		var error = new Error('hello');
		var CustomRule = Verifier.Rule.extend({
			check: function (value, params, done) {
				return error;
			}
		});

		Verifier.Rule.add("some", CustomRule);

		test.throws(function () {
			Verifier.Rule.add();
		});

		test.throws(function () {
			Verifier.Rule.add(123);
		});

		test.doesNotThrow(function () {
			Verifier.Rule.add("some", CustomRule, false);
		});

		test.throws(function () {
			Verifier.Rule.add("some");
		});

		test.throws(function () {
			Verifier.Rule.add("some1", 123);
		});

		test.throws(function () {
			Verifier.Rule.add("some1", function () {});
		});

		test.throws(function () {
			var f = function () {};
			f.prototype = { check: function () {} };
			Verifier.Rule.add("some1", function () {});
		});

		test.throws(function () {
			Verifier.Rule.get('some2');
		});

		test.doesNotThrow(function () {
			Verifier.Rule.get('some2', false);
		});

		var rule = new CustomRule();

		var MyCustomRule = CustomRule.extend({
			check: function () {
				throw error;
			}
		});

		var myRule = new MyCustomRule();

		rule.verify(123, function (err) {
			test.ok(err === error);

			myRule.verify(234, function (err) {
				test.ok(err === error);
				test.done();
			});
		});
	}
};

@@ -1980,36 +1980,32 @@
 var CustomRule =
- new
  Verifier.Rule.e
@@ -2010,16 +2010,18 @@
 .extend(
+%7B%7D
 );%0A%09%09%7D);
@@ -2069,20 +2069,16 @@
 omRule =
- new
  Verifie
@@ -3130,20 +3130,24 @@
 nction (
+aaaa
 ) %7B%0A
-
 %09%09%09%09thro
@@ -3334,16 +3334,250 @@
 error);%0A
+%0A%0A%09%09%09%09var MyRule2 = Verifier.Rule.extend(%7B%0A%09%09%09%09%09check: function (value, params, done) %7B%0A%09%09%09%09%09%09done();%0A%09%09%09%09%09%09done();%0A%09%09%09%09%09%09done();%0A%09%09%09%09%09%09done();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%0A%09%09%09%09var myRule2= new MyRule2(123);%0A%09%09%09%09myRule2.verify(123, function () %7B%0A%09
 %09%09%09%09test
@@ -3585,16 +3585,24 @@
 done();%0A
+%09%09%09%09%7D);%0A
 %09%09%09%7D);%0A%09

fix main test. add test for extend property "check" error. add test for multiple done callback call