'use strict';

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _mocha = require('mocha');

var _setVariable = require('../../src/behaviors/setVariable.js');

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _mocha.describe)('BehaviorSetVariable', function () {
    (0, _mocha.describe)('expression', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/setVariable.expression.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }

                var opts = JSON.parse(options);
                var expected = ['varMap["PMUSER_TEST_1"] = function()', '\tlocal function setVar()', '\t\tlocal result', '\t\t-- EXPRESSION', '\t\tresult = swapVars("hostname is {{builtin.AK_HOST}}")', '\t\treturn result', '\tend', '\treturn setVar()', 'end'];

                var behavior = new _setVariable.BehaviorSetVariable(opts);
                var actual = behavior.process();

                _assert2.default.equal(actual.join('\n'), expected.join('\n'));
                done();
            });
        });
    });

    (0, _mocha.describe)('generate', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/setVariable.generate.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }

                var opts = JSON.parse(options);
                var expected = ['varMap["PMUSER_TEST_1"] = function()', '\tlocal function setVar()', '\t\tlocal result', '\t\t-- GENERATE: ' + opts.generator, '\t\tmath.randomseed(os.time())', '\t\tresult = math.random(' + opts.minRandomNumber + ', ' + opts.maxRandomNumber + ')', '\t\treturn result', '\tend', '\treturn setVar()', 'end'];

                var behavior = new _setVariable.BehaviorSetVariable(opts);
                var actual = behavior.process();

                _assert2.default.equal(actual.join('\n'), expected.join('\n'));
                done();
            });
        });
    });

    (0, _mocha.describe)('extract', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/setVariable.extract-qs-upper.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }

                var opts = JSON.parse(options);
                var expected = ['varMap["PMUSER_TEST_1"] = function()', '\tlocal function setVar()', '\t\tlocal result', '\t\t-- EXTRACT: ' + opts.extractLocation, '\t\tresult = cs(ngx.req.get_uri_args()["' + opts.queryParameterName + '"])', '\t\t-- TRANSFORM: ' + opts.transform, '\t\tlocal options = { }', '\t\toptions["variableName"] = "' + opts.variableName + '"', '\t\toptions["valueSource"] = "' + opts.valueSource + '"', '\t\toptions["transform"] = "' + opts.transform + '"', '\t\toptions["extractLocation"] = "' + opts.extractLocation + '"', '\t\toptions["queryParameterName"] = "' + opts.queryParameterName + '"', '\t\treturn transformVariable(result, options)', '\tend', '\treturn setVar()', 'end'];

                var behavior = new _setVariable.BehaviorSetVariable(opts);
                var actual = behavior.process();

                _assert2.default.equal(actual.join('\n'), expected.join('\n'));
                done();
            });
        });
    });
});