require 'fish_history_repair'
require 'test/unit'

class TestFishHistoryElement < Test::Unit::TestCase
  def test_to_s
    a = FishHistoryElement.new( 1, '' )
    assert_equal( "# 1\n\n", a.to_s )
    
    b = FishHistoryElement.new( 1, '.' )
    assert_equal( "# 1\n.\n", b.to_s )

    c = FishHistoryElement.new( 1, ".\n" )
    assert_equal( "# 1\n.\n", c.to_s )

    d = FishHistoryElement.new( 1, ".\\\n..\\\n..." )
    assert_equal( "# 1\n.\\\n..\\\n...\n", d.to_s )

    e = FishHistoryElement.new( 1, ".\\\n..\\\n...\n" )
    assert_equal( "# 1\n.\\\n..\\\n...\n", e.to_s )
  end
end

class TestFishHistory < Test::Unit::TestCase
  def test__parse_str
    empty_str = ''
    assert_equal( [], FishHistory.parse_str( empty_str ) )

    simple_command = "# 12345\n" +
      'a command'
    hist_list = FishHistory.parse_str( simple_command )
    assert_equal( 1, hist_list.length )
    assert_equal( 12345, hist_list[0].epoch )
    assert_equal( 'a command', hist_list[0].cmd )
    
    split_command = "# 12346\n" +
      "a split command\\\n" +
      "the second\\\n" +
      "and third line"
    hist_list = FishHistory.parse_str( split_command )
    assert_equal( 1, hist_list.length )
    assert_equal( 12346, hist_list[0].epoch )
    assert_equal( split_command.split("\n")[1..-1].join("\n"), hist_list[0].cmd )

    combined_command = simple_command + "\n" + split_command + "\n"
    hist_list = FishHistory.parse_str( combined_command )
    assert_equal( 2, hist_list.length )
    assert_equal( 12345, hist_list[0].epoch )
    assert_equal( 'a command', hist_list[0].cmd )
    assert_equal( 12346, hist_list[1].epoch )
    assert_equal( split_command.split("\n")[1..-1].join("\n"), hist_list[1].cmd )

    command_with_hash = "#12347\n" +
      "# a comment\\\n" +
      "ls"
    hist_list = FishHistory.parse_str( command_with_hash )
    assert_equal( 1, hist_list.length )
    assert_equal( 12347, hist_list[0].epoch )
    assert_equal( "# a comment\\\nls", hist_list[0].cmd )
  end
  def test__merge
    a1 = FishHistoryElement.new( 1, 'a' )
    a2 = FishHistoryElement.new( 2, 'a' )
    b3 = FishHistoryElement.new( 3, 'b' )

    hist_list = FishHistory.merge( [], [] )
    assert_equal( [], hist_list )

    hist_list = FishHistory.merge( [a1], [] )
    assert_equal( [a1], hist_list )
    
    hist_list = FishHistory.merge( [], [a1] )
    assert_equal( [a1], hist_list )
    
    hist_list = FishHistory.merge( [a1], [a2] )
    assert_equal( [a2], hist_list )

    hist_list = FishHistory.merge( [b3,a1], [a2] )
    assert_equal( [a2, b3], hist_list )
  end
end
