fakir icon indicating copy to clipboard operation
fakir copied to clipboard

Support indexed getters (gettters with single argument)

Open npryce opened this issue 9 years ago • 3 comments

I'd like to easily fake an indexed getter. E.g. the method HttpServletRequest::getHeader(String) -> String.

I was thinking it could be done with a map in the faker delegate. Something like:

new Faker<HttpServletRequest>() {
    Map<String,String> header = ImmutableMap.of("Host", "www.example.com", ... );
}.get();

npryce avatar Aug 06 '15 09:08 npryce

Is the special case much more convenient than just providing an implementation?

Map<String, String> headers = ImmutableMap.of("Host", "www.example.com", ... );
new Faker<HttpServletRequest>() {
    String getHeader(String header) { return headers.get(header); }
}.get();

dmcg avatar Aug 08 '15 02:08 dmcg

Once is no problem. After repeating the same pattern of code for indexed properties of different types in several tests, I thought it would be worth supporting directly in the library.

Also, if the type had indexed getters and setters, Fakir could store key/value pairs passed to the setter into the backing map.

npryce avatar Aug 09 '15 17:08 npryce

Implementation in pull request #3

npryce avatar Aug 09 '15 20:08 npryce