jeffh / Fox

Property Based Testing Library for Objective-C and Swift. QuickCheck for Apple's Platforms.
http://fox-testing.rtfd.org
Other
624 stars 30 forks source link

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

Open AdrianFerreyra opened 9 years ago

AdrianFerreyra commented 9 years ago

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 9 years ago

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.