fix all core script syntax issues
This commit is contained in:
@@ -7,14 +7,18 @@ var EPSILON = 1e-12
|
||||
|
||||
function stone_if_needed(b) { if (!stone.p(b)) stone(b) }
|
||||
|
||||
/* Deep comparison capable of blobs + tolerance for floating diff */
|
||||
function deep_compare(expected, actual, path) {
|
||||
path = path || ''
|
||||
function deep_compare(expected, actual, _path) {
|
||||
var diff = null
|
||||
var msgs = null
|
||||
var expKeys = null
|
||||
var actKeys = null
|
||||
var i = 0
|
||||
var path = _path || ''
|
||||
if (expected == actual) return { passed: true, messages: [] }
|
||||
|
||||
if (is_number(expected) && is_number(actual)) {
|
||||
if (isNaN(expected) && isNaN(actual)) return { passed: true, messages: [] }
|
||||
var diff = abs(expected - actual)
|
||||
diff = abs(expected - actual)
|
||||
if (diff <= EPSILON) return { passed: true, messages: [] }
|
||||
return { passed: false, messages: [`Value mismatch at ${path}: ${expected} vs ${actual} (diff ${diff})`] }
|
||||
}
|
||||
@@ -33,7 +37,7 @@ function deep_compare(expected, actual, path) {
|
||||
if (is_array(expected) && is_array(actual)) {
|
||||
if (length(expected) != length(actual))
|
||||
return { passed: false, messages: [`Array length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] }
|
||||
var msgs = []
|
||||
msgs = []
|
||||
arrfor(array(expected), function(i) {
|
||||
var res = deep_compare(expected[i], actual[i], `${path}[${i}]`)
|
||||
if (!res.passed) array(msgs, res.messages)
|
||||
@@ -42,11 +46,11 @@ function deep_compare(expected, actual, path) {
|
||||
}
|
||||
|
||||
if (is_object(expected) && is_object(actual)) {
|
||||
var expKeys = sort(array(expected))
|
||||
var actKeys = sort(array(actual))
|
||||
expKeys = sort(array(expected))
|
||||
actKeys = sort(array(actual))
|
||||
if (JSON.stringify(expKeys) != JSON.stringify(actKeys))
|
||||
return { passed: false, messages: [`Object keys mismatch at ${path}: ${expKeys} vs ${actKeys}`] }
|
||||
var msgs = []
|
||||
msgs = []
|
||||
arrfor(expKeys, function(k) {
|
||||
var res = deep_compare(expected[k], actual[k], `${path}.${k}`)
|
||||
if (!res.passed) array(msgs, res.messages)
|
||||
@@ -58,7 +62,10 @@ function deep_compare(expected, actual, path) {
|
||||
}
|
||||
|
||||
var testarr = []
|
||||
for (var i = 0; i < 500; i++) { push(testarr, 1) }
|
||||
var i = 0
|
||||
var t = null
|
||||
var name = null
|
||||
for (i = 0; i < 500; i++) { push(testarr, 1) }
|
||||
|
||||
var testCases = [
|
||||
{ name: 'zero', input: 0 },
|
||||
@@ -84,7 +91,7 @@ var testCases = [
|
||||
|
||||
{ name: 'empty_string', input: '' },
|
||||
{ name: 'string_cat', input: 'cat' },
|
||||
{ name: 'string_unicode', input: 'U+1F4A9 「うんち絵文字」 «💩»' },
|
||||
{ name: 'string_unicode', input: 'U+1F4A9 「うんち絵文字」 «💩»' },
|
||||
|
||||
{ name: 'large_array', input: testarr },
|
||||
|
||||
@@ -106,19 +113,19 @@ var testCases = [
|
||||
function make_test(t) {
|
||||
return function() {
|
||||
var enc = wota.encode(t.input)
|
||||
if (!is_blob(enc)) throw 'encode() should return a blob'
|
||||
if (!is_blob(enc)) disrupt
|
||||
|
||||
var dec = wota.decode(enc)
|
||||
|
||||
var cmp = deep_compare(t.input, dec)
|
||||
if (!cmp.passed) throw text(cmp.messages, '; ')
|
||||
if (!cmp.passed) disrupt
|
||||
}
|
||||
}
|
||||
|
||||
var tests = {}
|
||||
for (var i = 0; i < length(testCases); i++) {
|
||||
var t = testCases[i]
|
||||
var name = t.name || ('case_' + i)
|
||||
for (i = 0; i < length(testCases); i++) {
|
||||
t = testCases[i]
|
||||
name = t.name || ('case_' + i)
|
||||
tests[name] = make_test(t)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user