ProtoParser(Location location, char[] data) { this.reader = new SyntaxReader(data, location); this.fileBuilder = ProtoFileElement.builder(location); }
@Test public void emptyToSchema() { ProtoFileElement file = ProtoFileElement.builder(location).build(); String expected = "// file.proto\n"; assertThat(file.toSchema()).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 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 emptyWithPackageToSchema() { ProtoFileElement file = ProtoFileElement.builder(location) .packageName("example.simple") .build(); String expected = "" + "// file.proto\n" + "package example.simple;\n"; assertThat(file.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 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 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 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 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 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 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 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 addMultipleExtends() { ExtendElement extend1 = ExtendElement.builder(location).name("Extend1").build(); ExtendElement extend2 = ExtendElement.builder(location).name("Extend2").build(); ProtoFileElement file = ProtoFileElement.builder(location) .extendDeclarations(ImmutableList.of(extend1, extend2)) .build(); assertThat(file.extendDeclarations()).hasSize(2); }
@Test public void addMultipleServices() { ServiceElement service1 = ServiceElement.builder(location).name("Service1").build(); ServiceElement service2 = ServiceElement.builder(location).name("Service2").build(); ProtoFileElement file = ProtoFileElement.builder(location) .services(ImmutableList.of(service1, service2)) .build(); assertThat(file.services()).hasSize(2); }
@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); }
ProtoFileElement toElement() { return ProtoFileElement.builder(location) .imports(imports) .publicImports(publicImports) .packageName(packageName) .types(Type.toElements(types)) .services(Service.toElements(services)) .extendDeclarations(Extend.toElements(extendList)) .options(options.toElements()) .syntax(syntax) .build(); }
@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); } }