collectiveidea / json_spec

Easily handle JSON in RSpec and Cucumber
rubygems.org/gems/json_spec
MIT License
919 stars 114 forks source link

return size of 0 if json ruby value is nil #80

Closed eluns closed 10 years ago

eluns commented 10 years ago

An entry can be null such as [null], which is 1 entry. If a null is a value of an Array, then the array has 0 entry. However a null value returns a size of 1

def matches?(json) ruby = parse_json(json, @path)

when ruby = nil

  #NoMethodError: undefined method `size' for nil:NilClass
  # so @actual = 1 when ruby is nil
 @actual = Enumerable === ruby ? ruby.size : 1
  @actual == @expected

end

eluns commented 10 years ago

This scenario failed from the master Failing Scenarios: cucumber features/equivalence.feature:262 # Scenario: Table format can fail equivalence

laserlemon commented 10 years ago

@eluns The features/equivalence.feature:262 test is expected to fails and actually asserts that it does.

I can't say that I agree with the presumption that null should be considered to have a size of zero. While I agree that if null were to have any size, it would be zero, I don't believe that the concept of size should apply to null at all.

Rather than evaluating null's size as zero, I would consider a change that includes a better failure message in the case that an enumerable is not encountered.

Thank you! I'll close this pull request in favor of a future request buttoning up the matcher's failure messaging.

eluns commented 10 years ago

Hi Steve, Wow, thank you for the quick response back! I honestly thought my request would be sent off to never, never land. I do agree that null should not have a size, but if it were to have a size it would be zero. However currently, the def matches returns a "size" of 1 for null values. In the ternary, if the variable ruby is null, which is false therefore the ternary returns 1. With the logic that I think we agree on... wouldn't it be 0? I created a test in the size.feature that I think helps commute the possible bug. Deciding the expected behaviors of null values reminds me of pondering the philosophical question to be or not to be. Thank you again for your time! Beth

eluns commented 10 years ago

Hi Steve, Ah, yes my logic doesn't follow my code. Null should not return either 0 or 1. In my pull request, I made unexpected null values return false. Please let me know what you think.

Thanks, Beth

eluns commented 9 years ago

Hi Steve, I created Raise error when checking a size of a json ruby value of nil #82 . Thanks, Beth