Search in sources :

Example 1 with Fairy

use of io.codearte.jfairy.Fairy in project providence by morimekta.

the class GeneratorWatcherTest method testRandom_customSerializer.

@Test
public void testRandom_customSerializer() throws IOException {
    Random random = new Random();
    Fairy fairy = Fairy.create(Locale.ENGLISH);
    GeneratorWatcher<SimpleGeneratorBase, SimpleGeneratorContext> watcher = GeneratorWatcher.create().setOutputSerializer(new JsonSerializer()).setMaxCollectionItems(2).setRandom(random).withGenerator(CompactFields.kDescriptor, gen -> gen.setAlwaysPresent(CompactFields._Field.NAME).setAlwaysAbsent(CompactFields._Field.LABEL).setValueGenerator(CompactFields._Field.NAME, ctx -> fairy.textProducer().word(1))).setFairy(fairy).dumpOnFailure();
    watcher.starting(Description.EMPTY);
    CompactFields compact = watcher.generate(CompactFields.kDescriptor);
    assertThat(compact.getLabel(), is(nullValue()));
    assertThat(compact.getName(), is(notNullValue()));
    assertThat(compact.getName(), not(containsString(" ")));
    assertThat(compact.hasId(), is(true));
    assertThat(watcher.allGenerated(), hasItem(compact));
    watcher.failed(new Throwable(), Description.EMPTY);
    assertThat(console.output(), is(""));
    assertThat(console.error(), is(json(compact) + "\n"));
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ConsoleWatcher(net.morimekta.testing.rules.ConsoleWatcher) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Random(java.util.Random) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) ArrayList(java.util.ArrayList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Assert.assertThat(org.junit.Assert.assertThat) ExtraStreams(net.morimekta.util.ExtraStreams) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test) Description(org.junit.runner.Description) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) PMessage(net.morimekta.providence.PMessage) PField(net.morimekta.providence.descriptor.PField) Rule(org.junit.Rule) FastBinarySerializer(net.morimekta.providence.serializer.FastBinarySerializer) MessageReader(net.morimekta.providence.mio.MessageReader) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) CompactFields(net.morimekta.test.android.CompactFields) ProvidenceMatchers.equalToMessage(net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) Fairy(io.codearte.jfairy.Fairy) Random(java.util.Random) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Fairy(io.codearte.jfairy.Fairy) CompactFields(net.morimekta.test.android.CompactFields) Test(org.junit.Test)

Example 2 with Fairy

use of io.codearte.jfairy.Fairy in project providence by morimekta.

the class GeneratorWatcher method setLocale.

/**
 * Set the locale to generate values for. Applies to default string
 * values. Known good locales are:
 * <ul>
 *     <li>English (US)
 *     <li>German  (DE)
 *     <li>French  (FR)
 *     <li>Italian (IT)
 *     <li>Spanish (ES)
 *     <li>Polish  (PL)
 *     <li>Swedish (SV)
 *     <li>Chinese (ZH)
 * </ul>
 *
 * @param locale The locale to set.
 * @return The message generator.
 */
public GeneratorWatcher<Base, Context> setLocale(Locale locale) {
    Fairy fairy = singletonFairyCache.get(locale);
    if (fairy == null) {
        fairy = Fairy.create(locale);
        singletonFairyCache.put(locale, fairy);
    }
    return setFairy(fairy);
}
Also used : Fairy(io.codearte.jfairy.Fairy)

Example 3 with Fairy

use of io.codearte.jfairy.Fairy in project providence by morimekta.

the class GeneratorWatcherTest method testRandom_customWriter.

@Test
public void testRandom_customWriter() throws IOException {
    Fairy fairy = Fairy.create(Locale.ENGLISH);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GeneratorWatcher<SimpleGeneratorBase, SimpleGeneratorContext> generator = GeneratorWatcher.create().setMessageWriter(new IOMessageWriter(baos, new FastBinarySerializer())).setMaxCollectionItems(2).withGenerator(CompactFields.kDescriptor, gen -> {
        gen.setAlwaysPresent(CompactFields._Field.NAME);
        gen.setValueGenerator(CompactFields._Field.NAME, ctx -> fairy.textProducer().word(1));
    }).dumpOnFailure();
    generator.starting(Description.EMPTY);
    CompactFields compact = generator.generate(CompactFields.kDescriptor);
    assertThat(compact.getLabel(), is(notNullValue()));
    assertThat(compact.getName(), is(notNullValue()));
    assertThat(compact.getName(), not(containsString(" ")));
    assertThat(compact.hasId(), is(true));
    assertThat(generator.allGenerated(), hasItem(compact));
    generator.failed(new Throwable(), Description.EMPTY);
    assertThat(console.output(), is(""));
    assertThat(console.error(), is(""));
    IOMessageReader reader = new IOMessageReader(new ByteArrayInputStream(baos.toByteArray()), new FastBinarySerializer());
    assertThat(reader.read(CompactFields.kDescriptor), is(equalToMessage(compact)));
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ConsoleWatcher(net.morimekta.testing.rules.ConsoleWatcher) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Random(java.util.Random) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) ArrayList(java.util.ArrayList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Assert.assertThat(org.junit.Assert.assertThat) ExtraStreams(net.morimekta.util.ExtraStreams) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test) Description(org.junit.runner.Description) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) PMessage(net.morimekta.providence.PMessage) PField(net.morimekta.providence.descriptor.PField) Rule(org.junit.Rule) FastBinarySerializer(net.morimekta.providence.serializer.FastBinarySerializer) MessageReader(net.morimekta.providence.mio.MessageReader) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) CompactFields(net.morimekta.test.android.CompactFields) ProvidenceMatchers.equalToMessage(net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) Fairy(io.codearte.jfairy.Fairy) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) FastBinarySerializer(net.morimekta.providence.serializer.FastBinarySerializer) ByteArrayInputStream(java.io.ByteArrayInputStream) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Fairy(io.codearte.jfairy.Fairy) CompactFields(net.morimekta.test.android.CompactFields) Test(org.junit.Test)

Example 4 with Fairy

use of io.codearte.jfairy.Fairy in project providence by morimekta.

the class ITGenerator method generate.

public void generate(final int n) throws IOException, TException {
    for (int i = 0; i < n; ++i) {
        Fairy fairy = LOCAL_FAIRIES[new Random().nextInt(LOCAL_FAIRIES.length)];
        generator.setFairy(fairy);
        PM providence = generator.generate(descriptor);
        TM thrift = convert(providence);
        pvdListBuilder.add(providence);
        thrListBuilder.add(thrift);
    }
}
Also used : Random(java.util.Random) Fairy(io.codearte.jfairy.Fairy)

Aggregations

Fairy (io.codearte.jfairy.Fairy)4 Random (java.util.Random)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 IntStream (java.util.stream.IntStream)2 PMessage (net.morimekta.providence.PMessage)2 PField (net.morimekta.providence.descriptor.PField)2 IOMessageReader (net.morimekta.providence.mio.IOMessageReader)2 IOMessageWriter (net.morimekta.providence.mio.IOMessageWriter)2 MessageReader (net.morimekta.providence.mio.MessageReader)2 FastBinarySerializer (net.morimekta.providence.serializer.FastBinarySerializer)2 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)2 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)2 ProvidenceMatchers.equalToMessage (net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage)2 CompactFields (net.morimekta.test.android.CompactFields)2 ConsoleWatcher (net.morimekta.testing.rules.ConsoleWatcher)2