summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatrinox <geofflee21@me.com>2016-02-01 20:41:07 -0800
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2016-02-19 04:15:08 -0300
commit4bb4b94439ecc41169cf5569709f8c78a37cd33e (patch)
tree65f71c5190d717e2aa0197eadc2c604c2495341f
parent24d8dd34e74a22d988bd4ab7bd678b877bf990bf (diff)
downloadhighline-4bb4b94439ecc41169cf5569709f8c78a37cd33e.tar.gz
Add configurable MenuItem via HighLine::Menu#add_item
-rw-r--r--lib/highline/menu.rb12
-rw-r--r--test/test_menu.rb32
2 files changed, 42 insertions, 2 deletions
diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb
index e953f0e..aae0fc0 100644
--- a/lib/highline/menu.rb
+++ b/lib/highline/menu.rb
@@ -173,6 +173,18 @@ class HighLine
end
#
+ # Adds an item directly to the menu. If you want more configuraiton or options,
+ # use this method
+ #
+ # @param item [Menu::MenuItem] item containing choice fields and more
+ # @return [void]
+ def add_item(item)
+ @items << item
+ @help.merge!(item.item_help)
+ update_responses
+ end
+
+ #
# A shortcut for multiple calls to the sister method {#choice}. <b>Be
# warned:</b> An _action_ set here will apply to *all* provided
# _names_. This is considered to be a feature, so you can easily
diff --git a/test/test_menu.rb b/test/test_menu.rb
index ce30178..058c1ba 100644
--- a/test/test_menu.rb
+++ b/test/test_menu.rb
@@ -81,7 +81,7 @@ class TestMenu < Minitest::Test
assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, { :undef => :replace }), @output.string)
end
- def test_menu_item_index_selects_name
+ def test_text_override_index_selects_name
@input << "1\n"
@input.rewind
@@ -97,7 +97,7 @@ class TestMenu < Minitest::Test
"? ", @output.string)
end
- def test_menu_item_selections_matches_name
+ def test_text_override_selections_matches_name
@input << "Sample2\n"
@input.rewind
@@ -113,6 +113,34 @@ class TestMenu < Minitest::Test
"? ", @output.string)
end
+ def test_menu_item_index_selects_name
+ @input << "1\n"
+ @input.rewind
+
+ selected = @terminal.choose do |menu|
+ menu.add_item(HighLine::Menu::MenuItem.new(name: "Sample1", text: "Sample2"))
+ menu.add_item(HighLine::Menu::MenuItem.new(name: "Sample2", text: "Sample1"))
+ end
+ assert_equal(selected, "Sample1")
+ assert_equal("1. Sample2\n" +
+ "2. Sample1\n" +
+ "? ", @output.string)
+ end
+
+ def test_menu_item_selections_matches_name
+ @input << "Sample2\n"
+ @input.rewind
+
+ selected = @terminal.choose do |menu|
+ menu.add_item(HighLine::Menu::MenuItem.new(name: "Sample1", text: "Sample2"))
+ menu.add_item(HighLine::Menu::MenuItem.new(name: "Sample2", text: "Sample1"))
+ end
+ assert_equal(selected, "Sample2")
+ assert_equal("1. Sample2\n" +
+ "2. Sample1\n" +
+ "? ", @output.string)
+ end
+
def test_help
@input << "help\nhelp load\nhelp rules\nhelp missing\n"
@input.rewind