Skip to content

Commit 59f4097

Browse files
author
Bradley Meck
committed
test cases up
1 parent d8a41a8 commit 59f4097

File tree

12 files changed

+182
-99
lines changed

12 files changed

+182
-99
lines changed

lib/proxy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
var Overload=require("overload");
22
var Watchable=Overload.Watchable;
33
var RePrototype=Overload.RePrototype;
4-
var Proxy = {
4+
module.exports = {
55
create: function(handler,prototype) {
66
var proxy=Watchable(
77
//get
8-
function(context) {
9-
return handler["get"].call(context.thisObject,context.holder,context.property)
8+
function(ArgInfo) {
9+
return handler["get"].call(ArgInfo.thisObject,ArgInfo.holder,ArgInfo.property)
1010
}
1111
//set
1212
,function(context) {
1313
return handler["set"].call(context.thisObject,context.holder,context.property,context.value)
1414
}
1515
//enum
1616
,function(context) {
17-
return handler["enumerate"]();
17+
return handler["enumerate"].call(context.thisObject);
1818
}
1919
//query
2020
,function(context) {

lib/util.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var overload=require("./overload")
22
, Watchable = overload.Watchable
33
, RePrototype = overload.RePrototype
44

5-
exports.ReadOnly=ReadOnly=function(obj,mask) {
5+
exports.ReadOnly=ReadOnly=function(obj,mask,filter) {
66
var $this=Watchable(function(args){
77
var v=mask
88
&& args.property in mask
@@ -12,7 +12,12 @@ exports.ReadOnly=ReadOnly=function(obj,mask) {
1212
return function(){return v.apply(obj,arguments)}
1313
}
1414
return v;
15-
});
15+
},filter?function(ArgInfo){
16+
var property = ArgInfo.property
17+
if(filter(property)) {
18+
obj[property] = ArgInfo.value
19+
}
20+
}:undefined);
1621
//only objects have prototypes
1722
if(obj instanceof Object) {
1823
RePrototype($this,Object.getPrototypeOf(obj))
@@ -29,6 +34,9 @@ ReadOnly.ArrayMask={
2934
,splice:function(){return [];}
3035
,unshift:function(){return this.length}
3136
}
37+
ReadOnly.ArrayFilter = function(property) {
38+
return typeof property !== "number"
39+
}
3240
ReadOnly.DateMask={
3341
setDate:noop
3442
,setFullYear:noop

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
, "version" : "1.2.3"
44
, "author": "bradleymeck"
55
, "main": "lib/overload.node"
6-
, "scripts": {
7-
"install" : "make"
6+
, "scripts":
7+
{ "install" : "make"
8+
, "test" : "test/all.js"
89
}
910
}

src/watchable.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// var debug = Watchable(function(p,v){if(typeof v == "number") return v; sys.puts(p);return v;})
99

1010
#include <v8.h>
11-
#include <map>
12-
#include <iostream>
1311

1412
using namespace std;
1513
using namespace v8;
@@ -93,15 +91,10 @@ Handle<Array> WatchableNamedPropertyEnumerator(
9391
Handle<Function> callback = Handle<Function>::Cast(data);
9492
Handle<Value> new_value = callback->Call(info.This(),1,values);
9593
//Return value is the return of the function call
96-
printf("test\n");
97-
9894
if(new_value->IsArray()) {
99-
printf("ok?\n");
10095
return scope.Close(Handle<Array>::Cast(new_value));
10196
}
10297
else {
103-
printf("wtf\n");
104-
ThrowException(Exception::Error(String::New("Callback must return an Array.")));
10598
return scope.Close(Array::New());
10699
}
107100
}
@@ -197,7 +190,6 @@ Handle<Value> WatchableIndexedPropertySetter(
197190
//Default
198191
Handle<Value> data = callbacks->GetInternalField(1);
199192
if(data->IsNull()) {
200-
printf("defaulted setter");
201193
return scope.Close(Undefined());
202194
}
203195
//Set up arguments
@@ -236,7 +228,6 @@ Handle<Array> WatchableIndexedPropertyEnumerator(
236228
return scope.Close(Handle<Array>::Cast(new_value));
237229
}
238230
else {
239-
ThrowException(String::New("Callback must return an Array."));
240231
return scope.Close(Array::New());
241232
}
242233
}

test/all.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require("./oncollect")
2+
require("./watchable")
3+
require("./proxy")
4+
require("./readonly")
5+
console.log("ALL PASSED")
6+
process.exit(0)

test/compilable.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/compilable_data.jsc

Whitespace-only changes.

test/compile.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/oncollect.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var assert = require("assert")
2+
var OnCollect = require("../lib/overload").OnCollect;
3+
var b = {}
4+
, c = {}
5+
, waiting = true
6+
b.a = c
7+
8+
OnCollect(b,function(obj) {
9+
assert.equal(obj.a,c,"revival object validation")
10+
waiting = false
11+
} )
12+
13+
b = null
14+
assert.equal(b,null,"removal")
15+
16+
setTimeout(
17+
function wait(){if(waiting)setTimeout(wait,1000)}
18+
,1000
19+
)

test/proxy.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,60 @@
1-
var Watchable = require("../lib/overload").Watchable
1+
var Proxy = require("../lib/proxy")
22
, sys = require("sys")
3-
var proxied={hello:""}
4-
var debug = Watchable(
5-
//GETTER
6-
function(ArgInfo){
7-
//check the type of the object contained by value
8-
//undefined if no value, or number if using []'s with a number
9-
var property=ArgInfo.property
10-
sys.puts("GET:"+JSON.stringify(property),"VALUE:"+String(proxied[property]));
11-
return proxied[property];
12-
}
13-
//SETTER
14-
, function(ArgInfo) {
15-
var property=ArgInfo.property
16-
var value=ArgInfo.value
17-
sys.puts("GET:"+JSON.stringify(property),"VALUE:"+String(proxied[property]),"NEW VALUE:"+String(value));
18-
return ArgInfo.value;
19-
}
20-
)
21-
debug.hello
22-
debug[" world!"]=true
23-
3+
, assert = require("assert")
244
var x={A:1,B:2},y={B:"B",C:"C"}
255
var proto_chain = [x,y];
26-
var multi_proto = Watchable(
6+
var multi_proto = Proxy.create({
277
//GETTER
28-
function(ArgInfo){
29-
//check the type of the object contained by value
30-
//undefined if no value, or number if using []'s with a number
31-
var property=ArgInfo.property
32-
for(var i=proto_chain.length;i>=0;i--) {
8+
"get": function(holder,property){
9+
for(var i=proto_chain.length-1;i>=0;i--) {
3310
var proto = proto_chain[i]
3411
if(property in proto) {
3512
return proto[property]
3613
}
3714
}
38-
return undefined
15+
return null
3916
}
4017
//SETTER
41-
, function(ArgInfo) {
18+
, "set": function(holder,property,value) {
4219
//DONT SET THE CHAINS VALUES!
4320
}
44-
, function(ArgInfo) {
45-
21+
//FOREACH
22+
, "enumerate": function() {
23+
var properties={}
24+
for(var i=proto_chain.length-1;i>=0;i--) {
25+
var proto = proto_chain[i]
26+
var keys = Object.getOwnPropertyNames(proto)
27+
keys = keys.filter(function(item){
28+
return !(item in properties)
29+
})
30+
keys.forEach(function(item){
31+
properties[item]=proto[item]
32+
})
33+
}
34+
return Object.getOwnPropertyNames(properties)
35+
}
36+
, "has": function(property) {
37+
return proto_chain.some(function(item){
38+
return property in item
39+
} )
40+
}
41+
, "delete": function(ArgInfo) {
4642
}
47-
, function(ArgInfo) {
48-
var property=ArgInfo.property
49-
return proto_chain.some(function(item){return property in item})
43+
//No DELETER
5044
}
5145
)
52-
53-
var z = function(){};
54-
z.prototype = multi_proto
55-
z = new z;
56-
console.log(z.A);
57-
console.log(z.B);
58-
console.log(z.C);
46+
var zz = function(){};
47+
zz.prototype = multi_proto
48+
var z = new zz;
49+
assert.equal(z.A,1,"2nd Chain");
50+
assert.equal(z.B,"B","1st Chain");
51+
assert.equal(z.C,"C","1st Chain");
52+
z.A = 2
53+
delete z.A
54+
assert.equal(z.A,1,"Setter");
55+
assert.equal("A" in z,true,"query")
56+
var hold = []
57+
for(var i in z) {
58+
hold.push(i)
59+
}
60+
assert.deepEqual(hold,["A","C","B"],"foreach")

0 commit comments

Comments
 (0)