Skip to content

Commit

Permalink
Trailing comments support on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Apr 15, 2024
1 parent 459b722 commit 0d53db4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,7 @@ class ProtoParser internal constructor(
companion object {
/** Parse a named `.proto` schema. */
fun parse(location: Location, data: String): ProtoFileElement {
// TODO Migrate to data.toCharArray() once stable in common code.
val chars = CharArray(data.length, data::get)
val chars = data.toCharArray()
return ProtoParser(location, chars).readProtoFile()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,13 @@ class ProtoParserTest {
}

@Test
fun messageFieldTrailingComment() {
fun messageFieldTrailingCommentWithCarriageReturn() {
// Trailing message field comment.
val proto = """
|message Test {
| optional string name = 1; // Test all the things!
|}
""".trimMargin()
""".trimMargin().replace("\n", "\r\n")
val parsed = ProtoParser.parse(location, proto)
val message = parsed.types[0] as MessageElement
val field = message.fields[0]
Expand Down Expand Up @@ -3066,6 +3066,51 @@ class ProtoParserTest {
assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected)
}

@Test fun parsingTrailingComments() {
val proto = """
|enum ImageState {
| IMAGE_STATE_UNSPECIFIED = 0;
| IMAGE_STATE_READONLY = 1; /* unlocked */
| IMAGE_STATE_MUSTLOCK = 2; /* must be locked */
|}
""".trimMargin()
val expected = ProtoFileElement(
location = location,
types = listOf(
EnumElement(
location = location.at(1, 1),
name = "ImageState",
documentation = "",
options = listOf(),
constants = listOf(
EnumConstantElement(
location = location.at(2, 5),
name = "IMAGE_STATE_UNSPECIFIED",
tag = 0,
documentation = "",
options = listOf(),
),
EnumConstantElement(
location = location.at(3, 5),
name = "IMAGE_STATE_READONLY",
tag = 1,
documentation = "unlocked",
options = listOf(),
),
EnumConstantElement(
location = location.at(4, 5),
name = "IMAGE_STATE_MUSTLOCK",
tag = 2,
documentation = "must be locked",
options = listOf(),
),
),
),
),
)
assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected)
}

@Test fun forbidMultipleSyntaxDefinitions() {
val proto = """
| syntax = "proto2";
Expand Down

0 comments on commit 0d53db4

Please sign in to comment.