@Test public void invalidTrailingComment() { String proto = "" + "enum Test {\n" + " FOO = 1; /\n" + "}"; try { ProtoParser.parse(location, proto); } catch (IllegalStateException e) { assertThat(e).hasMessage( "Syntax error in file.proto at 2:12: expected '//' or '/*'"); } }
@Test public void invalidSyntaxValueThrows() throws Exception { String proto = "" + "syntax = \"proto4\";\n" + "message Foo {}"; try { ProtoParser.parse(location, proto); } catch (IllegalStateException e) { assertThat(e).hasMessage("Syntax error in file.proto at 1:1: unexpected syntax: proto4"); } }
@Test public void syntaxInWrongContextThrows() { String proto = "" + "message Foo {\n" + " syntax = \"proto2\";\n" + "}"; try { ProtoParser.parse(location, proto); } catch (IllegalStateException e) { assertThat(e).hasMessage("Syntax error in file.proto at 2:3: 'syntax' in MESSAGE"); } }
@Test public void trailingMultilineCommentMustBeLastOnLineThrows() { String proto = "" + "enum Test {\n" + " FOO = 1; /* Test all the things! */ BAR = 2;\n" + "}"; try { ProtoParser.parse(location, proto); } catch (IllegalStateException e) { assertThat(e).hasMessage( "Syntax error in file.proto at 2:40: no syntax may follow trailing comment"); } }
@Test public void syntaxNotFirstDeclarationThrows() throws Exception { String proto = "" + "message Foo {}\n" + "syntax = \"proto3\";\n"; try { ProtoParser.parse(location, proto); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasMessage("Syntax error in file.proto at 2:1: " + "'syntax' element must be the first declaration in a file"); } }
@Test public void invalidHexStringEscape() throws Exception { String proto = "" + "message Foo {\n" + " optional string name = 1 " + "[default = \"\\xW\"];\n" + "}"; try { ProtoParser.parse(location, proto); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage().contains("expected a digit after \\x or \\X")); } }
@Test public void multipleSingleLineComments() { String proto = "" + "// Test all\n" + "// the things!\n" + "message Test {}"; String expected = "" + "Test all\n" + "the things!"; ProtoFileElement parsed = ProtoParser.parse(location, proto); TypeElement type = parsed.types().get(0); assertThat(type.documentation()).isEqualTo(expected); }
@Test public void singleLineJavadocComment() { String proto = "" + "/** Test */\n" + "message Test {}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); TypeElement type = parsed.types().get(0); assertThat(type.documentation()).isEqualTo("Test"); }
@Test public void singleLineComment() { String proto = "" + "// Test all the things!\n" + "message Test {}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); TypeElement type = parsed.types().get(0); assertThat(type.documentation()).isEqualTo("Test all the things!"); }
@Test public void enumValueTrailingComment() { String proto = "" + "enum Test {\n" + " FOO = 1; // Test all the things! \n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); EnumElement enumElement = (EnumElement) parsed.types().get(0); EnumConstantElement value = enumElement.constants().get(0); assertThat(value.documentation()).isEqualTo("Test all the things!"); }
@Test public void messageFieldTrailingComment() { // Trailing message field comment. String proto = "" + "message Test {\n" + " optional string name = 1; // Test all the things!\n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); MessageElement message = (MessageElement) parsed.types().get(0); FieldElement field = message.fields().get(0); assertThat(field.documentation()).isEqualTo("Test all the things!"); }
@Test public void messageFieldLeadingAndTrailingCommentAreCombined() { String proto = "" + "message Test {\n" + " // Test all...\n" + " optional string name = 1; // ...the things!\n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); MessageElement message = (MessageElement) parsed.types().get(0); FieldElement field = message.fields().get(0); assertThat(field.documentation()).isEqualTo("Test all...\n...the things!"); }
@Test public void trailingMultilineComment() { String proto = "" + "enum Test {\n" + " FOO = 1; /* Test all the\n" + "things! */ \n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); EnumElement enumElement = (EnumElement) parsed.types().get(0); EnumConstantElement value = enumElement.constants().get(0); assertThat(value.documentation()).isEqualTo("Test all the\nthings!"); }
@Test public void trailingCommentNotCombinedWhenEmpty() { String proto = "" + "enum Test {\n" + " // Test all...\n" + " FOO = 1; // \n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); EnumElement enumElement = (EnumElement) parsed.types().get(0); EnumConstantElement value = enumElement.constants().get(0); assertThat(value.documentation()).isEqualTo("Test all..."); }
@Test public void enumValueLeadingAndTrailingCommentsAreCombined() { String proto = "" + "enum Test {\n" + " // Test all...\n" + " FOO = 1; // ...the things!\n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); EnumElement enumElement = (EnumElement) parsed.types().get(0); EnumConstantElement value = enumElement.constants().get(0); assertThat(value.documentation()).isEqualTo("Test all...\n...the things!"); }
@Test public void trailingCommentNotAssignedToFollowingField() { String proto = "" + "message Test {\n" + " optional string first_name = 1; // Testing!\n" + " optional string last_name = 2;\n" + "}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); MessageElement message = (MessageElement) parsed.types().get(0); FieldElement field1 = message.fields().get(0); assertThat(field1.documentation()).isEqualTo("Testing!"); FieldElement field2 = message.fields().get(1); assertThat(field2.documentation()).isEqualTo(""); }
@Test public void publicImports() throws Exception { String proto = "import public \"src/test/resources/unittest_import.proto\";\n"; ProtoFileElement expected = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("src/test/resources/unittest_import.proto")) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void imports() throws Exception { String proto = "import \"src/test/resources/unittest_import.proto\";\n"; ProtoFileElement expected = ProtoFileElement.builder(location) .imports(ImmutableList.of( "src/test/resources/unittest_import.proto")) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void syntaxNotRequired() throws Exception { String proto = "message Foo {}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); assertThat(parsed.syntax()).isNull(); }
@Test public void syntaxSpecified() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Foo {}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Foo") .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }