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

Allow to Override Order of Commands in Help #642

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tests: added tests for Thor.sort_commands!
  • Loading branch information
alessio-signorini authored and rafaelfranca committed May 11, 2023
commit e5031d2254c6be09063510197bc80a597ec6aed2
75 changes: 75 additions & 0 deletions spec/sort_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require "helper"

describe Thor do
def shell
@shell ||= Thor::Base.shell.new
end

describe "#sort - default" do
my_script = Class.new(Thor) do
desc "a", "First Command"
def a; end

desc "z", "Last Command"
def z; end
end

before do
@content = capture(:stdout) { my_script.help(shell) }
end

it "sorts them lexicographillay" do
expect(@content).to match(/:a.+:help.+:z/m)
end
end


describe "#sort - simple override" do
my_script = Class.new(Thor) do
desc "a", "First Command"
def a; end

desc "z", "Last Command"
def z; end

def self.sort_commands!(list)
list.sort!
list.reverse!
end

end

before do
@content = capture(:stdout) { my_script.help(shell) }
end

it "sorts them in reverse" do
expect(@content).to match(/:z.+:help.+:a/m)
end
end


describe "#sort - simple override" do
my_script = Class.new(Thor) do
desc "a", "First Command"
def a; end

desc "z", "Last Command"
def z; end

def self.sort_commands!(list)
list.sort_by! do |a,b|
a[0] == :help ? -1 : a[0] <=> b[0]
end
end
end

before do
@content = capture(:stdout) { my_script.help(shell) }
end

it "puts help first then sorts them lexicographillay" do
expect(@content).to match(/:help.+:a.+:z/m)
end
end
end