Skip to content

Commit ca5faf4

Browse files
committed
Add files
1 parent 2dcafb9 commit ca5faf4

File tree

10 files changed

+811
-0
lines changed

10 files changed

+811
-0
lines changed

.github/workflows/publish_package.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish Package
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
publish:
7+
runs-on: ubuntu-18.04
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-node@v1
11+
with:
12+
node-version: "12.x"
13+
registry-url: https://registry.npmjs.org
14+
- run: yarn
15+
- run: yarn publish --access public
16+
env:
17+
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
18+
- uses: actions/setup-node@v1
19+
with:
20+
node-version: "12.x"
21+
registry-url: https://npm.pkg.github.com
22+
- run: yarn publish --access public
23+
env:
24+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
lib/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Qualified, Inc
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# @codewars/test-compat
2+
3+
Provides functions from the deprecated custom test framework.
4+
5+
The goal of this package is to ease the language version upgrade.
6+
Any new tests should use proper assertion library.

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@codewars/test-compat",
3+
"version": "1.0.0",
4+
"description": "Provides functions from the deprecated custom test framework",
5+
"main": "lib/index.js",
6+
"files": [
7+
"lib/"
8+
],
9+
"repository": "https://github.com/codewars/javascript-test-compat",
10+
"license": "MIT",
11+
"scripts": {
12+
"prepare": "tsc"
13+
},
14+
"peerDependencies": {
15+
"chai": ">=4.x"
16+
},
17+
"devDependencies": {
18+
"@types/chai": "^4.2.11",
19+
"chai": "^4.2.0",
20+
"husky": "^4.2.5",
21+
"prettier": "^2.0.5",
22+
"pretty-quick": "^2.0.1",
23+
"typescript": "^3.9.6"
24+
},
25+
"husky": {
26+
"hooks": {
27+
"pre-commit": "pretty-quick --staged"
28+
}
29+
}
30+
}

src/assertions.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Assertions aliased to names from the custom test framework.
2+
import chai from "chai";
3+
const assert = chai.assert;
4+
5+
export const pass = () => {};
6+
export const fail = assert.fail;
7+
export const expect = assert;
8+
export const assertEquals = assert.strictEqual;
9+
export const assertNotEquals = assert.notStrictEqual;
10+
export const assertContains = assert.include;
11+
export const assertNotContains = assert.notInclude;
12+
13+
export const assertSimilar = (actual: any, expected: any, msg?: any) => {
14+
console.error(`assertSimilar is deprecated, use assert.deepEqual`);
15+
assert.deepEqual(actual, expected, msg);
16+
};
17+
18+
export const assertNotSimilar = (actual: any, expected: any, msg?: any) => {
19+
console.error(`assertNotSimilar is deprecated, use assert.notDeepEqual`);
20+
assert.notDeepEqual(actual, expected, msg);
21+
};
22+
23+
// not using `assert.throws` because it can't test for fn to throw "anything"
24+
export const expectError = (msg: string | (() => void), fn?: () => void) => {
25+
let message: string;
26+
let fun: () => void;
27+
if (typeof msg === "function") {
28+
fun = msg;
29+
message = "Expected an error to be thrown";
30+
} else {
31+
fun = fn!;
32+
message = msg;
33+
}
34+
35+
let passed = false;
36+
try {
37+
fun();
38+
} catch {
39+
passed = true;
40+
}
41+
assert(passed, message);
42+
};
43+
44+
export const expectNoError = (msg: string | (() => void), fn?: () => void) => {
45+
let message: string;
46+
let fun: () => void;
47+
if (typeof msg === "function") {
48+
fun = msg;
49+
message = "Unexpected error was thrown";
50+
} else {
51+
fun = fn!;
52+
message = msg;
53+
}
54+
55+
try {
56+
fun();
57+
} catch (ex) {
58+
assert.fail(appendToMessage(message, ex.message));
59+
}
60+
};
61+
62+
// Compares two floating point values and checks whether they are approximately equal to each other
63+
export const assertApproxEquals = (
64+
actual: number,
65+
expected: number,
66+
msg?: string
67+
) => {
68+
// uses absolute error when |expected| <= 1, compatible with old version
69+
if (Math.abs(expected) <= 1) {
70+
assert.closeTo(actual, expected, 1e-9);
71+
} else {
72+
msg = appendToMessage(
73+
msg,
74+
"Expected actual value " +
75+
actual +
76+
" to approximately equal expected value " +
77+
expected +
78+
" (accepted relative error: 1e-9)"
79+
);
80+
assert(Math.abs((expected - actual) / expected) <= 1e-9, msg);
81+
}
82+
};
83+
84+
// Compares two floating point values and checks whether they are sufficiently different from each other
85+
export const assertNotApproxEquals = (
86+
actual: number,
87+
unexpected: number,
88+
msg?: string
89+
) => {
90+
msg = appendToMessage(
91+
msg,
92+
"Actual value " +
93+
actual +
94+
" should not approximately equal unexpected value " +
95+
unexpected +
96+
" (rejected relative error: 1e-9)"
97+
);
98+
if (Math.abs(unexpected) <= 1) {
99+
assert(Math.abs(unexpected - actual) > 1e-9, msg);
100+
} else {
101+
assert(Math.abs((unexpected - actual) / unexpected) > 1e-9, msg);
102+
}
103+
};
104+
105+
const appendToMessage = (msg: string | undefined, s: string) =>
106+
msg ? msg + " - " + s : s;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./assertions";
2+
export * from "./utils";

src/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const randomNumber = () => Math.floor(Math.random() * 101);
2+
3+
export const randomToken = () => Math.random().toString(36).substr(8);
4+
5+
export const randomize = (array: any[]) => {
6+
const arr = array.slice();
7+
let i = arr.length;
8+
let j, x;
9+
while (i) {
10+
j = (Math.random() * i) | 0;
11+
x = arr[--i];
12+
arr[i] = arr[j];
13+
arr[j] = x;
14+
}
15+
return arr;
16+
};
17+
18+
export const sample = (array: any[]) => array[~~(array.length * Math.random())];
19+
20+
export const escapeHtml = (html: any) =>
21+
String(html)
22+
.replace(/&/g, "&amp;")
23+
.replace(/"/g, "&quot;")
24+
.replace(/'/g, "&#39;")
25+
.replace(/</g, "&lt;")
26+
.replace(/>/g, "&gt;");

tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"esModuleInterop": true,
5+
"target": "ES2019",
6+
"module": "commonjs",
7+
"outDir": "./lib",
8+
"rootDir": "./src",
9+
"declaration": true,
10+
"sourceMap": true,
11+
"removeComments": true
12+
}
13+
}

0 commit comments

Comments
 (0)