"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _U8a = _interopRequireDefault(require("../codec/U8a"));

var _Text = _interopRequireDefault(require("./Text"));

// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('Text', () => {
  describe('decode', () => {
    const testDecode = function testDecode(type, input, expected) {
      let toFn = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'toString';
      return it("can decode from ".concat(type), () => {
        expect(new _Text.default(input)[toFn]()).toBe(expected);
      });
    };

    testDecode('string', 'foo', 'foo');
    testDecode('Text', new _Text.default('foo'), 'foo');
    testDecode('Uint8Array', Uint8Array.from([12, 102, 111, 111]), 'foo');
    testDecode('U8a', new _U8a.default(Uint8Array.from([12, 102, 111, 111])), 'foo');
    testDecode('object with `toString()`', {
      toString() {
        return 'foo';
      }

    }, 'foo');
    testDecode('hex input value', new _Text.default('0x12345678'), '0x12345678', 'toHex');
  });
  describe('encode', () => {
    const testEncode = (to, expected) => it("can encode ".concat(to), () => {
      expect(new _Text.default('foo')[to]()).toEqual(expected);
    });

    testEncode('toHex', '0x666f6f');
    testEncode('toString', 'foo');
    testEncode('toU8a', Uint8Array.from([12, 102, 111, 111]));
  });
  describe('utils', () => {
    it('compares actual string values', () => {
      expect(new _Text.default('123').eq('123')).toBe(true);
    });
    it('compares actual String values', () => {
      expect(new _Text.default('XYX').eq(String('XYX'))).toBe(true);
    });
    it('compares actual non-string values (fails)', () => {
      expect(new _Text.default('123').eq(123)).toBe(false);
    });
    it('calulates the length & encoded length correctly for ASCII', () => {
      const test = new _Text.default('abcde');
      expect(test.encodedLength).toEqual(6);
      expect(test).toHaveLength(5);
    });
    it('calulates the length & encoded length correctly for non-ASCII', () => {
      const test = new _Text.default('中文');
      expect(test.encodedLength).toEqual(7);
      expect(test).toHaveLength(2);
    });
  });
});