MessageElement toElement() { return MessageElement.builder(location) .name(type.simpleName()) .nestedTypes(Type.toElements(nestedTypes)) .build(); } }
@Test public void emptyToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); String expected = "message Message {}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@Test public void addMultiplePublicDependencies() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("example.other", "example.another")) .types(ImmutableList.of(element)) .build(); assertThat(file.publicImports()).hasSize(2); }
@Test public void addMultipleDependencies() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.other", "example.another")) .types(ImmutableList.of(element)) .build(); assertThat(file.imports()).hasSize(2); }
@Test public void simpleToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void simpleWithImportsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.other")) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "import \"example.other\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void simpleWithPublicImportsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("example.other")) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "import public \"example.other\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void simpleWithBothImportsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.thing")) .publicImports(ImmutableList.of("example.other")) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "import \"example.thing\";\n" + "import public \"example.other\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void addMultipleOptions() { TypeElement element = MessageElement.builder(location).name("Message").build(); OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); ProtoFileElement file = ProtoFileElement.builder(location) .options(ImmutableList.of(kitKat, fooBar)) .types(ImmutableList.of(element)) .build(); assertThat(file.options()).hasSize(2); }
@Test public void reserved() { String proto = "" + "message Foo {\n" + " reserved 10, 12 to 14, 'foo';" + "}"; TypeElement message = MessageElement.builder(location.at(1, 1)) .name("Foo") .reserveds(ImmutableList.of(ReservedElement.create(location.at(2, 3), "", ImmutableList.<Object>of(10, Range.closed(12, 14), "foo")))) .build(); ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.of(message)) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void simpleWithOptionsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); OptionElement option = OptionElement.create("kit", Kind.STRING, "kat"); ProtoFileElement file = ProtoFileElement.builder(location) .options(ImmutableList.of(option)) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "option kit = \"kat\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void addMultipleTypes() { TypeElement nested1 = MessageElement.builder(location).name("Nested1").build(); TypeElement nested2 = MessageElement.builder(location).name("Nested2").build(); TypeElement element = MessageElement.builder(location) .name("Message") .fields(ImmutableList.of(FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .nestedTypes(ImmutableList.of(nested1, nested2)) .build(); assertThat(element.nestedTypes()).hasSize(2); }
@Test public void syntaxToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .syntax(PROTO_2) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "syntax = \"proto2\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); } }
@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); }
@Test public void simpleWithServicesToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ServiceElement service = ServiceElement.builder(location).name("Service").build(); ProtoFileElement file = ProtoFileElement.builder(location) .types(ImmutableList.of(element)) .services(ImmutableList.of(service)) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n" + "\n" + "service Service {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void syntaxMayFollowCommentsAndEmptyLines() throws Exception { String proto = "" + "/* comment 1 */\n" + "// comment 2\n" + "\n" + "syntax = \"proto3\";\n" + "message Foo {}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(5, 1)) .name("Foo") .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void addMultipleExtensions() { ExtensionsElement fives = ExtensionsElement.create(location, 500, 501, ""); ExtensionsElement sixes = ExtensionsElement.create(location, 600, 601, ""); MessageElement element = MessageElement.builder(location) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .extensions(ImmutableList.of(fives, sixes)) .build(); assertThat(element.extensions()).hasSize(2); }
@Test public void addMultipleOptions() { FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build(); OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); MessageElement element = MessageElement.builder(location) .name("Message") .fields(ImmutableList.of(field)) .options(ImmutableList.of(kitKat, fooBar)) .build(); assertThat(element.options()).hasSize(2); }
@Test public void noWhitespace() { String proto = "message C {optional A.B ab = 1;}"; ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(1, 1)) .name("C").fields(ImmutableList.of(FieldElement.builder(location.at(1, 12)) .label(OPTIONAL) .type("A.B") .name("ab") .tag(1) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); } }
MessageElement toElement() { return MessageElement.builder(location) .documentation(documentation) .name(name) .options(options.toElements()) .fields(Field.toElements(declaredFields)) .nestedTypes(Type.toElements(nestedTypes)) .oneOfs(OneOf.toElements(oneOfs)) .extensions(Extensions.toElements(extensionsList)) .reserveds(Reserved.toElements(reserveds)) .build(); } }