initial commit
[squeep-roman] / test / toRoman.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const { toRoman } = require('../index');
6
7 const noExpectedException = 'did not get expected exception';
8
9 // Ⅰ Ⅰ
10 // Ⅱ Ⅱ
11 // Ⅲ Ⅲ
12 // Ⅳ Ⅳ
13 // Ⅴ Ⅴ
14 // Ⅵ Ⅵ
15 // Ⅶ Ⅶ
16 // Ⅷ Ⅷ
17 // Ⅸ Ⅸ
18 // Ⅹ Ⅹ
19 // Ⅺ Ⅺ
20 // Ⅻ Ⅻ
21 // ⅩⅬ ⅩⅬ
22 // Ⅼ Ⅼ
23 // ⅩⅭ ⅩⅭ
24 // Ⅽ Ⅽ
25 // ⅭⅮ ⅭⅮ
26 // Ⅾ Ⅾ
27 // ⅭⅯ ⅭⅯ
28 // Ⅿ Ⅿ
29
30 const spotChecks = {
31 13: 'ⅩⅠⅠⅠ',
32 40: 'ⅩⅬ',
33 90: 'ⅩⅭ',
34 99: 'ⅩⅭⅠⅩ',
35 400: 'ⅭⅮ',
36 900: 'ⅭⅯ',
37 2022: 'ⅯⅯⅩⅩⅠⅠ',
38 };
39
40 describe('toRoman', function () {
41 it('requires a value', function () {
42 try {
43 toRoman();
44 assert.fail(noExpectedException);
45 } catch (e) {
46 assert(e instanceof RangeError, noExpectedException);
47 }
48 });
49
50 it('requires a numeric value', function () {
51 try {
52 toRoman('foo');
53 assert.fail(noExpectedException);
54 } catch (e) {
55 assert(e instanceof RangeError, noExpectedException);
56 }
57 });
58
59 it('accepts a numeric-like value', function () {
60 const result = toRoman('5');
61 assert.strictEqual(result, 'Ⅴ');
62 });
63
64 it('requires a positive numeric value', function () {
65 try {
66 toRoman(-99);
67 assert.fail(noExpectedException);
68 } catch (e) {
69 assert(e instanceof RangeError, noExpectedException);
70 }
71 });
72
73 it('returns single-characters for small numbers', function () {
74 const expectedLength = 'Ⅹ'.length;
75 for (let i = 1; i <= 12; i++) {
76 const result = toRoman(i);
77 assert.strictEqual(result.length, expectedLength, `input '${i}' -> '${result}' (length ${result.length})`);
78 }
79 });
80
81 it('returns single-glyph entities for small numbers', function () {
82 const expectedLength = '&#8544;'.length;
83 for (let i = 1; i <= 12; i++) {
84 const result = toRoman(i, true);
85 assert.strictEqual(result.length, expectedLength, `input '${i}' -> '${result}' (length ${result.length})`);
86 }
87 });
88
89 it('converts correctly', function () {
90 Object.entries(spotChecks).forEach(([num, expected]) => {
91 const result = toRoman(num);
92 assert.strictEqual(result, expected);
93 })
94 });
95
96 }); // toRoman