4
<title>YUI substitute Tests</title>
7
<body class="yui3-skin-sam">
8
<h1>substitute Tests</h1>
9
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
11
<script type="text/javascript" src="../../../build/yui/yui.js"></script>
12
<script type="text/javascript">
16
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
18
useBrowserConsole: false
19
}).use("dump", "test", "console", "event-custom", function(Y) {
21
var button = Y.one('#btnRun'),
23
ObjectAssert = Y.ObjectAssert;
26
button.set("disabled", false);
27
Y.on("click", function() {
31
var myConsole = new Y.Console().render();
33
var testSubstitute= new Y.Test.Case({
34
name: "Substitute tests",
37
test_substitute: function() {
39
////////////////////////////////////////////////////////////////////////////////
41
var param = {domain: 'valvion.com',
42
media: 'http://media.{domain}/', /* nested */
43
contextdomain: { context1: 'context{domain}', context2: 'yahoo.com' }, /* the value is an object, we will use a custom function to extract the correct data */
44
contextmedia: 'http://contextmedia.{contextdomain context1}/'};
47
// standard replace, nested
48
url = Y.substitute("{media}logo.gif", param, null, true);
49
Assert.areEqual(url, "http://media.valvion.com/logo.gif");
52
// If the replacement value is an object, use the meta info as a key to extract the
53
// correct data. Otherwise just return the value.
54
function multipleChoice(key, val, meta) {
55
return (Y.Lang.isObject(val)) ? val[meta] : val;
58
// "random data" is not used since the value for the contextmedia key is a string.
59
// contextdomain uses "context1" as a key to expand the string correctly
60
url = Y.substitute("{contextmedia random data}logo.gif", param, multipleChoice, true);
61
Assert.areEqual(url, "http://contextmedia.contextvalvion.com/logo.gif");
63
////////////////////////////////////////////////////////////////////////////////
84
Y.substitute("{testobj 0}", { testobj: obj }, null, true),
85
"{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {...}, level1_4 => 1.4}",
86
"failed one level object dump"
90
Y.substitute("{testobj 1}", { testobj: obj }, null, true),
91
"{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}",
92
"failed two level object dump"
97
"{testobj 10}", { testobj: obj }, null, true),
98
"{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}",
99
"failed deep object dump"
120
Y.substitute("{testarr 1}", { testarr: arr }, null, true),
121
"[1.1, 1.2, [2.1, 2.2, {...}, 2.4], 1.4]",
122
"failed two level array dump"
143
var result = Y.substitute("{testmix 1}", { testmix: mix }, null, true);
146
(result.indexOf("GMT" > -1)) ? true : false,
147
"failed two level mixed object with a date (date should have produced an output with GMT in it)"
151
////////////////////////////////////////////////////////////////////////////////
168
toString: function() {
169
return "custom toString executed";
174
Y.substitute("{customtostr 1}", { customtostr: tostr }, null, true),
175
"custom toString executed",
176
"failed: custom toString should have been used"
180
// Y.substitute("{customtostr dump 1}", { customtostr: tostr }, null, true),
181
// "{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(){...}}",
182
// "failed: custom toString should NOT have been used because the dump keyword should override it"
185
////////////////////////////////////////////////////////////////////////////////
187
var template = 'My {prefix} name is {name}. {signature}';
191
signature: '{I am a replacement string }{}'
194
// testing {} in the replacement text
195
Assert.areEqual("My last name is Zappa. {I am a replacement string }{}", Y.substitute(template, obj));
199
Y.substitute('-{LBRACE}test{RBRACE}-', {test:'test'}, null, true),
200
'replacement of literals {LBRACE} and/or {RBRACE} failed'
205
Y.substitute('{{{three}}}', {
217
Y.Test.Runner.setName("Substitute");
218
Y.Test.Runner.add(testSubstitute);