Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added / Edited ActiveModel::Errors test cases. #11183

Closed

Conversation

yangchenyun
Copy link
Contributor

  • Added test cases to demo the difference between include? and empty?
  • Make size and count behave consistently.

@@ -93,14 +93,45 @@ def test_has_key?
assert_equal [:foo, :baz], errors.keys
end

test "detecting whether there are errors with empty?, blank?, include?" do
test 'empty?, blank? doesn\'t detect initial empty value as an error' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave it with double quotes to use doesn't, or change it to does not (I think it might be better to leave as double quotes as the rest of the file - also check other tests.)

@carlosantoniodasilva
Copy link
Member

Please rebase and remove your first commit (the doc fix) since I cherry-picked it already from the other pull request.

person.errors[:foo] = []
assert person.errors.empty?
assert person.errors.blank?
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this valid API? I think I never used it like that.

/cc @rafaelfranca @josevalim

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally doubt it to be a valid API too and the difference result from include?/empty? and size/count(before this PR) is confusing. Just write test cases to reveal the difference here.

Need more suggestion.

- `empty?`, `blank?` and `include?` has different logics to detect an error, new test cases demostrate the difference
- Empty String(`''`) and Empty Array('[]') are edge cases, and they differentiate the behavior of the above methods.
Given the following code, size and count calculates to different behavior
```ruby
person.errors.add(:name, [])
person.errors.add(:name, '')
person.errors.size
person.errors.count
```

According to the doc, I alias `size` to `count`. When a error is added, it should be reported by `count` regardless of its message type.
@rafaelfranca
Copy link
Member

When you say Make size and count behave consistently. was they behaving differently?

Looking for the implementation looks like the behavior is the same, only the implementation that is different

@yangchenyun
Copy link
Contributor Author

There is subtle(if not buggy) behavior when message comes in as an Array which is allowed

Here is the demo of difference: https://gist.github.com/yangchenyun/5915718

@ghost ghost assigned rafaelfranca Dec 5, 2013
@rafaelfranca
Copy link
Member

Message as Array should not be allowed. It can be a proc, symbol, or string. There is no sense to pass a complex object or a array as message. We should fix it.

@morgoth
Copy link
Member

morgoth commented Mar 30, 2015

Looks like this can be closed since #19021 was merged

@rafaelfranca rafaelfranca modified the milestones: 5.0.0 [temp], 5.0.0 Dec 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants