|
17 | 17 | package com.google.javascript.jscomp;
|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
| 20 | +import static com.google.javascript.jscomp.ReplaceToggles.FALSE_VALUE; |
| 21 | +import static com.google.javascript.jscomp.ReplaceToggles.TRUE_VALUE; |
20 | 22 |
|
21 | 23 | import com.google.common.collect.ImmutableMap;
|
22 | 24 | import org.junit.Before;
|
@@ -77,6 +79,25 @@ public void testBootstrapOrdinals_ignoresExtraUninitializedDefinitions() {
|
77 | 79 | expectOrdinals(ImmutableMap.of("foo", 1)));
|
78 | 80 | }
|
79 | 81 |
|
| 82 | + @Test |
| 83 | + public void testBootstrapOrdinals_booleanAllowed() { |
| 84 | + check = true; |
| 85 | + testSame( |
| 86 | + srcs("var _F_toggleOrdinals = {'foo_bar': false, 'baz': true};"), |
| 87 | + expectOrdinals(ImmutableMap.of("foo_bar", FALSE_VALUE, "baz", TRUE_VALUE))); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testBootstrapOrdinals_booleanAllowsDuplicates() { |
| 92 | + check = true; |
| 93 | + testSame( |
| 94 | + srcs("var _F_toggleOrdinals = {'foo_bar': false, 'baz': false, 'qux': 0};"), |
| 95 | + expectOrdinals(ImmutableMap.of("foo_bar", FALSE_VALUE, "baz", FALSE_VALUE, "qux", 0))); |
| 96 | + testSame( |
| 97 | + srcs("var _F_toggleOrdinals = {'foo_bar': true, 'baz': true, 'qux': 0};"), |
| 98 | + expectOrdinals(ImmutableMap.of("foo_bar", TRUE_VALUE, "baz", TRUE_VALUE, "qux", 0))); |
| 99 | + } |
| 100 | + |
80 | 101 | @Test
|
81 | 102 | public void testBootstrapOrdinals_notAnObject() {
|
82 | 103 | check = true;
|
@@ -115,42 +136,38 @@ public void testBootstrapOrdinals_duplicateKey() {
|
115 | 136 | @Test
|
116 | 137 | public void testBootstrapOrdinals_badValue() {
|
117 | 138 | check = true;
|
118 |
| - test( |
119 |
| - srcs("var _F_toggleOrdinals = {x: null};"), |
120 |
| - error(ReplaceToggles.INVALID_ORDINAL_MAPPING) |
121 |
| - .withMessageContaining("value not a whole number literal")); |
122 | 139 | test(
|
123 | 140 | srcs("var _F_toggleOrdinals = {x: 'y'};"),
|
124 | 141 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
125 |
| - .withMessageContaining("value not a whole number literal")); |
| 142 | + .withMessageContaining("value not a boolean or whole number literal")); |
126 | 143 | test(
|
127 | 144 | srcs("var _F_toggleOrdinals = {x};"),
|
128 | 145 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
129 |
| - .withMessageContaining("value not a whole number literal")); |
130 |
| - test( |
131 |
| - srcs("var _F_toggleOrdinals = {x: true};"), |
132 |
| - error(ReplaceToggles.INVALID_ORDINAL_MAPPING) |
133 |
| - .withMessageContaining("value not a whole number literal")); |
| 146 | + .withMessageContaining("value not a boolean or whole number literal")); |
134 | 147 | test(
|
135 | 148 | srcs("var _F_toggleOrdinals = {x: 1 + 2};"),
|
136 | 149 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
137 |
| - .withMessageContaining("value not a whole number literal")); |
| 150 | + .withMessageContaining("value not a boolean or whole number literal")); |
138 | 151 | test(
|
139 | 152 | srcs("var _F_toggleOrdinals = {x: NaN};"),
|
140 | 153 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
141 |
| - .withMessageContaining("value not a whole number literal")); |
| 154 | + .withMessageContaining("value not a boolean or whole number literal")); |
142 | 155 | test(
|
143 | 156 | srcs("var _F_toggleOrdinals = {x: Infinity};"),
|
144 | 157 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
145 |
| - .withMessageContaining("value not a whole number literal")); |
| 158 | + .withMessageContaining("value not a boolean or whole number literal")); |
146 | 159 | test(
|
147 | 160 | srcs("var _F_toggleOrdinals = {x: -1};"),
|
148 | 161 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
149 |
| - .withMessageContaining("value not a whole number literal")); |
| 162 | + .withMessageContaining("value not a boolean or whole number literal")); |
150 | 163 | test(
|
151 | 164 | srcs("var _F_toggleOrdinals = {x: 1.2};"),
|
152 | 165 | error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
|
153 |
| - .withMessageContaining("value not a whole number literal")); |
| 166 | + .withMessageContaining("value not a boolean or whole number literal")); |
| 167 | + test( |
| 168 | + srcs("var _F_toggleOrdinals = {x: null};"), |
| 169 | + error(ReplaceToggles.INVALID_ORDINAL_MAPPING) |
| 170 | + .withMessageContaining("value not a boolean or whole number literal")); |
154 | 171 | }
|
155 | 172 |
|
156 | 173 | @Test
|
@@ -183,6 +200,16 @@ public void testSimpleToggles() {
|
183 | 200 | "const baz = !!(goog.TOGGLES_[1] >> 29 & 1);"));
|
184 | 201 | }
|
185 | 202 |
|
| 203 | + @Test |
| 204 | + public void testUnsetToggles() { |
| 205 | + toggles = ImmutableMap.of("foo", FALSE_VALUE, "bar", TRUE_VALUE); |
| 206 | + test( |
| 207 | + lines( |
| 208 | + "const foo = goog.readToggleInternalDoNotCallDirectly('foo');", |
| 209 | + "const bar = goog.readToggleInternalDoNotCallDirectly('bar');"), |
| 210 | + lines("const foo = false;", "const bar = true;")); |
| 211 | + } |
| 212 | + |
186 | 213 | @Test
|
187 | 214 | public void testCheckMode() {
|
188 | 215 | check = true;
|
|
0 commit comments