Skip to content
This repository has been archived by the owner on Jun 13, 2018. It is now read-only.

Clean up tests and formatting for Location #446

Merged
merged 4 commits into from
Dec 14, 2016

Conversation

kmcphillips
Copy link
Contributor

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.

@kmcphillips kmcphillips added this to the Version 2.0 milestone Dec 13, 2016
hash_access = begin
object[:some_symbol]
true
rescue
false
end
attr_mappings.each do |pair|

ATTRIBUTE_MAPPINGS.each do |pair|
Copy link
Contributor

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 to ATTRIBUTE_ALIASES
  • Change the each pair parameter to an actual pair of named parameters, attribute and aliases:
  • Change the pair[1].each to be a detect since that's what we're really doing anyway with the break.

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.

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 just noticed the if value = (object[sym] if hash_access) gross.

Copy link
Contributor

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.

Copy link
Contributor

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}"
Copy link
Contributor

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

Copy link
Contributor

@thegedge thegedge left a 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)
Copy link
Contributor

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?
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kmcphillips
Copy link
Contributor Author

Reping with changes.

assert_equal @attributes_hash[:name], location.name
end

test ".from sets from an object with properties" do
Copy link
Contributor Author

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.

@kmcphillips kmcphillips force-pushed the modernize-location-tests branch from b010ed9 to 8e01d09 Compare December 14, 2016 16:45
break

ATTRIBUTE_ALIASES.each do |attribute, aliases|
aliases.detect do |sym|
Copy link
Contributor Author

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)
Copy link
Contributor

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:

  1. .public_instance_methods returns an array of symbols, so the include would always be false and result in this predicate always being true.
  2. "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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

@kmcphillips
Copy link
Contributor Author

Good @jonathankwok ?

@kmcphillips kmcphillips merged commit dd35f38 into master Dec 14, 2016
@kmcphillips kmcphillips deleted the modernize-location-tests branch December 14, 2016 19:31
@kmcphillips
Copy link
Contributor Author

Oops, didn't squash because I'm a chuuuuuuump... 😣

@jonathankwok
Copy link
Contributor

At least they're not goofy commit names 😛

maartenvg pushed a commit that referenced this pull request Nov 9, 2017
Clean up tests and formatting for Location
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants