substitute.html revision 637be785ab98741335d5cd6634fddc5f6ecdb41b
<html>
<head>
<title>YUI substitute Tests</title>
</head>
<body class="yui-skin-sam">
<h1>substitute Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
<script type="text/javascript">
(function() {
YUI({
base: "/build/",
filter: "debug",
useConsole: true,
// logInclude: ['event', 'test']
//logExclude: ['Dom', 'Selector', 'Node', 'attribute']
logExclude: {Dom:true,
Selector:true, Node:true, attribute:true, event:true, base:true, widget:true}
}).use("dump", "test", "console", "event-custom", function(Y) {
var button = Y.get('#btnRun'),
Assert = Y.Assert,
ObjectAssert = Y.ObjectAssert;
// Set up the page
button.set("disabled", false);
Y.on("click", function() {
}, button);
var myConsole = new Y.Console().render();
var testSubstitute= new Y.Test.Case({
name: "Substitute tests",
test_substitute: function() {
////////////////////////////////////////////////////////////////////////////////
var param = {domain: 'valvion.com',
media: 'http://media.{domain}/', /* nested */
contextdomain: { context1: 'context{domain}', context2: 'yahoo.com' }, /* the value is an object, we will use a custom function to extract the correct data */
contextmedia: 'http://contextmedia.{contextdomain context1}/'};
var url;
// standard replace, nested
url = Y.substitute("{media}logo.gif", param);
// If the replacement value is an object, use the meta info as a key to extract the
// correct data. Otherwise just return the value.
function multipleChoice(key, val, meta) {
return (Y.Lang.isObject(val)) ? val[meta] : val;
}
// "random data" is not used since the value for the contextmedia key is a string.
// contextdomain uses "context1" as a key to expand the string correctly
url = Y.substitute("{contextmedia random data}logo.gif", param, multipleChoice);
////////////////////////////////////////////////////////////////////////////////
var obj = {
level1_1: 1.1,
level1_2: 1.2,
level1_3: {
level2_1: 2.1,
level2_2: 2.2,
level2_3: {
level3_1: 3.1,
level3_2: 3.2,
level3_3: 3.3,
level3_4: 3.4
},
level2_4: 2.4
},
level1_4: 1.4
};
Y.substitute("{testobj 0}", { testobj: obj }),
"{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {...}, level1_4 => 1.4}",
"failed one level object dump"
);
Y.substitute("{testobj 1}", { testobj: obj }),
"{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {level2_1 => 2.1, level2_2 => 2.2, level2_3 => {...}, level2_4 => 2.4}, level1_4 => 1.4}",
"failed two level object dump"
);
"{testobj 10}", { testobj: obj }),
"{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {level2_1 => 2.1, level2_2 => 2.2, level2_3 => {level3_1 => 3.1, level3_2 => 3.2, level3_3 => 3.3, level3_4 => 3.4}, level2_4 => 2.4}, level1_4 => 1.4}",
"failed deep object dump"
);
var arr = [
1.1,
1.2,
[
2.1,
2.2,
[
3.1,
3.2,
3.3,
3.4
],
2.4
],
1.4
];
Y.substitute("{testarr 1}", { testarr: arr }),
"[1.1, 1.2, [2.1, 2.2, {...}, 2.4], 1.4]",
"failed two level array dump"
);
var mix = [
1.1,
new Date(),
{
level2_1: 2.1,
level2_2: 2.2,
level2_3: [
3.1,
3.2,
3.3,
3.4
],
level2_4: 2.4
},
1.4,
function(){}
];
var result = Y.substitute("{testmix 1}", { testmix: mix });
(result.indexOf("GMT" > -1)) ? true : false,
"failed two level mixed object with a date (date should have produced an output with GMT in it)"
);
////////////////////////////////////////////////////////////////////////////////
var tostr = {
custom1_1: 1.1,
custom1_2: 1.2,
custom1_3: {
custom2_1: 2.1,
custom2_2: 2.2,
custom2_3: {
custom3_1: 3.1,
custom3_2: 3.2,
custom3_3: 3.3,
custom3_4: 3.4
},
custom2_4: 2.4
},
custom1_4: 1.4,
toString: function() {
return "custom toString executed";
}
};
Y.substitute("{customtostr 1}", { customtostr: tostr }),
"custom toString executed",
"failed: custom toString should have been used"
);
Y.substitute("{customtostr dump 1}", { customtostr: tostr }),
"{custom1_1 => 1.1, custom1_2 => 1.2, custom1_3 => {custom2_1 => 2.1, custom2_2 => 2.2, custom2_3 => {...}, custom2_4 => 2.4}, custom1_4 => 1.4, toString => f(){...}}",
"failed: custom toString should NOT have been used because the dump keyword should override it"
);
////////////////////////////////////////////////////////////////////////////////
var url2 = Y.substitute("[media]logo.gif", param, null, '[', ']');
////////////////////////////////////////////////////////////////////////////////
},
});
Y.Test.Runner.add(testSubstitute);
});
})();
</script>
</body>
</html>