Skip to content

Commit

Permalink
Extract keys out to helper files and use js style code for assertion
Browse files Browse the repository at this point in the history
afterwards run formatter for standardized formatting
  • Loading branch information
suvash committed Feb 27, 2021
1 parent 2df34d2 commit 983f2c4
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"browser": true,
"es6": true,
"node": true
"node": true,
"mocha": true
},
"extends": [
"standard",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"build:cjsmin": "npm run webpack -- --env.mode production --env.target=commonjs2",
"build:cleanexample": "rimraf umd/index.html cjs/index.html",
"build": "npm-run-all -s clean build:*",
"format:pretty": "prettier --write src/",
"format:eslint": "eslint --fix src/**/*.js",
"format:pretty": "prettier --write src/ test/",
"format:eslint": "eslint --fix src/**/*.js test/**/*.js",
"format": "npm-run-all --sequential format:*",
"release": "npm-run-all --sequential build np",
"mocha": "mocha --recursive --require esm",
Expand Down
85 changes: 81 additions & 4 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,81 @@
export const testKeys = ["+", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "<", "=", ">", "?", "A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~"];
export const testKeys = [
"+",
".",
"/",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"<",
"=",
">",
"?",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"[",
"\\",
"]",
"^",
"_",
"`",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"{",
"|",
"}",
"~",
];
81 changes: 81 additions & 0 deletions test/layouts/romanized.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
export const expectedKeys = [
"\u200C",
"।",
"्",
"०",
"१",
"२",
"३",
"४",
"५",
"६",
"७",
"८",
"९",
"ङ",
"\u200D",
"॥",
"?",
"आ",
"भ",
"च",
"ध",
"ै",
"ऊ",
"घ",
"अ",
"ी",
"झ",
"ख",
"ळ",
"ं",
"ण",
"ओ",
"फ",
"ठ",
"ृ",
"श",
"थ",
"ू",
"ँ",
"औ",
"ढ",
"ञ",
"ऋ",
"इ",
"ॐ",
"ए",
"^",
"॒",
"ऽ",
"ा",
"ब",
"छ",
"द",
"े",
"उ",
"ग",
"ह",
"ि",
"ज",
"क",
"ल",
"म",
"न",
"ो",
"प",
"ट",
"र",
"स",
"त",
"ु",
"व",
"ौ",
"ड",
"य",
"ष",
"ई",
"ः",
"ऐ",
"़",
];
40 changes: 16 additions & 24 deletions test/layouts/romanized.spec.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import assert from 'assert';
import romanized from '../../src/layouts/romanized';
import { testKeys } from '../helper.js';
import assert from "assert";
import romanized from "../../src/layouts/romanized";
import { testKeys } from "../helper.js";
import { expectedKeys } from "./romanized.helper.js";

describe('romanized', () => {
describe('formatKey()', () => {
it('should return corresponding Nepali character.', () => {
const expected = ["\u200C", "।", "्", "०", "१", "२", "३", "४", "५", "६", "७", "८", "९", "ङ", "\u200D",
"॥", "?", "आ", "भ", "च", "ध", "ै", "ऊ", "घ", "अ", "ी", "झ", "ख", "ळ", "ं", "ण", "ओ", "फ", "ठ", "ृ",
"श", "थ", "ू", "ँ", "औ", "ढ", "ञ", "ऋ", "इ", "ॐ", "ए", "^", " ", "ऽ", "ा", "ब", "छ", "द", "े", "उ",
"ग", "ह", "ि", "ज", "क", "ल", "म", "न", "ो", "प", "ट", "र", "स", "त", "ु", "व", "ौ", "ड",
"य", "ष", "ई", "ः", "ऐ", "़"];

testKeys.every((element, index, array) => {
assert.strictEqual(romanized.formatKey(element), expected[index]);
});
describe("romanized", () => {
describe("formatKey()", () => {
it("should return corresponding romanized Nepali character.", () => {
const receivedKeys = testKeys.map((key) => romanized.formatKey(key));
assert.deepEqual(expectedKeys, receivedKeys);
});

it('should return undefined if it fails to map', () => {

for (var i = 0; i < 127; i++) {
let inputStr = String.fromCharCode(i);
let found = testKeys.find(element => element == inputStr);
it("should return undefined if no mapping exists", () => {
const asciiCodes = [...Array(255 + 1).keys()];
const asciiKeys = asciiCodes.map((key) => String.fromCharCode(key));
const filteredKeys = asciiKeys.filter((key) => testKeys.indexOf(key) < 0);

if (!found) {
assert.strictEqual(romanized.formatKey(inputStr), undefined);
}
}
const receivedKeys = filteredKeys.map((key) => romanized.formatKey(key));
const expectedKeys = [...Array(receivedKeys.length)];

assert.deepEqual(expectedKeys, receivedKeys);
});
});
});

0 comments on commit 983f2c4

Please # to comment.