My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(function () {
var testobj = new Test.TAP.Class();
testobj.plan(13);

testobj.testMultiMethod = function() {
var t = this;
t.diag("Sanity");
t.ok(Joose, "Joose is here");
t.ok(Joose.MultiMethod, "We have the MultiMethod class");

Class("MultiMethodTestClass", {
methods: {
test: function () { return "foo" }
}
});

var method = new Joose.MultiMethod("multi", function () {}, {});
var patterns = [
{
signature : [],
method : function() {
return "foo";
}
},
// Order matters. So this signature has to come before the other
// Rule of thumb more specific matches go before more general matches.
// this is considered a feature
{
signature : ["quux", TYPE.Func],
method : function() {
return "fooQUUX";
}
},
{
signature : [TYPE.Str, TYPE.Func],
method : function() {
return "fooStringFunc";
}
}
];
method.setPatterns(patterns);
MultiMethodTestClass.meta.addMethodObject(method);

var o = new MultiMethodTestClass();

t.ok(o.meta.can("multi"), "Method is there");
t.ok(typeof o.multi === "function", "There is actually a function in the spot");

var result;
t.lives_ok(function () {
result = o.multi();
}, "calling with correct argument signature lives")

t.is(result, "foo", "result from method call is correct")

var result2;
t.lives_ok(function () {
result2 = o.multi("blah", function() {});
}, "calling with a different correct argument signature lives")

t.ok(result != result2,
"result from method calls with different signatures "
+ "produce different results")

t.is(result2, "fooStringFunc", "result from second signature for method"
+ "call is correct")

// exceptions
t.throws_ok(function () {
o.multi(1,2,3)
}, /multi-method type method call with no matching signature/,
"Calling with incorrect signature throws exception")

t.is(o.multi("quux", function() {}), "fooQUUX",
'dispatched on value also')
// builder syntax
//t.diag("Testing builder syntax for typed methods")

Class('MultiDispatchClassSyntax', {
has: {
infoLog: {is: 'rw'}
, debugLog: {is: 'rw'}
},
methods: {
log: [
{
signature: ["info", TYPE.Str],
method: function(type, str) {
this.setInfoLog(str);
}
}
, {
signature: ["debug", TYPE.Str],
method: function(type, str) {
this.setDebugLog(str);
}
}
, {
signature: [TYPE.Str, Object],
method: function(type, obj) {
this.log(type, "Object: foo Encountered");
}
}
]
}
});

var testobj = new MultiDispatchClassSyntax();
t.ok(typeof testobj.log == 'function', 'our function is there');

testobj.log("info", "log one for info");
t.is(testobj.getInfoLog(), "log one for info",
'the method dispatched correctly');
};

return testobj;
})()

Change log

r459 by Jer...@marzhillstudios.com on Jan 19, 2009   Diff
refactored MultiMethods namespace out of
JooseX
Go to: 
Sign in to write a code review

Older revisions

r457 by Jer...@marzhillstudios.com on Jan 19, 2009   Diff
Multi-Method builder syntax
r456 by Jer...@marzhillstudios.com on Jan 19, 2009   Diff
descriptive comments on the test
patterns
r455 by Jer...@marzhillstudios.com on Jan 19, 2009   Diff
dispatching on value besides just
instanceof and Joose.TypeConstraint
now
All revisions of this file

File info

Size: 3530 bytes, 117 lines
Powered by Google Project Hosting