Search in sources :

Example 1 with CompactFields

use of net.morimekta.test.providence.testing.CompactFields in project providence by morimekta.

the class ProvidenceBuilderTest method testMerge.

@Test
public void testMerge() {
    OptionalFields a = OptionalFields.builder().setIntegerValue(1).setDoubleValue(1.1).setCompactValue(new CompactFields("a", 1, "al")).build();
    OptionalFields b = OptionalFields.builder().setIntegerValue(2).setCompactValue(new CompactFields("b", 2, null)).build();
    OptionalFields exp = OptionalFields.builder().setIntegerValue(2).setDoubleValue(1.1).setCompactValue(new CompactFields("b", 2, "al")).build();
    OptionalFields c = a.mergeWith(b);
    assertEquals(exp, c);
}
Also used : OptionalFields(net.morimekta.test.providence.testing.OptionalFields) CompactFields(net.morimekta.test.providence.testing.CompactFields) Test(org.junit.Test)

Example 2 with CompactFields

use of net.morimekta.test.providence.testing.CompactFields in project providence by morimekta.

the class ProvidenceTest method testUnion.

@Test
public void testUnion() {
    UnionFields uf = UnionFields.withCompactValue(new CompactFields("a", 4, null));
    assertThat(uf.unionField(), is(UnionFields._Field.COMPACT_VALUE));
    assertThat(uf.hasCompactValue(), is(true));
    assertThat(uf.getCompactValue(), is(any(CompactFields.class)));
    assertThat(uf.getEnumValue(), is(nullValue()));
    assertThat(uf.getBinaryValue(), is(nullValue()));
    assertThat(uf.hasBooleanValue(), is(false));
    assertThat(uf.hasByteValue(), is(false));
}
Also used : UnionFields(net.morimekta.test.providence.testing.UnionFields) CompactFields(net.morimekta.test.providence.testing.CompactFields) Test(org.junit.Test)

Example 3 with CompactFields

use of net.morimekta.test.providence.testing.CompactFields in project providence by morimekta.

the class ProvidenceTest method testHashCode.

@Test
public void testHashCode() {
    OptionalFields of = OptionalFields.builder().build();
    OptionalFields of2 = OptionalFields.builder().build();
    RequiredFields rf = RequiredFields.builder().build();
    UnionFields uf = UnionFields.withCompactValue(new CompactFields("a", 4, null));
    assertThat(of, not(sameInstance(of2)));
    assertThat(of.hashCode(), is(equalTo(of2.hashCode())));
    assertThat(of.hashCode(), not(equalTo(rf.hashCode())));
    assertThat(uf.hashCode(), not(equalTo(rf.hashCode())));
}
Also used : UnionFields(net.morimekta.test.providence.testing.UnionFields) OptionalFields(net.morimekta.test.providence.testing.OptionalFields) RequiredFields(net.morimekta.test.providence.testing.RequiredFields) CompactFields(net.morimekta.test.providence.testing.CompactFields) Test(org.junit.Test)

Example 4 with CompactFields

use of net.morimekta.test.providence.testing.CompactFields in project providence by morimekta.

the class ProvidenceBuilderTest method testMergeBuilder.

@Test
public void testMergeBuilder() {
    OptionalFields._Builder a = OptionalFields.builder().setIntegerValue(1).setCompactValue(new CompactFields("a", 1, "al"));
    OptionalFields b = OptionalFields.builder().setDoubleValue(1.1).setCompactValue(new CompactFields("b", 2, null)).build();
    // C will have modified from both a and b.
    OptionalFields._Builder c = a.merge(b);
    assertThat(c.isModified(OptionalFields._Field.INTEGER_VALUE), is(true));
    assertThat(c.isModified(OptionalFields._Field.COMPACT_VALUE), is(true));
    assertThat(c.isModified(OptionalFields._Field.DOUBLE_VALUE), is(true));
    assertThat(c.isModified(OptionalFields._Field.ENUM_VALUE), is(false));
    assertThat(c.modifiedFields().size(), is(3));
    assertThat(c.modifiedFields(), hasItems(OptionalFields._Field.INTEGER_VALUE, OptionalFields._Field.COMPACT_VALUE, OptionalFields._Field.DOUBLE_VALUE));
    // enum value will also be modified, although nothing has changed internally.
    c.clearEnumValue();
    assertThat(c.isModified(OptionalFields._Field.INTEGER_VALUE), is(true));
    assertThat(c.isModified(OptionalFields._Field.COMPACT_VALUE), is(true));
    assertThat(c.isModified(OptionalFields._Field.DOUBLE_VALUE), is(true));
    assertThat(c.isModified(OptionalFields._Field.ENUM_VALUE), is(true));
    assertThat(c.modifiedFields().size(), is(4));
    assertThat(c.modifiedFields(), hasItems(OptionalFields._Field.INTEGER_VALUE, OptionalFields._Field.COMPACT_VALUE, OptionalFields._Field.DOUBLE_VALUE, OptionalFields._Field.ENUM_VALUE));
    // See that all fields expected are set.
    b = c.build();
    for (OptionalFields._Field field : OptionalFields.kDescriptor.getFields()) {
        switch(field) {
            case INTEGER_VALUE:
            case COMPACT_VALUE:
            case DOUBLE_VALUE:
                assertThat(field.getName(), b.has(field), is(true));
                break;
            case ENUM_VALUE:
                // this is a cleared value so it was modified, but won't have a valid value.
                assertThat(field.getName(), b.has(field), is(false));
                break;
            default:
                assertThat(field.getName(), b.has(field), is(false));
                break;
        }
    }
    // See that all fields modified after reset is false.
    c = b.mutate();
    for (OptionalFields._Field field : OptionalFields.kDescriptor.getFields()) {
        assertThat(c.isModified(field), is(false));
    }
    assertThat(c.modifiedFields().size(), is(0));
}
Also used : OptionalFields(net.morimekta.test.providence.testing.OptionalFields) CompactFields(net.morimekta.test.providence.testing.CompactFields) Test(org.junit.Test)

Example 5 with CompactFields

use of net.morimekta.test.providence.testing.CompactFields in project providence by morimekta.

the class ProvidenceTest method testAutoIdFields.

@Test
public void testAutoIdFields() throws IOException {
    AutoIdFields af = AutoIdFields.builder().setBooleanValue(false).setByteValue((byte) 4).setShortValue((short) 6).setIntegerValue(8).setLongValue(10L).setDoubleValue(12.12).setStringValue("a string").setBinaryValue(Binary.fromHexString("0123456789abcdef")).setCompactValue(new CompactFields("name", 14, "label")).setEnumValue(Value.SIXTEENTH).build();
    BinarySerializer serializer = new BinarySerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(baos, af);
    AutoIdFields af2 = serializer.deserialize(new ByteArrayInputStream(baos.toByteArray()), AutoIdFields.kDescriptor);
    assertThat(af2, is(equalToMessage(af)));
    // An interesting side effect from auto ID fields, is that the numeric
    // order is reverse that of declared order. Always.
    assertEquals("{\n" + "  compactValue = {\n" + "    name = \"name\"\n" + "    id = 14\n" + "    label = \"label\"\n" + "  }\n" + "  enumValue = SIXTEENTH\n" + "  binaryValue = b64(ASNFZ4mrze8)\n" + "  stringValue = \"a string\"\n" + "  doubleValue = 12.12\n" + "  longValue = 10\n" + "  integerValue = 8\n" + "  shortValue = 6\n" + "  byteValue = 4\n" + "  booleanValue = false\n" + "}", debugString(af2));
}
Also used : AutoIdFields(net.morimekta.test.providence.testing.AutoIdFields) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompactFields(net.morimekta.test.providence.testing.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Aggregations

CompactFields (net.morimekta.test.providence.testing.CompactFields)7 Test (org.junit.Test)7 OptionalFields (net.morimekta.test.providence.testing.OptionalFields)4 UnionFields (net.morimekta.test.providence.testing.UnionFields)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)1 AutoIdFields (net.morimekta.test.providence.testing.AutoIdFields)1 Containers (net.morimekta.test.providence.testing.Containers)1 RequiredFields (net.morimekta.test.providence.testing.RequiredFields)1