@Test public void fieldToSchema() { EnumConstantElement value = EnumConstantElement.builder(location).name("NAME").tag(1).build(); String expected = "NAME = 1;\n"; assertThat(value.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); }
@Test public void fieldWithDocumentationToSchema() { EnumConstantElement value = EnumConstantElement.builder(location) .name("NAME") .tag(1) .documentation("Hello") .build(); String expected = "" + "// Hello\n" + "NAME = 1;\n"; assertThat(value.toSchema()).isEqualTo(expected); }
.name(label) .tag(readInt());
/** Reads an enum constant like "ROCK = 0;". The label is the constant name. */ private EnumConstantElement readEnumConstant( String documentation, Location location, String label) { reader.require('='); int tag = reader.readInt(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); if (reader.peekChar() == '[') { reader.readChar(); while (true) { options.add(readOption('=')); char c = reader.readChar(); if (c == ']') { break; } if (c != ',') { throw reader.unexpected("Expected ',' or ']"); } } } reader.require(';'); documentation = reader.tryAppendTrailingDocumentation(documentation); return EnumConstantElement.builder(location) .name(label) .tag(tag) .documentation(documentation) .options(options.build()) .build(); }
EnumConstantElement toElement() { return EnumConstantElement.builder(location) .documentation(documentation) .name(name) .tag(tag) .options(options.toElements()) .build(); }
EnumConstantElement toElement() { return EnumConstantElement.builder(location) .documentation(documentation) .name(name) .tag(tag) .options(options.toElements()) .build(); }
.name("CType") .constants(ImmutableList.of(EnumConstantElement.builder(location.at(4, 5)) .name("STRING") .tag(0) .options(ImmutableList.of(
.constants(ImmutableList.of( EnumConstantElement.builder(location.at(8, 3)) .name("FRUIT") .tag(1) .options(ImmutableList.of( .build(), EnumConstantElement.builder(location.at(10, 3)) .name("CREAM") .tag(2) .documentation("Yummy, yummy cream.") .build(), EnumConstantElement.builder(location.at(13, 3)) .name("SYRUP") .tag(3) .documentation("Quebec Maple syrup")
.constants(ImmutableList.of( EnumConstantElement.builder(location.at(6, 3)) .name("FRUIT") .tag(1) .build(), EnumConstantElement.builder(location.at(8, 3)) .name("CREAM") .tag(2) .documentation("Yummy, yummy cream.") .build(), EnumConstantElement.builder(location.at(11, 3)) .name("SYRUP") .tag(3) .documentation("Quebec Maple syrup")
@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 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 fieldWithOptionsToSchema() { EnumConstantElement value = EnumConstantElement.builder(location) .name("NAME") .tag(1) .options(ImmutableList.of( OptionElement.create("kit", STRING, "kat", true), OptionElement.create("tit", STRING, "tat"))) .build(); String expected = "NAME = 1 [\n" + " (kit) = \"kat\",\n" + " tit = \"tat\"\n" + "];\n"; assertThat(value.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); }
/** Reads an enum constant like "ROCK = 0;". The label is the constant name. */ private EnumConstantElement readEnumConstant( String documentation, Location location, String label) { reader.require('='); int tag = reader.readInt(); ImmutableList<OptionElement> options = new OptionReader(reader).readOptions(); reader.require(';'); documentation = reader.tryAppendTrailingDocumentation(documentation); return EnumConstantElement.builder(location) .name(label) .tag(tag) .documentation(documentation) .options(options) .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); }
EnumConstantElement toElement() { return EnumConstantElement.builder(location) .documentation(documentation) .name(name) .tag(tag) .options(options.toElements()) .build(); }