sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit aa099697ec4db9ef18141af402db425e68a1747d
parent d21f027afcd6a4031de9619acd8dacbd2f2f4fd4
Author: Whyme Lyu <callme5long@gmail.com>
Date:   Sat, 25 May 2013 05:03:48 -0700

Merge pull request #66 from 5long/more-solid-account-selector

More Solid Account Selector
Diffstat:
M lib/sup/horizontal_selector.rb | 11 ++++++++++-
M lib/sup/modes/edit_message_mode.rb | 11 ++++++++---
A test/unit/test_horizontal_selector.rb | 40 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 58 insertions(+), 4 deletions(-)
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/lib/sup/modes/edit_message_mode.rb b/lib/sup/modes/edit_message_mode.rb
@@ -131,13 +131,18 @@ EOS
         HorizontalSelector.new "Account:", AccountManager.user_emails + [nil], user_emails_copy + ["Customized"]
 
       if @header["From"] =~ /<?(\S+@(\S+?))>?$/
-        @account_selector.set_to $1
-        @account_user = ""
+        # TODO: this is ugly. might implement an AccountSelector and handle
+        # special cases more transparently.
+        account_from = @account_selector.can_set_to?($1) ? $1 : nil
+        @account_selector.set_to account_from
       else
         @account_selector.set_to nil
-        @account_user = @header["From"]
       end
 
+      # A single source of truth might better than duplicating this in both
+      # @account_user and @account_selector.
+      @account_user = @header["From"]
+
       add_selector @account_selector
     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