« get me outta code hell

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:
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;
+  }
+});