require 'test/unit'
require 'criteria/file'
include Criteria


class TestFile < Test::Unit::TestCase
  def test_file
    tab=query=actual=nil

    assert_nothing_raised {
      tab = FileTable.new("t/file.dat",
                          :name,
                          [:dmg, Integer],
                          [:acc, Integer],
                          [:def, Integer],
                          :type,
                          :elem)

      # Split on at least two spaces/tabs
      tab.split = /[\s\t][\s\t]+/

      # Skip the header
      tab.skip  = 2
    }

    assert_nothing_raised {
      query          = (tab.elem == "Water")
      query.order_by = :name
      actual         = query.select(:name)
    }

    expected = [["Water Axe"], ["Water Sword"]]
    assert_equal(expected, actual)

    assert_nothing_raised {
      query          = (tab.type == "club")
      query.order_by = :dmg
      query.order    = :desc
      actual         = query.select(:name, :dmg)
    }

    expected = [
      ["Club",  2],
      ["Stick", 1]
    ]

    assert_equal(expected, actual)

    assert_nothing_raised {
      query          = (tab.name)
      query.limit    = 1
      query.offset   = 5
      actual         = query.select(:name)
    }

    expected = [["Wind Sword"]]
    assert_equal(expected, actual)
  end
end
