Permalink
Browse files

test cases up

  • Loading branch information...
1 parent d8a41a8 commit 59f409754f7cae70f043c9397158f2e43c4c6bba Bradley Meck committed Jul 28, 2010
Showing with 182 additions and 99 deletions.
  1. +4 −4 lib/proxy.js
  2. +10 −2 lib/util.js
  3. +3 −2 package.json
  4. +0 −9 src/watchable.cc
  5. +6 −0 test/all.js
  6. +0 −1 test/compilable.js
  7. 0 test/compilable_data.jsc
  8. +0 −39 test/compile.js
  9. +19 −0 test/oncollect.js
  10. +44 −42 test/proxy.js
  11. +11 −0 test/readonly.js
  12. +85 −0 test/watchable.js
View
@@ -1,20 +1,20 @@
var Overload=require("overload");
var Watchable=Overload.Watchable;
var RePrototype=Overload.RePrototype;
-var Proxy = {
+module.exports = {
create: function(handler,prototype) {
var proxy=Watchable(
//get
- function(context) {
- return handler["get"].call(context.thisObject,context.holder,context.property)
+ function(ArgInfo) {
+ return handler["get"].call(ArgInfo.thisObject,ArgInfo.holder,ArgInfo.property)
}
//set
,function(context) {
return handler["set"].call(context.thisObject,context.holder,context.property,context.value)
}
//enum
,function(context) {
- return handler["enumerate"]();
+ return handler["enumerate"].call(context.thisObject);
}
//query
,function(context) {
View
@@ -2,7 +2,7 @@ var overload=require("./overload")
, Watchable = overload.Watchable
, RePrototype = overload.RePrototype
-exports.ReadOnly=ReadOnly=function(obj,mask) {
+exports.ReadOnly=ReadOnly=function(obj,mask,filter) {
var $this=Watchable(function(args){
var v=mask
&& args.property in mask
@@ -12,7 +12,12 @@ exports.ReadOnly=ReadOnly=function(obj,mask) {
return function(){return v.apply(obj,arguments)}
}
return v;
- });
+ },filter?function(ArgInfo){
+ var property = ArgInfo.property
+ if(filter(property)) {
+ obj[property] = ArgInfo.value
+ }
+ }:undefined);
//only objects have prototypes
if(obj instanceof Object) {
RePrototype($this,Object.getPrototypeOf(obj))
@@ -29,6 +34,9 @@ ReadOnly.ArrayMask={
,splice:function(){return [];}
,unshift:function(){return this.length}
}
+ReadOnly.ArrayFilter = function(property) {
+ return typeof property !== "number"
+}
ReadOnly.DateMask={
setDate:noop
,setFullYear:noop
View
@@ -3,7 +3,8 @@
, "version" : "1.2.3"
, "author": "bradleymeck"
, "main": "lib/overload.node"
-, "scripts": {
- "install" : "make"
+, "scripts":
+ { "install" : "make"
+ , "test" : "test/all.js"
}
}
View
@@ -8,8 +8,6 @@
// var debug = Watchable(function(p,v){if(typeof v == "number") return v; sys.puts(p);return v;})
#include <v8.h>
-#include <map>
-#include <iostream>
using namespace std;
using namespace v8;
@@ -93,15 +91,10 @@ Handle<Array> WatchableNamedPropertyEnumerator(
Handle<Function> callback = Handle<Function>::Cast(data);
Handle<Value> new_value = callback->Call(info.This(),1,values);
//Return value is the return of the function call
- printf("test\n");
-
if(new_value->IsArray()) {
- printf("ok?\n");
return scope.Close(Handle<Array>::Cast(new_value));
}
else {
- printf("wtf\n");
- ThrowException(Exception::Error(String::New("Callback must return an Array.")));
return scope.Close(Array::New());
}
}
@@ -197,7 +190,6 @@ Handle<Value> WatchableIndexedPropertySetter(
//Default
Handle<Value> data = callbacks->GetInternalField(1);
if(data->IsNull()) {
- printf("defaulted setter");
return scope.Close(Undefined());
}
//Set up arguments
@@ -236,7 +228,6 @@ Handle<Array> WatchableIndexedPropertyEnumerator(
return scope.Close(Handle<Array>::Cast(new_value));
}
else {
- ThrowException(String::New("Callback must return an Array."));
return scope.Close(Array::New());
}
}
View
@@ -0,0 +1,6 @@
+require("./oncollect")
+require("./watchable")
+require("./proxy")
+require("./readonly")
+console.log("ALL PASSED")
+process.exit(0)
View
Oops, something went wrong.
View
No changes.
View
@@ -1,39 +0,0 @@
-var overload=require("../lib/overload");
-var fs=require("fs");
-var sys=require("sys");
-var Buffer=require("buffer").Buffer;
-sys.puts(process.argv)
-var time
-
-function CompileTo(src,path) {
- var CompileData=overload.Compile(fs.readFileSync("compilable.js"));
- //sys.puts(CompileData.byteLength())
- sys.puts(CompileData.length)
- fs.writeFileSync("compilable_data.jsc",CompileData);
-}
-
-function LoadFrom(path) {
- overload.Load(
- fs.readFileSync("compilable_data.jsc",'binary')
- )
-}
-
-if(process.argv[2]=="compile") {
- time=new Date().getTime();
- CompileTo(fs.readFileSync("compilable.js"),"compilable_data.jsc");
-}
-else if(process.argv[2]=="require") {
- time=new Date().getTime();
- var cat=require("./compilable").cat;
-}
-else if(process.argv[2]=="compiledload") {
- time=new Date().getTime();
-}
-else {
- CompileTo(fs.readFileSync("compilable.js"),"compilable_data.jsc");
- LoadFrom("compilable_data.jsc")
-}
-sys.puts("START:"+time)
-if(typeof a!="undefined") sys.puts("CAT:"+JSON.stringify(a));
-var newt=(new Date()).getTime()
-sys.puts("End:"+(newt-time),newt,time)
View
@@ -0,0 +1,19 @@
+var assert = require("assert")
+var OnCollect = require("../lib/overload").OnCollect;
+var b = {}
+ , c = {}
+ , waiting = true
+b.a = c
+
+OnCollect(b,function(obj) {
+ assert.equal(obj.a,c,"revival object validation")
+ waiting = false
+} )
+
+b = null
+assert.equal(b,null,"removal")
+
+setTimeout(
+ function wait(){if(waiting)setTimeout(wait,1000)}
+ ,1000
+)
View
@@ -1,58 +1,60 @@
-var Watchable = require("../lib/overload").Watchable
+var Proxy = require("../lib/proxy")
, sys = require("sys")
-var proxied={hello:""}
-var debug = Watchable(
- //GETTER
- function(ArgInfo){
- //check the type of the object contained by value
- //undefined if no value, or number if using []'s with a number
- var property=ArgInfo.property
- sys.puts("GET:"+JSON.stringify(property),"VALUE:"+String(proxied[property]));
- return proxied[property];
- }
- //SETTER
- , function(ArgInfo) {
- var property=ArgInfo.property
- var value=ArgInfo.value
- sys.puts("GET:"+JSON.stringify(property),"VALUE:"+String(proxied[property]),"NEW VALUE:"+String(value));
- return ArgInfo.value;
- }
-)
-debug.hello
-debug[" world!"]=true
-
+ , assert = require("assert")
var x={A:1,B:2},y={B:"B",C:"C"}
var proto_chain = [x,y];
-var multi_proto = Watchable(
+var multi_proto = Proxy.create({
//GETTER
- function(ArgInfo){
- //check the type of the object contained by value
- //undefined if no value, or number if using []'s with a number
- var property=ArgInfo.property
- for(var i=proto_chain.length;i>=0;i--) {
+ "get": function(holder,property){
+ for(var i=proto_chain.length-1;i>=0;i--) {
var proto = proto_chain[i]
if(property in proto) {
return proto[property]
}
}
- return undefined
+ return null
}
//SETTER
- , function(ArgInfo) {
+ , "set": function(holder,property,value) {
//DONT SET THE CHAINS VALUES!
}
- , function(ArgInfo) {
-
+ //FOREACH
+ , "enumerate": function() {
+ var properties={}
+ for(var i=proto_chain.length-1;i>=0;i--) {
+ var proto = proto_chain[i]
+ var keys = Object.getOwnPropertyNames(proto)
+ keys = keys.filter(function(item){
+ return !(item in properties)
+ })
+ keys.forEach(function(item){
+ properties[item]=proto[item]
+ })
+ }
+ return Object.getOwnPropertyNames(properties)
+ }
+ , "has": function(property) {
+ return proto_chain.some(function(item){
+ return property in item
+ } )
+ }
+ , "delete": function(ArgInfo) {
}
- , function(ArgInfo) {
- var property=ArgInfo.property
- return proto_chain.some(function(item){return property in item})
+ //No DELETER
}
)
-
-var z = function(){};
-z.prototype = multi_proto
-z = new z;
-console.log(z.A);
-console.log(z.B);
-console.log(z.C);
+var zz = function(){};
+zz.prototype = multi_proto
+var z = new zz;
+assert.equal(z.A,1,"2nd Chain");
+assert.equal(z.B,"B","1st Chain");
+assert.equal(z.C,"C","1st Chain");
+z.A = 2
+delete z.A
+assert.equal(z.A,1,"Setter");
+assert.equal("A" in z,true,"query")
+var hold = []
+for(var i in z) {
+ hold.push(i)
+}
+assert.deepEqual(hold,["A","C","B"],"foreach")
View
@@ -0,0 +1,11 @@
+var ReadOnly = require("../lib/util").ReadOnly
+ , assert = require("assert")
+var a = []
+ , ra = ReadOnly(a,ReadOnly.ArrayMask,ReadOnly.ArrayFilter)
+ra.push(1)
+assert.equal(a.length,0,"push")
+ra[0]=2
+assert.equal(a.length,0,"index")
+assert.equal(a[0],null,"get")
+a[0]=1
+assert.equal(ra.length,1,"index")
View
@@ -0,0 +1,85 @@
+var Watchable = require("../lib/overload").Watchable
+ , sys = require("sys")
+ , assert = require("assert")
+var proxied={hello:""}
+var debug = Watchable(
+ //GETTER
+ function(ArgInfo){
+ //check the type of the object contained by value
+ //undefined if no value, or number if using []'s with a number
+ var property=ArgInfo.property
+ assert.equal(property,"hello","GETTER PROPERTY NAME");
+ //sys.puts("GET:"+JSON.stringify(property),"VALUE:"+String(proxied[property]));
+ return proxied[property];
+ }
+ //SETTER
+ , function(ArgInfo) {
+ var property=ArgInfo.property
+ assert.equal(property," world!","SETTER PROPERTY NAME");
+ var value=ArgInfo.value
+ assert.equal(value,true,"SETTER PROPERTY VALUE");
+ //sys.puts("GET:"+JSON.stringify(property),"VALUE:"+String(proxied[property]),"NEW VALUE:"+String(value));
+ return ArgInfo.value;
+ }
+)
+debug.hello
+debug[" world!"]=true
+
+var x={A:1,B:2},y={B:"B",C:"C"}
+var proto_chain = [x,y];
+var multi_proto = Watchable(
+ //GETTER
+ function(ArgInfo){
+ //check the type of the object contained by value
+ //undefined if no value, or number if using []'s with a number
+ var property=ArgInfo.property
+ for(var i=proto_chain.length-1;i>=0;i--) {
+ var proto = proto_chain[i]
+ if(property in proto) {
+ return proto[property]
+ }
+ }
+ return null
+ }
+ //SETTER
+ , function(ArgInfo) {
+ //DONT SET THE CHAINS VALUES!
+ }
+ //FOREACH
+ , function(ArgInfo) {
+ var properties={}
+ for(var i=proto_chain.length-1;i>=0;i--) {
+ var proto = proto_chain[i]
+ var keys = Object.getOwnPropertyNames(proto)
+ keys = keys.filter(function(item){
+ return !(item in properties)
+ })
+ keys.forEach(function(item){
+ properties[item]=proto[item]
+ })
+ }
+ return Object.getOwnPropertyNames(properties)
+ }
+ , function(ArgInfo) {
+ var property=ArgInfo.property
+ return proto_chain.some(function(item){
+ return property in item
+ } )
+ }
+ //No DELETER
+)
+var zz = function(){};
+zz.prototype = multi_proto
+z = new zz;
+assert.equal(z.A,1,"2nd Chain");
+assert.equal(z.B,"B","1st Chain");
+assert.equal(z.C,"C","1st Chain");
+z.A = 2
+delete z.A
+assert.equal(z.A,1,"Setter");
+assert.ok("A" in z,"query")
+var hold = []
+for(var i in z) {
+ hold.push(i)
+}
+assert.deepEqual(hold,["A","C","B"],"foreach")

0 comments on commit 59f4097

Please sign in to comment.