From 321d50cd8bcd71ad9b6c8a2c63d2335872f9bca7 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Sun, 10 Mar 2019 05:23:04 +0100 Subject: [PATCH] (pair) test coverage --- dom.js | 2 +- pair.js | 16 +++++++++------- pair.test.js | 18 ++++++++++++++++++ test.js | 4 +++- 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 pair.test.js diff --git a/dom.js b/dom.js index aaadf17..3965868 100644 --- a/dom.js +++ b/dom.js @@ -74,7 +74,7 @@ export const removeEventListeners = (node, listeners) => { /** * @param {string} name - * @param {Array>} attrs Array of key-value pairs + * @param {Array|pair.Pair>} attrs Array of key-value pairs * @param {Array} children * @return {Element} */ diff --git a/pair.js b/pair.js index c089dd4..a52d72c 100644 --- a/pair.js +++ b/pair.js @@ -1,7 +1,6 @@ /** - * @template L - * @template R + * @template L,R */ export class Pair { /** @@ -14,21 +13,24 @@ export class Pair { } } +/** + * @template L,R + * @param {L} left + * @param {R} right + * @return {Pair} + */ export const create = (left, right) => new Pair(left, right) export const createReversed = (right, left) => new Pair(left, right) /** - * @template L - * @template R + * @template L,R * @param {Array>} arr * @param {function(L, R):any} f */ export const forEach = (arr, f) => arr.forEach(p => f(p.left, p.right)) /** - * @template L - * @template R - * @template X + * @template L,R,X * @param {Array>} arr * @param {function(L, R):X} f * @return {Array} diff --git a/pair.test.js b/pair.test.js new file mode 100644 index 0000000..0179ac4 --- /dev/null +++ b/pair.test.js @@ -0,0 +1,18 @@ +import * as t from './testing.js' +import * as pair from './pair.js' +import * as math from './math.js' + +export const testPair = tc => { + const ps = [pair.create(1, 2), pair.create(3, 4), pair.createReversed(6, 5)] + t.describe('Counting elements in pair list') + let countLeft = 0 + let countRight = 0 + pair.forEach(ps, (left, right) => { + countLeft += left + countRight += right + }) + t.assert(countLeft === 9) + t.assert(countRight === 12) + t.assert(countLeft === pair.map(ps, left => left).reduce(math.add)) + t.assert(countRight === pair.map(ps, (left, right) => right).reduce(math.add)) +} diff --git a/test.js b/test.js index d4b6a09..80ad944 100644 --- a/test.js +++ b/test.js @@ -15,6 +15,7 @@ import * as promise from './promise.test.js' import * as map from './map.test.js' import * as eventloop from './eventloop.test.js' import * as time from './time.test.js' +import * as pair from './pair.test.js' import { isBrowser, isNode } from './environment.js' @@ -37,7 +38,8 @@ runTests({ promise, map, eventloop, - time + time, + pair }).then(success => { /* istanbul ignore next */ if (isNode) {