static ImmutableList<FieldElement> toElements(ImmutableList<Field> fields) { ImmutableList.Builder<FieldElement> elements = new ImmutableList.Builder<>(); for (Field field : fields) { elements.add(FieldElement.builder(field.location) .label(field.label) .name(field.name) .documentation(field.documentation) .tag(field.tag) .defaultValue(field.defaultValue) .options(field.options.toElements()) .type(field.elementType) .build()); } return elements.build(); }
@Test public void oneOfFieldToSchema() { FieldElement field = FieldElement.builder(location) .type("string") .name("name") .tag(1) .build(); String expected = "string name = 1;\n"; assertThat(field.toSchema()).isEqualTo(expected); }
@Test public void fieldToSchema() { FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build(); String expected = "required string name = 1;\n"; assertThat(field.toSchema()).isEqualTo(expected); }
@Test public void fieldWithOptionsToSchema() { FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .options(ImmutableList.of(OptionElement.create("kit", Kind.STRING, "kat"))) .build(); String expected = "required string name = 1 [\n" + " kit = \"kat\"\n" + "];\n"; assertThat(field.toSchema()).isEqualTo(expected); } }
@Test public void addMultipleOptions() { OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .options(ImmutableList.of(kitKat, fooBar)) .build(); assertThat(field.options()).hasSize(2); } }
@Test public void fieldWithDocumentationToSchema() { FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .documentation("Hello") .build(); String expected = "" + "// Hello\n" + "required string name = 1;\n"; assertThat(field.toSchema()).isEqualTo(expected); }
/** Reads an field declaration and returns it. */ private FieldElement readField( Location location, String documentation, @Nullable Field.Label label, String type) { String name = reader.readName(); reader.require('='); int tag = reader.readInt(); FieldElement.Builder builder = FieldElement.builder(location) .label(label) .type(type) .name(name) .tag(tag); List<OptionElement> options = new OptionReader(reader).readOptions(); reader.require(';'); options = new ArrayList<>(options); // Mutable copy for extractDefault. String defaultValue = stripDefault(options); documentation = reader.tryAppendTrailingDocumentation(documentation); return builder.documentation(documentation) .defaultValue(defaultValue) .options(ImmutableList.copyOf(options)) .build(); }
@Test public void simpleToSchema() { ExtendElement extend = ExtendElement.builder(location) .name("Name") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .build(); String expected = "" + "extend Name {\n" + " required string name = 1;\n" + "}\n"; assertThat(extend.toSchema()).isEqualTo(expected); }
@Test public void simpleToSchema() { TypeElement element = MessageElement.builder(location) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .build(); String expected = "" + "message Message {\n" + " required string name = 1;\n" + "}\n"; assertThat(element.toSchema()).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 addMultipleFields() { FieldElement firstName = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("first_name") .tag(1) .build(); FieldElement lastName = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("last_name") .tag(2) .build(); MessageElement element = MessageElement.builder(location) .name("Message") .fields(ImmutableList.of(firstName, lastName)) .build(); assertThat(element.fields()).hasSize(2); }
@Test public void addMultipleFields() { FieldElement firstName = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("first_name") .tag(1) .build(); FieldElement lastName = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("last_name") .tag(2) .build(); ExtendElement extend = ExtendElement.builder(location) .name("Name") .fields(ImmutableList.of(firstName, lastName)) .build(); assertThat(extend.fields()).hasSize(2); }
@Test public void simpleWithExtensionsToSchema() { TypeElement element = MessageElement.builder(location) .name("Message") .fields(ImmutableList.of(FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .extensions(ImmutableList.of( ExtensionsElement.create(location, 500, 501, ""))) .build(); String expected = "" + "message Message {\n" + " required string name = 1;\n" + "\n" + " extensions 500 to 501;\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@Test public void simpleWithDocumentationToSchema() { TypeElement element = MessageElement.builder(location) .name("Message") .documentation("Hello") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .build(); String expected = "" + "// Hello\n" + "message Message {\n" + " required string name = 1;\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@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); } }
@Test public void oneOfToSchema() { TypeElement element = MessageElement.builder(location) .name("Message") .oneOfs(ImmutableList.of( OneOfElement.builder() .name("hi") .fields(ImmutableList.of( FieldElement.builder(location) .type("string") .name("name") .tag(1) .build())) .build())) .build(); String expected = "" + "message Message {\n" + " oneof hi {\n" + " string name = 1;\n" + " }\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@Test public void simpleWithDocumentationToSchema() { ExtendElement extend = ExtendElement.builder(location) .name("Name") .documentation("Hello") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .build(); String expected = "" + "// Hello\n" + "extend Name {\n" + " required string name = 1;\n" + "}\n"; assertThat(extend.toSchema()).isEqualTo(expected); } }
@Test public void field() { FieldElement field = FieldElement.builder(location) .label(OPTIONAL) .type("CType") .name("ctype") .tag(1) .options(ImmutableList.of( OptionElement.create("default", Kind.ENUM, "TEST"), OptionElement.create("deprecated", Kind.BOOLEAN, "true"))) .build(); assertThat(field.options()).containsOnly( // OptionElement.create("default", Kind.ENUM, "TEST"), // OptionElement.create("deprecated", Kind.BOOLEAN, "true")); }
@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); }