utatane.tea@gmail.com | d68d248 | 2015-05-14 16:07:54 +0000 | [diff] [blame] | 1 | function shouldBe(actual, expected) { |
| 2 | if (actual !== expected) |
| 3 | throw new Error('bad value: ' + JSON.stringify(actual)); |
| 4 | } |
| 5 | |
| 6 | var templates = []; |
| 7 | function tag(siteObject) { |
| 8 | templates.push(siteObject); |
| 9 | } |
| 10 | |
| 11 | tag`Hello`; |
| 12 | tag`World`; |
| 13 | tag`Hello`; |
| 14 | shouldBe(templates.length, 3); |
| 15 | shouldBe(templates[0] !== templates[1], true); |
caitp@igalia.com | a8b896c | 2018-02-13 18:10:30 +0000 | [diff] [blame] | 16 | shouldBe(templates[0] !== templates[2], true); |
utatane.tea@gmail.com | d68d248 | 2015-05-14 16:07:54 +0000 | [diff] [blame] | 17 | |
| 18 | templates = []; |
| 19 | tag`Hello\n`; |
| 20 | tag`Hello\r`; |
| 21 | tag`Hello\u2028`; |
| 22 | shouldBe(templates.length, 3); |
| 23 | shouldBe(templates[0] !== templates[1], true); |
| 24 | shouldBe(templates[0] !== templates[2], true); |
| 25 | |
| 26 | templates = []; |
| 27 | eval("tag`Hello\n`"); |
| 28 | eval("tag`Hello\r`"); |
| 29 | eval("tag`Hello\u2028`"); |
| 30 | shouldBe(templates.length, 3); |
caitp@igalia.com | a8b896c | 2018-02-13 18:10:30 +0000 | [diff] [blame] | 31 | shouldBe(templates[0] !== templates[1], true); |
utatane.tea@gmail.com | d68d248 | 2015-05-14 16:07:54 +0000 | [diff] [blame] | 32 | shouldBe(templates[0] !== templates[2], true); |
| 33 | |
| 34 | templates = []; |
| 35 | eval("tag`Hello\n`"); |
| 36 | eval("tag`Hello\\n`"); |
| 37 | eval("tag`Hello\r`"); |
| 38 | eval("tag`Hello\\r`"); |
| 39 | shouldBe(templates.length, 4); |
| 40 | shouldBe(templates[0] !== templates[1], true); |
caitp@igalia.com | a8b896c | 2018-02-13 18:10:30 +0000 | [diff] [blame] | 41 | shouldBe(templates[0] !== templates[2], true); |
utatane.tea@gmail.com | d68d248 | 2015-05-14 16:07:54 +0000 | [diff] [blame] | 42 | shouldBe(templates[0] !== templates[3], true); |
| 43 | shouldBe(templates[1] !== templates[2], true); |
| 44 | shouldBe(templates[1] !== templates[3], true); |
| 45 | shouldBe(templates[2] !== templates[3], true); |
| 46 | |
| 47 | var v = 0; |
| 48 | templates = []; |
| 49 | eval("tag`Hello\n${v}world`"); |
| 50 | eval("tag`Hello\n${v}world`"); |
| 51 | shouldBe(templates.length, 2); |
caitp@igalia.com | a8b896c | 2018-02-13 18:10:30 +0000 | [diff] [blame] | 52 | shouldBe(templates[0] !== templates[1], true); |
utatane.tea@gmail.com | d68d248 | 2015-05-14 16:07:54 +0000 | [diff] [blame] | 53 | |
| 54 | var v = 0; |
| 55 | templates = []; |
| 56 | eval("tag`Hello${v}\nworld`"); |
| 57 | eval("tag`Hello\n${v}world`"); |
| 58 | shouldBe(templates.length, 2); |
| 59 | shouldBe(templates[0] !== templates[1], true); |
caitp@igalia.com | a8b896c | 2018-02-13 18:10:30 +0000 | [diff] [blame] | 60 | |
| 61 | var v = 0; |
| 62 | templates = []; |
| 63 | for (v = 0; v < 3; ++v) { |
| 64 | tag`Hello${v}world`; |
| 65 | if (!v) continue; |
| 66 | shouldBe(templates[v] === templates[v - 1], true); |
| 67 | } |
| 68 | |
| 69 | templates = []; |
| 70 | tag`Hello${1}world`; |
| 71 | tag`Hello${2}world`; |
| 72 | shouldBe(templates[0] !== templates[1], true); |
| 73 | |
| 74 | // Disable eval caching if a tagged template occurs in eval code |
| 75 | var v = 0; |
| 76 | templates = []; |
| 77 | for (v = 0; v < 3; ++v) { |
| 78 | eval("tag`Hello${v}world`"); |
| 79 | if (!v) continue; |
| 80 | shouldBe(templates[v] !== templates[v - 1], true); |
| 81 | } |
| 82 | |
| 83 | templates = []; |
| 84 | eval("tag`Hello${1}world`"); |
| 85 | eval("tag`Hello${2}world`"); |
| 86 | eval("tag`Hello${3}world`"); |
| 87 | shouldBe(templates[0] !== templates[1], true); |
| 88 | shouldBe(templates[1] !== templates[2], true); |
| 89 | |
| 90 | |
| 91 | // Disable eval caching if a tagged template occurs in a nested function |
| 92 | var v = 0; |
| 93 | templates = []; |
| 94 | for (v = 0; v < 6; v += 2) { |
| 95 | eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${v}world` })()"); |
| 96 | shouldBe(templates[v] === templates[v + 1], true); |
| 97 | } |
| 98 | |
| 99 | shouldBe(templates[0] !== templates[2], true); |
| 100 | shouldBe(templates[0] !== templates[4], true); |
| 101 | shouldBe(templates[1] !== templates[3], true); |
| 102 | shouldBe(templates[1] !== templates[5], true); |
| 103 | shouldBe(templates[2] !== templates[4], true); |
| 104 | shouldBe(templates[3] !== templates[5], true); |
| 105 | |
| 106 | templates = []; |
| 107 | eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${1}world` })()"); |
| 108 | eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })()"); |
| 109 | eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })()"); |
| 110 | shouldBe(templates[0] === templates[1], true); |
| 111 | shouldBe(templates[0] !== templates[2], true); |
| 112 | shouldBe(templates[0] !== templates[4], true); |
| 113 | shouldBe(templates[1] !== templates[3], true); |
| 114 | shouldBe(templates[1] !== templates[5], true); |
| 115 | shouldBe(templates[2] === templates[3], true); |
| 116 | shouldBe(templates[2] !== templates[4], true); |
| 117 | shouldBe(templates[3] !== templates[5], true); |
| 118 | shouldBe(templates[4] === templates[5], true); |
| 119 | |
| 120 | // Disable eval caching if a tagged template occurs in an even deeper nested function |
| 121 | var v = 0; |
| 122 | templates = []; |
| 123 | for (v = 0; v < 3; ++v) { |
| 124 | eval("(function() { (function() { tag`Hello${v}world` })() })()"); |
| 125 | if (!v) continue; |
| 126 | shouldBe(templates[v] !== templates[v - 1], true); |
| 127 | } |
| 128 | |
| 129 | templates = []; |
| 130 | eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${1}world` })() })()"); |
| 131 | eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })() })()"); |
| 132 | eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })() })()"); |
| 133 | shouldBe(templates[0] === templates[1], true); |
| 134 | shouldBe(templates[0] !== templates[2], true); |
| 135 | shouldBe(templates[0] !== templates[4], true); |
| 136 | shouldBe(templates[1] !== templates[3], true); |
| 137 | shouldBe(templates[1] !== templates[5], true); |
| 138 | shouldBe(templates[2] === templates[3], true); |
| 139 | shouldBe(templates[2] !== templates[4], true); |
| 140 | shouldBe(templates[3] !== templates[5], true); |
| 141 | shouldBe(templates[4] === templates[5], true); |