diff --git a/smoke.sh b/smoke.sh index c1ad035..b477e0f 100755 --- a/smoke.sh +++ b/smoke.sh @@ -1,4 +1,6 @@ #!/bin/bash node-waf configure && node-waf build && echo '-- Compiled OK -- -' && node smoketest \ No newline at end of file +' && node test/smoke.js && echo '-- Tests Done, running smoke -- + +' && node smoketest.js diff --git a/smoketest.js b/smoketest.js index 7d091fd..b2f1932 100755 --- a/smoketest.js +++ b/smoketest.js @@ -1,14 +1,5 @@ var opencv = require('./lib/opencv') , assert = require('assert') -assert.ok(!!opencv) -assert.ok(!!opencv.Point) -assert.ok(!!new opencv.Point(1, 2)) -assert.equal(new opencv.Point(1, 2).x, 1) -assert.equal(new opencv.Point(1, 2).y, 2) -assert.equal(Math.round(new opencv.Point(1.1, 2).x * 100), 110) -assert.equal(Math.round(new opencv.Point(1.2, 2.75).y *100), 275) - - - +console.log(opencv.version) console.log(new opencv.Image("./examples/mona.jpg").width, "!!") \ No newline at end of file diff --git a/src/OpenCV.cc b/src/OpenCV.cc index 862774b..d9292dc 100644 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -4,5 +4,9 @@ void OpenCV::Init(Handle target) { HandleScope scope; - target->Set(String::NewSymbol("version"), String::New("1")); + + // Version string. + char out [21]; + int n = sprintf(out, "%i.%i", CV_MAJOR_VERSION, CV_MINOR_VERSION); + target->Set(String::NewSymbol("version"), String::New(out, n)); } \ No newline at end of file diff --git a/src/OpenCV.h b/src/OpenCV.h index 67a2f57..1db7094 100644 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -12,7 +12,6 @@ using namespace v8; using namespace node; - class OpenCV: public node::ObjectWrap{ public: static void Init(Handle target); diff --git a/src/Point.cc b/src/Point.cc index 3d86c14..c4e7ba8 100644 --- a/src/Point.cc +++ b/src/Point.cc @@ -15,22 +15,21 @@ Point::Init(Handle target) { // Prototype Local proto = constructor->PrototypeTemplate(); - proto->SetAccessor(String::NewSymbol("x"), GetX); - proto->SetAccessor(String::NewSymbol("y"), GetY); + proto->SetAccessor(String::NewSymbol("x"), GetX, RaiseImmutable); + proto->SetAccessor(String::NewSymbol("y"), GetY, RaiseImmutable); - - /*proto->SetAccessor(String::NewSymbol("source"), GetSource, SetSource); - proto->SetAccessor(String::NewSymbol("complete"), GetComplete); - proto->SetAccessor(String::NewSymbol("width"), GetWidth); - proto->SetAccessor(String::NewSymbol("height"), GetHeight); - proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload); - proto->SetAccessor(String::NewSymbol("onerror"), GetOnerror, SetOnerror);*/ + NODE_SET_PROTOTYPE_METHOD(constructor, "dot", Dot); + target->Set(String::NewSymbol("Point"), constructor->GetFunction()); }; Handle Point::New(const Arguments &args) { HandleScope scope; + + if (args.This()->InternalFieldCount() == 0) + return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new"))); + double x = 0, y = 0; if (args[0]->IsNumber()) x = args[0]->NumberValue(); if (args[1]->IsNumber()) y = args[1]->NumberValue(); @@ -53,6 +52,21 @@ Point::GetY(Local prop, const AccessorInfo &info) { return scope.Close(Number::New(pt->point.y)); } +void +Point::RaiseImmutable(Local property, Local value, const AccessorInfo& info) { + v8::ThrowException(v8::Exception::TypeError(v8::String::New("Point is immutable"))); +} + +Handle +Point::Dot(const v8::Arguments& args){ + HandleScope scope; + Point *p1 = ObjectWrap::Unwrap(args.This()); + Point *p2 = ObjectWrap::Unwrap(args[0]->ToObject()); + + // Since V 2.3 Native Dot no longer supported + return scope.Close(Number::New(p1->point.x * p2->point.x + p1->point.y * p2->point.y)); +} + Point::Point(double x, double y): ObjectWrap() { point = cvPoint2D32f(x, y); diff --git a/src/Point.h b/src/Point.h index ac2616b..dbb6663 100644 --- a/src/Point.h +++ b/src/Point.h @@ -12,4 +12,7 @@ class Point: public node::ObjectWrap { static Handle GetX(Local prop, const AccessorInfo &info); static Handle GetY(Local prop, const AccessorInfo &info); + static void RaiseImmutable(Local property, Local value, const AccessorInfo& info); + static Handle Dot(const v8::Arguments&); + }; \ No newline at end of file diff --git a/test/smoke.js b/test/smoke.js new file mode 100644 index 0000000..630ee4f --- /dev/null +++ b/test/smoke.js @@ -0,0 +1,67 @@ +var vows = require('vows') + , assert = require('assert') + + + +vows.describe('Smoke Tests OpenCV').addBatch({ + "Importing": { + + topic: require('../lib/opencv') + + , "returns obj": function(topic){ + assert.ok(!!topic) + } + + , '.version' : function(topic){ + assert.ok(!!topic.version) + } + + , '.Point imports': function(topic){ + assert.ok(!!topic.Point) + } + + , '.Image imports' : function(topic){ + assert.ok(!!topic.Image) + + } + } + + , "Point" : { + topic : require('../lib/opencv') + + , 'constructor' : function(cv){ + assert.ok(!!new cv.Point(1, 2)) + assert.throws(function () { cv.Point(1, 2)}, TypeError); // cannot call without new + } + + , 'accessors' : function(cv){ + assert.equal(new cv.Point(1, 2).x, 1) + assert.equal(new cv.Point(1, 2).y, 2) + assert.equal(Math.round(new cv.Point(1.1, 2).x * 100), 110) + assert.equal(Math.round(new cv.Point(1.2, 2.75).y *100), 275) + + assert.throws(function () {new cv.Point(1.1, 2).x = 5}, Error); // Points are immutable + assert.throws(function () {new cv.Point(1.1, 2).y = 5}, Error); // Points are immutable + + } + + + , '.dot': function(cv){ + var p1 = new cv.Point(3, 6) + , p2 = new cv.Point(5, 7) + + assert.ok(p1.dot); + assert.ok(p1.dot(p2)); + + + } + + , '.inside' : function(){} + } + + + , "Image" : { + + + } +}).run(); \ No newline at end of file