# encoding: utf-8
require 'spec_helper'

describe Mail::SenderField do
  # sender          =       "Sender:" mailbox CRLF
  # 
  
  describe "initialization" do

    it "should initialize" do
      doing { Mail::SenderField.new("Sender: Mikel") }.should_not raise_error
    end

    it "should mix in the CommonAddress module" do
      Mail::SenderField.included_modules.should include(Mail::CommonAddress) 
    end

    it "should accept a string with the field name" do
      t = Mail::SenderField.new('Sender: Mikel Lindsaar <mikel@test.lindsaar.net>')
      t.name.should eq 'Sender'
      t.value.should eq 'Mikel Lindsaar <mikel@test.lindsaar.net>'
    end

    it "should accept a string without the field name" do
      t = Mail::SenderField.new('Mikel Lindsaar <mikel@test.lindsaar.net>')
      t.name.should eq 'Sender'
      t.value.should eq 'Mikel Lindsaar <mikel@test.lindsaar.net>'
    end

    it "should reject headers with multiple mailboxes" do
      pending 'Sender accepts an address list now, but should only accept a single address'
      doing { Mail::SenderField.new('Sender: Mikel Lindsaar <mikel@test.lindsaar.net>, "Bob Smith" <bob@me.com>') }.should raise_error
    end

  end
  
  # Actual testing of CommonAddress methods oSenderurs in the address field spec file

  describe "instance methods" do
    it "should return an address" do
      t = Mail::SenderField.new('Mikel Lindsaar <mikel@test.lindsaar.net>')
      t.formatted.should eq ['Mikel Lindsaar <mikel@test.lindsaar.net>']
    end

    it "should return two addresses" do
      t = Mail::SenderField.new('Mikel Lindsaar <mikel@test.lindsaar.net>')
      t.address.to_s.should eq 'Mikel Lindsaar <mikel@test.lindsaar.net>'
    end
    
    it "should return the formatted line on to_s" do
      t = Mail::SenderField.new('Mikel Lindsaar <mikel@test.lindsaar.net>')
      t.value.should eq 'Mikel Lindsaar <mikel@test.lindsaar.net>'
    end
    
    it "should return the encoded line" do
      t = Mail::SenderField.new('Mikel Lindsaar <mikel@test.lindsaar.net>')
      t.encoded.should eq "Sender: Mikel Lindsaar <mikel@test.lindsaar.net>\r\n"
    end
    
  end
  
  
end
