'use strict';

import assert from 'assert';

import { MessageContext } from '../../src/intl/context';

describe('Formatting messages:', function(){
  let ctx, args, errs;

  before(function() {
    ctx = new MessageContext('en-US');
    ctx.addMessages([
      'key1 = Value 1',
      'key2 = Value 2',
      '  [a] A2',
      '  [b] B2',
      'key3 = Value 3',
      '  [a] A3',
      ' *[b] B3',
      'key4 =',
      '  [a] A4',
      ' *[b] B4',
      'key5 =',
      '  [a] A5',
      '  [b] B5',
    ].join('\n'));
  });

  beforeEach(function() {
    errs = [];
  });

  it('uses the value when no traits are defined', function(){
    const msg = ctx.messages.get('key1');
    const val = ctx.format(msg, args, errs);
    assert.equal(val, 'Value 1');
    assert.deepEqual(errs, []);
  });

  it('uses the value even if traits are defined', function(){
    const msg = ctx.messages.get('key2');
    const val = ctx.format(msg, args, errs);
    assert.equal(val, 'Value 2');
    assert.deepEqual(errs, []);
  });

  it('uses the value even if one trait is marked as default', function(){
    const msg = ctx.messages.get('key3');
    const val = ctx.format(msg, args, errs);
    assert.equal(val, 'Value 3');
    assert.deepEqual(errs, []);
  });

  it('uses the default trait if there is no value', function(){
    const msg = ctx.messages.get('key4');
    const val = ctx.format(msg, args, errs);
    assert.equal(val, 'B4');
    assert.deepEqual(errs, []);
  });

  it('returns null if there is no value and no default', function(){
    const msg = ctx.messages.get('key5');
    const val = ctx.format(msg, args, errs);
    assert.strictEqual(val, null);
    assert.deepEqual(errs, []);
  });

});

@@ -121,17 +121,14 @@
 ing 
-messag
+valu
 es
-:
 ', f
@@ -1694,12 +1694,285 @@
 %0A  %7D);%0A%0A
+  it('allows to pass traits directly to ctx.format', function()%7B%0A    const msg = ctx.messages.get('key2');%0A    const vals = msg.traits.map(%0A      trait =%3E ctx.format(trait, args, errs)%0A    );%0A    assert.deepEqual(vals, %5B'A2', 'B2'%5D);%0A    assert.deepEqual(errs, %5B%5D);%0A  %7D);%0A%0A
 %7D);%0A

test formatting trait values