-
Notifications
You must be signed in to change notification settings - Fork 545
Clean up tests and formatting for Location #446
Conversation
hash_access = begin | ||
object[:some_symbol] | ||
true | ||
rescue | ||
false | ||
end | ||
attr_mappings.each do |pair| | ||
|
||
ATTRIBUTE_MAPPINGS.each do |pair| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're here, we can make this a little clearer to read too. What do you think about these changes?
- Renaming
ATTRIBUTE_MAPPINGS
toATTRIBUTE_ALIASES
- Change the
each
pair
parameter to an actual pair of named parameters,attribute
andaliases
: - Change the
pair[1].each
to be adetect
since that's what we're really doing anyway with thebreak
.
So something like:
ATTRIBUTE_ALIASES.each do |attribute, aliases|
aliases.detect do |alias|
# blah blah
It makes it abundantly clear that we're assigning the aliased attribute's value to whichever aliased attribute what passed in for that attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed the if value = (object[sym] if hash_access)
gross.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, mentioned it offline but object.send(sym) if object.respond_to?(sym)
should be synonymous to object.try(sym)
in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why it's so complicated, value = hash_access ? object[sym] : object.try(sym)
should actually do what we want - use hash access if possible, otherwise use instance method if applicable.
I think that makes it much more readable too.
@@ -129,9 +149,9 @@ def inspect | |||
# Returns the postal code as a properly formatted Zip+4 code, e.g. "77095-2233" | |||
def zip_plus_4 | |||
if /(\d{5})(\d{4})/ =~ @postal_code | |||
return "#{$1}-#{$2}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify this, I believe;
def zip_plus_4
"#{$1}-#{2} if /(\d{5})-?(\d{4})/ =~ @postal_code
end
Tested it out a bit:
irb(main):001:0> "#{$1}-#{$2}" if /(\d{5})-?(\d{4})/ =~ "12345-1234"
=> "12345-1234"
irb(main):002:0> "#{$1}-#{$2}" if /(\d{5})-?(\d{4})/ =~ "123451234"
=> "12345-1234"
irb(main):003:0> "#{$1}-#{$2}" if /(\d{5})-?(\d{4})/ =~ "K2P 1L4"
=> nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 after addressing @jonathankwok's comments
:address_type => [:address_type], | ||
:company_name => [:company, :company_name] | ||
} | ||
return object if object.is_a?(ActiveShipping::Location) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to consider for a future PR: perhaps this should dupe the object? I feel like a from
method should return something I can mutate without having side effects somewhere else.
location = location_fixtures[:ottawa] | ||
assert !location.commercial? | ||
test "#address_type= assigns a type of address as commercial" do | ||
refute @location.commercial? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should switch to assert_predicate
here and everywhere else. Okay to leave this for a future PR, but best to create an issue if deferring. Feel free to assign to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reping with changes. |
assert_equal @attributes_hash[:name], location.name | ||
end | ||
|
||
test ".from sets from an object with properties" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't actually have any tests for .from
an object, not a hash.
This Class.new
is a little nasty, but I tried with OpenStruct
and it still responds to :[]
so doesn't work.
b010ed9
to
8e01d09
Compare
break | ||
|
||
ATTRIBUTE_ALIASES.each do |attribute, aliases| | ||
aliases.detect do |sym| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little weird in that detect
we don't care about the return value, but we do want it to exit early. Test fails if you change it to each
.
aliases.detect do |sym| | ||
value = if hash_access | ||
object[sym] | ||
elsif object.respond_to?(sym) && !Hash.public_instance_methods.include?(sym.to_s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to understand what the second check there is for. Two questions:
.public_instance_methods
returns an array of symbols, so theinclude
would always befalse
and result in this predicate always beingtrue
.- "Call this method only if Hash objects don't have it"? What does that give us? None of the aliased symbols are instance methods on Hash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. You're of course right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
Good @jonathankwok ? |
Oops, didn't squash because I'm a chuuuuuuump... 😣 |
At least they're not goofy commit names 😛 |
Clean up tests and formatting for Location
All cleanup in
Location
.No functional change. Cleans up formatting, extracts a constant, removes old hash syntax, expands methods to be more consistent.
Reformats the tests to be
test "" do
and adds a bunch of missed coverage.