commit 9912422ac302bfeb6934fbe584bc010e520e8d52
parent d21f027afcd6a4031de9619acd8dacbd2f2f4fd4
Author: Whyme Lyu <callme5long@gmail.com>
Date: Fri, 24 May 2013 00:39:21 +0800
Make HorizontalSelector solid
* #set_to() should raise error when receiving unknown value
* Implement #can_set_to?
* Unit test for #set_to, #val and #can_set_to?
Diffstat:
2 files changed, 50 insertions(+), 1 deletion(-)
diff --git a/lib/sup/horizontal_selector.rb b/lib/sup/horizontal_selector.rb
@@ -1,6 +1,8 @@
module Redwood
class HorizontalSelector
+ class UnknownValue < StandardError; end
+
attr_accessor :label, :changed_by_user
def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
@@ -13,7 +15,14 @@ class HorizontalSelector
@changed_by_user = false
end
- def set_to val; @selection = @vals.index(val) end
+ def set_to val
+ raise UnknownValue, val.inspect unless can_set_to? val
+ @selection = @vals.index(val)
+ end
+
+ def can_set_to? val
+ @vals.include? val
+ end
def val; @vals[@selection] end
diff --git a/test/unit/test_horizontal_selector.rb b/test/unit/test_horizontal_selector.rb
@@ -0,0 +1,40 @@
+require "test_helper"
+
+require "sup/horizontal_selector"
+
+describe Redwood::HorizontalSelector do
+ let(:values) { %w[foo@example.com bar@example.com] }
+ let(:strange_value) { "strange@example.com" }
+
+ before do
+ @selector = Redwood::HorizontalSelector.new(
+ 'Acc:', values, [])
+ end
+
+ it "init w/ the first value selected" do
+ first_value = values.first
+ @selector.val.must_equal first_value
+ end
+
+ it "stores value for selection" do
+ second_value = values[1]
+ @selector.set_to second_value
+ @selector.val.must_equal second_value
+ end
+
+ describe "for unknown value" do
+ it "cannot select unknown value" do
+ @selector.wont_be :can_set_to?, strange_value
+ end
+
+ it "refuses selecting unknown value" do
+ old_value = @selector.val
+
+ assert_raises Redwood::HorizontalSelector::UnknownValue do
+ @selector.set_to strange_value
+ end
+
+ @selector.val.must_equal old_value
+ end
+ end
+end