« get me outta code hell

contract: BlackBox stub & NormalizedArrayMap - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/unit
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-05-07 19:21:41 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-05-07 19:21:41 -0300
commit86e8b47b5aeeae5f2fc3b87bb5930fb4c25f88ab (patch)
tree558ad9c3a647a139b0ca7d0f936c7861fa6162d3 /test/unit
parent63b1b5b6fd14d3bacdcb979298b4fa669de4f20b (diff)
contract: BlackBox stub & NormalizedArrayMap
Spooky scary skeletons

Also includes a bonus very dumb performance test!
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/contract/black-box.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/test/unit/contract/black-box.js b/test/unit/contract/black-box.js
new file mode 100644
index 00000000..21c05b52
--- /dev/null
+++ b/test/unit/contract/black-box.js
@@ -0,0 +1,81 @@
+import {BlackBox} from '../../../src/contract.js';
+import {mockFunction} from '../../lib/generic-mock.js';
+import {showAggregate} from '../../../src/util/sugar.js';
+
+import t from 'tap';
+
+t.test(`BlackBox - caching`, t => {
+  t.plan(8);
+
+  const obj1 = {foo: 3, bar: 4};
+  const obj2 = {baz: 5, qux: 6};
+
+  let {value: fn, close: closeMock} =
+    mockFunction((object, key) => object[key] ** 2)
+
+  fn = fn
+    .args([obj1, 'foo']).next()
+    .args([obj2, 'qux']).next()
+    .args([obj1, 'bar']).next()
+    .args([obj2, 'baz']);
+
+  const bb = new BlackBox(fn);
+  const evaluate = bb.getEvaluator();
+
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+  t.equal(evaluate(obj2, 'qux'), 6 ** 2);
+  t.equal(evaluate(obj2, 'qux'), 6 ** 2);
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+
+  t.equal(evaluate(obj1, 'bar'), 4 ** 2);
+  t.equal(evaluate(obj2, 'baz'), 5 ** 2);
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+
+  try {
+    closeMock();
+  } catch (error) {
+    showAggregate(error);
+    throw error;
+  }
+});
+
+t.test(`BlackBox - no caching`, t => {
+  t.plan(8);
+
+  const obj1 = {foo: 3, bar: 4};
+  const obj2 = {baz: 5, qux: 6};
+
+  let {value: fn, close: closeMock} =
+    mockFunction((object, key) => object[key] ** 2)
+
+  fn = fn
+    .args([obj1, 'foo']).repeat(2)
+    .args([obj2, 'qux']).repeat(2)
+    .args([obj1, 'foo']).next()
+    .args([obj1, 'bar']).next()
+    .args([obj2, 'baz']).next()
+    .args([obj1, 'foo']);
+
+  const bb = new BlackBox(fn);
+  const evaluate = bb.getEvaluator();
+
+  bb.caching = false;
+
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+  t.equal(evaluate(obj2, 'qux'), 6 ** 2);
+  t.equal(evaluate(obj2, 'qux'), 6 ** 2);
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+
+  t.equal(evaluate(obj1, 'bar'), 4 ** 2);
+  t.equal(evaluate(obj2, 'baz'), 5 ** 2);
+  t.equal(evaluate(obj1, 'foo'), 3 ** 2);
+
+  try {
+    closeMock();
+  } catch (error) {
+    showAggregate(error);
+    throw error;
+  }
+});