utils-test.js 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
describe('utils',function(){
  var colors = require('colors/safe');
  var chai = require('chai');
  var utils = require('../src/utils');
  var expect = chai.expect;

  var strlen = utils.strlen;
  var repeat = utils.repeat;
  var pad = utils.pad;
  var truncate = utils.truncate;
  var mergeOptions = utils.mergeOptions;
  var wordWrap = utils.wordWrap;


  describe('strlen',function(){
    it('length of "hello" is 5',function(){
      expect(strlen('hello')).to.equal(5);
    });

    it('length of "hi" is 2',function(){
      expect(strlen('hi')).to.equal(2);
    });

    it('length of "hello" in red is 5',function(){
      expect(strlen(colors.red('hello'))).to.equal(5);
    });

    it('length of "hello" in zebra is 5',function(){
      expect(strlen(colors.zebra('hello'))).to.equal(5);
    });

    it('length of "hello\\nhi\\nheynow" is 6',function(){
      expect(strlen('hello\nhi\nheynow')).to.equal(6);
    });

    it('length of "中文字符" is 8',function(){
      expect(strlen('中文字符')).to.equal(8);
    });

    it('length of "日本語の文字" is 12',function(){
      expect(strlen('日本語の文字')).to.equal(12);
    });

    it('length of "한글" is 4',function(){
      expect(strlen('한글')).to.equal(4);
    });
  });

  describe('repeat',function(){
    it('"-" x 3',function(){
      expect(repeat('-',3)).to.equal('---');
    });

    it('"-" x 4',function(){
      expect(repeat('-',4)).to.equal('----');
    });

    it('"=" x 4',function(){
      expect(repeat('=',4)).to.equal('====');
    });
  });

  describe('pad',function(){
    it("pad('hello',6,' ', right) == ' hello'", function () {
      expect(pad('hello',6,' ','right')).to.equal(' hello');
    });

    it("pad('hello',7,' ', left) == 'hello  '", function () {
      expect(pad('hello',7,' ','left')).to.equal('hello  ');
    });

    it("pad('hello',8,' ', center) == ' hello  '", function () {
      expect(pad('hello',8,' ','center')).to.equal(' hello  ');
    });

    it("pad('hello',9,' ', center) == '  hello  '", function () {
      expect(pad('hello',9,' ','center')).to.equal('  hello  ');
    });

    it("pad('yo',4,' ', center) == ' yo '", function () {
      expect(pad('yo',4,' ','center')).to.equal(' yo ');
    });

    it('pad red(hello)', function(){
      expect(pad(colors.red('hello'),7,' ','right')).to.equal('  ' + colors.red('hello'));
    });

    it("pad('hello', 2, ' ', right) == 'hello'", function(){
      expect(pad('hello', 2, ' ', 'right')).to.equal('hello');
    });
  });

  describe('truncate',function(){
    it('truncate("hello", 5) === "hello"',function(){
      expect(truncate('hello',5)).to.equal('hello');
    });

    it('truncate("hello sir", 7, "…") == "hello …"',function(){
      expect(truncate('hello sir', 7, '…')).to.equal('hello …');
    });

    it('truncate("hello sir", 6, "…") == "hello…"',function(){
      expect(truncate('hello sir', 6, '…')).to.equal('hello…');
    });

    it('truncate("goodnight moon", 8, "…") == "goodnig…"',function(){
      expect(truncate('goodnight moon', 8, '…')).to.equal('goodnig…');
    });

    it('truncate(colors.zebra("goodnight moon"), 15, "…") == colors.zebra("goodnight moon")',function(){
      var original = colors.zebra('goodnight moon');
      expect(truncate(original, 15, '…')).to.equal(original);
    });

    it('truncate(colors.zebra("goodnight moon"), 8, "…") == colors.zebra("goodnig") + "…"',function(){
      var original = colors.zebra('goodnight moon');
      var expected = colors.zebra('goodnig') + '…';
      expect(truncate(original, 8, '…')).to.equal(expected);
    });

    it('truncate(colors.zebra("goodnight moon"), 9, "…") == colors.zebra("goodnig") + "…"',function(){
      var original = colors.zebra('goodnight moon');
      var expected = colors.zebra('goodnigh') + '…';
      expect(truncate(original, 9, '…')).to.equal(expected);
    });

    it('red(hello) + green(world) truncated to 9 chars',function(){
      var original = colors.red('hello') + colors.green(' world');
      var expected = colors.red('hello') + colors.green(' wo') + '…';
      expect(truncate(original, 9)).to.equal(expected);
    });

    it('red-on-green(hello) + green-on-red(world) truncated to 9 chars',function(){
      var original = colors.red.bgGreen('hello') + colors.green.bgRed(' world');
      var expected = colors.red.bgGreen('hello') + colors.green.bgRed(' wo') + '…';
      expect(truncate(original,9)).to.equal(expected);
    });

    it('red-on-green(hello) + green-on-red(world) truncated to 10 chars - using inverse',function(){
      var original = colors.red.bgGreen('hello' + colors.inverse(' world'));
      var expected = colors.red.bgGreen('hello' + colors.inverse(' wor')) + '…';
      expect(truncate(original,10)).to.equal(expected);
    });

    it('red-on-green( zebra (hello world) ) truncated to 11 chars',function(){
      var original = colors.red.bgGreen(colors.zebra('hello world'));
      var expected = colors.red.bgGreen(colors.zebra('hello world'));
      expect(truncate(original,11)).to.equal(expected);
    });

    it('red-on-green( zebra (hello world) ) truncated to 10 chars',function(){
      var original = colors.red.bgGreen(colors.zebra('hello world'));
      var expected = colors.red.bgGreen(colors.zebra('hello wor')) + '…';
      expect(truncate(original,10)).to.equal(expected);
    });

    it('handles reset code', function() {
      var original = '\x1b[31mhello\x1b[0m world';
      var expected = '\x1b[31mhello\x1b[0m wor…';
      expect(truncate(original,10)).to.equal(expected);
    });

    it('handles reset code (EMPTY VERSION)', function() {
      var original = '\x1b[31mhello\x1b[0m world';
      var expected = '\x1b[31mhello\x1b[0m wor…';
      expect(truncate(original,10)).to.equal(expected);
    });

    it('truncateWidth("漢字テスト", 15) === "漢字テスト"',function(){
      expect(truncate('漢字テスト',15)).to.equal('漢字テスト');
    });

    it('truncateWidth("漢字テスト", 6) === "漢字…"',function(){
      expect(truncate('漢字テスト',6)).to.equal('漢字…');
    });

    it('truncateWidth("漢字テスト", 5) === "漢字…"',function(){
      expect(truncate('漢字テスト',5)).to.equal('漢字…');
    });

    it('truncateWidth("漢字testてすと", 12) === "漢字testて…"',function(){
      expect(truncate('漢字testてすと',12)).to.equal('漢字testて…');
    });

    it('handles color code with CJK chars',function(){
      var original = '漢字\x1b[31m漢字\x1b[0m漢字';
      var expected = '漢字\x1b[31m漢字\x1b[0m漢…';
      expect(truncate(original,11)).to.equal(expected);
    });
  });

  function defaultOptions(){
    return {
      chars: {
        'top': '─'
        , 'top-mid': '┬'
        , 'top-left': '┌'
        , 'top-right': '┐'
        , 'bottom': '─'
        , 'bottom-mid': '┴'
        , 'bottom-left': '└'
        , 'bottom-right': '┘'
        , 'left': '│'
        , 'left-mid': '├'
        , 'mid': '─'
        , 'mid-mid': '┼'
        , 'right': '│'
        , 'right-mid': '┤'
        , 'middle': '│'
      }
      , truncate: '…'
      , colWidths: []
      , rowHeights: []
      , colAligns: []
      , rowAligns: []
      , style: {
        'padding-left': 1
        , 'padding-right': 1
        , head: ['red']
        , border: ['grey']
        , compact : false
      }
      , head: []
    };
  }

  describe('mergeOptions',function(){
    it('allows you to override chars',function(){
      expect(mergeOptions()).to.eql(defaultOptions());
    });

    it('chars will be merged deeply',function(){
      var expected = defaultOptions();
      expected.chars.left = 'L';
      expect(mergeOptions({chars:{left:'L'}})).to.eql(expected);
    });

    it('style will be merged deeply',function(){
      var expected = defaultOptions();
      expected.style['padding-left'] = 2;
      expect(mergeOptions({style:{'padding-left':2}})).to.eql(expected);
    });

    it('head will be overwritten',function(){
      var expected = defaultOptions();
      expected.style.head = [];
      //we can't use lodash's `merge()` in implementation because it would deeply copy array.
      expect(mergeOptions({style:{'head':[]}})).to.eql(expected);
    });

    it('border will be overwritten',function(){
      var expected = defaultOptions();
      expected.style.border = [];
      //we can't use lodash's `merge()` in implementation because it would deeply copy array.
      expect(mergeOptions({style:{'border':[]}})).to.eql(expected);
    });
  });

  describe('wordWrap',function(){
    it('length',function(){
      var input = 'Hello, how are you today? I am fine, thank you!';

      var expected = 'Hello, how\nare you\ntoday? I\nam fine,\nthank you!';

      expect(wordWrap(10,input).join('\n')).to.equal(expected);
    });

    it('length with colors',function(){
      var input = colors.red('Hello, how are') + colors.blue(' you today? I') + colors.green(' am fine, thank you!');

      var expected = colors.red('Hello, how\nare') + colors.blue(' you\ntoday? I') + colors.green('\nam fine,\nthank you!');

      expect(wordWrap(10,input).join('\n')).to.equal(expected);
    });

    it('will not create an empty last line',function(){
      var input = 'Hello Hello ';

      var expected = 'Hello\nHello';

      expect(wordWrap(5,input).join('\n')).to.equal(expected);
    });

    it('will handle color reset code',function(){
      var input = '\x1b[31mHello\x1b[0m Hello ';

      var expected = '\x1b[31mHello\x1b[0m\nHello';

      expect(wordWrap(5,input).join('\n')).to.equal(expected);
    });

    it('will handle color reset code (EMPTY version)',function(){
      var input = '\x1b[31mHello\x1b[m Hello ';

      var expected = '\x1b[31mHello\x1b[m\nHello';

      expect(wordWrap(5,input).join('\n')).to.equal(expected);
    });

    it('words longer than limit will not create extra newlines',function(){
      var input = 'disestablishment is a multiplicity someotherlongword';

      var expected = 'disestablishment\nis a\nmultiplicity\nsomeotherlongword';

      expect(wordWrap(7,input).join('\n')).to.equal(expected);
    });

    it('multiple line input',function(){
      var input = 'a\nb\nc d e d b duck\nm\nn\nr';
      var expected = ['a', 'b', 'c d', 'e d', 'b', 'duck', 'm', 'n', 'r'];

      expect(wordWrap(4,input)).to.eql(expected);
    });

    it('will not start a line with whitespace', function(){
      var input = 'ab cd  ef gh  ij kl';
      var expected = ['ab cd','ef gh', 'ij kl'];
      expect(wordWrap(7, input)).to.eql(expected);
    });

    it('wraps CJK chars', function(){
      var input = '漢字 漢\n字 漢字';
      var expected = ['漢字 漢','字 漢字'];
      expect(wordWrap(7, input)).to.eql(expected);
    });

    it('wraps CJK chars with colors', function(){
      var input = '\x1b[31m漢字\x1b[0m\n 漢字';
      var expected = ['\x1b[31m漢字\x1b[0m', ' 漢字'];
      expect(wordWrap(5, input)).to.eql(expected);
    });
  });

  describe('colorizeLines',function(){
    it('foreground colors continue on each line',function(){
      var input = colors.red('Hello\nHi').split('\n');

      expect(utils.colorizeLines(input)).to.eql([
        colors.red('Hello'),
        colors.red('Hi')
      ]);
    });

    it('foreground colors continue on each line',function(){
      var input = colors.bgRed('Hello\nHi').split('\n');

      expect(utils.colorizeLines(input)).to.eql([
        colors.bgRed('Hello'),
        colors.bgRed('Hi')
      ]);
    });

    it('styles will continue on each line',function(){
      var input = colors.underline('Hello\nHi').split('\n');

      expect(utils.colorizeLines(input)).to.eql([
        colors.underline('Hello'),
        colors.underline('Hi')
      ]);
    });

    it('styles that end before the break will not be applied to the next line',function(){
      var input = (colors.underline('Hello') +'\nHi').split('\n');

      expect(utils.colorizeLines(input)).to.eql([
        colors.underline('Hello'),
        'Hi'
      ]);
    });

    it('the reset code can be used to drop styles', function() {
      var input = '\x1b[31mHello\x1b[0m\nHi'.split('\n');
      expect(utils.colorizeLines(input)).to.eql([
        "\x1b[31mHello\x1b[0m",
        "Hi"
      ]);
    });

    it('handles aixterm 16-color foreground', function() {
      var input = '\x1b[90mHello\nHi\x1b[0m'.split('\n');
      expect(utils.colorizeLines(input)).to.eql([
        '\x1b[90mHello\x1b[39m',
        '\x1b[90mHi\x1b[0m'
      ]);
    });

    it('handles aixterm 16-color background', function() {
      var input = '\x1b[100mHello\nHi\x1b[m\nHowdy'.split('\n');
      expect(utils.colorizeLines(input)).to.eql([
        '\x1b[100mHello\x1b[49m',
        '\x1b[100mHi\x1b[m',
        'Howdy'
      ]);
    });

    it('handles aixterm 256-color foreground', function() {
      var input ='\x1b[48;5;8mHello\nHi\x1b[0m\nHowdy'.split('\n');
      expect(utils.colorizeLines(input)).to.eql([
        '\x1b[48;5;8mHello\x1b[49m',
        '\x1b[48;5;8mHi\x1b[0m',
        'Howdy'
      ]);
    });

    it('handles CJK chars',function(){
      var input = colors.red('漢字\nテスト').split('\n');

      expect(utils.colorizeLines(input)).to.eql([
        colors.red('漢字'),
        colors.red('テスト')
      ]);
    });
  });
});