nomemory/mockneat

Support for random length strings

keremulutas opened this issue · 4 comments

When we call:

mock.fmt("SOME_PREFIX_#{randomsuffix}")
.param("randomsuffix", mock.strings().size(
    mock.ints().range(42, 62).val()
))

the library naturally generates a random int between 42 and 62 once and then generates randomsuffix with this length.

But the problem is, it doesn't vary between calls because mock.strings().size() method only accepts an int parameter (and not a MockUnit) so we can't pass the method call without the .val() call made.

Can you please add support for variable length strings? Perhaps for every other API methods this applies to?

Hey, thanks for notifying this limitation.

Yes, this can be implemented. Promise to work on it when I have some spare time, in the next weeks.

@keremulutas

Meanwhile, until this feature will be implemented you can do something like this:

        MockNeat mock = MockNeat.secure();
        final MockUnitInt variableLength = mock.ints().range(1, 62);
        List<String> fmt = mock.fmt("SOME_PREFIX_")
                               .map(s -> {
                                        int vl = variableLength.val();
                                        return s + mock.strings().size(vl).val();
                                })
                                .list(100)
                                .val();

        System.out.println(fmt);

It works because the method passed to the map(...) is called each time, thus vl is always refreshed.

It's not perfect but this can be a workaround for the moment.

@keremulutas

The feature was implemented in the latest version: 0.1.3.

Documented here:
https://github.com/nomemory/mockneat/wiki/MockNeat#strings

Thanks a lot, keep up the good work! Thanks for this cool library btw ;)