Skip to content

cmp different from the string representation #41

@pirj

Description

@pirj

There's an ordering difference when using the built-in <=> vs the string representation one:

> Pathname.new(File.expand_path('.')).glob('**/*.rb').sort.first(5)
=> [#<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expectation_chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/message_chains.rb>]
> Pathname.new(File.expand_path('.')).glob('**/*.rb').sort_by(&:to_s).first(5)
=> [#<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb>]

And the latter corresponds to the classic alternative:

> Dir[File.expand_path('**/*.rb')].sort.first(5)
=> ["/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb"]

Just by looking at the cmp implementation and tests I couldn't understand, is this the designed behaviour, or the behaviour is undefined?

Semantically, it would make sense to load rspec/mocks.rb first, and then rspec/mocks/any_instance.rb, not the other way around.
Some discussion in rspec-rails regarding the issues it may cause.
rspec-rails PR to change the helper template to use Pathname#glob instead of Dir.[].

A workaround rspec-rails plans to take is to sort_by(&:to_s) to get back to the string representation-like ordering.

(semi-)Related cops:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions