X-Git-Url: http://git.squeep.com/?p=squeep-roman;a=blobdiff_plain;f=test%2FtoRoman.js;fp=test%2FtoRoman.js;h=ea3ad1e4afed588360ac32f1748d79548c3c4ffd;hp=0000000000000000000000000000000000000000;hb=b05ede38a9aaa33ef7e7362137638d6baf61de0d;hpb=9d71de26ede6923215251b563060d7b43b9784ee diff --git a/test/toRoman.js b/test/toRoman.js new file mode 100644 index 0000000..ea3ad1e --- /dev/null +++ b/test/toRoman.js @@ -0,0 +1,96 @@ +/* eslint-env mocha */ +'use strict'; + +const assert = require('assert'); +const { toRoman } = require('../index'); + +const noExpectedException = 'did not get expected exception'; + +// Ⅰ Ⅰ +// Ⅱ Ⅱ +// Ⅲ Ⅲ +// Ⅳ Ⅳ +// Ⅴ Ⅴ +// Ⅵ Ⅵ +// Ⅶ Ⅶ +// Ⅷ Ⅷ +// Ⅸ Ⅸ +// Ⅹ Ⅹ +// Ⅺ Ⅺ +// Ⅻ Ⅻ +// ⅩⅬ ⅩⅬ +// Ⅼ Ⅼ +// ⅩⅭ ⅩⅭ +// Ⅽ Ⅽ +// ⅭⅮ ⅭⅮ +// Ⅾ Ⅾ +// ⅭⅯ ⅭⅯ +// Ⅿ Ⅿ + +const spotChecks = { + 13: 'ⅩⅠⅠⅠ', + 40: 'ⅩⅬ', + 90: 'ⅩⅭ', + 99: 'ⅩⅭⅠⅩ', + 400: 'ⅭⅮ', + 900: 'ⅭⅯ', + 2022: 'ⅯⅯⅩⅩⅠⅠ', +}; + +describe('toRoman', function () { + it('requires a value', function () { + try { + toRoman(); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof RangeError, noExpectedException); + } + }); + + it('requires a numeric value', function () { + try { + toRoman('foo'); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof RangeError, noExpectedException); + } + }); + + it('accepts a numeric-like value', function () { + const result = toRoman('5'); + assert.strictEqual(result, 'Ⅴ'); + }); + + it('requires a positive numeric value', function () { + try { + toRoman(-99); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof RangeError, noExpectedException); + } + }); + + it('returns single-characters for small numbers', function () { + const expectedLength = 'Ⅹ'.length; + for (let i = 1; i <= 12; i++) { + const result = toRoman(i); + assert.strictEqual(result.length, expectedLength, `input '${i}' -> '${result}' (length ${result.length})`); + } + }); + + it('returns single-glyph entities for small numbers', function () { + const expectedLength = 'Ⅰ'.length; + for (let i = 1; i <= 12; i++) { + const result = toRoman(i, true); + assert.strictEqual(result.length, expectedLength, `input '${i}' -> '${result}' (length ${result.length})`); + } + }); + + it('converts correctly', function () { + Object.entries(spotChecks).forEach(([num, expected]) => { + const result = toRoman(num); + assert.strictEqual(result, expected); + }) + }); + +}); // toRoman \ No newline at end of file