def __DIR__; File.dirname( File.expand_path( __FILE__ ) ); end

$:.unshift(__DIR__ + '/..')
$:.unshift(__DIR__ + '/../../../../config/')


require 'environment'
require 'test/unit'
require 'init'

if File.directory?("#{RAILS_ROOT}/vendor/rails")
  require "#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/test_process"
end

ActionController::Base.template_root = __DIR__ + '/views/'

class StringLayoutController < ActionController::Base
  skip_before_filter :set_current_user
  skip_before_filter :login_required

  layout 'boston'

  def nature
  end

  def brahma
  end

  def threnody
    @what_else = 'desire'
    render :template => 'string_layout/threnody'
  end

  def transcendentalist
    render :string_layout => 'The idealist, in speaking of events, sees them as spirits.'
  end

  def transcendentalist_with_no_template_on_disk
    render :inline => 'Waldo', :string_layout => 'Shall we say, then, that Transcendentalism is the Saturnalia or excess of Faith;'
  end

  def english_traits
    render :inline => 'Each keeps its own round.'
  end

  def representative_men
    @prelude = render_to_string( :inline => 'It is natural to believe in great men.' )
  end
  
  def representative_men_no_string_layout
    @prelude = render_to_string( :inline => 'It is natural to believe in great men.', :string_layout => false )
    render :action => :representative_men, :string_layout => false
  end
end



class StringLayoutTest < Test::Unit::TestCase
  def test_default_string_layout_class
    s = StringLayout.new(:layout => 'Epictetus')
    assert !s.nil?
    assert_equal 'Epictetus', s.layout
    
    s = StringLayout.new('Epicurus')
    assert !s.nil?
    assert_equal 'Epicurus', s.layout
    
    s = StringLayout.new( :something => 'Erigena' )
    assert !s.nil?
    assert s.layout.nil?
  end
end



class EnvironmentTest < Test::Unit::TestCase
  def test_environment
    assert StringLayoutController.respond_to?( :string_layout )

    expected_rails = '1.1.6'
    assert_equal expected_rails, Rails::VERSION::STRING, "\n\nWhoops!\nThis version of string_layout expects Rails #{expected_rails}, but encountered #{Rails::VERSION::STRING}.\n\n"
  end
end



class TheGoodsTest < Test::Unit::TestCase
  def setup
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    StringLayoutController.class_eval do
      string_layout nil
    end

    @controller = StringLayoutController.new
  end



  def test_standard_layout_on_disk
    get :nature
    # this uses the old-school, file-based layout
    assert_equal 'Ralph Waldo', @response.body

    @controller.class.class_eval do
      # change to new-school, string-based layout
      string_layout '<%= @content_for_layout %> Emerson'
    end
    
    get :nature
    assert_equal 'Ralph Waldo Emerson', @response.body
  end



  def test_render_template
    get :threnody
    assert_equal 'The south-wind brings
Life, sunshine, and desire,', @response.body

    @controller.class.class_eval do
      string_layout '<%= @content_for_layout %>
And on every mount and meadow
Breathes aromatic fire,'
    end
    
    get :threnody
    assert_equal 'The south-wind brings
Life, sunshine, and desire,
And on every mount and meadow
Breathes aromatic fire,', @response.body
  end



  def test_render_string_layout
    #standard layout
    get :transcendentalist
    assert_equal 'The idealist, in speaking of events, sees them as spirits.', @response.body

    get :transcendentalist_with_no_template_on_disk
    assert_equal 'Shall we say, then, that Transcendentalism is the Saturnalia or excess of Faith;', @response.body

    #string_layout
    @controller.class.class_eval do
      string_layout 'These two states of thought diverge every moment, and stand in wild contrast.'
    end

    get :transcendentalist
    assert_equal 'The idealist, in speaking of events, sees them as spirits.', @response.body

    get :transcendentalist_with_no_template_on_disk
    assert_equal 'Shall we say, then, that Transcendentalism is the Saturnalia or excess of Faith;', @response.body

    #nil string_layout
    @controller.class.class_eval do
      string_layout nil
    end

    get :transcendentalist
    assert_equal 'The idealist, in speaking of events, sees them as spirits.', @response.body

    get :transcendentalist_with_no_template_on_disk
    assert_equal 'Shall we say, then, that Transcendentalism is the Saturnalia or excess of Faith;', @response.body
  end



  def test_render_inline_with_string_layout_but_no_layout_or_template_on_disk
    #string_layout
    @controller.class.class_eval do
      string_layout 'He liked the huge machine.  <%= @content_for_layout %>'
    end

    get :english_traits
    assert_equal 'He liked the huge machine.  Each keeps its own round.', @response.body
  end



  def test_render_to_string
    get :representative_men
    assert_equal 'It is natural to believe in great men.  If the companions of our childhood should turn out to be heroes, and their condition regal it would not surprise us.', @response.body

    @controller.class.class_eval do
      string_layout 'All mythology opens with demigods, and the circumstance is high and poetic; that is, their genius is paramount.'
    end

    get :representative_men
    assert_equal 'All mythology opens with demigods, and the circumstance is high and poetic; that is, their genius is paramount.', @response.body

    get :representative_men_no_string_layout
    assert_equal 'It is natural to believe in great men.  If the companions of our childhood should turn out to be heroes, and their condition regal it would not surprise us.', @response.body

  end



  def test_using_a_proc
    @controller.class.class_eval do
      string_layout proc { |controller| 'In this refulgent summer, it has been a luxury to draw the breath of life.' }
    end

    get :nature
    assert_equal 'In this refulgent summer, it has been a luxury to draw the breath of life.', @response.body

    @controller.class.class_eval do
      string_layout proc { |controller| StringLayout.new( :layout => 'The grass grows, the buds burst, the meadow is spotted with fire and gold in the tint of flowers.' ) }
    end
    
    get :nature
    assert_equal 'The grass grows, the buds burst, the meadow is spotted with fire and gold in the tint of flowers.', @response.body
  end



  def test_using_a_symbol
    @controller.instance_eval do
      def uriel_string
        'It fell in the ancient periods
Which the brooding soul surveys,'
      end
      def uriel_responds_to_layout
        StringLayout.new :layout => 'Or ever the wild Time coin\'d itself 
Into calendar months and days.'
      end
    end

    @controller.class.class_eval do
      string_layout :uriel_string
    end

    get :nature
    assert_equal 'It fell in the ancient periods
Which the brooding soul surveys,', @response.body

    @controller.class.class_eval do
      string_layout :uriel_responds_to_layout
    end
    
    get :nature
    assert_equal 'Or ever the wild Time coin\'d itself 
Into calendar months and days.', @response.body
  end




  def test_except
    @controller.class.class_eval do
      string_layout 'The strong gods pine for my abode,', :except => :brahma
    end

    get :nature
    assert_equal 'The strong gods pine for my abode,', @response.body

    get :brahma
    assert_equal 'They reckon ill who leave me out;
When me they fly, I am the wings;
I am the doubter and the doubt,
And I the hymn the Brahmin sings.
', @response.body
  end




  def test_only
    @controller.class.class_eval do
      string_layout 'And pine in vain the sacred Seven;', :only => :brahma
    end

#    get :nature
#    assert_equal 'Ralph Waldo', @response.body

    get :brahma
    assert_equal 'And pine in vain the sacred Seven;', @response.body
  end
end

