Skip to content

Commit

Permalink
describe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfortis committed Jan 20, 2024
1 parent 4ee7604 commit 808c6d2
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 484 deletions.
9 changes: 0 additions & 9 deletions _testAll.js

This file was deleted.

4 changes: 2 additions & 2 deletions base52/base52.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

export function toBase52(n) {
export function base52(n) {
let encoded = ''
do {
encoded = Charset[n % 52] + encoded
Expand All @@ -9,7 +9,7 @@ export function toBase52(n) {
return encoded
}

export function fromBase52(str) {
export function base52Decode(str) {
let j = 0
let decoded = 0
for (let i = str.length - 1; i >= 0; i--)
Expand Down
38 changes: 20 additions & 18 deletions base52/base52.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import test from 'node:test'
import { describe, test } from 'node:test'
import { equal } from 'node:assert/strict'
import { toBase52, fromBase52 } from './base52.js'
import { base52, base52Decode } from './base52.js'


const tests = [
[0, 'A'],
[1, 'B'],
[25, 'Z'],
[26, 'a'],
[51, 'z'],
[52, 'BA'],
[2 ** 30, 'CqsVtM']
]

tests.forEach(([input, expected]) =>
test(`base52(${input}) => ${expected}`, () =>
equal(toBase52(input), expected)))
describe('base52', () => {
const tests = [
[0, 'A'],
[1, 'B'],
[25, 'Z'],
[26, 'a'],
[51, 'z'],
[52, 'BA'],
[2 ** 30, 'CqsVtM']
]

tests.forEach(([expected, input]) =>
test(`base52Decode(${input}) => ${expected}`, () =>
equal(fromBase52(input), expected)))
tests.forEach(([input, expected]) =>
test(`base52(${input}) => ${expected}`, () =>
equal(base52(input), expected)))

tests.forEach(([expected, input]) =>
test(`base52Decode(${input}) => ${expected}`, () =>
equal(base52Decode(input), expected)))
})
27 changes: 15 additions & 12 deletions is-dark-color/is-dark-color.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import test from 'node:test'
import { describe, test } from 'node:test'
import { equal, throws } from 'node:assert/strict'
import { isDarkColor } from './is-dark-color.js'

const isTrue = color => equal(isDarkColor(color), true)
const isFalse = color => equal(isDarkColor(color), false)

test('Requires 6 hex digits', () =>
throws(() => isDarkColor('#abc')))
describe('isDarkColor', () => {
const isTrue = color => equal(isDarkColor(color), true)
const isFalse = color => equal(isDarkColor(color), false)

test('is dark color', () => {
isTrue('#000000')
isTrue('#123456')
isTrue('#555555')
test('Requires 6 hex digits', () =>
throws(() => isDarkColor('#abc')))

isFalse('#aaaaaa')
isFalse('#abcdef')
isFalse('#ffffff')
test('is dark color', () => {
isTrue('#000000')
isTrue('#123456')
isTrue('#555555')

isFalse('#aaaaaa')
isFalse('#abcdef')
isFalse('#ffffff')
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"node": ">=18"
},
"scripts": {
"test": "node _testAll.js"
"test": "node --test"
}
}
19 changes: 0 additions & 19 deletions proxy-fields-obfuscator/base52.js

This file was deleted.

21 changes: 0 additions & 21 deletions proxy-fields-obfuscator/base52.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion proxy-fields-obfuscator/proxyFieldNames.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base52 } from './base52.js'
import { base52 } from '../base52/base52.js'


export function proxyFieldNames(devPrefix, dict, isProduction = process.env.NODE_ENV === 'production') {
Expand Down
89 changes: 42 additions & 47 deletions proxy-fields-obfuscator/proxyFieldNames.test.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,59 @@
import test from 'node:test'
import { describe, test } from 'node:test'
import { throws, equal } from 'node:assert/strict'
import { proxyFieldNames } from './proxyFieldNames.js'


function prodTest(a, b) {
_test('PrefixNotUsed', a, b, true)
}
describe('Proxy Field Names', () => {
function prodTest(a, b) { _test('PrefixNotUsed', a, b, true) }
function devTest(prefix, a, b) { _test(prefix, a, b, false) }

function _test(prefix, a, b, isProduction) {
const proxy = proxyFieldNames(prefix, a, isProduction)
for (const k in proxy)
equal(proxy[k], b[k])
}

function devTest(prefix, a, b) {
_test(prefix, a, b, false)
}
console.log('proxyFields')

function _test(prefix, a, b, isProduction) {
const proxy = proxyFieldNames(prefix, a, isProduction)
for (const k in proxy)
equal(proxy[k], b[k])
}
test('Dev prefixes', () =>
devTest('PRE',
{ x: null, y: null },
{ x: 'PRE_x', y: 'PRE_y' }))

console.log('proxyFields')
test('Dev prefixes ignoring pre-baked values', () =>
devTest('PRE',
{ x: 'val0', y: 1 },
{ x: 'PRE_x', y: 'PRE_y' }))

test('Dev prefixes', () =>
devTest('PRE',
{ x: null, y: null },
{ x: 'PRE_x', y: 'PRE_y' }))

test('Dev prefixes ignoring pre-baked values', () =>
devTest('PRE',
{ x: 'val0', y: 1 },
{ x: 'PRE_x', y: 'PRE_y' }))
test('Prod uses pre-baked values', () =>
prodTest(
{ x: 'val0', y: 1 },
{ x: 'val0', y: 1 }))

test('Prod generates base52 values', () =>
prodTest(
{ x: null, y: null },
{ x: 'A', y: 'B' }))

test('Prod uses pre-baked values', () =>
prodTest(
{ x: 'val0', y: 1 },
{ x: 'val0', y: 1 }))

test('Prod generates base52 values', () =>
prodTest(
{ x: null, y: null },
{ x: 'A', y: 'B' }))
test('Repeated values throw', () =>
throws(() => proxyFieldNames('PRE', {
x: 0,
y: 0
}), /value "0" is repeated/))


test('Repeated values throw', () =>
throws(() => proxyFieldNames('PRE', {
x: 0,
y: 0
}), /value "0" is repeated/))
test('Mixing null and numeric values throws', () =>
throws(() => proxyFieldNames('PRE', {
x: 0,
y: null
}), /Mixing null and numeric values in: "PRE"/))


test('Mixing null and numeric values throws', () =>
throws(() => proxyFieldNames('PRE', {
x: 0,
y: null
}), /Mixing null and numeric values in: "PRE"/))


test('Undefined key access throws', () => {
const proxy = proxyFieldNames('PRE', { x: 'a' })
throws(() => proxy['missingKey'],
/Accessing an undefined field: PRE_missingKey/)
test('Undefined key access throws', () => {
const proxy = proxyFieldNames('PRE', { x: 'a' })
throws(() => proxy['missingKey'],
/Accessing an undefined field: PRE_missingKey/)
})
})


Loading

0 comments on commit 808c6d2

Please sign in to comment.