summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatrinox <geofflee21@me.com>2016-01-25 21:34:32 -0800
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2016-02-19 03:57:18 -0300
commit499f393ec533239f1457a77d2004fab6d454fe10 (patch)
tree8746ef69473731d5dfc55edbc54796a266efed90
parent8957d31c0407fe1b1b32c442dbe8217a026a3427 (diff)
downloadhighline-499f393ec533239f1457a77d2004fab6d454fe10.tar.gz
Remove keyword args to preserve backwards compatability (and ruby 1.9 support)
Allow passing in HighLine::Menu::MenuItem directly. Menu choice can be either name or the MenuItem.
-rw-r--r--lib/highline/menu.rb22
-rw-r--r--test/test_menu.rb40
2 files changed, 39 insertions, 23 deletions
diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb
index 5d5e6b5..e953f0e 100644
--- a/lib/highline/menu.rb
+++ b/lib/highline/menu.rb
@@ -161,8 +161,12 @@ class HighLine
# menu.help("rules", "The rules of this system are as follows...")
# end
- def choice( name, text: nil, help: nil, &action )
- item = MenuItem.new(name: name, text: text, help: help, action: action)
+ def choice( name_or_item, help = nil, &action )
+ if name_or_item.is_a?(MenuItem)
+ item = name_or_item
+ else
+ item = MenuItem.new(name: name_or_item, text: name_or_item, help: help, action: action)
+ end
@items << item
@help.merge!(item.item_help)
update_responses # rebuild responses based on our settings
@@ -237,7 +241,7 @@ class HighLine
"a specific topic enter:\n\thelp [TOPIC]\nTry asking " +
"for help on any of the following:\n\n" +
"<%= list(#{topics.inspect}, :columns_across) %>"
- choice(:help, help: help_help) do |command, topic|
+ choice(:help, help_help) do |command, topic|
topic.strip!
topic.downcase!
if topic.empty?
@@ -485,12 +489,12 @@ class HighLine
# @param help: help, see above (not sure how it works)
# @param action: a block that gets called when choice is selected
#
- def initialize(name:, text: nil, help: nil, action: nil)
- text ||= name
- @name = name
- @text = text
- @help = help
- @action = action
+ def initialize(attributes)
+ attributes[:text] ||= attributes[:name]
+ @name = attributes[:name]
+ @text = attributes[:text]
+ @help = attributes[:help]
+ @action = attributes[:action]
end
def item_help
diff --git a/test/test_menu.rb b/test/test_menu.rb
index 5da8331..ce30178 100644
--- a/test/test_menu.rb
+++ b/test/test_menu.rb
@@ -81,24 +81,36 @@ class TestMenu < Minitest::Test
assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, { :undef => :replace }), @output.string)
end
- def test_text_overide
+ def test_menu_item_index_selects_name
@input << "1\n"
@input.rewind
- @terminal.choose do |menu|
- menu.choice "Sample1", text: 'more choice1'
- end
- assert_equal("1. more choice1\n? ", @output.string)
+ selected = @terminal.choose do |menu|
+ item1 = HighLine::Menu::MenuItem.new(name: "Sample1", text: "Sample2")
+ item2 = HighLine::Menu::MenuItem.new(name: "Sample2", text: "Sample1")
+ menu.choice(item1)
+ menu.choice(item2)
+ end
+ assert_equal(selected, "Sample1")
+ assert_equal("1. Sample2\n" +
+ "2. Sample1\n" +
+ "? ", @output.string)
end
- def test_name_with_text
+ def test_menu_item_selections_matches_name
@input << "Sample2\n"
@input.rewind
- @terminal.choose do |menu|
- menu.choice "Sample2", text: 'Sample1'
- end
- assert_equal("1. Sample1\n? ", @output.string)
+ selected = @terminal.choose do |menu|
+ item1 = HighLine::Menu::MenuItem.new(name: "Sample1", text: "Sample2")
+ item2 = HighLine::Menu::MenuItem.new(name: "Sample2", text: "Sample1")
+ menu.choice(item1)
+ menu.choice(item2)
+ end
+ assert_equal(selected, "Sample2")
+ assert_equal("1. Sample2\n" +
+ "2. Sample1\n" +
+ "? ", @output.string)
end
def test_help
@@ -109,9 +121,9 @@ class TestMenu < Minitest::Test
@terminal.choose do |menu|
menu.shell = true
- menu.choice(:load, help: "Load a file.")
- menu.choice(:save, help: "Save data in file.")
- menu.choice(:quit, help: "Exit program.")
+ menu.choice(:load, "Load a file.")
+ menu.choice(:save, "Save data in file.")
+ menu.choice(:quit, "Exit program.")
menu.help("rules", "The rules of this system are as follows...")
end
@@ -486,7 +498,7 @@ class TestMenu < Minitest::Test
selected = @terminal.choose do |menu|
menu.responses[:ask_on_error] = "> "
menu.prompt = "> "
- menu.choice :exit, help: "Exit cube editor"
+ menu.choice :exit, "Exit cube editor"
end
prompt = "> "