class TestParse

Public Instance Methods

setup() click to toggle source
# File test/test_parse.rb, line 9
def setup
  @parser = Class.new(CSVobj)
end
teardown() click to toggle source
# File test/test_parse.rb, line 13
def teardown
  @parser = nil
end
test_array() click to toggle source
# File test/test_parse.rb, line 101
def test_array
  a = [
    [ 'foo', 'bar', 'baz' ],
    [ '1', '2', '3' ],
    [ '4', '5', '6' ]
  ]
  objs = @parser.parse(a)
  assert_equal(2, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_equal('3', objs[0].baz)
  assert_equal('4', objs[1].foo)
  assert_equal('5', objs[1].bar)
  assert_equal('6', objs[1].baz)
end
test_duplicate_header() click to toggle source
# File test/test_parse.rb, line 17
def test_duplicate_header
  s = "foo,bar,bar,baz\n1,2,3,4\n"
  assert_raise CSVobjDuplicateHeader do
    @parser.parse(s)
  end
end
test_empty() click to toggle source
# File test/test_parse.rb, line 96
def test_empty
  objs = @parser.parse('')
  assert_equal([], objs)
end
test_missing_data() click to toggle source
# File test/test_parse.rb, line 90
def test_missing_data
  s = "foo,bar,baz\n"
  objs = @parser.parse(s)
  assert_equal([], objs)
end
test_missing_data_cell() click to toggle source
# File test/test_parse.rb, line 67
def test_missing_data_cell
  s = "foo,bar,baz\n1,2\n4,5,6\n"
  objs = @parser.parse(s)
  assert_equal(2, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_equal(nil, objs[0].baz)
  assert_equal('4', objs[1].foo)
  assert_equal('5', objs[1].bar)
  assert_equal('6', objs[1].baz)
end
test_missing_header_cell() click to toggle source
# File test/test_parse.rb, line 79
def test_missing_header_cell
  s = "foo,bar\n1,2,3\n"
  objs = @parser.parse(s)
  assert_equal(1, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_raise NoMethodError do
    objs[0].baz
  end
end
test_no_trailing_newline() click to toggle source
# File test/test_parse.rb, line 58
def test_no_trailing_newline
  s = "foo,bar,baz\n1,2,3"
  objs = @parser.parse(s)
  assert_equal(1, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_equal('3', objs[0].baz)
end
test_one_obj_from_string() click to toggle source
# File test/test_parse.rb, line 24
def test_one_obj_from_string
  s = "foo,bar,baz\n1,2,3\n"
  objs = @parser.parse(s)
  assert_equal(1, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_equal('3', objs[0].baz)
end
test_two_objs_from_file() click to toggle source
# File test/test_parse.rb, line 45
def test_two_objs_from_file
  s = "foo,bar,baz\n1,2,3\n4,5,6\n"
  fileish = StringIO.new(s)
  objs = @parser.parse(fileish)
  assert_equal(2, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_equal('3', objs[0].baz)
  assert_equal('4', objs[1].foo)
  assert_equal('5', objs[1].bar)
  assert_equal('6', objs[1].baz)
end
test_two_objs_from_string() click to toggle source
# File test/test_parse.rb, line 33
def test_two_objs_from_string
  s = "foo,bar,baz\n1,2,3\n4,5,6\n"
  objs = @parser.parse(s)
  assert_equal(2, objs.size)
  assert_equal('1', objs[0].foo)
  assert_equal('2', objs[0].bar)
  assert_equal('3', objs[0].baz)
  assert_equal('4', objs[1].foo)
  assert_equal('5', objs[1].bar)
  assert_equal('6', objs[1].baz)
end