'use strict';


var assert = require('chai').assert;


describe('Utils', function () {

  it('fromCodePoint', function () {
    var fromCodePoint = require('../lib/common/utils').fromCodePoint;

    assert.strictEqual(fromCodePoint(0x20), ' ');
    assert.strictEqual(fromCodePoint(0x1F601), '😁');
  });

  it('isValidEntityCode', function () {
    var isValidEntityCode = require('../lib/common/utils').isValidEntityCode;

    assert.strictEqual(isValidEntityCode(0x20), true);
    assert.strictEqual(isValidEntityCode(0xD800), false);
    assert.strictEqual(isValidEntityCode(0xFDD0), false);
    assert.strictEqual(isValidEntityCode(0x1FFFF), false);
    assert.strictEqual(isValidEntityCode(0x1FFFE), false);
    assert.strictEqual(isValidEntityCode(0x00), false);
    assert.strictEqual(isValidEntityCode(0x0B), false);
    assert.strictEqual(isValidEntityCode(0x0E), false);
    assert.strictEqual(isValidEntityCode(0x7F), false);
  });

  it('replaceEntities', function () {
    var replaceEntities = require('../lib/common/utils').replaceEntities;

    assert.strictEqual(replaceEntities('&'), '&');
    assert.strictEqual(replaceEntities(' '), ' ');
    assert.strictEqual(replaceEntities(' '), ' ');
    assert.strictEqual(replaceEntities('&&'), '&&');

    assert.strictEqual(replaceEntities('&am;'), '&am;');
    assert.strictEqual(replaceEntities('�'), '�');
  });

  it('assign', function () {
    var assign = require('../lib/common/utils').assign;

    assert.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 });
    assert.throws(function () {
      assign({}, 123);
    });
  });

  it('fixBrokenSurrogates', function () {
    var fixBrokenSurrogates = require('../lib/common/utils').fixBrokenSurrogates;

    // Bad
    assert.strictEqual(fixBrokenSurrogates('\uD800foo'), '\uFFFDfoo');
    assert.strictEqual(fixBrokenSurrogates('foo\uD800'), 'foo\uFFFD');
    assert.strictEqual(fixBrokenSurrogates('\uDC00foo'), '\uFFFDfoo');
    assert.strictEqual(fixBrokenSurrogates('foo\uDC00'), 'foo\uFFFD');

    // Good
    assert.strictEqual(fixBrokenSurrogates('\uD800\uDC00'), '\uD800\uDC00');
  });

  it('normalizeLink', function () {
    var normalizeLink = require('../lib/common/utils').normalizeLink;

    // broken surrogates sequence (encodeURI should not throw)
    assert.strictEqual(normalizeLink('/\uD800foo'), '/%EF%BF%BDfoo');
    assert.strictEqual(normalizeLink('/\uD900foo'), '/%EF%BF%BDfoo');

    // broken utf-8 encoding (catch decodeURI exception)
    assert.strictEqual(normalizeLink('\u0025test'), '%25test');
  });

  it('escapeRE', function () {
    var escapeRE = require('../lib/common/utils').escapeRE;

    assert.strictEqual(escapeRE(' .?*+^$[]\\(){}|-'), ' \\.\\?\\*\\+\\^\\$\\[\\]\\\\\\(\\)\\{\\}\\|\\-');
  });

  it('isWhiteSpace', function () {
    var isWhiteSpace = require('../lib/common/utils').isWhiteSpace;

    assert.strictEqual(isWhiteSpace(0x2000), true);
    assert.strictEqual(isWhiteSpace(0x09), true);

    assert.strictEqual(isWhiteSpace(0x30), false);
  });

  it('isMdAsciiPunct', function () {
    var isMdAsciiPunct = require('../lib/common/utils').isMdAsciiPunct;

    assert.strictEqual(isMdAsciiPunct(0x30), false);

    '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.split('').forEach(function (ch) {
      assert.strictEqual(isMdAsciiPunct(ch.charCodeAt(0)), true);
    });


  });

});