/** Reads an enumerated type declaration and returns it. */ private EnumElement readEnumElement(Location location, String documentation) { String name = reader.readName(); EnumElement.Builder builder = EnumElement.builder(location) .name(name) .documentation(documentation); ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); reader.require('{'); while (true) { String valueDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(valueDocumentation, Context.ENUM); if (declared instanceof EnumConstantElement) { constants.add((EnumConstantElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .constants(constants.build()) .build(); }
@Test public void emptyToSchema() { EnumElement element = EnumElement.builder(location).name("Enum").build(); String expected = "enum Enum {}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@Test public void addMultipleOptions() { OptionElement kitKat = OptionElement.create("kit", STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", STRING, "bar"); EnumElement element = EnumElement.builder(location) .name("Enum") .options(ImmutableList.of(kitKat, fooBar)) .constants(ImmutableList.of( EnumConstantElement.builder(location).name("ONE").tag(1).build())) .build(); assertThat(element.options()).hasSize(2); }
/** Reads an enumerated type declaration and returns it. */ private EnumElement readEnumElement(Location location, String documentation) { String name = readName(); EnumElement.Builder builder = EnumElement.builder(location) .name(name) .documentation(documentation); if (readChar() != '{') throw unexpected("expected '{'"); ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); while (true) { String valueDocumentation = readDocumentation(); if (peekChar() == '}') { pos++; break; } Object declared = readDeclaration(valueDocumentation, Context.ENUM); if (declared instanceof EnumConstantElement) { constants.add((EnumConstantElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .constants(constants.build()) .build(); }
/** Reads an enumerated type declaration and returns it. */ private EnumElement readEnumElement(Location location, String documentation) { String name = reader.readName(); EnumElement.Builder builder = EnumElement.builder(location) .name(name) .documentation(documentation); ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); reader.require('{'); while (true) { String valueDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(valueDocumentation, Context.ENUM); if (declared instanceof EnumConstantElement) { constants.add((EnumConstantElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .constants(constants.build()) .build(); }
EnumElement toElement() { return EnumElement.builder(location) .name(name) .documentation(documentation) .constants(EnumConstant.toElements(constants)) .options(options.toElements()) .build(); } }
EnumElement toElement() { return EnumElement.builder(location) .name(name) .documentation(documentation) .constants(EnumConstant.toElements(constants)) .options(options.toElements()) .build(); } }
+ "}\n"; TypeElement enumElement = EnumElement.builder(location.at(3, 3)) .name("CType") .constants(ImmutableList.of(EnumConstantElement.builder(location.at(4, 5)) .name("STRING")
.types(ImmutableList.<TypeElement>of( EnumElement.builder(location.at(5, 1)) .name("Topping") .documentation("What's on my waffles.\nAlso works on pancakes.") .options(
.types(ImmutableList.<TypeElement>of( EnumElement.builder(location.at(5, 1)) .name("Topping") .documentation("What's on my waffles.\nAlso works on pancakes.") .constants(ImmutableList.of(
@Test public void simpleWithDocumentationToSchema() { EnumElement element = EnumElement.builder(location) .name("Enum") .documentation("Hello") .constants(ImmutableList.of( EnumConstantElement.builder(location).name("ONE").tag(1).build(), EnumConstantElement.builder(location).name("TWO").tag(2).build(), EnumConstantElement.builder(location).name("SIX").tag(6).build())) .build(); String expected = "" + "// Hello\n" + "enum Enum {\n" + " ONE = 1;\n" + " TWO = 2;\n" + " SIX = 6;\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@Test public void simpleWithOptionsToSchema() { EnumElement element = EnumElement.builder(location) .name("Enum").options(ImmutableList.of(OptionElement.create("kit", STRING, "kat"))) .constants(ImmutableList.of( EnumConstantElement.builder(location).name("ONE").tag(1).build(), EnumConstantElement.builder(location).name("TWO").tag(2).build(), EnumConstantElement.builder(location).name("SIX").tag(6).build())) .build(); String expected = "" + "enum Enum {\n" + " option kit = \"kat\";\n" + "\n" + " ONE = 1;\n" + " TWO = 2;\n" + " SIX = 6;\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
@Test public void simpleToSchema() { EnumElement element = EnumElement.builder(location) .name("Enum") .constants(ImmutableList.of( EnumConstantElement.builder(location).name("ONE").tag(1).build(), EnumConstantElement.builder(location).name("TWO").tag(2).build(), EnumConstantElement.builder(location).name("SIX").tag(6).build())) .build(); String expected = "" + "enum Enum {\n" + " ONE = 1;\n" + " TWO = 2;\n" + " SIX = 6;\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
EnumElement toElement() { return EnumElement.builder(location) .name(name) .documentation(documentation) .constants(EnumConstant.toElements(constants)) .options(options.toElements()) .build(); } }
@Test public void addMultipleConstants() { EnumConstantElement one = EnumConstantElement.builder(location).name("ONE").tag(1).build(); EnumConstantElement two = EnumConstantElement.builder(location).name("TWO").tag(2).build(); EnumConstantElement six = EnumConstantElement.builder(location).name("SIX").tag(6).build(); EnumElement element = EnumElement.builder(location) .name("Enum") .constants(ImmutableList.of(one, two, six)) .build(); assertThat(element.constants()).hasSize(3); }