|
1 | 1 | import assert from 'assert';
|
2 | 2 | import { ftl } from './util';
|
3 | 3 |
|
4 |
| -import { parse, serialize } from '../src'; |
| 4 | +import { FluentParser, FluentSerializer } from '../src'; |
5 | 5 |
|
6 | 6 |
|
7 |
| -function pretty(text) { |
8 |
| - const res = parse(text); |
9 |
| - return serialize(res, { |
10 |
| - withJunk: false |
| 7 | +suite('Serialize resource', function() { |
| 8 | + let pretty; |
| 9 | + |
| 10 | + setup(function() { |
| 11 | + const parser = new FluentParser(); |
| 12 | + const serializer = new FluentSerializer({ |
| 13 | + withJunk: false |
| 14 | + }); |
| 15 | + |
| 16 | + pretty = function pretty(text) { |
| 17 | + const res = parser.parse(text); |
| 18 | + return serializer.serialize(res); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + test('invalid resource', function() { |
| 23 | + const serializer = new FluentSerializer(); |
| 24 | + assert.throws( |
| 25 | + () => serializer.serialize(null), |
| 26 | + /Cannot read property 'type'/ |
| 27 | + ); |
| 28 | + assert.throws( |
| 29 | + () => serializer.serialize({}), |
| 30 | + /Unknown resource type/ |
| 31 | + ); |
11 | 32 | });
|
12 |
| -} |
13 | 33 |
|
14 |
| -suite('Serializer', function() { |
15 | 34 | test('simple message', function() {
|
16 | 35 | const input = ftl`
|
17 | 36 | foo = Foo
|
@@ -400,3 +419,90 @@ suite('Serializer', function() {
|
400 | 419 | assert.equal(pretty(input), input);
|
401 | 420 | });
|
402 | 421 | });
|
| 422 | + |
| 423 | +suite('Serialize expression', function() { |
| 424 | + let pretty; |
| 425 | + |
| 426 | + setup(function() { |
| 427 | + const parser = new FluentParser(); |
| 428 | + const serializer = new FluentSerializer({ |
| 429 | + withJunk: false |
| 430 | + }); |
| 431 | + |
| 432 | + pretty = function pretty(text) { |
| 433 | + const {value: {elements: [placeable]}} = parser.parseEntry(text); |
| 434 | + return serializer.serializeExpression(placeable.expression); |
| 435 | + } |
| 436 | + }); |
| 437 | + |
| 438 | + test('invalid expression', function() { |
| 439 | + const serializer = new FluentSerializer(); |
| 440 | + assert.throws( |
| 441 | + () => serializer.serializeExpression(null), |
| 442 | + /Cannot read property 'type'/ |
| 443 | + ); |
| 444 | + assert.throws( |
| 445 | + () => serializer.serializeExpression({}), |
| 446 | + /Unknown expression type/ |
| 447 | + ); |
| 448 | + }); |
| 449 | + |
| 450 | + test('string expression', function() { |
| 451 | + const input = ftl` |
| 452 | + foo = { "str" } |
| 453 | + `; |
| 454 | + assert.equal(pretty(input), '"str"'); |
| 455 | + }); |
| 456 | + |
| 457 | + test('number expression', function() { |
| 458 | + const input = ftl` |
| 459 | + foo = { 3 } |
| 460 | + `; |
| 461 | + assert.equal(pretty(input), '3'); |
| 462 | + }); |
| 463 | + |
| 464 | + test('message reference', function() { |
| 465 | + const input = ftl` |
| 466 | + foo = { msg } |
| 467 | + `; |
| 468 | + assert.equal(pretty(input), 'msg'); |
| 469 | + }); |
| 470 | + |
| 471 | + test('external argument', function() { |
| 472 | + const input = ftl` |
| 473 | + foo = { $ext } |
| 474 | + `; |
| 475 | + assert.equal(pretty(input), '$ext'); |
| 476 | + }); |
| 477 | + |
| 478 | + test('attribute expression', function() { |
| 479 | + const input = ftl` |
| 480 | + foo = { msg.attr } |
| 481 | + `; |
| 482 | + assert.equal(pretty(input), 'msg.attr'); |
| 483 | + }); |
| 484 | + |
| 485 | + test('variant expression', function() { |
| 486 | + const input = ftl` |
| 487 | + foo = { -msg[variant] } |
| 488 | + `; |
| 489 | + assert.equal(pretty(input), '-msg[variant]'); |
| 490 | + }); |
| 491 | + |
| 492 | + test('call expression', function() { |
| 493 | + const input = ftl` |
| 494 | + foo = { BUILTIN(3.14, kwarg: "value") } |
| 495 | + `; |
| 496 | + assert.equal(pretty(input), 'BUILTIN(3.14, kwarg: "value")'); |
| 497 | + }); |
| 498 | + |
| 499 | + test('select expression', function() { |
| 500 | + const input = ftl` |
| 501 | + foo = |
| 502 | + { $num -> |
| 503 | + *[one] One |
| 504 | + } |
| 505 | + `; |
| 506 | + assert.equal(pretty(input), '$num ->\n *[one] One\n'); |
| 507 | + }); |
| 508 | +}); |
0 commit comments