buddy lib for tests, replace disposeBindings with unbindAll
This commit is contained in:
-59
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+4
-4
@@ -30,8 +30,8 @@ class Bind {
|
|||||||
return internalNotify(field, oldValue, newValue);
|
return internalNotify(field, oldValue, newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@:noUsing macro static public function disposeBindings(object:ExprOf<IBindable>):Expr {
|
@:noUsing macro static public function unbindAll(object:ExprOf<IBindable>):Expr {
|
||||||
return internalDisposeBindings(object);
|
return internalUnbindAll(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if macro
|
#if macro
|
||||||
@@ -54,12 +54,12 @@ class Bind {
|
|||||||
return BindMacros.bindingSignalProvider.getClassFieldChangedExpr(fieldData.e, fieldData.field, oldValue, newValue);
|
return BindMacros.bindingSignalProvider.getClassFieldChangedExpr(fieldData.e, fieldData.field, oldValue, newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function internalDisposeBindings(object:ExprOf<IBindable>):Expr {
|
public static function internalUnbindAll(object:ExprOf<IBindable>):Expr {
|
||||||
var type = Context.typeof(object).follow();
|
var type = Context.typeof(object).follow();
|
||||||
if (!isBindable(type.getClass())) {
|
if (!isBindable(type.getClass())) {
|
||||||
Context.error('\'${object.toString()}\' must be bindx.IBindable', object.pos);
|
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} {
|
public static function checkField(field:Expr):{e:Expr, field:ClassField} {
|
||||||
|
|||||||
@@ -88,14 +88,15 @@ class BindSignalProvider implements IBindingSignalProvider {
|
|||||||
return dispatchSignal(expr, field.name, args, hasLazy(field.bindableMeta()));
|
return dispatchSignal(expr, field.name, args, hasLazy(field.bindableMeta()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDisposeBindingsExpr(expr:ExprOf<IBindable>, type:Type):Expr {
|
public function getUnbindAllExpr(expr:ExprOf<IBindable>, type:Type):Expr {
|
||||||
return macro {
|
return macro {
|
||||||
var meta = haxe.rtti.Meta.getFields(std.Type.getClass($expr));
|
var meta = haxe.rtti.Meta.getFields(std.Type.getClass($expr));
|
||||||
if (meta != null) for (m in std.Reflect.fields(meta)) {
|
if (meta != null) for (m in std.Reflect.fields(meta)) {
|
||||||
var data:Dynamic<String> = std.Reflect.field(meta, m);
|
var data:Dynamic<String> = std.Reflect.field(meta, m);
|
||||||
if (std.Reflect.hasField(data, $v{BIND_SIGNAL_META})) {
|
if (std.Reflect.hasField(data, $v{BIND_SIGNAL_META})) {
|
||||||
var signal:bindx.BindSignal.Signal<Dynamic> = cast Reflect.field($expr, m);
|
var signal:bindx.BindSignal.Signal<Dynamic> = cast Reflect.field($expr, m);
|
||||||
if (signal != null) signal.dispose();
|
if (signal != null)
|
||||||
|
signal.removeAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,7 +196,7 @@ class Signal<T> {
|
|||||||
|
|
||||||
var listeners:Array<T>;
|
var listeners:Array<T>;
|
||||||
|
|
||||||
var lock = 0;
|
var lock:Int = 0;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
removeAll();
|
removeAll();
|
||||||
@@ -203,10 +204,7 @@ class Signal<T> {
|
|||||||
|
|
||||||
public inline function removeAll() {
|
public inline function removeAll() {
|
||||||
listeners = [];
|
listeners = [];
|
||||||
}
|
lock = 0;
|
||||||
|
|
||||||
public inline function dispose() {
|
|
||||||
listeners = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add(listener:T):Void {
|
public function add(listener:T):Void {
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ interface IBindingSignalProvider {
|
|||||||
function getClassFieldBindToExpr(expr:Expr, field:ClassField, target:Expr):Expr;
|
function getClassFieldBindToExpr(expr:Expr, field:ClassField, target:Expr):Expr;
|
||||||
function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr;
|
function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr;
|
||||||
function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr;
|
function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr;
|
||||||
function getDisposeBindingsExpr(expr:ExprOf<IBindable>, type:Type):Expr;
|
function getUnbindAllExpr(expr:ExprOf<IBindable>, type:Type):Expr;
|
||||||
#end
|
#end
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
-main Tests
|
||||||
|
-cp src
|
||||||
|
-cp test
|
||||||
|
-D dump=pretty
|
||||||
|
-neko bin/test.n
|
||||||
|
-lib buddy
|
||||||
|
-lib utest
|
||||||
|
-debug
|
||||||
|
-D fdb
|
||||||
+95
-97
@@ -2,115 +2,113 @@ package ;
|
|||||||
|
|
||||||
import bindx.Bind;
|
import bindx.Bind;
|
||||||
import bindx.Bind.bind in 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() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
}
|
|
||||||
|
|
||||||
function test1() {
|
describe("Base bindx functionality", {
|
||||||
var b = new Bindable1();
|
|
||||||
b.str = "a";
|
var b:Bindable1;
|
||||||
var callNum = 0;
|
var callNum:Int;
|
||||||
bind(b.str, function (from, to) {
|
|
||||||
assertEquals(from, "a");
|
before({
|
||||||
assertEquals(to, "b");
|
b = new Bindable1();
|
||||||
callNum ++;
|
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 ++;
|
||||||
|
});
|
||||||
|
|
||||||
|
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
|
@:bindable
|
||||||
class Bindable1 implements bindx.IBindable {
|
class Bindable1 implements bindx.IBindable {
|
||||||
|
|
||||||
|
@:bindable(lazySignal=false)
|
||||||
public var str:String;
|
public var str:String;
|
||||||
|
|
||||||
@:bindable
|
@:bindable
|
||||||
|
|||||||
+3
-11
@@ -1,14 +1,6 @@
|
|||||||
package ;
|
package ;
|
||||||
|
|
||||||
import haxe.unit.TestRunner;
|
import buddy.BuddySuite;
|
||||||
|
|
||||||
class Tests {
|
@:build(buddy.GenerateMain.build(null, ["test"]))
|
||||||
|
class Tests extends BuddySuite {}
|
||||||
static function main() {
|
|
||||||
var runner = new TestRunner();
|
|
||||||
runner.add(new BaseTest());
|
|
||||||
runner.add(new InheritanceTest());
|
|
||||||
runner.add(new TestProperty());
|
|
||||||
runner.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user