"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var assert = require("assert");
var oniguruma_1 = require("../src/oniguruma");
describe('OnigScanner', function () {
    describe('::findNextMatchSync', function () {
        it('returns the index of the matching pattern', function () {
            var scanner = new oniguruma_1.OnigScanner(['a', 'b', 'c']);
            assert.equal(scanner.findNextMatchSync('x', 0), null);
            assert.equal(scanner.findNextMatchSync('xxaxxbxxc', 0).index, 0);
            assert.equal(scanner.findNextMatchSync('xxaxxbxxc', 4).index, 1);
            assert.equal(scanner.findNextMatchSync('xxaxxbxxc', 7).index, 2);
            assert.equal(scanner.findNextMatchSync('xxaxxbxxc', 9), null);
        });
        describe('when the string searched contains unicode characters', function () {
            return it('returns the correct matching pattern', function () {
                var scanner = new oniguruma_1.OnigScanner(['1', '2']);
                var match = scanner.findNextMatchSync('ab…cde21', 5);
                assert.equal(match.index, 1);
                scanner = new oniguruma_1.OnigScanner(['\"']);
                match = scanner.findNextMatchSync('{"…": 1}', 1);
                assert.deepEqual(match.captureIndices, [{ index: 0, start: 1, end: 2, length: 1 }]);
            });
        });
        describe('when the string searched contains surrogate pairs', function () {
            return it('counts paired characters as 2 characters in both arguments and return values', function () {
                var scanner = new oniguruma_1.OnigScanner(['Y', 'X']);
                var match = scanner.findNextMatchSync('a💻bYX', 0);
                assert.deepEqual(match.captureIndices, [{ index: 0, start: 4, end: 5, length: 1 }]);
                match = scanner.findNextMatchSync('a💻bYX', 1);
                assert.deepEqual(match.captureIndices, [{ index: 0, start: 4, end: 5, length: 1 }]);
                match = scanner.findNextMatchSync('a💻bYX', 3);
                assert.deepEqual(match.captureIndices, [{ index: 0, start: 4, end: 5, length: 1 }]);
                match = scanner.findNextMatchSync('a💻bYX', 4);
                assert.deepEqual(match.captureIndices, [{ index: 0, start: 4, end: 5, length: 1 }]);
                match = scanner.findNextMatchSync('a💻bYX', 5);
                assert.equal(match.index, 1);
                assert.deepEqual(match.captureIndices, [{ index: 0, start: 5, end: 6, length: 1 }]);
            });
        });
        it("returns false when the input string isn't a string", function () {
            var scanner = new oniguruma_1.OnigScanner(['1']);
            assert.throws(function () { return scanner.findNextMatchSync(); }, /invalid value: undefined/);
            assert.throws(function () { return scanner.findNextMatchSync(null); }, /invalid value: object/);
            assert.throws(function () { return scanner.findNextMatchSync(undefined); }, /invalid value: undefined/);
            assert.throws(function () { return scanner.findNextMatchSync(2); }, /invalid value: number/);
            assert.throws(function () { return scanner.findNextMatchSync(false); }, /invalid value: boolean/);
        });
        it("uses 0 as the start position when the input start position isn't a valid number", function () {
            var scanner = new oniguruma_1.OnigScanner(['1']);
            assert.equal(scanner.findNextMatchSync('a1', Infinity).index, 0);
            assert.equal(scanner.findNextMatchSync('a1', -1).index, 0);
            assert.equal(scanner.findNextMatchSync('a1', false).index, 0);
            assert.equal(scanner.findNextMatchSync('a1', 'food').index, 0);
        });
    });
    describe('when the regular expression contains double byte characters', function () {
        return it('returns the correct match length', function () {
            var scanner = new oniguruma_1.OnigScanner(['Возврат']);
            var match = scanner.findNextMatchSync('Возврат long_var_name;', 0);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 0, end: 7, length: 7 }]);
        });
    });
    describe('when the input string contains invalid surrogate pairs', function () {
        return it('interprets them as a code point', function () {
            var scanner = new oniguruma_1.OnigScanner(['X']);
            var match = scanner.findNextMatchSync("X" + String.fromCharCode(0xd83c) + "X", 0);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 0, end: 1, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xd83c) + "X", 1);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 2, end: 3, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xd83c) + "X", 2);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 2, end: 3, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xdfff) + "X", 0);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 0, end: 1, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xdfff) + "X", 1);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 2, end: 3, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xdfff) + "X", 2);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 2, end: 3, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xd800) + String.fromCharCode(0xdc00) + "X", 2);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 3, end: 4, length: 1 }]);
            match = scanner.findNextMatchSync("X" + String.fromCharCode(0xdbff) + String.fromCharCode(0xdfff) + "X", 2);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 3, end: 4, length: 1 }]);
        });
    });
    describe('when the start offset is out of bounds', function () {
        return it('it gets clamped', function () {
            var scanner = new oniguruma_1.OnigScanner(['X']);
            var match = scanner.findNextMatchSync('X💻X', -1000);
            assert.deepEqual(match.captureIndices, [{ index: 0, start: 0, end: 1, length: 1 }]);
            match = scanner.findNextMatchSync('X💻X', 1000);
            assert.equal(match, null);
        });
    });
});
//# sourceMappingURL=scanner_test.js.map