Skip to content

Commit 9d5d5c4

Browse files
committed
first pass at reflection API tests
1 parent 82c680e commit 9d5d5c4

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

test/reflection.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* global describe, it */
2+
var assert = require('assert')
3+
var protocol = require('../')
4+
5+
describe('reflection API', function () {
6+
describe('isDerivable', function () {
7+
it('returns true if all protocol methods have default impls', function () {
8+
var deriv = protocol({
9+
foo: [function () {}],
10+
bar: [function () {}],
11+
baz: [function () {}]
12+
})
13+
assert.ok(protocol.isDerivable(deriv), 'derivable protocol returns true')
14+
15+
var oneMethod = protocol({
16+
foo: []
17+
})
18+
assert.ok(!protocol.isDerivable(oneMethod))
19+
20+
var partialDefaults = protocol({
21+
foo: [],
22+
bar: [function () {}]
23+
})
24+
assert.ok(!protocol.isDerivable(partialDefaults))
25+
})
26+
})
27+
describe('hasImpl', function () {
28+
it('returns true for implemented method-style protocols', function () {
29+
var obj = {}
30+
var proto = protocol({
31+
foo: [],
32+
bar: []
33+
})
34+
assert.ok(!protocol.hasImpl(proto, obj))
35+
proto(obj, {
36+
foo: function () {},
37+
bar: function () {}
38+
})
39+
assert.ok(protocol.hasImpl(proto, obj))
40+
assert.ok(!protocol.hasImpl(proto, {}))
41+
})
42+
it('returns true for implemented static protocols', function () {
43+
var obj1 = {}
44+
var obj2 = {}
45+
var proto = protocol(['a', 'b'], {
46+
foo: ['a', 'b'],
47+
bar: ['a', 'b']
48+
})
49+
proto([obj1, obj2], {
50+
foo: function () {},
51+
bar: function () {}
52+
})
53+
assert.ok(protocol.hasImpl(proto, [obj1, obj2]))
54+
assert.ok(!protocol.hasImpl(proto, [obj2, obj1]))
55+
assert.ok(!protocol.hasImpl(proto, [obj1, {}]))
56+
assert.ok(!protocol.hasImpl(proto, [5, 'ok']))
57+
})
58+
it('returns true for implemented multimethod protocols', function () {
59+
var obj1 = {}
60+
var obj2 = {}
61+
var proto = protocol(['a'], {
62+
foo: ['a'],
63+
bar: ['a']
64+
})
65+
proto(obj1, [obj2], {
66+
foo: function () {},
67+
bar: function () {}
68+
})
69+
assert.ok(protocol.hasImpl(proto, obj1, [obj2]))
70+
assert.ok(!protocol.hasImpl(proto, obj2, [obj1]))
71+
assert.ok(!protocol.hasImpl(proto, 5, [obj1]))
72+
assert.ok(!protocol.hasImpl(proto, {}, [obj2]))
73+
})
74+
})
75+
describe('MOP', function () {
76+
describe('createGenfun', function () {
77+
it('is called when static methods are created')
78+
it('is called for individual method-style implementations')
79+
it('defaults to creating a genfun')
80+
it('defaults to adding the protocol-impl error message')
81+
})
82+
describe('addMethod', function () {
83+
it('is called when methods are added to method-style methods')
84+
it('is called when methods are added to static methods')
85+
it('defaults to calling .add on the respective genfun')
86+
})
87+
})
88+
})

0 commit comments

Comments
 (0)