From 8ef4aec7fabb290381511a6606f4a762798ae787 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Fri, 6 Apr 2018 12:26:12 -0700 Subject: [PATCH] Add tests for maybeFormatMessage --- test/unit/maybe_format_message.js | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/unit/maybe_format_message.js diff --git a/test/unit/maybe_format_message.js b/test/unit/maybe_format_message.js new file mode 100644 index 000000000..969e25a2e --- /dev/null +++ b/test/unit/maybe_format_message.js @@ -0,0 +1,77 @@ +const test = require('tap').test; +const maybeFormatMessage = require('../../src/util/maybe-format-message'); + +const nonMessages = [ + 'hi', + 42, + true, + function () { + return 'unused'; + }, + { + a: 1, + b: 2 + }, + { + id: 'almost a message', + notDefault: 'but missing the "default" property' + }, + { + notId: 'this one is missing the "id" property', + default: 'but has "default"' + } +]; + +const argsQuick = { + speed: 'quick' +}; + +const argsOther = { + speed: 'slow' +}; + +const argsEmpty = {}; + +const simpleMessage = { + id: 'test.simpleMessage', + default: 'The quick brown fox jumped over the lazy dog.' +}; + +const complexMessage = { + id: 'test.complexMessage', + default: '{speed, select, quick {The quick brown fox jumped over the lazy dog.} other {Too slow, Gobo!}}' +}; + +const quickExpectedResult = 'The quick brown fox jumped over the lazy dog.'; +const otherExpectedResult = 'Too slow, Gobo!'; + +test('preserve non-messages', t => { + t.plan(nonMessages.length); + + for (const x of nonMessages) { + const result = maybeFormatMessage(x); + t.strictSame(x, result); + } + + t.end(); +}); + +test('format messages', t => { + const quickResult1 = maybeFormatMessage(simpleMessage); + t.strictNotSame(quickResult1, simpleMessage); + t.same(quickResult1, quickExpectedResult); + + const quickResult2 = maybeFormatMessage(complexMessage, argsQuick); + t.strictNotSame(quickResult2, complexMessage); + t.same(quickResult2, quickExpectedResult); + + const otherResult1 = maybeFormatMessage(complexMessage, argsOther); + t.strictNotSame(otherResult1, complexMessage); + t.same(otherResult1, otherExpectedResult); + + const otherResult2 = maybeFormatMessage(complexMessage, argsEmpty); + t.strictNotSame(otherResult2, complexMessage); + t.same(otherResult2, otherExpectedResult); + + t.end(); +});