attribute.html revision 7892f3d8ca60a683faeac9707e277bb47eec4126
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Attribute Tests</title>
<script type="text/javascript" src="/build/yui/yui.js"></script>
<style type="text/css">
#console .yui-console-entry-fail .yui-console-entry-cat {
background-color:red;
}
#console .yui-console-entry-pass .yui-console-entry-cat {
background-color:green;
}
#console {
position:static;
}
</style>
<script type="text/javascript">
YUI({base:"/build/", filter:"raw", useBrowserConsole:false}).use("base", "console", "test", function(Y) {
function areObjectsReallyEqual(o1, o2) {
Y.ObjectAssert.areEqual(o1, o2);
Y.ObjectAssert.areEqual(o2, o1);
}
function AttrHost(config) {
AttrHost.superclass.constructor.apply(this, arguments);
}
AttrHost.NAME = "attHost";
AttrHost.ATTRS = {
A: {
value:"AVal",
validator: Y.Lang.isString
},
B: {
validator: function(value) {
return (value === undefined || Y.Lang.isString(value) || Y.Lang.isNumber(value));
}
},
C: {
writeOnce: true
},
D: {
value:"DVal",
readOnly: true
},
E: {
value:"EVal",
writeOnce: true
},
DE: {
valueFn: function() {
return this.get("D") + this.get("E");
}
},
complex: {
value: {
X : {
A: 1
},
Y : {
A: 2
},
Z : {
A: 3
}
}
}
};
Y.extend(AttrHost, Y.Base);
// -----
function ExtendedAttrHost(config) {
AttrHost.superclass.constructor.apply(this, arguments);
}
ExtendedAttrHost.NAME = "extendedAttrHost";
ExtendedAttrHost.ATTRS = {
A: {
value:"ExtAVal"
},
B: {
value:"ExtBVal",
validator: function(value) {
return ((value == undefined) || Y.Lang.isString(value));
}
},
D: {
value:"ExtDVal",
setter: function(val) {
return (Y.Lang.isString(val)) ? val.toUpperCase() : val;
}
},
E: {
value:"ExtEVal",
getter: function(val) {
return (Y.Lang.isString(val)) ? val.toLowerCase() : val;
}
},
F: {
value:"ExtFVal",
setter: function(val) {
return (Y.Lang.isString(val)) ? val : Y.Attribute.INVALID_VALUE;
}
},
"complex.X.A" : {
value: 1111
},
"complex.Y.A" : {
value: 2222,
setter: function(val) { // Should be ignored. Can't set setters for complex sub vals
return val + 10000;
}
},
G : {
valueFn:function(val) {
// Referring to H before it's set up
return this.get("H") + 10;
}
},
H: {
value:5,
getter: function(val) {
return val*5;
}
}
};
Y.extend(ExtendedAttrHost, AttrHost);
//---
var sharedEventTests = {
testEventPrevent : function() {
var h = this.createHost();
var expectedEvents = ["BeforeMyNewAVal", "AfterMyNewAVal", "BeforePREVENT"];
var actualEvents = [];
h.on("AChange", function(e) {
actualEvents.push("Before" + e.newVal);
if (e.newVal == "PREVENT") {
e.preventDefault();
}
});
h.after("AChange", function(e) {
actualEvents.push("After" + e.newVal);
Y.Assert.areEqual("MyNewAVal", this.get("A"));
});
h.set("A", "MyNewAVal");
h.set("A", "PREVENT");
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
},
testEventBasic : function() {
var h = this.createHost({A:"MyAVal"});
var expectedEvents = ["BeforeMyNewAVal", "AfterMyNewAVal"];
var actualEvents = [];
h.on("AChange", function(e) {
actualEvents.push("Before" + e.newVal);
Y.Assert.areEqual("A", e.attrName);
Y.Assert.areEqual(null, e.subAttrName);
Y.Assert.areEqual("MyAVal", e.prevVal);
Y.Assert.areEqual("MyNewAVal", e.newVal);
});
h.after("AChange", function(e) {
actualEvents.push("After" + e.newVal);
Y.Assert.areEqual("A", e.attrName);
Y.Assert.areEqual(null, e.subAttrName);
Y.Assert.areEqual("MyAVal", e.prevVal);
Y.Assert.areEqual("MyNewAVal", e.newVal);
});
h.set("A", "MyNewAVal");
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
},
testEventStopImmediatePropagation: function() {
var h = this.createHost();
var expectedEvents = ["Before1MyNewAVal", "Before2MyNewAVal", "After1MyNewAVal", "After2MyNewAVal", "Before1STOPAFTER", "Before2STOPAFTER", "After1STOPAFTER", "Before1STOPBEFORE"];
var actualEvents = [];
h.on("AChange", function(e) {
actualEvents.push("Before1" + e.newVal);
if (e.newVal == "STOPBEFORE") {
e.stopImmediatePropagation();
}
});
h.after("AChange", function(e) {
actualEvents.push("After1" + e.newVal);
if (e.newVal == "STOPAFTER") {
e.stopImmediatePropagation();
}
});
h.on("AChange", function(e) {
actualEvents.push("Before2" + e.newVal);
});
h.after("AChange", function(e) {
actualEvents.push("After2" + e.newVal);
});
h.set("A", "MyNewAVal");
h.set("A", "STOPAFTER");
h.set("A", "STOPBEFORE");
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
},
testEventValidationReadonlyWriteOnce : function() {
var h = this.createHost({A:"MyAVal", C:"MyCVal", D:"MyDVal"});
var expectedEvents = ["BeforeAChange"];
var actualEvents = [];
h.on("AChange", function() {
actualEvents.push("BeforeAChange");
});
h.after("AChange", function() {
actualEvents.push("AfterAChange");
});
h.on("CChange", function() {
actualEvents.push("BeforeCChange");
});
h.on("CChange", function() {
actualEvents.push("BeforeCChange");
});
h.on("DChange", function() {
actualEvents.push("BeforeDChange");
});
h.on("DChange", function() {
actualEvents.push("BeforeDChange");
});
h.set("A", 200); // Invalid - before fired, after not fired [ value can be changed to be made valid ]
h.set("C", "MyNewCVal"); // Write Once - neither before nor after are fired
h.set("D", "MyNewDVal"); // Read Only - neither before not after are fired
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
},
testEventModify : function() {
var h = this.createHost({A:"MyAVal"});
h.on("AChange", function(e) {
Y.Assert.areEqual("MyAVal", e.prevVal);
e.newVal = e.newVal.toUpperCase();
});
h.after("AChange", function(e) {
Y.Assert.areEqual("MYNEWAVAL", e.newVal);
});
h.set("A", "MyNewAVal");
Y.Assert.areEqual("MYNEWAVAL", h.get("A"));
},
testEventSameValue : function() {
var h = this.createHost({A:"MyAVal"});
var expectedEvents = ["BeforeAChange", "BeforeAChange", "BeforeAChange", "AfterAChange", "BeforeComplexChange", "BeforeComplexChange", "AfterComplexChange", "BeforeComplexChange", "AfterComplexChange", "BeforeComplexChange", "AfterComplexChange"];
var actualEvents = [];
h.on("AChange", function(e) {
actualEvents.push("BeforeAChange");
});
h.after("AChange", function(e) {
actualEvents.push("AfterAChange");
Y.Assert.areNotSame(e.newVal, e.preVal);
});
h.on("complexChange", function(e) {
actualEvents.push("BeforeComplexChange");
});
h.after("complexChange", function(e) {
actualEvents.push("AfterComplexChange");
if (!e.subAttrName) {
Y.Assert.areNotSame(e.newVal, e.preVal, "Should not fire after event if object values are same");
}
});
h.set("A", h.get("A")); // No Change
h.set("A", "MyAVal"); // No Change
h.set("A", "MyNewAVal"); // Change
h.set("complex", h.get("complex")); // No Change
h.set("complex.Z.A", 3); // Change, even though value of complex is unchanged, we don't deep compare objects.
h.set("complex", Y.clone(h.get("complex"))); // Change, obj reference differs
h.set("complex", {A:1, B:2, C:3}); // Change, obj reference differs
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
},
testEventRegistrationThroughConstructor : function() {
// Initial Set does not fire events
var expectedEvents = ["OnInit", "AfterInit", "OnAChange", "AfterAChange"];
var actualEvents = [];
var h = this.createHost({
on: {
"AChange" : function() {
actualEvents.push("OnAChange");
},
"init" : function() {
actualEvents.push("OnInit");
}
},
after: {
"AChange" : function() {
actualEvents.push("AfterAChange");
},
"init" : function() {
actualEvents.push("AfterInit");
}
}
});
h.set("A", "Foo");
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
}
};
var basicTemplate = {
name: "Core Base Class Tests",
createHost : function(cfg) {
return new AttrHost(cfg);
},
setUp : function() {},
tearDown : function() {},
testDefault : function() {
var h = this.createHost();
Y.Assert.areEqual("AVal", h.get("A"));
Y.Assert.areEqual(undefined, h.get("B"));
Y.Assert.areEqual(undefined, h.get("C"));
Y.Assert.areEqual("DVal", h.get("D")); // Readonly
Y.Assert.areEqual("EVal", h.get("E")); // Write once, but not twice
Y.Assert.areEqual("DValEVal", h.get("DE"));
},
testConstructor : function() {
var h = this.createHost({A:"MyAVal", B:"MyBVal", C:"MyCVal", D:"MyDVal", E:"MyEVal", DE:"MyDEVal"});
h.set("C", "MyNewCVal");
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
Y.Assert.areEqual("MyAVal", h.get("A"));
Y.Assert.areEqual("MyBVal", h.get("B"));
Y.Assert.areEqual("MyCVal", h.get("C")); // Write Once, set in constructor
Y.Assert.areEqual("DVal", h.get("D")); // Read Only
Y.Assert.areEqual("MyEVal", h.get("E")); // Write Once, set in constructor
Y.Assert.areEqual("MyDEVal", h.get("DE"));
},
testSet : function() {
var h = this.createHost();
h.set("A", "MyNewAVal");
h.set("B", "MyNewBVal");
h.set("C", "MyNewCVal");
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
h.set("DE", "MyNewDEVal");
Y.Assert.areEqual("MyNewAVal", h.get("A"));
Y.Assert.areEqual("MyNewBVal", h.get("B"));
Y.Assert.areEqual("MyNewCVal", h.get("C")); // Write once, set on first set.
Y.Assert.areEqual("DVal", h.get("D")); // Read Only
Y.Assert.areEqual("EVal", h.get("E")); // Write Once
Y.Assert.areEqual("MyNewDEVal", h.get("DE"));
},
testReset : function() {
var h = this.createHost({A:"MyAVal", B:"MyBVal", C:"MyCVal", D:"MyDVal", E:"MyEVal", DE:"MyDEVal"});
h.set("A", "MyNewAVal");
h.set("B", "MyNewBVal");
h.set("C", "MyNewCVal");
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
h.set("DE", "MyNewDEVal");
Y.Assert.areEqual("MyNewAVal", h.get("A"));
Y.Assert.areEqual("MyNewBVal", h.get("B"));
Y.Assert.areEqual("MyCVal", h.get("C")); // Write once, set on first set.
Y.Assert.areEqual("DVal", h.get("D")); // Read Only
Y.Assert.areEqual("MyEVal", h.get("E")); // Write Once
Y.Assert.areEqual("MyNewDEVal", h.get("DE"));
h.reset("A");
h.reset("D");
Y.Assert.areEqual("MyAVal", h.get("A"));
Y.Assert.areEqual("DVal", h.get("D"));
h.reset();
Y.Assert.areEqual("MyAVal", h.get("A"));
Y.Assert.areEqual("MyBVal", h.get("B"));
Y.Assert.areEqual("MyCVal", h.get("C"));
Y.Assert.areEqual("DVal", h.get("D"));
Y.Assert.areEqual("MyEVal", h.get("E"));
Y.Assert.areEqual("MyDEVal", h.get("DE"));
},
testMassSetGet : function() {
var h = this.createHost();
h.setAttrs({
"A" : "MyNewAVal",
"B": "MyNewBVal",
"C": "MyNewCVal",
"D": "MyNewDVal",
"E": "MyNewEVal",
"DE": "MyNewDEVal",
complex: "MyNewComplexVal"
});
var expectedVals = {
A: "MyNewAVal",
B: "MyNewBVal",
C: "MyNewCVal",
D: "DVal",
E: "EVal",
DE: "MyNewDEVal",
complex: "MyNewComplexVal",
initialized:true,
destroyed:false
};
Y.Assert.areEqual(expectedVals.A, h.get("A"));
Y.Assert.areEqual(expectedVals.B, h.get("B"));
Y.Assert.areEqual(expectedVals.C, h.get("C")); // Write once, set on first set.
Y.Assert.areEqual(expectedVals.D, h.get("D")); // Read Only
Y.Assert.areEqual(expectedVals.E, h.get("E")); // Write Once
Y.Assert.areEqual(expectedVals.DE, h.get("DE"));
areObjectsReallyEqual(expectedVals, h.getAttrs());
},
testModifiedAttrs : function() {
var h = this.createHost();
h.setAttrs({
A: "MyNewAVal",
C: "MyNewCVal",
D: "MyNewDVal"
});
var expectedVals = {
A: "MyNewAVal",
initialized:true
};
areObjectsReallyEqual(expectedVals, h.getAttrs(true));
},
testValidation : function() {
var h = this.createHost();
h.set("A", "MyAVal");
Y.Assert.areEqual("MyAVal", h.get("A"));
h.set("A", 100);
Y.Assert.areEqual("MyAVal", h.get("A")); // Validation should prevent the attribute from being set
h.set("B", "two");
Y.Assert.areEqual("two", h.get("B"));
h.set("B", 2);
Y.Assert.areEqual(2, h.get("B"));
h.set("B", false);
Y.Assert.areEqual(2, h.get("B")); // Validation should prevent the attribute from being set
},
testPrivateSet : function() {
var h = this.createHost();
var expectedEvents = ["BeforeTryDAgain", "AfterTryDAgain", "BeforeTryEAgain", "AfterTryEAgain"];
var actualEvents = [];
h.on("DChange", function(e) {
actualEvents.push("Before" + e.newVal);
});
h.on("EChange", function(e) {
actualEvents.push("Before" + e.newVal);
});
h.after("DChange", function(e) {
actualEvents.push("After" + e.newVal);
});
h.after("EChange", function(e) {
actualEvents.push("After" + e.newVal);
});
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
Y.Assert.areEqual("DVal", h.get("D"));
Y.Assert.areEqual("EVal", h.get("E"));
h._set("D", "TryDAgain");
h._set("E", "TryEAgain");
Y.Assert.areEqual("TryDAgain", h.get("D"));
Y.Assert.areEqual("TryEAgain", h.get("E"));
},
testComplexDefault : function() {
var h = this.createHost();
var o = {
X : {
A: 1
},
Y : {
A: 2
},
Z : {
A: 3
}
};
Y.Assert.areEqual(1, h.get("complex.X.A"));
Y.Assert.areEqual(2, h.get("complex.Y.A"));
Y.Assert.areEqual(3, h.get("complex.Z.A"));
areObjectsReallyEqual({A:1}, h.get("complex.X"));
areObjectsReallyEqual({A:2}, h.get("complex.Y"));
areObjectsReallyEqual({A:3}, h.get("complex.Z"));
var val = h.get("complex");
Y.each(val, function(v, k) {
areObjectsReallyEqual(v, o[k]);
});
},
testComplexConstructor : function() {
var h = this.createHost({
"complex.X.A": 11,
"complex.Y.A": 12,
"complex.Z.A": 13,
"complex.W.A": 14 // Does not exist, not allowed to set
});
areObjectsReallyEqual({A:11}, h.get("complex.X"));
areObjectsReallyEqual({A:12}, h.get("complex.Y"));
areObjectsReallyEqual({A:13}, h.get("complex.Z"));
Y.Assert.areEqual(undefined, h.get("complex.W"));
},
testComplexSet : function() {
var h = this.createHost();
h.set("complex.X.A", 111);
Y.Assert.areEqual(111, h.get("complex.X.A"));
h.set("complex.X.B", 112);
Y.Assert.areEqual(112, h.get("complex.X.B"));
areObjectsReallyEqual({A:111, B:112}, h.get("complex.X"));
h.set("complex.W.B", 113);
Y.Assert.areEqual(undefined, h.get("complex.W"));
Y.Assert.areEqual(undefined, h.get("complex.W.B"));
h.set("complex.Y", {B:222});
Y.Assert.areEqual(222, h.get("complex.Y.B"));
Y.Assert.areEqual(undefined, h.get("complex.Y.A"));
},
testComplexEvents : function() {
var h = this.createHost();
var expectedEvents = ["Beforecomplex.X.A", "Aftercomplex.X.A", "Beforecomplex.Y.A", "Aftercomplex.Y.A", "Beforecomplex.Y", "Aftercomplex.Y"];
var actualEvents = [];
h.on("complexChange", function(e) {
actualEvents.push("Before" + e.subAttrName);
if (e.subAttrName == "complex.X.A") {
Y.Assert.areEqual(1111, e.newVal.X.A);
areObjectsReallyEqual({A:1111}, e.newVal.X);
areObjectsReallyEqual({A:2}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y.A") {
Y.Assert.areEqual(2222, e.newVal.Y.A);
areObjectsReallyEqual({A:1111}, e.newVal.X);
areObjectsReallyEqual({A:2222}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y") {
Y.Assert.areEqual(2223, e.newVal.Y);
areObjectsReallyEqual({A:1111}, e.newVal.X);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
});
h.after("complexChange", function(e) {
actualEvents.push("After" + e.subAttrName);
if (e.subAttrName == "complex.X.A") {
Y.Assert.areEqual(1111, e.newVal.X.A);
areObjectsReallyEqual({A:1111}, e.newVal.X);
areObjectsReallyEqual({A:2}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y.A") {
Y.Assert.areEqual(2222, e.newVal.Y.A);
areObjectsReallyEqual({A:1111}, e.newVal.X);
areObjectsReallyEqual({A:2222}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y") {
Y.Assert.areEqual(2223, e.newVal.Y);
areObjectsReallyEqual({A:1111}, e.newVal.X);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
});
h.set("complex.X.A", 1111);
h.set("complex.Y.A", 2222);
h.set("complex.Y", 2223);
h.set("complex.W.A", 3333);
}
};
basicTemplate = Y.merge(basicTemplate, sharedEventTests);
var extendedTemplate = {
name: "Core Extended Class Tests",
createHost : function(cfg) {
return new ExtendedAttrHost(cfg);
},
setUp : function() {},
tearDown : function() {},
testDefault : function() {
var h = this.createHost();
Y.Assert.areEqual("ExtAVal", h.get("A"));
Y.Assert.areEqual("ExtBVal", h.get("B"));
Y.Assert.areEqual(undefined, h.get("C"));
Y.Assert.areEqual("EXTDVAL", h.get("D"));
Y.Assert.areEqual("exteval", h.get("E"));
Y.Assert.areEqual("ExtFVal", h.get("F"));
Y.Assert.areEqual("EXTDVALexteval", h.get("DE"));
},
testConstructor : function() {
var h = this.createHost({A:"MyAVal", B:"MyBVal", C:"MyCVal", D:"MyDVal", E:"MyEVal", F:"MyFVal"});
h.set("C", "MyNewCVal");
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
Y.Assert.areEqual("MyAVal", h.get("A"));
Y.Assert.areEqual("MyBVal", h.get("B"));
Y.Assert.areEqual("MyCVal", h.get("C"));
Y.Assert.areEqual("EXTDVAL", h.get("D"));
Y.Assert.areEqual("myeval", h.get("E"));
Y.Assert.areEqual("MyFVal", h.get("F"));
Y.Assert.areEqual("EXTDVALmyeval", h.get("DE"));
},
testSet : function() {
var h = this.createHost();
h.set("A", "MyNewAVal");
h.set("B", "MyNewBVal");
h.set("C", "MyNewCVal");
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
h.set("F", "MyNewFVal");
h.set("DE", "MyNewDEVal");
Y.Assert.areEqual("MyNewAVal", h.get("A"));
Y.Assert.areEqual("MyNewBVal", h.get("B"));
Y.Assert.areEqual("MyNewCVal", h.get("C"));
Y.Assert.areEqual("EXTDVAL", h.get("D"));
Y.Assert.areEqual("exteval", h.get("E"));
Y.Assert.areEqual("MyNewFVal", h.get("F"));
Y.Assert.areEqual("MyNewDEVal", h.get("DE"));
},
testReset : function() {
var h = this.createHost({A:"MyAVal", B:"MyBVal", C:"MyCVal", D:"MyDVal", E:"MyEVal", F:"MyFVal", DE:"MyDEVal"});
h.set("A", "MyNewAVal");
h.set("B", "MyNewBVal");
h.set("C", "MyNewCVal");
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
h.set("F", "MyNewFVal");
h.set("DE", "MyNewDEVal");
Y.Assert.areEqual("MyNewAVal", h.get("A"));
Y.Assert.areEqual("MyNewBVal", h.get("B"));
Y.Assert.areEqual("MyCVal", h.get("C")); // Write once, set on first set.
Y.Assert.areEqual("EXTDVAL", h.get("D")); // Read Only
Y.Assert.areEqual("myeval", h.get("E")); // Write Once
Y.Assert.areEqual("MyNewFVal", h.get("F")); // Write Once
Y.Assert.areEqual("MyNewDEVal", h.get("DE"));
h.reset("A");
h.reset("D");
Y.Assert.areEqual("MyAVal", h.get("A"));
Y.Assert.areEqual("EXTDVAL", h.get("D"));
h.reset();
Y.Assert.areEqual("MyAVal", h.get("A"));
Y.Assert.areEqual("MyBVal", h.get("B"));
Y.Assert.areEqual("MyCVal", h.get("C"));
Y.Assert.areEqual("EXTDVAL", h.get("D"));
Y.Assert.areEqual("myeval", h.get("E"));
Y.Assert.areEqual("MyFVal", h.get("F"));
Y.Assert.areEqual("MyDEVal", h.get("DE"));
},
testMassSetGet : function() {
var h = this.createHost();
h.setAttrs({
"A" : "MyNewAVal",
"B": "MyNewBVal",
"C": "MyNewCVal",
"D": "MyNewDVal",
"E": "MyNewEVal",
"F": "MyNewFVal",
"DE": "MyNewDEVal",
complex: "MyNewComplexVal"
});
var expectedVals = {
A: "MyNewAVal",
B: "MyNewBVal",
C: "MyNewCVal",
D: "EXTDVAL",
E: "exteval",
F: "MyNewFVal",
DE: "MyNewDEVal",
complex: "MyNewComplexVal",
G: 35,
H: 25,
initialized:true,
destroyed:false
};
Y.Assert.areEqual(expectedVals.A, h.get("A"));
Y.Assert.areEqual(expectedVals.B, h.get("B"));
Y.Assert.areEqual(expectedVals.C, h.get("C")); // Write once, set on first set.
Y.Assert.areEqual(expectedVals.D, h.get("D")); // Read Only
Y.Assert.areEqual(expectedVals.E, h.get("E")); // Write Once
Y.Assert.areEqual(expectedVals.F, h.get("F"));
Y.Assert.areEqual(expectedVals.DE, h.get("DE"));
areObjectsReallyEqual(expectedVals, h.getAttrs());
},
testModifiedAttrs : function() {
var h = this.createHost();
h.setAttrs({
A: "MyNewAVal",
C: "MyNewCVal",
D: "MyNewDVal",
F: "MyNewFVal"
});
var expectedVals = {
A: "MyNewAVal",
F: "MyNewFVal",
initialized:true
};
areObjectsReallyEqual(expectedVals, h.getAttrs(true));
},
testValidation : function() {
var h = this.createHost();
h.set("A", "MyAVal");
Y.Assert.areEqual("MyAVal", h.get("A"));
h.set("A", 100);
Y.Assert.areEqual("MyAVal", h.get("A")); // Validation should prevent the attribute from being set
h.set("B", "two");
Y.Assert.areEqual("two", h.get("B"));
h.set("B", 2);
Y.Assert.areEqual("two", h.get("B")); // Validation should prevent the attribute from being set
h.set("B", true);
Y.Assert.areEqual("two", h.get("B")); // Validation should prevent the attribute from being set
h.set("F", "MyNewFVal");
Y.Assert.areEqual("MyNewFVal", h.get("F"));
h.set("F", 3);
Y.Assert.areEqual("MyNewFVal", h.get("F")); // Validation should prevent the attribute from being set
},
testPrivateSet : function() {
var h = this.createHost();
var expectedEvents = ["BeforeTryDAgain", "AfterTRYDAGAIN", "BeforeTryEAgain", "AfterTryEAgain" ]; // Last entry: e.newVal is not "get" normalized
var actualEvents = [];
h.on("DChange", function(e) {
actualEvents.push("Before" + e.newVal);
});
h.on("EChange", function(e) {
actualEvents.push("Before" + e.newVal);
});
h.after("DChange", function(e) {
actualEvents.push("After" + e.newVal);
});
h.after("EChange", function(e) {
actualEvents.push("After" + e.newVal);
});
h.set("D", "MyNewDVal");
h.set("E", "MyNewEVal");
Y.Assert.areEqual("EXTDVAL", h.get("D"));
Y.Assert.areEqual("exteval", h.get("E"));
h._set("D", "TryDAgain");
h._set("E", "TryEAgain");
Y.Assert.areEqual("TRYDAGAIN", h.get("D"));
Y.Assert.areEqual("tryeagain", h.get("E"));
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents);
},
testComplexDefault : function() {
var h = this.createHost();
var o = {
X : {
A: 1111
},
Y : {
A: 2222
},
Z : {
A: 3
}
};
Y.Assert.areEqual(1111, h.get("complex.X.A"));
Y.Assert.areEqual(2222, h.get("complex.Y.A"));
Y.Assert.areEqual(3, h.get("complex.Z.A"));
areObjectsReallyEqual({A:1111}, h.get("complex.X"));
areObjectsReallyEqual({A:2222}, h.get("complex.Y"));
areObjectsReallyEqual({A:3}, h.get("complex.Z"));
var val = h.get("complex");
Y.each(val, function(v, k) {
areObjectsReallyEqual(v, o[k]);
});
},
testComplexConstructor : function() {
var h = this.createHost({
"complex.X.A": 11,
"complex.Y.A": 12,
"complex.Z.A": 13,
"complex.W.A": 14 // Does not exist, not allowed to set
});
areObjectsReallyEqual({A:11}, h.get("complex.X"));
areObjectsReallyEqual({A:12}, h.get("complex.Y"));
areObjectsReallyEqual({A:13}, h.get("complex.Z"));
Y.Assert.areEqual(undefined, h.get("complex.W"));
},
testComplexSet : function() {
var h = this.createHost();
h.set("complex.X.A", 111);
Y.Assert.areEqual(111, h.get("complex.X.A"));
h.set("complex.X.B", 112);
Y.Assert.areEqual(112, h.get("complex.X.B"));
areObjectsReallyEqual({A:111, B:112}, h.get("complex.X"));
h.set("complex.W.B", 113);
Y.Assert.areEqual(undefined, h.get("complex.W"));
Y.Assert.areEqual(undefined, h.get("complex.W.B"));
h.set("complex.Y", {B:222});
Y.Assert.areEqual(222, h.get("complex.Y.B"));
Y.Assert.areEqual(undefined, h.get("complex.Y.A"));
},
testComplexEvents : function() {
var h = this.createHost();
var expectedEvents = ["Beforecomplex.X.A", "Aftercomplex.X.A", "Beforecomplex.Y.A", "Aftercomplex.Y.A", "Beforecomplex.Y", "Aftercomplex.Y"];
var actualEvents = [];
h.on("complexChange", function(e) {
actualEvents.push("Before" + e.subAttrName);
if (e.subAttrName == "complex.X.A") {
Y.Assert.areEqual(11111, e.newVal.X.A);
areObjectsReallyEqual({A:11111}, e.newVal.X);
areObjectsReallyEqual({A:2222}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y.A") {
Y.Assert.areEqual(22222, e.newVal.Y.A);
areObjectsReallyEqual({A:11111}, e.newVal.X);
areObjectsReallyEqual({A:22222}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y") {
Y.Assert.areEqual(22233, e.newVal.Y);
areObjectsReallyEqual({A:11111}, e.newVal.X);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
});
h.after("complexChange", function(e) {
actualEvents.push("After" + e.subAttrName);
if (e.subAttrName == "complex.X.A") {
Y.Assert.areEqual(11111, e.newVal.X.A);
areObjectsReallyEqual({A:11111}, e.newVal.X);
areObjectsReallyEqual({A:2222}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y.A") {
Y.Assert.areEqual(22222, e.newVal.Y.A);
areObjectsReallyEqual({A:11111}, e.newVal.X);
areObjectsReallyEqual({A:22222}, e.newVal.Y);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
if (e.subAttrName == "complex.Y") {
Y.Assert.areEqual(22233, e.newVal.Y);
areObjectsReallyEqual({A:11111}, e.newVal.X);
areObjectsReallyEqual({A:3}, e.newVal.Z);
}
});
h.set("complex.X.A", 11111);
h.set("complex.Y.A", 22222);
h.set("complex.Y", 22233);
h.set("complex.W.A", 33333);
},
testOnDemandInit : function() {
var h = this.createHost();
Y.Assert.areEqual(35, h.get("G"));
Y.Assert.areEqual(25, h.get("H"));
}
};
extendedTemplate = Y.merge(extendedTemplate, sharedEventTests);
//create the console
new Y.Console({
width:"100%",
height:"50em",
boundingBox:"#console",
verbose : false,
printTimeout: 0,
newestOnTop : false,
entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
'<span class="{entry_cat_class}">{label}</span>'+
'<span class="{entry_content_class}">{message}</span>'+
'</pre>'
}).render();
Y.Test.Runner.clear();
Y.Test.Runner.add(new Y.Test.Case(basicTemplate));
Y.Test.Runner.add(new Y.Test.Case(extendedTemplate));
Y.Test.Runner.add(new Y.Test.Case(basicTemplate)); // Running again, to make sure static attributes aren't modified
Y.Test.Runner.add(new Y.Test.Case(extendedTemplate)); // Running again, to make sure static attributes aren't modified
Y.Test.Runner.run();
});
</script>
</head>
<body class="yui-skin-sam">
<div id="console"></div>
</body>
</html>