jeffh/Fox

FOXOptional is returning NSNull singleton instead of nil pointer (as docs indicates)

Opened this issue · 1 comments

According to FOX's documentation:

FOXOptional - "Creates a new generator that has a 25% chance of returning nil instead of the provided generated value"

But when I tried to use Optionals in my tests, I get a refence to [NSNull null] singleton. So I alway end up managing this myself in tests.

id<FOXGenerator> adultsAndChildrenDictionaryGenerator = FOXDictionary(@{
                                                                        @"adults": FOXOptional(FOXInteger()),
                                                                        @"children": FOXOptional(FOXArray(FOXInteger()))
                                                                        });

FOXAssert(FOXForAll(adultsAndChildrenDictionaryGenerator, ^BOOL(NSDictionary *adultsAndChildrenDictionary) {

    NSLog(@"%@",adultsAndChildrenDictionary);

    NSNumber *adults = adultsAndChildrenDictionary[@"adults"];

    if ([adults isEqual:[NSNull null]]) {
        adults = nil;
    }
    NSArray *childrenArray = adultsAndChildrenDictionary[@"children"];

    if ([childrenArray isEqual:[NSNull null]]) {
        childrenArray = nil;
    }

    //Test
    NSString *currentDistribution = [self distributionWithAdultsQuantity:adults
                                                             childrenAge:childrenArray];

Can you do anything to directly retrieve a nil pointer?

thanks!

jeffh commented

I see the issue now. FOXOptional does produce nils, but NSDictionary and NSArray will box it up into an NSNull since they cannot hold nil values. (Eg. JSON null becomes NSNull).

Unfortunately, Fox currently doesn't have a direct method of producing nil for objc container types. You can emulate it using FOXFrequency (see FOXOptional implementation for an example) and producing dictionaries without those keys.