initial commit
[squeep-roman] / test / toRoman.js
diff --git a/test/toRoman.js b/test/toRoman.js
new file mode 100644 (file)
index 0000000..ea3ad1e
--- /dev/null
@@ -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 = '&#8544;'.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