Skip to content

Commit d8268b7

Browse files
lauraharkercopybara-github
authored andcommitted
Simplify some boilerplate in TypedAstIntegrationTest
PiperOrigin-RevId: 621266247
1 parent 07080fa commit d8268b7

File tree

1 file changed

+43
-99
lines changed

1 file changed

+43
-99
lines changed

test/com/google/javascript/jscomp/integration/TypedAstIntegrationTest.java

+43-99
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ public void simpleAlertCall() throws IOException {
8383

8484
Compiler compiler = compileTypedAstShards(options);
8585

86-
Node expectedRoot = parseExpectedCode("alert(10);");
87-
assertNode(compiler.getRoot().getSecondChild())
88-
.usingSerializer(compiler::toSource)
89-
.isEqualTo(expectedRoot);
86+
assertCompiledCodeEquals(compiler, "alert(10);");
9087
}
9188

9289
@Test
@@ -104,10 +101,7 @@ public void alertCallWithCrossLibraryVarReference() throws IOException {
104101

105102
Compiler compiler = compileTypedAstShards(options);
106103

107-
Node expectedRoot = parseExpectedCode("", "alert(10);");
108-
assertNode(compiler.getRoot().getSecondChild())
109-
.usingSerializer(compiler::toSource)
110-
.isEqualTo(expectedRoot);
104+
assertCompiledCodeEquals(compiler, "", "alert(10);");
111105
}
112106

113107
@Test
@@ -125,12 +119,8 @@ public void disambiguatesGoogScopeAcrossLibraries() throws IOException {
125119
options.setClosurePass(true);
126120

127121
Compiler compiler = compileTypedAstShards(options);
128-
Node expectedRoot =
129-
parseExpectedCode(
130-
"var $jscomp$scope$1954846972$0$x=3;", "var $jscomp$scope$1954846973$0$x=4");
131-
assertNode(compiler.getRoot().getSecondChild())
132-
.usingSerializer(compiler::toSource)
133-
.isEqualTo(expectedRoot);
122+
assertCompiledCodeEquals(
123+
compiler, "var $jscomp$scope$1954846972$0$x=3;", "var $jscomp$scope$1954846973$0$x=4");
134124
}
135125

136126
@Test
@@ -152,10 +142,7 @@ public void disambiguatesAndDeletesMethodsAcrossLibraries() throws IOException {
152142

153143
Compiler compiler = compileTypedAstShards(options);
154144

155-
Node expectedRoot = parseExpectedCode("", "", "alert('lib1'); alert('lib2')");
156-
assertNode(compiler.getRoot().getSecondChild())
157-
.usingSerializer(compiler::toSource)
158-
.isEqualTo(expectedRoot);
145+
assertCompiledCodeEquals(compiler, "", "", "alert('lib1'); alert('lib2')");
159146
}
160147

161148
@Test
@@ -178,10 +165,7 @@ public void disambiguatesAndDeletesMethodsAcrossLibraries_withTranspilation() th
178165

179166
Compiler compiler = compileTypedAstShards(options);
180167

181-
Node expectedRoot = parseExpectedCode("", "", "alert('lib1'); alert('lib2')");
182-
assertNode(compiler.getRoot().getSecondChild())
183-
.usingSerializer(compiler::toSource)
184-
.isEqualTo(expectedRoot);
168+
assertCompiledCodeEquals(compiler, "", "", "alert('lib1'); alert('lib2')");
185169
}
186170

187171
@Test
@@ -250,10 +234,7 @@ public void disambiguatesAndDeletesMethodsAcrossLibraries_ignoresInvalidationsIn
250234

251235
Compiler compiler = compileTypedAstShards(options);
252236

253-
Node expectedRoot = parseExpectedCode("", "", "alert('lib1'); alert('lib2')");
254-
assertNode(compiler.getRoot().getSecondChild())
255-
.usingSerializer(compiler::toSource)
256-
.isEqualTo(expectedRoot);
237+
assertCompiledCodeEquals(compiler, "", "", "alert('lib1'); alert('lib2')");
257238
}
258239

259240
@Test
@@ -280,17 +261,14 @@ public void exportAnnotationOnPropertyPreventsRenaming() throws IOException {
280261

281262
Compiler compiler = compileTypedAstShards(options);
282263

283-
Node expectedRoot =
284-
parseExpectedCode(
285-
lines(
286-
"class a {", //
287-
"constructor() { this.foo = 1; }",
288-
"}",
289-
"alert(new a());"),
290-
"");
291-
assertNode(compiler.getRoot().getSecondChild())
292-
.usingSerializer(compiler::toSource)
293-
.isEqualTo(expectedRoot);
264+
assertCompiledCodeEquals(
265+
compiler,
266+
lines(
267+
"class a {", //
268+
"constructor() { this.foo = 1; }",
269+
"}",
270+
"alert(new a());"),
271+
"");
294272
}
295273

296274
@Test
@@ -325,14 +303,11 @@ public void exportAnnotationOnProperty_ignoredInUnusedTypedAstShard() throws IOE
325303

326304
Compiler compiler = compileTypedAstShards(options);
327305

328-
Node expectedRoot =
329-
parseExpectedCode(
330-
lines(
331-
"class a {}", //
332-
"alert(new a());"));
333-
assertNode(compiler.getRoot().getSecondChild())
334-
.usingSerializer(compiler::toSource)
335-
.isEqualTo(expectedRoot);
306+
assertCompiledCodeEquals(
307+
compiler,
308+
lines(
309+
"class a {}", //
310+
"alert(new a());"));
336311
}
337312

338313
@Test
@@ -364,10 +339,7 @@ public void lateFulfilledGlobalVariableIsRenamed() throws IOException {
364339
"}"),
365340
"var $lib2Var$$ = 10; $lib1$$();"
366341
};
367-
Node expectedRoot = parseExpectedCode(expected);
368-
assertNode(compiler.getRoot().getSecondChild())
369-
.usingSerializer(compiler::toSource)
370-
.isEqualTo(expectedRoot);
342+
assertCompiledCodeEquals(compiler, expected);
371343
}
372344

373345
@Test
@@ -394,11 +366,7 @@ public void lateFulfilledNameReferencedInExternsAndCode_notRenamed() throws IOEx
394366

395367
Compiler compiler = compileTypedAstShards(options);
396368

397-
Node expectedRoot =
398-
parseExpectedCode("lateDefinedVar;", "var lateDefinedVar; var $normalVar$$;");
399-
assertNode(compiler.getRoot().getSecondChild())
400-
.usingSerializer(compiler::toSource)
401-
.isEqualTo(expectedRoot);
369+
assertCompiledCodeEquals(compiler, "lateDefinedVar;", "var lateDefinedVar; var $normalVar$$;");
402370
}
403371

404372
@Test
@@ -421,10 +389,7 @@ public void externJSDocPropertiesNotRenamed() throws IOException {
421389

422390
Compiler compiler = compileTypedAstShards(options);
423391

424-
Node expectedRoot = parseExpectedCode("takeCoord({x: 1, y: 2});");
425-
assertNode(compiler.getRoot().getSecondChild())
426-
.usingSerializer(compiler::toSource)
427-
.isEqualTo(expectedRoot);
392+
assertCompiledCodeEquals(compiler, "takeCoord({x: 1, y: 2});");
428393
}
429394

430395
@Test
@@ -447,10 +412,7 @@ public void gatherExternProperties() throws IOException {
447412

448413
Compiler compiler = compileTypedAstShards(options);
449414

450-
Node expectedRoot = parseExpectedCode("ns.a = 2; console.log(ns.x);console.log(ns.a);");
451-
assertNode(compiler.getRoot().getSecondChild())
452-
.usingSerializer(compiler::toSource)
453-
.isEqualTo(expectedRoot);
415+
assertCompiledCodeEquals(compiler, "ns.a = 2; console.log(ns.x);console.log(ns.a);");
454416
}
455417

456418
@Test
@@ -480,10 +442,7 @@ public void protectsHiddenSideEffects() throws IOException {
480442

481443
Compiler compiler = compileTypedAstShards(options);
482444

483-
Node expectedRoot = parseExpectedCode("foo.bar");
484-
assertNode(compiler.getRoot().getSecondChild())
485-
.usingSerializer(compiler::toSource)
486-
.isEqualTo(expectedRoot);
445+
assertCompiledCodeEquals(compiler, "foo.bar");
487446
}
488447

489448
@Test
@@ -506,10 +465,7 @@ public void protectsHiddenSideEffects_ignoringProtectSideEffectOption() throws I
506465

507466
Compiler compiler = compileTypedAstShards(options);
508467

509-
Node expectedRoot = parseExpectedCode("foo.bar");
510-
assertNode(compiler.getRoot().getSecondChild())
511-
.usingSerializer(compiler::toSource)
512-
.isEqualTo(expectedRoot);
468+
assertCompiledCodeEquals(compiler, "foo.bar");
513469
}
514470

515471
@Test
@@ -523,10 +479,7 @@ public void removesRegExpCallsIfSafe() throws IOException {
523479

524480
Compiler compiler = compileTypedAstShards(options);
525481

526-
Node expectedRoot = parseExpectedCode("");
527-
assertNode(compiler.getRoot().getSecondChild())
528-
.usingSerializer(compiler::toSource)
529-
.isEqualTo(expectedRoot);
482+
assertCompiledCodeEquals(compiler, "");
530483
}
531484

532485
@Test
@@ -543,10 +496,7 @@ public void removesRegExpCallsIfUnsafelyReferenced() throws IOException {
543496

544497
Compiler compiler = compileTypedAstShards(options);
545498

546-
Node expectedRoot = parseExpectedCode("(/abc/gi).exec(''); console.log(RegExp.$1);");
547-
assertNode(compiler.getRoot().getSecondChild())
548-
.usingSerializer(compiler::toSource)
549-
.isEqualTo(expectedRoot);
499+
assertCompiledCodeEquals(compiler, "(/abc/gi).exec(''); console.log(RegExp.$1);");
550500
}
551501

552502
@Test
@@ -570,10 +520,7 @@ public void runsJ2clOptimizations() throws IOException {
570520

571521
Compiler compiler = compileTypedAstShards(options);
572522

573-
Node expectedRoot = parseExpectedCode("");
574-
assertNode(compiler.getRoot().getSecondChild())
575-
.usingSerializer(compiler::toSource)
576-
.isEqualTo(expectedRoot);
523+
assertCompiledCodeEquals(compiler, "");
577524
}
578525

579526
@Test
@@ -590,15 +537,12 @@ public void testAngularPass() throws IOException {
590537

591538
Compiler compiler = compileTypedAstShards(options);
592539

593-
Node expectedRoot =
594-
parseExpectedCode(
595-
lines(
596-
"function f() {} ",
597-
"function g(a) {} g['$inject']=['a'];",
598-
"var b = function f(a, b, c) {}; b['$inject']=['a', 'b', 'c']"));
599-
assertNode(compiler.getRoot().getSecondChild())
600-
.usingSerializer(compiler::toSource)
601-
.isEqualTo(expectedRoot);
540+
assertCompiledCodeEquals(
541+
compiler,
542+
lines(
543+
"function f() {} ",
544+
"function g(a) {} g['$inject']=['a'];",
545+
"var b = function f(a, b, c) {}; b['$inject']=['a', 'b', 'c']"));
602546
}
603547

604548
@Test
@@ -646,10 +590,7 @@ public void testCrossChunkMethodMotion() throws IOException {
646590
+ "Foo.prototype.bar=JSCompiler_stubMethod(0); var x=new Foo;",
647591
"Foo.prototype.bar=JSCompiler_unstubMethod(0,function(){}); x.bar()",
648592
};
649-
Node expectedRoot = parseExpectedCode(expected);
650-
assertNode(compiler.getRoot().getSecondChild())
651-
.usingSerializer(compiler::toSource)
652-
.isEqualTo(expectedRoot);
593+
assertCompiledCodeEquals(compiler, expected);
653594
}
654595

655596
@Test
@@ -776,9 +717,12 @@ private static InputStream toInputStream(ArrayList<Path> typedAsts) throws IOExc
776717
return new SequenceInputStream(Collections.enumeration(inputShards));
777718
}
778719

779-
private Node parseExpectedCode(String... files) {
780-
// pass empty CompilerOptions; CompilerOptions only matters for CommonJS module parsing
781-
CompilerOptions options = new CompilerOptions();
782-
return super.parseExpectedCode(files, options);
720+
private void assertCompiledCodeEquals(Compiler compiler, String... expected) {
721+
// Passing the default CompilerOptions to parse the expected code is OK, since it configures the
722+
// parser to support all input languages.
723+
Node expectedRoot = super.parseExpectedCode(expected, new CompilerOptions());
724+
assertNode(compiler.getRoot().getSecondChild())
725+
.usingSerializer(compiler::toSource)
726+
.isEqualTo(expectedRoot);
783727
}
784728
}

0 commit comments

Comments
 (0)