-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add special case support for single line generated java files to the …
…package statement parser. Since the parser is also used for generated source files in some cases, we can't assume that the input is a "normal" source file with a package statement on a line by itself. Generated source files sometimes have the entire content on a single line, so add support for that. We do it this way to avoid using a full java parser which add extra complexity (though maybe not too much?). PiperOrigin-RevId: 585650766
- Loading branch information
1 parent
d3c0a3f
commit 2046e85
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
querysync/javatests/com/google/idea/blaze/qsync/PackageStatementParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2023 The Bazel Authors. All rights reserved. | ||
* | ||
* 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.idea.blaze.qsync; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.base.Joiner; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class PackageStatementParserTest { | ||
|
||
@Test | ||
public void basic_package_statement() throws IOException { | ||
PackageStatementParser psp = new PackageStatementParser(); | ||
assertThat( | ||
psp.readPackage( | ||
new ByteArrayInputStream( | ||
Joiner.on("\n") | ||
.join("package com.myorg.somepackage;", "", "public class MyClass {}") | ||
.getBytes(StandardCharsets.UTF_8)))) | ||
.isEqualTo("com.myorg.somepackage"); | ||
} | ||
|
||
@Test | ||
public void comment_before_package_statement() throws IOException { | ||
PackageStatementParser psp = new PackageStatementParser(); | ||
assertThat( | ||
psp.readPackage( | ||
new ByteArrayInputStream( | ||
Joiner.on("\n") | ||
.join( | ||
"/**", | ||
" * Copyright statement!", | ||
" * Another line", | ||
" */", | ||
"package com.myorg.otherpackage;", | ||
"", | ||
"public class MyClass {}") | ||
.getBytes(StandardCharsets.UTF_8)))) | ||
.isEqualTo("com.myorg.otherpackage"); | ||
} | ||
|
||
@Test | ||
public void single_line_generated_file() throws IOException { | ||
PackageStatementParser psp = new PackageStatementParser(); | ||
assertThat( | ||
psp.readPackage( | ||
new ByteArrayInputStream( | ||
("/* This is a generated file */package com.myorg.package.generated;public" | ||
+ " final class SomeClass {public final static boolean GENERATED_THING =" | ||
+ " true;}") | ||
.getBytes(StandardCharsets.UTF_8)))) | ||
.isEqualTo("com.myorg.package.generated"); | ||
} | ||
|
||
@Test | ||
public void basic_kotlin() throws IOException { | ||
PackageStatementParser psp = new PackageStatementParser(); | ||
assertThat( | ||
psp.readPackage( | ||
new ByteArrayInputStream( | ||
Joiner.on("\n") | ||
.join( | ||
"package com.myorg.kotlinpackage", | ||
"", | ||
"import kotlin.text.*", | ||
"", | ||
"fun main() {", | ||
" println(\"Hello world!\")", | ||
"}") | ||
.getBytes(StandardCharsets.UTF_8)))) | ||
.isEqualTo("com.myorg.kotlinpackage"); | ||
} | ||
|
||
@Test | ||
public void kotlin_package_annotation() throws IOException { | ||
PackageStatementParser psp = new PackageStatementParser(); | ||
assertThat( | ||
psp.readPackage( | ||
new ByteArrayInputStream( | ||
Joiner.on("\n") | ||
.join( | ||
"@file:JvmName(\"MyFile\")", | ||
"package com.myorg.kotlinpackage", | ||
"", | ||
"object MyObject {", | ||
"}") | ||
.getBytes(StandardCharsets.UTF_8)))) | ||
.isEqualTo("com.myorg.kotlinpackage"); | ||
} | ||
|
||
@Test | ||
public void kotlin_shebang() throws IOException { | ||
PackageStatementParser psp = new PackageStatementParser(); | ||
assertThat( | ||
psp.readPackage( | ||
new ByteArrayInputStream( | ||
Joiner.on("\n") | ||
.join( | ||
"#!/bin/interpreter", | ||
"package com.myorg.kotlinpackage", | ||
"", | ||
"object MyObject {", | ||
"}") | ||
.getBytes(StandardCharsets.UTF_8)))) | ||
.isEqualTo("com.myorg.kotlinpackage"); | ||
} | ||
} |