1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright 2009 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.parsing;
import static com.google.javascript.jscomp.parsing.JsDocToken.ANNOTATION;
import static com.google.javascript.jscomp.parsing.JsDocToken.BANG;
import static com.google.javascript.jscomp.parsing.JsDocToken.COLON;
import static com.google.javascript.jscomp.parsing.JsDocToken.COMMA;
import static com.google.javascript.jscomp.parsing.JsDocToken.ELLIPSIS;
import static com.google.javascript.jscomp.parsing.JsDocToken.EOC;
import static com.google.javascript.jscomp.parsing.JsDocToken.EOF;
import static com.google.javascript.jscomp.parsing.JsDocToken.EOL;
import static com.google.javascript.jscomp.parsing.JsDocToken.EQUALS;
import static com.google.javascript.jscomp.parsing.JsDocToken.GT;
import static com.google.javascript.jscomp.parsing.JsDocToken.LB;
import static com.google.javascript.jscomp.parsing.JsDocToken.LC;
import static com.google.javascript.jscomp.parsing.JsDocToken.LP;
import static com.google.javascript.jscomp.parsing.JsDocToken.LT;
import static com.google.javascript.jscomp.parsing.JsDocToken.PIPE;
import static com.google.javascript.jscomp.parsing.JsDocToken.QMARK;
import static com.google.javascript.jscomp.parsing.JsDocToken.RB;
import static com.google.javascript.jscomp.parsing.JsDocToken.RC;
import static com.google.javascript.jscomp.parsing.JsDocToken.RP;
import static com.google.javascript.jscomp.parsing.JsDocToken.STAR;
import static com.google.javascript.jscomp.parsing.JsDocToken.STRING;
import com.google.common.collect.ImmutableList;
import junit.framework.TestCase;
import java.util.List;
/**
* Tests for {@link JsDocTokenStream}.
*/
public class JsDocTokenStreamTest extends TestCase {
public void testJsDocTokenization1() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
STAR, ANNOTATION, LC, STRING, RC, EOL, STAR, ANNOTATION);
List<String> strings = ImmutableList.of("type", "string", "private");
testJSDocTokenStream(" * @type {string}\n * @private", tokens, strings);
testJSDocTokenStream(" * @type { string } \n * @private",
tokens, strings);
testJSDocTokenStream(" * @type { string}\n * @private", tokens, strings);
testJSDocTokenStream(" * @type {string }\n * @private", tokens, strings);
testJSDocTokenStream(" * @type {string}\n * @private", tokens, strings);
testJSDocTokenStream(" * @type {string} \n * @private", tokens, strings);
}
public void testJsDocTokenization2() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, LC, STRING, LT, STRING, PIPE, STRING, GT, RC);
List<String> strings = ImmutableList.of("param", "Array", "string", "null");
testJSDocTokenStream("@param {Array.<string|null>}", tokens, strings);
testJSDocTokenStream("@param {Array.<string|null>}", tokens, strings);
testJSDocTokenStream("@param {Array.<string |null>}", tokens, strings);
testJSDocTokenStream(" @param {Array.<string | null>}", tokens, strings);
testJSDocTokenStream(" @param {Array.<string|null >}", tokens, strings);
testJSDocTokenStream("@param {Array .<string|null>}", tokens, strings);
testJSDocTokenStream("@param {Array.<string|null>}", tokens, strings);
testJSDocTokenStream("@param { Array.<string|null>}", tokens, strings);
testJSDocTokenStream("@param {Array.<string| null>} ", tokens, strings);
testJSDocTokenStream("@param {Array.<string|null>}", tokens, strings);
testJSDocTokenStream(" @param { Array .< string |null > } ",
tokens, strings);
}
public void testJsDocTokenization3() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, LC, STRING, LT, STRING, PIPE, STRING, GT, RC);
List<String> strings = ImmutableList.of("param", "Array", "string", "null");
testJSDocTokenStream("@param {Array.<string||null>}", tokens, strings);
testJSDocTokenStream("@param {Array.< string || null> }", tokens, strings);
testJSDocTokenStream("@param {Array.<string || null > } ",
tokens, strings);
testJSDocTokenStream("@param {Array .<string ||null>}", tokens, strings);
testJSDocTokenStream("@param {Array.< string||null>}", tokens, strings);
testJSDocTokenStream("@param { Array.<string||null>}", tokens, strings);
testJSDocTokenStream(" @param {Array.<string||null>}", tokens, strings);
testJSDocTokenStream("@param { Array.<string|| null> }",
tokens, strings);
}
public void testJsDocTokenization4() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, LC, STRING, LT, LP, STRING, COMMA, STRING, RP, GT, RC, EOF);
List<String> strings = ImmutableList.of("param", "Array", "string", "null");
testJSDocTokenStream("@param {Array.<(string,null)>}", tokens, strings);
testJSDocTokenStream("@param {Array .<(string,null)> } ", tokens, strings);
testJSDocTokenStream(" @param {Array.< ( string,null)>}",
tokens, strings);
testJSDocTokenStream("@param {Array.<(string , null)>}", tokens, strings);
testJSDocTokenStream("@param {Array.<(string, null) > } ",
tokens, strings);
testJSDocTokenStream("@param { Array .< (string,null)>} ",
tokens, strings);
}
public void testJsDocTokenization5() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(ANNOTATION, STRING, EOC, EOF);
List<String> strings = ImmutableList.of("param", "foo.Bar");
testJSDocTokenStream("@param foo.Bar*/", tokens, strings);
testJSDocTokenStream(" @param foo.Bar*/", tokens, strings);
testJSDocTokenStream(" @param foo.Bar */", tokens, strings);
}
public void testJsDocTokenization6() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, EOL, ANNOTATION, EOL, ANNOTATION, EOC);
List<String> strings = ImmutableList.of("hidden", "static", "desc");
testJSDocTokenStream("@hidden\n@static\n@desc*/", tokens, strings);
testJSDocTokenStream("@hidden\n @static\n@desc*/", tokens, strings);
testJSDocTokenStream("@hidden\n@static\n @desc*/", tokens, strings);
testJSDocTokenStream("@hidden\n@static\n@desc */", tokens, strings);
testJSDocTokenStream(" @hidden \n@static\n @desc*/", tokens, strings);
testJSDocTokenStream("@hidden\n@static \n @desc */", tokens, strings);
testJSDocTokenStream("@hidden\n@static\n@desc*/", tokens, strings);
testJSDocTokenStream("@hidden \n@static \n @desc*/", tokens, strings);
}
public void testJsDocTokenization7() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ELLIPSIS, ELLIPSIS, ELLIPSIS, ELLIPSIS, ELLIPSIS, LT, EOC);
List<String> strings = ImmutableList.of();
testJSDocTokenStream("................<*/", tokens, strings);
testJSDocTokenStream("............... .<*/", tokens, strings);
testJSDocTokenStream("................< */", tokens, strings);
testJSDocTokenStream("............... .< */", tokens, strings);
testJSDocTokenStream("............... .< */ ", tokens, strings);
testJSDocTokenStream(" ............... .< */ ", tokens, strings);
}
public void testJsDocTokenization8() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
STAR, ANNOTATION, STRING, STRING, STRING, STRING, STRING, STRING,
STRING, EOL, EOC);
List<String> strings = ImmutableList.of(
"param", "foo.Bar", "opt_name", "this", "parameter", "is", "a", "name");
testJSDocTokenStream(
" * @param foo.Bar opt_name this parameter is a name\n" +
" */", tokens, strings);
testJSDocTokenStream(
" * @param foo.Bar opt_name this parameter is a name \n" +
" */ ", tokens, strings);
}
public void testJsDocTokenization9() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
STAR, ANNOTATION, STRING, STRING, STRING, STRING, STRING, ANNOTATION,
STRING, EOL, EOC);
List<String> strings = ImmutableList.of(
"param", "foo.Bar", "opt_name", "this", "parameter", "does",
"media", "blah");
testJSDocTokenStream(
" * @param foo.Bar opt_name this parameter does @media blah\n" +
" */", tokens, strings);
}
public void testJsDocTokenization10() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(STRING, GT, EOC);
List<String> strings = ImmutableList.of("Array<String");
testJSDocTokenStream("Array<String>*/", tokens, strings);
}
public void testJsDocTokenization11() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, LC, STRING, QMARK, RC, EOC, EOF);
List<String> strings = ImmutableList.of("param", "string");
testJSDocTokenStream("@param {string?}*/", tokens, strings);
testJSDocTokenStream(" @param {string?}*/", tokens, strings);
testJSDocTokenStream("@param { string?}*/", tokens, strings);
testJSDocTokenStream("@param {string ?}*/", tokens, strings);
testJSDocTokenStream("@param {string ? } */", tokens, strings);
testJSDocTokenStream("@param { string ? }*/", tokens, strings);
testJSDocTokenStream("@param {string? }*/", tokens, strings);
}
public void testJsDocTokenization12() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(STRING, ELLIPSIS, EOC);
List<String> strings = ImmutableList.of("function");
testJSDocTokenStream("function ...*/", tokens, strings);
}
public void testJsDocTokenization13() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(ELLIPSIS, LB, STRING, RB, EOC);
List<String> strings = ImmutableList.of("number");
testJSDocTokenStream("...[number]*/", tokens, strings);
}
public void testJsDocTokenization14() throws Exception {
// Since ES4 type parsing only requires to parse an ellipsis when it is
// followed by a comma (,) we are allowing this case to parse this way.
// This is a simplification of the tokenizer, but the extra complexity is
// never used.
List<JsDocToken> tokens = ImmutableList.of(STRING, LB, STRING, EOC);
List<String> strings = ImmutableList.of("foo", "bar...");
testJSDocTokenStream("foo[ bar...*/", tokens, strings);
}
public void testJsDocTokenization15() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
STRING, LB, STRING, COMMA, ELLIPSIS, EOC);
List<String> strings = ImmutableList.of("foo", "bar");
testJSDocTokenStream("foo[ bar,...*/", tokens, strings);
testJSDocTokenStream("foo[ bar ,...*/", tokens, strings);
testJSDocTokenStream("foo[bar, ...*/", tokens, strings);
testJSDocTokenStream("foo[ bar , ... */", tokens, strings);
testJSDocTokenStream("foo [bar,... */", tokens, strings);
}
public void testJsDocTokenization16() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
STRING, COLON, COLON, COLON, ELLIPSIS, STRING, COLON, STRING, EOC);
List<String> strings = ImmutableList.of("foo", "bar", "bar2");
testJSDocTokenStream("foo:::...bar:bar2*/", tokens, strings);
}
public void testJsDocTokenization17() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(STRING, EOL, EOC);
List<String> strings = ImmutableList.of("..");
testJSDocTokenStream("..\n*/", tokens, strings);
}
public void testJsDocTokenization18() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(STRING, EOL, EOC);
List<String> strings = ImmutableList.of(".");
testJSDocTokenStream(".\n*/", tokens, strings);
}
public void testJsDocTokenization19() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(ANNOTATION, LC, STAR, RC, EOC);
List<String> strings = ImmutableList.of("type", "*");
testJSDocTokenStream("@type {*}*/", tokens, strings);
}
public void testJsDocTokenization20() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, LC, BANG, STRING, RC, EOC, EOF);
List<String> strings = ImmutableList.of("param", "Object");
testJSDocTokenStream("@param {!Object}*/", tokens, strings);
testJSDocTokenStream(" @param {!Object}*/", tokens, strings);
testJSDocTokenStream("@param {! Object}*/", tokens, strings);
testJSDocTokenStream("@param { !Object}*/", tokens, strings);
testJSDocTokenStream("@param {!Object } */", tokens, strings);
testJSDocTokenStream("@param { ! Object }*/", tokens, strings);
testJSDocTokenStream("@param {!Object }*/", tokens, strings);
}
public void testJsDocTokenization21() throws Exception {
List<JsDocToken> tokens = ImmutableList.of(
ANNOTATION, LC, STRING, EQUALS, RC, EOC, EOF);
List<String> strings = ImmutableList.of("param", "Object");
testJSDocTokenStream("@param {Object=}*/", tokens, strings);
testJSDocTokenStream(" @param {Object=}*/", tokens, strings);
testJSDocTokenStream("@param { Object =}*/", tokens, strings);
testJSDocTokenStream("@param { Object=}*/", tokens, strings);
testJSDocTokenStream("@param {Object= } */", tokens, strings);
testJSDocTokenStream("@param { Object = }*/", tokens, strings);
testJSDocTokenStream("@param {Object= }*/", tokens, strings);
}
private void testJSDocTokenStream(String comment, List<JsDocToken> tokens,
List<String> strings) {
JsDocTokenStream stream = new JsDocTokenStream(comment, 0);
int stringsIndex = 0;
for (JsDocToken token : tokens) {
JsDocToken readToken = stream.getJsDocToken();
// token equality
if (token != readToken) {
assertEquals(token, readToken);
}
// string equality
if (token == ANNOTATION || token == STRING) {
assertEquals(strings.get(stringsIndex++), stream.getString());
}
}
}
}
|