diff --git a/src/Main.hx b/src/Main.hx deleted file mode 100644 index fccaa37..0000000 --- a/src/Main.hx +++ /dev/null @@ -1,59 +0,0 @@ -package ; -class Main { - - static function main() { - trace("tada"); - - bindx.BindSignal.BindSignalProvider.register(); - - var v = new Value(); - var s = {t:0}; - - bindx.Bind.bindx(v.str, function (from, to) {trace('str changed from $from to $to');}); - v.strChanged.add(function (from, to) {trace('str changed from $from to $to');}); - v.str = "12"; - bindx.Bind.bindx(v.int, function (a, b) { trace(b); }); - var unbind = bindx.Bind.bindTo(v.int, s.t); - v.int = 10; - trace(s.t); - unbind(); - v.int = 12; - trace(s.t); - trace(v.str); - - } -} - -@:bindable -class Value implements bindx.IBindable { - - public function new() { - } - - @:bindable(lazySignal=true, inlineSignalGetter=false, inlineSetter=true) - public var str:String; - - @:bindable(force=true, inlineSetter=true) - public var int(default, set):Int; - - private var noBindPrivate:Int; - - function set_int(v):Int { - - if (v < 0) { - int = 0; - toStringChanged.dispatch(); - intChanged.dispatch(v, int); - return int; - } - - intChanged.dispatch(int, int = v); - toStringChanged.dispatch(); - return v; - } - - @:bindable() - public function toString() { - return str + int; - } -} diff --git a/src/bindx/Bind.hx b/src/bindx/Bind.hx index 7a85b02..7947dd8 100644 --- a/src/bindx/Bind.hx +++ b/src/bindx/Bind.hx @@ -30,8 +30,8 @@ class Bind { return internalNotify(field, oldValue, newValue); } - @:noUsing macro static public function disposeBindings(object:ExprOf):Expr { - return internalDisposeBindings(object); + @:noUsing macro static public function unbindAll(object:ExprOf):Expr { + return internalUnbindAll(object); } #if macro @@ -54,12 +54,12 @@ class Bind { return BindMacros.bindingSignalProvider.getClassFieldChangedExpr(fieldData.e, fieldData.field, oldValue, newValue); } - public static function internalDisposeBindings(object:ExprOf):Expr { + public static function internalUnbindAll(object:ExprOf):Expr { var type = Context.typeof(object).follow(); if (!isBindable(type.getClass())) { Context.error('\'${object.toString()}\' must be bindx.IBindable', object.pos); } - return BindMacros.bindingSignalProvider.getDisposeBindingsExpr(object, type); + return BindMacros.bindingSignalProvider.getUnbindAllExpr(object, type); } public static function checkField(field:Expr):{e:Expr, field:ClassField} { diff --git a/src/bindx/BindSignal.hx b/src/bindx/BindSignal.hx index 7cf787b..aa9ddec 100644 --- a/src/bindx/BindSignal.hx +++ b/src/bindx/BindSignal.hx @@ -88,14 +88,15 @@ class BindSignalProvider implements IBindingSignalProvider { return dispatchSignal(expr, field.name, args, hasLazy(field.bindableMeta())); } - public function getDisposeBindingsExpr(expr:ExprOf, type:Type):Expr { + public function getUnbindAllExpr(expr:ExprOf, type:Type):Expr { return macro { var meta = haxe.rtti.Meta.getFields(std.Type.getClass($expr)); if (meta != null) for (m in std.Reflect.fields(meta)) { var data:Dynamic = std.Reflect.field(meta, m); if (std.Reflect.hasField(data, $v{BIND_SIGNAL_META})) { var signal:bindx.BindSignal.Signal = cast Reflect.field($expr, m); - if (signal != null) signal.dispose(); + if (signal != null) + signal.removeAll(); } } } @@ -195,7 +196,7 @@ class Signal { var listeners:Array; - var lock = 0; + var lock:Int = 0; public function new() { removeAll(); @@ -203,10 +204,7 @@ class Signal { public inline function removeAll() { listeners = []; - } - - public inline function dispose() { - listeners = null; + lock = 0; } public function add(listener:T):Void { diff --git a/src/bindx/IBindingSignalProvider.hx b/src/bindx/IBindingSignalProvider.hx index eed6abd..17b3918 100644 --- a/src/bindx/IBindingSignalProvider.hx +++ b/src/bindx/IBindingSignalProvider.hx @@ -12,6 +12,6 @@ interface IBindingSignalProvider { function getClassFieldBindToExpr(expr:Expr, field:ClassField, target:Expr):Expr; function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr; function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr; - function getDisposeBindingsExpr(expr:ExprOf, type:Type):Expr; + function getUnbindAllExpr(expr:ExprOf, type:Type):Expr; #end } diff --git a/test.hxml b/test.hxml new file mode 100644 index 0000000..5f4c041 --- /dev/null +++ b/test.hxml @@ -0,0 +1,9 @@ +-main Tests +-cp src +-cp test +-D dump=pretty +-neko bin/test.n +-lib buddy +-lib utest +-debug +-D fdb diff --git a/test/BaseTest.hx b/test/BaseTest.hx index fd3f91b..34a59c3 100644 --- a/test/BaseTest.hx +++ b/test/BaseTest.hx @@ -2,115 +2,113 @@ package ; import bindx.Bind; import bindx.Bind.bind in bind; -import haxe.unit.TestCase; +import buddy.*; -class BaseTest extends TestCase { +using buddy.Should; + +class BaseTest extends BuddySuite { public function new() { super(); - } + + describe("Base bindx functionality", { + + var b:Bindable1; + var callNum:Int; + + before({ + b = new Bindable1(); + callNum = 0; + }); + + it ("bindx should bind properties", { + var strFrom = b.str = "a"; + + bind(b.str, function (from, to) { + from.should.be(strFrom); + to.should.be(b.str); + callNum ++; + }); - function test1() { - var b = new Bindable1(); - b.str = "a"; - var callNum = 0; - bind(b.str, function (from, to) { - assertEquals(from, "a"); - assertEquals(to, "b"); - callNum ++; + Bind.bind(b.str, function (from, to) { + from.should.be(strFrom); + to.should.be(b.str); + callNum ++; + }); + + b.str = "b"; + callNum.should.be(2); + }); + + it("bindx should bind 'null' values", { + b.str = null; + var listener = function (from:String, to:String) { + from.should.be(null); + to.should.be(b.str); + callNum ++; + } + + bind(b.str, listener); + Bind.bind(b.str, listener); + b.str = ""; + callNum.should.be(1); + + bindx.Bind.unbind(b.str, listener); + b.str = "1"; + callNum.should.be(1); + }); + + it("bindx should notify manual", { + b.str = "3"; + var f = "1"; + var t = "2"; + var listener = function (from:String, to:String) { + from.should.be(f); + to.should.be(t); + callNum ++; + }; + bind(b.str, listener); + + Bind.notify(b.str, f, t); + callNum.should.be(1); + }); + + it("bindx should unbind all listeners", { + bind(b.str, function (from, to) callNum++); + bind(b.str, function (from, to) callNum++); + + Bind.unbind(b.str); + b.str = b.str + "1"; + + callNum.should.be(0); + }); + + it("bindx should unbind all bindings (signal exists)", { + bind(b.str, function (_, _) callNum++); // create binding signal + bind(b.bind, function () callNum++); + + Bind.unbindAll(b); + + b.str = b.str + "1"; + b.bind(); + callNum.should.be(0); + }); + + it("bindx should unbind all bindings (signal expected)", { + Bind.unbindAll(b); + + b.str = b.str + "1"; + b.bind(); + callNum.should.be(0); + }); }); - - bind(b.str, function (from, to) { - assertEquals(from, "a"); - assertEquals(to, "b"); - callNum ++; - }); - b.str = "b"; - assertEquals(callNum, 2); - } - - function test2() { - var b = new Bindable1(); - b.str = null; - var callNum = 0; - var listener = function (from, to) { - assertEquals(from, null); - assertEquals(to, ""); - callNum ++; - } - - bind(b.str, listener); - bind(b.str, listener); - b.str = ""; - assertEquals(callNum, 1); - - Bind.bind(b.str, listener); - bindx.Bind.unbind(b.str, listener); - b.str = "1"; - assertEquals(callNum, 1); - } - - function test3() { - var b = new Bindable1(); - b.str = null; - var callNum = 0; - - bind(b.str, function (_, _) callNum++); - bind(b.str, function (_, _) callNum++); - b.str = ""; - assertEquals(callNum, 2); - - Bind.unbind(b.str); - b.str = "1"; - assertEquals(callNum, 2); - - Bind.disposeBindings(b); - var addError = false; - try { - Bind.bind(b.str, function (_, _) {}); - } catch (e:Dynamic) { - addError = true; - } - assertTrue(addError); - } - - function test4() { - var b = new Bindable1(); - b.str = null; - var callNum = 0; - var listener = function (from, to) { - assertEquals(from, "1"); - assertEquals(to, "2"); - callNum ++; - } - - Bind.bind(b.str, listener); - Bind.notify(b.str, "1", "2"); - assertEquals(callNum, 1); - - Bind.notify(b.str, "1", "2"); - assertEquals(callNum, 2); - } - - function test5() { - var b = new Bindable1(); - b.str = null; - var callNum = 0; - Bind.bind(b.bind, function () callNum++); - - b.i = 10; - assertEquals(callNum, 1); - assertFalse(Reflect.hasField(b, "noBindChanged")); - - Bind.notify(b.bind); - assertEquals(callNum, 2); } } @:bindable class Bindable1 implements bindx.IBindable { - + @:bindable(lazySignal=false) public var str:String; @:bindable diff --git a/test/Tests.hx b/test/Tests.hx index 7234740..2fea814 100644 --- a/test/Tests.hx +++ b/test/Tests.hx @@ -1,14 +1,6 @@ package ; -import haxe.unit.TestRunner; +import buddy.BuddySuite; -class Tests { - - static function main() { - var runner = new TestRunner(); - runner.add(new BaseTest()); - runner.add(new InheritanceTest()); - runner.add(new TestProperty()); - runner.run(); - } -} \ No newline at end of file +@:build(buddy.GenerateMain.build(null, ["test"])) +class Tests extends BuddySuite {} \ No newline at end of file