var validator = require('../validator')
  , format = require('util').format;

function test(options) {
    var args = options.args || [];
    args.unshift(null);
    Object.keys(options.expect).forEach(function (input) {
        args[0] = input;
        var result = validator[options.sanitizer].apply(validator, args)
          , expected = options.expect[input];
        if (isNaN(result) && isNaN(expected)) {
            return;
        }
        if (result !== expected) {
            var warning = format('validator.%s(%s) returned "%s" but should have returned "%s"',
                options.sanitizer, args.join(', '), result, expected);
            throw new Error(warning);
        }
    });
}

describe('Sanitizers', function () {

    it('should sanitize boolean strings', function () {
        test({
            sanitizer: 'toBoolean'
          , expect: {
                '0': false
              , '': false
              , '1': true
              , 'true': true
              , 'foobar': true
              , '   ': true
            }
        });
        test({
            sanitizer: 'toBoolean'
          , args: [ true ] //strict
          , expect: {
                '0': false
              , '': false
              , '1': true
              , 'true': true
              , 'foobar': false
              , '   ': false
            }
        });
    });

    it('should trim whitespace', function () {
        test({
            sanitizer: 'trim'
          , expect: { '  \r\n\tfoo  \r\n\t   ': 'foo' }
        });
        test({
            sanitizer: 'ltrim'
          , expect: { '  \r\n\tfoo  \r\n\t   ': 'foo  \r\n\t   ' }
        });
        test({
            sanitizer: 'rtrim'
          , expect: { '  \r\n\tfoo  \r\n\t   ': '  \r\n\tfoo' }
        });
    });

    it('should trim custom characters', function () {
        test({
            sanitizer: 'trim'
          , args: [ '01' ]
          , expect: { '010100201000': '2' }
        });
        test({
            sanitizer: 'ltrim'
          , args: [ '01' ]
          , expect: { '010100201000': '201000' }
        });
        test({
            sanitizer: 'rtrim'
          , args: [ '01' ]
          , expect: { '010100201000': '0101002' }
        });
    });

    it('should convert strings to integers', function () {
        test({
            sanitizer: 'toInt'
          , expect: {
                '3': 3
              , ' 3 ': 3
              , '2.4': 2
              , 'foo': NaN
            }
        });
        test({
            sanitizer: 'toInt'
          , args: [ 16 ]
          , expect: { 'ff': 255 }
        });
    });

    it('should convert strings to floats', function () {
        test({
            sanitizer: 'toFloat'
          , expect: {
                '2': 2.0
              , '2.': 2.0
              , '-2.5': -2.5
              , '.5': 0.5
              , 'foo': NaN
            }
        });
    });

    it('should escape HTML', function () {
        test({
            sanitizer: 'escape'
          , expect: {
                '<img alt="foo&bar">': '&lt;img alt=&quot;foo&amp;bar&quot;&gt;'
            }
        });
    });

    it('should sanitize a string based on a whitelist', function () {
        test({
            sanitizer: 'whitelist'
          , args: [ 'abc' ]
          , expect: {
                'abcdef': 'abc'
              , 'aaaaaaaaaabbbbbbbbbb': 'aaaaaaaaaabbbbbbbbbb'
              , 'a1b2c3': 'abc'
              , '   ': ''
            }
        });
    });

    it('should sanitize a string based on a blacklist', function () {
        test({
            sanitizer: 'blacklist'
          , args: [ 'abc' ]
          , expect: {
                'abcdef': 'def'
              , 'aaaaaaaaaabbbbbbbbbb': ''
              , 'a1b2c3': '123'
              , '   ': '   '
            }
        });
    });

});
