Search in sources :

Example 1 with OptionalFields

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

the class MessageGeneratorTest method testGenerate_presenceOverrides.

@Test
public void testGenerate_presenceOverrides() {
    base.withMessageGenerator(OptionalFields.kDescriptor, gen -> gen.setAlwaysPresent(OptionalFields._Field.BINARY_VALUE, OptionalFields._Field.BOOLEAN_VALUE, OptionalFields._Field.BYTE_VALUE).setAlwaysAbsent(OptionalFields._Field.INTEGER_VALUE, OptionalFields._Field.SHORT_VALUE, OptionalFields._Field.LONG_VALUE));
    OptionalFields res = generator.generate(base.createContext());
    assertThat(res.hasBinaryValue(), is(true));
    assertThat(res.hasBooleanValue(), is(true));
    assertThat(res.hasByteValue(), is(true));
    assertThat(res.hasIntegerValue(), is(false));
    assertThat(res.hasShortValue(), is(false));
    assertThat(res.hasLongValue(), is(false));
}
Also used : OptionalFields(net.morimekta.test.providence.testing.OptionalFields) Test(org.junit.Test)

Example 2 with OptionalFields

use of net.morimekta.test.providence.testing.OptionalFields 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 3 with OptionalFields

use of net.morimekta.test.providence.testing.OptionalFields 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 OptionalFields

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

the class MessageGeneratorTest method testGenerate_withCutsomContext.

@Test
public void testGenerate_withCutsomContext() {
    base.setDefaultFillRate(1.0).withMessageGenerator(OptionalFields.kDescriptor, generator -> generator.setValueGenerator(OptionalFields._Field.STRING_VALUE, context -> context.getUuid().toString()).setValueGenerator(OptionalFields._Field.LONG_VALUE, context -> context.getUuid().getMostSignificantBits()));
    FixedUuidContext reused = base.createContext();
    OptionalFields first = generator.generate(reused);
    OptionalFields second = generator.generate(base.createContext());
    assertThat(first.getStringValue(), is(not(second.getStringValue())));
    long firstMost = UUID.fromString(first.getStringValue()).getMostSignificantBits();
    long secondMost = UUID.fromString(second.getStringValue()).getMostSignificantBits();
    assertThat(first.getLongValue(), is(firstMost));
    assertThat(second.getLongValue(), is(secondMost));
    OptionalFields whenReused = generator.generate(reused);
    assertThat(whenReused.getStringValue(), is(first.getStringValue()));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Test(org.junit.Test) UUID(java.util.UUID) PMessage(net.morimekta.providence.PMessage) Assert.assertThat(org.junit.Assert.assertThat) PField(net.morimekta.providence.descriptor.PField) UnionFields(net.morimekta.test.providence.testing.UnionFields) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OptionalFields(net.morimekta.test.providence.testing.OptionalFields) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Before(org.junit.Before) OptionalFields(net.morimekta.test.providence.testing.OptionalFields) Test(org.junit.Test)

Example 5 with OptionalFields

use of net.morimekta.test.providence.testing.OptionalFields 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)

Aggregations

OptionalFields (net.morimekta.test.providence.testing.OptionalFields)7 Test (org.junit.Test)7 CompactFields (net.morimekta.test.providence.testing.CompactFields)4 UnionFields (net.morimekta.test.providence.testing.UnionFields)2 UUID (java.util.UUID)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PMessage (net.morimekta.providence.PMessage)1 PField (net.morimekta.providence.descriptor.PField)1 Containers (net.morimekta.test.providence.testing.Containers)1 RequiredFields (net.morimekta.test.providence.testing.RequiredFields)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 CoreMatchers.not (org.hamcrest.CoreMatchers.not)1 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)1 Matchers.isOneOf (org.hamcrest.Matchers.isOneOf)1 Assert.assertThat (org.junit.Assert.assertThat)1 Before (org.junit.Before)1