1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
|
require 'runit/testcase'
require 'runit/cui/testrunner'
require 'tempfile'
require '../lib/csv.rb'
class CSV
class StreamBuf
# Let buffer work hard.
BufSize = 2
end
end
class TestCSV < RUNIT::TestCase
class << self
def d( data, isNull = false )
CSV::Cell.new( data.to_s, isNull )
end
end
@@colData = [ '', nil, true, false, 'foo', '!' * 1000 ]
@@simpleCSVData = {
[ nil ] => '',
[ '' ] => '""',
[ nil, nil ] => ',',
[ nil, nil, nil ] => ',,',
[ 'foo' ] => 'foo',
[ ',' ] => '","',
[ ',', ',' ] => '",",","',
[ ';' ] => ';',
[ ';', ';' ] => ';,;',
[ "\"\r", "\"\r" ] => "\"\"\"\r\",\"\"\"\r\"",
[ "\"\n", "\"\n" ] => "\"\"\"\n\",\"\"\"\n\"",
[ "\t" ] => "\t",
[ "\t", "\t" ] => "\t,\t",
[ 'foo', 'bar' ] => 'foo,bar',
[ 'foo', '"bar"', 'baz' ] => 'foo,"""bar""",baz',
[ 'foo', 'foo,bar', 'baz' ] => 'foo,"foo,bar",baz',
[ 'foo', '""', 'baz' ] => 'foo,"""""",baz',
[ 'foo', '', 'baz' ] => 'foo,"",baz',
[ 'foo', nil, 'baz' ] => 'foo,,baz',
[ nil, 'foo', 'bar' ] => ',foo,bar',
[ 'foo', 'bar', nil ] => 'foo,bar,',
[ 'foo', "\r", 'baz' ] => "foo,\"\r\",baz",
[ 'foo', "\n", 'baz' ] => "foo,\"\n\",baz",
[ 'foo', "\r\n\r", 'baz' ] => "foo,\"\r\n\r\",baz",
[ 'foo', "\r\n", 'baz' ] => "foo,\"\r\n\",baz",
[ 'foo', "\r.\n", 'baz' ] => "foo,\"\r.\n\",baz",
[ 'foo', "\r\n\n", 'baz' ] => "foo,\"\r\n\n\",baz",
[ 'foo', '"', 'baz' ] => 'foo,"""",baz',
}
@@fullCSVData = {
[ d( '', true ) ] => '',
[ d( '' ) ] => '""',
[ d( '', true ), d( '', true ) ] => ',',
[ d( '', true ), d( '', true ), d( '', true ) ] => ',,',
[ d( 'foo' ) ] => 'foo',
[ d( 'foo' ), d( 'bar' ) ] => 'foo,bar',
[ d( 'foo' ), d( '"bar"' ), d( 'baz' ) ] => 'foo,"""bar""",baz',
[ d( 'foo' ), d( 'foo,bar' ), d( 'baz' ) ] => 'foo,"foo,bar",baz',
[ d( 'foo' ), d( '""' ), d( 'baz' ) ] => 'foo,"""""",baz',
[ d( 'foo' ), d( '' ), d( 'baz' ) ] => 'foo,"",baz',
[ d( 'foo' ), d( '', true ), d( 'baz' ) ] => 'foo,,baz',
[ d( 'foo' ), d( "\r" ), d( 'baz' ) ] => "foo,\"\r\",baz",
[ d( 'foo' ), d( "\n" ), d( 'baz' ) ] => "foo,\"\n\",baz",
[ d( 'foo' ), d( "\r\n" ), d( 'baz' ) ] => "foo,\"\r\n\",baz",
[ d( 'foo' ), d( "\r.\n" ), d( 'baz' ) ] => "foo,\"\r.\n\",baz",
[ d( 'foo' ), d( "\r\n\n" ), d( 'baz' ) ] => "foo,\"\r\n\n\",baz",
[ d( 'foo' ), d( '"' ), d( 'baz' ) ] => 'foo,"""",baz',
}
@@tmpdir = 'tmp'
@@infile = File.join( @@tmpdir, 'in.csv' )
@@infiletsv = File.join( @@tmpdir, 'in.tsv' )
@@emptyfile = File.join( @@tmpdir, 'empty.csv' )
@@outfile = File.join( @@tmpdir, 'out.csv' )
@@fullCSVDataArray = @@fullCSVData.collect { | key, value | key }
CSV.open( @@infile, "w" ) do | writer |
@@fullCSVDataArray.each do | row |
writer.addRow( row )
end
end
CSV.open( @@infiletsv, "w", ?\t ) do | writer |
@@fullCSVDataArray.each do | row |
writer.addRow( row )
end
end
def ssv2csv( ssvStr )
sepConv( ssvStr, ?;, ?, )
end
def csv2ssv( csvStr )
sepConv( csvStr, ?,, ?; )
end
def tsv2csv( tsvStr )
sepConv( tsvStr, ?\t, ?, )
end
def csv2tsv( csvStr )
sepConv( csvStr, ?,, ?\t )
end
def sepConv( srcStr, srcSep, destSep )
rows = CSV::Row.new
cols, idx = CSV.parseLine( srcStr, 0, rows, srcSep )
destStr = ''
cols = CSV.createLine( rows, rows.size, destStr, destSep )
destStr
end
public
def setup
# Nothing to do.
end
def teardown
# Nothing to do.
end
def d( *arg )
TestCSV.d( *arg )
end
#### CSV::Cell unit test
def test_Cell_EQUAL # '=='
d1 = CSV::Cell.new( 'd', false )
d2 = CSV::Cell.new( 'd', false )
d3 = CSV::Cell.new( 'd', true )
d4 = CSV::Cell.new( 'd', true )
assert( d1 == d2, "Normal case." )
assert( d1 != d3, "RHS is null." )
assert( d4 != d1, "LHS is null." )
assert( d3 != d4, "Either is null." )
end
def test_Cell_match
d1 = CSV::Cell.new( 'd', false )
d2 = CSV::Cell.new( 'd', false )
d3 = CSV::Cell.new( 'd', true )
d4 = CSV::Cell.new( 'd', true )
assert( d1.match( d2 ), "Normal case." )
assert( !d1.match( d3 ), "RHS is null." )
assert( !d4.match( d1 ), "LHS is null." )
assert( d3.match( d4 ), "Either is null." )
end
def test_Cell_data
d = CSV::Cell.new()
@@colData.each do | v |
d.data = v
assert_equal( d.data, v, "Case: #{ v }." )
end
end
def test_Cell_data=
d = CSV::Cell.new()
@@colData.each do | v |
d.data = v
assert_equal( d.data, v, "Case: #{ v }." )
end
end
def test_Cell_isNull
d = CSV::Cell.new()
d.isNull = true
assert_equal( d.isNull, true, "Case: true." )
d.isNull = false
assert_equal( d.isNull, false, "Case: false." )
end
def test_Cell_isNull=
d = CSV::Cell.new()
d.isNull = true
assert_equal( d.isNull, true, "Case: true." )
d.isNull = false
assert_equal( d.isNull, false, "Case: false." )
end
def test_Cell_s_new
d1 = CSV::Cell.new()
assert_equal( d1.data, '', "Default: data." )
assert_equal( d1.isNull, true, "Default: isNull." )
@@colData.each do | v |
d = CSV::Cell.new( v )
assert_equal( d.data, v, "Data: #{ v }." )
end
d2 = CSV::Cell.new( nil, true )
assert_equal( d2.isNull, true, "Data: true." )
d3 = CSV::Cell.new( nil, false )
assert_equal( d3.isNull, false, "Data: false." )
end
#### CSV::Row unit test
def test_Row_s_match
c1 = CSV::Row[ d( 1 ), d( 2 ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( 2, false ), d( 3, false ) ]
assert( c1.match( c2 ), "Normal case." )
c1 = CSV::Row[ d( 1 ), d( 'foo', true ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( 'bar', true ), d( 3, false ) ]
assert( c1.match( c2 ), "Either is null." )
c1 = CSV::Row[ d( 1 ), d( 'foo', true ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( 'bar', false ), d( 3, false ) ]
assert( !c1.match( c2 ), "LHS is null." )
c1 = CSV::Row[ d( 1 ), d( 'foo' ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( 'bar', true ), d( 3, false ) ]
assert( !c1.match( c2 ), "RHS is null." )
c1 = CSV::Row[ d( 1 ), d( '', true ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( '', true ), d( 3, false ) ]
assert( c1.match( c2 ), "Either is null(empty data)." )
c1 = CSV::Row[ d( 1 ), d( '', true ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( '', false ), d( 3, false ) ]
assert( !c1.match( c2 ), "LHS is null(empty data)." )
c1 = CSV::Row[ d( 1 ), d( '' ), d( 3 ) ]
c2 = CSV::Row[ d( 1, false ), d( '', true ), d( 3, false ) ]
assert( !c1.match( c2 ), "RHS is null(empty data)." )
c1 = CSV::Row[]
c2 = CSV::Row[]
assert( c1.match( c2 ))
c1 = CSV::Row[]
c2 = CSV::Row[ d( 1 ) ]
assert( !c1.match( c2 ))
end
def test_Row_to_a
r = CSV::Row[ d( 1 ), d( 2 ), d( 3 ) ]
assert_equal( [ '1', '2', '3' ], r.to_a, 'Normal case' )
r = CSV::Row[ d( 1 ) ]
assert_equal( [ '1' ], r.to_a, '1 item' )
r = CSV::Row[ d( nil, true ), d( 2 ), d( 3 ) ]
assert_equal( [ nil, '2', '3' ], r.to_a, 'Null in data' )
r = CSV::Row[ d( nil, true ), d( nil, true ), d( nil, true ) ]
assert_equal( [ nil, nil, nil ], r.to_a, 'Nulls' )
r = CSV::Row[ d( nil, true ) ]
assert_equal( [ nil ], r.to_a, '1 Null' )
r = CSV::Row[]
assert_equal( [], r.to_a, 'Empty' )
end
#### CSV::Reader unit test
def test_Reader_each
file = File.open( @@infile, "rb" )
begin
reader = CSV::Reader.create( file )
expectedArray = @@fullCSVDataArray.dup
first = true
ret = reader.each { | row |
if first
assert_instance_of( CSV::Row, row )
first = false
end
expected = expectedArray.shift
assert( row.match( expected ))
}
assert_nil( ret, "Return is nil" )
assert( expectedArray.empty? )
ensure
file.close
end
# Illegal format.
reader = CSV::Reader.create( "a,b\r\na,b,\"c\"\ra" )
assert_exception( CSV::IllegalFormatError ) do
reader.each do | row |
end
end
reader = CSV::Reader.create( "a,b\r\n\"" )
assert_exception( CSV::IllegalFormatError ) do
reader.each do | row |
end
end
end
def test_Reader_shift
file = File.open( @@infile, "rb" )
begin
reader = CSV::Reader.create( file )
first = true
checked = 0
@@fullCSVDataArray.each do | expected |
actual = reader.shift
if first
assert_instance_of( CSV::Row, actual )
first = false
end
assert( actual.match( expected ))
checked += 1
end
assert( checked == @@fullCSVDataArray.size )
ensure
file.close
end
# Illegal format.
reader = CSV::Reader.create( "a,b\r\na,b,\"c\"\ra" )
assert_exception( CSV::IllegalFormatError ) do
reader.shift
reader.shift
end
reader = CSV::Reader.create( "a,b\r\na,b,\"c\"\ra" )
assert_exception( CSV::IllegalFormatError ) do
reader.shift
reader.shift
end
end
def test_Reader_getRow
if CSV::Reader.respond_to?( :allocate )
obj = CSV::Reader.allocate
assert_exception( NotImplementedError ) do
row = []
obj.shift
end
end
end
def test_IOReader_closeOnTerminate
f = File.open( @@infile, "r" )
reader = CSV::IOReader.create( f )
reader.close
assert( !f.closed? )
f.close
f = File.open( @@infile, "r" )
writer = CSV::IOReader.create( f )
writer.closeOnTerminate
writer.close
assert( f.closed? )
end
def test_Reader_close
f = File.open( @@infile, "r" )
reader = CSV::IOReader.create( f )
reader.closeOnTerminate
reader.close
assert( f.closed? )
end
def test_Reader_s_new
assert_exception( RuntimeError ) do
CSV::Reader.new( nil )
end
end
def test_Reader_s_create
reader = CSV::Reader.create( "abc" )
assert_instance_of( CSV::StringReader, reader, "With a String" )
reader = CSV::Reader.create( File.open( @@infile, "rb" ))
assert_instance_of( CSV::IOReader, reader, 'With an IO' )
obj = Object.new
def obj.sysread( size )
"abc"
end
reader = CSV::Reader.create( obj )
assert_instance_of( CSV::IOReader, reader, "With not an IO or String" )
# No need to test Tempfile because it's a pseudo IO. I test this here
# fors other tests.
reader = CSV::Reader.create( Tempfile.new( "in.csv" ))
assert_instance_of( CSV::IOReader, reader, "With an pseudo IO." )
end
def test_Reader_s_parse
ret = CSV::Reader.parse( "a,b,c" ) { | row |
assert_instance_of( CSV::Row, row, "Block parameter" )
}
assert_nil( ret, "Return is nil" )
ret = CSV::Reader.parse( "a;b;c", ?; ) { | row |
assert_instance_of( CSV::Row, row, "Block parameter" )
}
file = Tempfile.new( "in.csv" )
file << "a,b,c"
file.open
ret = CSV::Reader.parse( file ) { | row |
assert_instance_of( CSV::Row, row, "Block parameter" )
}
assert_nil( ret, "Return is nil" )
file = Tempfile.new( "in.csv" )
file << "a,b,c"
file.open
ret = CSV::Reader.parse( file, ?, ) { | row |
assert_instance_of( CSV::Row, row, "Block parameter" )
}
# Illegal format.
assert_exception( CSV::IllegalFormatError ) do
CSV::Reader.parse( "a,b\r\na,b,\"c\"\ra" ) do | row |
end
end
assert_exception( CSV::IllegalFormatError ) do
CSV::Reader.parse( "a,b\r\na,b\"" ) do | row |
end
end
end
#### CSV::Writer unit test
def test_Writer_s_new
assert_exception( RuntimeError ) do
CSV::Writer.new( nil )
end
end
def test_Writer_s_generate
ret = CSV::Writer.generate( STDOUT ) { | writer |
assert_instance_of( CSV::BasicWriter, writer, "Block parameter" )
}
ret = CSV::Writer.generate( STDOUT, ?; ) { | writer |
assert_instance_of( CSV::BasicWriter, writer, "Block parameter" )
}
assert_nil( ret, "Return is nil" )
end
def test_Writer_s_create
writer = CSV::Writer.create( STDERR )
assert_instance_of( CSV::BasicWriter, writer, "String" )
writer = CSV::Writer.create( STDERR, ?; )
assert_instance_of( CSV::BasicWriter, writer, "String" )
writer = CSV::Writer.create( Tempfile.new( "out.csv" ))
assert_instance_of( CSV::BasicWriter, writer, "IO" )
end
def test_Writer_LSHIFT # '<<'
file = Tempfile.new( "out.csv" )
CSV::Writer.generate( file ) do | writer |
ret = writer << [ 'a', 'b', 'c' ]
assert_instance_of( CSV::BasicWriter, ret, 'Return is self' )
writer << [ nil, 'e', 'f' ] << [ nil, nil, '' ]
end
str = file.open.read
assert_equal( "a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal' )
end
def test_Writer_addRow
file = Tempfile.new( "out.csv" )
CSV::Writer.generate( file ) do | writer |
ret = writer.addRow(
[ d( 'a', false ), d( 'b', false ), d( 'c', false ) ] )
assert_instance_of( CSV::BasicWriter, ret, 'Return is self' )
writer.addRow(
[ d( 'dummy', true ), d( 'e', false ), d( 'f', false ) ]
).addRow(
[ d( 'a', true ), d( 'b', true ), d( '', false ) ]
)
end
str = file.open.read
assert_equal( "a,b,c\r\n,e,f\r\n,,\"\"\r\n", str, 'Normal' )
end
def test_Writer_close
f = File.open( @@outfile, "w" )
writer = CSV::BasicWriter.create( f )
writer.closeOnTerminate
writer.close
assert( f.closed? )
end
def test_BasicWriter_closeOnTerminate
f = File.open( @@outfile, "w" )
writer = CSV::BasicWriter.create( f )
writer.close
assert( !f.closed? )
f = File.open( @@outfile, "w" )
writer = CSV::BasicWriter.create( f )
writer.closeOnTerminate
writer.close
assert( f.closed? )
end
#### CSV unit test
def test_s_open_reader
assert_exception( ArgumentError, 'Illegal mode' ) do
CSV.open( "temp", "a" )
end
assert_exception( ArgumentError, 'Illegal mode' ) do
CSV.open( "temp", "a", ?; )
end
# Reader side.
reader = CSV.open( @@infile, "r" )
assert_instance_of( CSV::IOReader, reader )
reader.close
reader = CSV.open( @@infile, "rb" )
assert_instance_of( CSV::IOReader, reader )
reader.close
reader = CSV.open( @@infile, "r", ?; )
assert_instance_of( CSV::IOReader, reader )
reader.close
CSV.open( @@infile, "r" ) do | row |
assert_instance_of( CSV::Row, row )
break
end
CSV.open( @@infiletsv, "r", ?\t ) do | row |
assert_instance_of( CSV::Row, row )
break
end
assert_exception( Errno::ENOENT ) do
CSV.open( "NoSuchFileOrDirectory", "r" )
end
assert_exception( Errno::ENOENT ) do
CSV.open( "NoSuchFileOrDirectory", "r", ?; )
end
# Illegal format.
File.open( @@outfile, "w" ) do | f |
f << "a,b\r\na,b,\"c\"\ra"
end
assert_exception( CSV::IllegalFormatError ) do
CSV.open( @@outfile, "r" ) do | row |
end
end
File.open( @@outfile, "w") do | f |
f << "a,b\r\na,b\""
end
assert_exception( CSV::IllegalFormatError ) do
CSV.open( @@outfile, "r" ) do | row |
end
end
end
def test_s_open_writer
writer = CSV.open( @@outfile, "w" )
assert_instance_of( CSV::BasicWriter, writer )
writer.close
writer = CSV.open( @@outfile, "wb" )
assert_instance_of( CSV::BasicWriter, writer )
writer.close
writer = CSV.open( @@outfile, "wb", ?; )
assert_instance_of( CSV::BasicWriter, writer )
writer.close
CSV.open( @@outfile, "w" ) do | writer |
assert_instance_of( CSV::BasicWriter, writer )
end
CSV.open( @@outfile, "w", ?; ) do | writer |
assert_instance_of( CSV::BasicWriter, writer )
end
begin
CSV.open( @@tmpdir, "w" )
assert( false )
rescue Exception => ex
assert( ex.is_a?( Errno::EEXIST ) || ex.is_a?( Errno::EISDIR ))
end
# Empty file.
CSV.open( @@emptyfile, "w" ) do | writer |
# Create empty file.
end
CSV.open( @@emptyfile, "r" ) do | row |
assert_fail( "Must not reach here" )
end
end
def test_s_create
str = CSV.create( [] )
assert_equal( '', str, "Extra boundary check." )
str = CSV.create( [], ?; )
assert_equal( '', str, "Extra boundary check." )
@@simpleCSVData.each do | col, str |
buf = CSV.create( col )
assert_equal( str, buf )
end
@@simpleCSVData.each do | col, str |
buf = CSV.create( col, ?; )
assert_equal( str + "\r\n", ssv2csv( buf ))
end
@@simpleCSVData.each do | col, str |
buf = CSV.create( col, ?\t )
assert_equal( str + "\r\n", tsv2csv( buf ))
end
end
def test_s_createLine
buf = ''
cols = CSV.createLine( [], 0, buf )
assert_equal( 0, cols )
assert_equal( "\r\n", buf, "Extra boundary check." )
buf = ''
cols = CSV.createLine( [], 0, buf, ?; )
assert_equal( 0, cols )
assert_equal( "\r\n", buf, "Extra boundary check." )
buf = ''
cols = CSV.createLine( [], 0, buf, ?\t )
assert_equal( 0, cols )
assert_equal( "\r\n", buf, "Extra boundary check." )
buf = ''
cols = CSV.createLine( [ d( 1 ) ], 2, buf )
assert_equal( '1,', buf )
buf = ''
cols = CSV.createLine( [ d( 1 ) ], 2, buf, ?; )
assert_equal( '1;', buf )
buf = ''
cols = CSV.createLine( [ d( 1 ) ], 2, buf, ?\t )
assert_equal( "1\t", buf )
buf = ''
cols = CSV.createLine( [ d( 1 ), d( 2 ) ], 1, buf )
assert_equal( "1\r\n", buf )
buf = ''
cols = CSV.createLine( [ d( 1 ), d( 2 ) ], 1, buf, ?; )
assert_equal( "1\r\n", buf )
buf = ''
cols = CSV.createLine( [ d( 1 ), d( 2 ) ], 1, buf, ?\t )
assert_equal( "1\r\n", buf )
@@fullCSVData.each do | col, str |
buf = ''
cols = CSV.createLine( col, col.size, buf )
assert_equal( col.size, cols )
assert_equal( str + "\r\n", buf )
end
@@fullCSVData.each do | col, str |
buf = ''
cols = CSV.createLine( col, col.size, buf, ?; )
assert_equal( col.size, cols )
assert_equal( str + "\r\n", ssv2csv( buf ))
end
@@fullCSVData.each do | col, str |
buf = ''
cols = CSV.createLine( col, col.size, buf, ?\t )
assert_equal( col.size, cols )
assert_equal( str + "\r\n", tsv2csv( buf ))
end
buf = ''
toBe = ''
cols = 0
colsToBe = 0
@@fullCSVData.each do | col, str |
cols += CSV.createLine( col, col.size, buf )
toBe << str << "\r\n"
colsToBe += col.size
end
assert_equal( colsToBe, cols )
assert_equal( toBe, buf )
buf = ''
toBe = ''
cols = 0
colsToBe = 0
@@fullCSVData.each do | col, str |
lineBuf = ''
cols += CSV.createLine( col, col.size, lineBuf, ?; )
buf << ssv2csv( lineBuf ) << "\r\n"
toBe << ssv2csv( lineBuf ) << "\r\n"
colsToBe += col.size
end
assert_equal( colsToBe, cols )
assert_equal( toBe, buf )
buf = ''
toBe = ''
cols = 0
colsToBe = 0
@@fullCSVData.each do | col, str |
lineBuf = ''
cols += CSV.createLine( col, col.size, lineBuf, ?\t )
buf << tsv2csv( lineBuf ) << "\r\n"
toBe << tsv2csv( lineBuf ) << "\r\n"
colsToBe += col.size
end
assert_equal( colsToBe, cols )
assert_equal( toBe, buf )
end
def test_s_parse
@@simpleCSVData.each do | col, str |
row = CSV.parse( str )
assert_instance_of( CSV::Row, row )
assert_equal( col.size, row.size )
assert_equal( col, row )
end
@@simpleCSVData.each do | col, str |
str = csv2ssv( str )
row = CSV.parse( str, ?; )
assert_instance_of( CSV::Row, row )
assert_equal( col.size, row.size )
assert_equal( col, row )
end
@@simpleCSVData.each do | col, str |
str = csv2tsv( str )
row = CSV.parse( str, ?\t )
assert_instance_of( CSV::Row, row )
assert_equal( col.size, row.size )
assert_equal( col, row )
end
# Illegal format.
buf = CSV::Row.new
row = CSV.parse( "a,b,\"c\"\ra" )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
buf = CSV::Row.new
row = CSV.parse( "a;b;\"c\"\ra", ?; )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
buf = CSV::Row.new
row = CSV.parse( "a\tb\t\"c\"\ra", ?\t )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "a,b\"" )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "a;b\"", ?; )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "a\tb\"", ?\t )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "\"a,b\"\r," )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "\"a;b\"\r;", ?; )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "\"a\tb\"\r\t", ?\t )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "\"a,b\"\r\"" )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "\"a;b\"\r\"", ?; )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
row = CSV.parse( "\"a\tb\"\r\"", ?\t )
assert_instance_of( CSV::Row, row )
assert_equal( 0, row.size )
end
def test_s_parseLine
@@fullCSVData.each do | col, str |
buf = CSV::Row.new
cols, idx = CSV.parseLine( str + "\r\n", 0, buf )
assert_equal( cols, buf.size, "Reported size." )
assert_equal( col.size, buf.size, "Size." )
assert( buf.match( col ))
buf = CSV::Row.new
cols, idx = CSV.parseLine( str + "\n", 0, buf )
assert_equal( cols, buf.size, "Reported size." )
assert_equal( col.size, buf.size, "Size." )
assert( buf.match( col ))
end
@@fullCSVData.each do | col, str |
str = csv2ssv( str )
buf = CSV::Row.new
cols, idx = CSV.parseLine( str + "\r\n", 0, buf, ?; )
assert_equal( cols, buf.size, "Reported size." )
assert_equal( col.size, buf.size, "Size." )
assert( buf.match( col ))
end
@@fullCSVData.each do | col, str |
str = csv2tsv( str )
buf = CSV::Row.new
cols, idx = CSV.parseLine( str + "\r\n", 0, buf, ?\t )
assert_equal( cols, buf.size, "Reported size." )
assert_equal( col.size, buf.size, "Size." )
assert( buf.match( col ))
end
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a,b,\"c\r\"", 0, buf )
assert_equal( [ "a", "b", "c\r" ], buf.to_a )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a;b;\"c\r\"", 0, buf, ?; )
assert_equal( [ "a", "b", "c\r" ], buf.to_a )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\tb\t\"c\r\"", 0, buf, ?\t )
assert_equal( [ "a", "b", "c\r" ], buf.to_a )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a,b,c\n", 0, buf )
assert_equal( [ "a", "b", "c" ], buf.to_a )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\tb\tc\n", 0, buf, ?\t )
assert_equal( [ "a", "b", "c" ], buf.to_a )
# Illegal format.
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a,b,c\"", 0, buf )
assert_equal( 0, cols, "Illegal format; unbalanced double-quote." )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a;b;c\"", 0, buf, ?; )
assert_equal( 0, cols, "Illegal format; unbalanced double-quote." )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a,b,\"c\"\ra", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a,b,\"c\"\ra", 0, buf, ?; )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a,b\"", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a;b\"", 0, buf, ?; )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "\"a,b\"\r,", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\r,", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\r", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\rbc", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\r\"\"", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "a\r\rabc,", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "\"a;b\"\r;", 0, buf, ?; )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "\"a,b\"\r\"", 0, buf )
assert_equal( 0, cols )
assert_equal( 0, idx )
buf = CSV::Row.new
cols, idx = CSV.parseLine( "\"a;b\"\r\"", 0, buf, ?; )
assert_equal( 0, cols )
assert_equal( 0, idx )
end
def test_s_parseLineEOF
@@fullCSVData.each do | col, str |
if str == ''
# String "" is not allowed.
next
end
buf = CSV::Row.new
cols, idx = CSV.parseLine( str, 0, buf )
assert_equal( col.size, cols, "Reported size." )
assert_equal( col.size, buf.size, "Size." )
assert( buf.match( col ))
end
end
def test_s_parseLineConcat
buf = ''
toBe = []
@@fullCSVData.each do | col, str |
buf << str << "\r\n"
toBe.concat( col )
end
idx = 0
cols = 0
parsed = CSV::Row.new
parsedCols = 0
begin
cols, idx = CSV.parseLine( buf, idx, parsed )
parsedCols += cols
end while cols > 0
assert_equal( toBe.size, parsedCols )
assert_equal( toBe.size, parsed.size )
assert( parsed.match( toBe ))
buf = ''
toBe = []
@@fullCSVData.each do | col, str |
buf << str << "\n"
toBe.concat( col )
end
idx = 0
cols = 0
parsed = CSV::Row.new
parsedCols = 0
begin
cols, idx = CSV.parseLine( buf, idx, parsed )
parsedCols += cols
end while cols > 0
assert_equal( toBe.size, parsedCols )
assert_equal( toBe.size, parsed.size )
assert( parsed.match( toBe ))
buf = ''
toBe = []
@@fullCSVData.sort { |a, b|
a[0].length <=> b[0].length
}.each do | col, str |
buf << str << "\n"
toBe.concat( col )
end
idx = 0
cols = 0
parsed = CSV::Row.new
parsedCols = 0
begin
cols, idx = CSV.parseLine( buf, idx, parsed )
parsedCols += cols
end while cols > 0
assert_equal( toBe.size, parsedCols )
assert_equal( toBe.size, parsed.size )
assert( parsed.match( toBe ))
end
def test_utf8
rows = []
CSV.open("bom.csv", "r") do |row|
rows << row.to_a
end
assert_equal( [["foo"], ["bar"]], rows )
rows = []
CSV::Reader.parse(File.open("bom.csv").read) do | row |
rows << row.to_a
end
assert_equal( [["foo"], ["bar"]], rows )
end
#### CSV unit test
InputStreamPattern = '0123456789'
InputStreamPatternSize = InputStreamPattern.size
def expChar( idx )
InputStreamPattern[ idx % InputStreamPatternSize ]
end
def expStr( idx, n )
if n > InputStreamPatternSize
InputStreamPattern + expStr( 0, n - InputStreamPatternSize )
else
InputStreamPattern[ idx % InputStreamPatternSize, n ]
end
end
def setupInputStream( size, bufSize = nil )
setBufSize( bufSize ) if bufSize
m = (( size / InputStreamPatternSize ) + 1 ).to_i
File.open( @@outfile, "wb" ) do | f |
f << ( InputStreamPattern * m )[ 0, size ]
end
CSV::IOBuf.new( File.open( @@outfile, "rb" ))
end
def setBufSize( size )
# Ruby kindly warns me but...
CSV::StreamBuf.module_eval( "BufSize = #{ size }" )
end
class StrBuf < CSV::StreamBuf
private
def initialize( string )
@str = string
@idx = 0
super()
end
def read( size )
str = @str[ @idx, size ]
@idx += str.size
str
end
end
class ErrBuf < CSV::StreamBuf
class Error < RuntimeError; end
private
def initialize
@first = true
super()
end
def read( size )
if @first
@first = false
"a" * size
else
raise Error.new
end
end
end
def test_StreamBuf_MyBuf
s = StrBuf.new( "abc" )
assert_equal( ?a, s[ 0 ] )
assert_equal( ?b, s.get( 1 ))
assert_equal( ?c, s[ 2 ] )
assert_equal( nil, s.get( 3 ))
assert_equal( "a", s[ 0, 1 ] )
assert_equal( "b", s.get( 1, 1 ))
assert_equal( "c", s[ 2, 1 ] )
assert_equal( "", s.get( 3, 1 ))
assert_equal( nil, s[ 4, 1 ] )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( ?b, s[ 0 ] )
assert( !s.isEOS? )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( ?c, s[ 0 ] )
assert( !s.isEOS? )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( nil, s[ 0 ] )
assert( s.isEOS? )
dropped = s.drop( 1 )
assert_equal( 0, dropped )
assert_equal( nil, s[ 0 ] )
assert( s.isEOS? )
s = StrBuf.new( "" )
assert_equal( nil, s[ 0 ] )
s = StrBuf.new( "" )
dropped = s.drop( 1 )
assert_equal( 0, dropped )
assert_exception( TestCSV::ErrBuf::Error ) do
s = ErrBuf.new
s[ 1024 ]
end
assert_exception( TestCSV::ErrBuf::Error ) do
s = ErrBuf.new
s.drop( 1024 )
end
end
def test_StreamBuf_AREF # '[ idx ]'
s = setupInputStream( 22, 1024 )
[ 0, 1, 9, 10, 19, 20, 21 ].each do | idx |
assert_equal( expChar( idx ), s[ idx ], idx.to_s )
end
[ 22, 23 ].each do | idx |
assert_equal( nil, s[ idx ], idx.to_s )
end
assert_equal( nil, s[ -1 ] )
s = setupInputStream( 22, 1 )
[ 0, 1, 9, 10, 19, 20, 21 ].each do | idx |
assert_equal( expChar( idx ), s[ idx ], idx.to_s )
end
[ 22, 23 ].each do | idx |
assert_equal( nil, s[ idx ], idx.to_s )
end
s = setupInputStream( 1024, 1 )
[ 1023, 0 ].each do | idx |
assert_equal( expChar( idx ), s[ idx ], idx.to_s )
end
[ 1024, 1025 ].each do | idx |
assert_equal( nil, s[ idx ], idx.to_s )
end
s = setupInputStream( 1, 1 )
[ 0 ].each do | idx |
assert_equal( expChar( idx ), s[ idx ], idx.to_s )
end
[ 1, 2 ].each do | idx |
assert_equal( nil, s[ idx ], idx.to_s )
end
end
def test_StreamBuf_AREF_n # '[ idx, n ]'
# At first, check ruby's behaviour.
assert_equal( "", "abc"[ 3, 1 ] )
assert_equal( nil, "abc"[ 4, 1 ] )
s = setupInputStream( 22, 1024 )
[ 0, 1, 9, 10, 19, 20, 21 ].each do | idx |
assert_equal( expStr( idx, 1 ), s[ idx, 1 ], idx.to_s )
end
assert_equal( "", s[ 22, 1 ] )
assert_equal( nil, s[ 23, 1 ] )
s = setupInputStream( 22, 1 )
[ 0, 1, 9, 10, 19, 20, 21 ].each do | idx |
assert_equal( expStr( idx, 1 ), s[ idx, 1 ], idx.to_s )
end
assert_equal( "", s[ 22, 1 ] )
assert_equal( nil, s[ 23, 1 ] )
s = setupInputStream( 1024, 1 )
[ 1023, 0 ].each do | idx |
assert_equal( expStr( idx, 1 ), s[ idx, 1 ], idx.to_s )
end
assert_equal( "", s[ 1024, 1 ] )
assert_equal( nil, s[ 1025, 1 ] )
s = setupInputStream( 1, 1 )
[ 0 ].each do | idx |
assert_equal( expStr( idx, 1 ), s[ idx, 1 ], idx.to_s )
end
assert_equal( "", s[ 1, 1 ] )
assert_equal( nil, s[ 2, 1 ] )
s = setupInputStream( 22, 11 )
[ 0, 1, 10, 11, 20 ].each do | idx |
assert_equal( expStr( idx, 2 ), s[ idx, 2 ], idx.to_s )
end
assert_equal( expStr( 21, 1 ), s[ 21, 2 ] )
assert_equal( expStr( 0, 12 ), s[ 0, 12 ] )
assert_equal( expStr( 10, 12 ), s[ 10, 12 ] )
assert_equal( expStr( 10, 12 ), s[ 10, 13 ] )
assert_equal( expStr( 10, 12 ), s[ 10, 14 ] )
assert_equal( expStr( 10, 12 ), s[ 10, 1024 ] )
assert_equal( nil, s[ 0, -1 ] )
assert_equal( nil, s[ 21, -1 ] )
assert_equal( nil, s[ -1, 10 ] )
assert_equal( nil, s[ -1, -1 ] )
end
def test_StreamBuf_get
s = setupInputStream( 22, 1024 )
[ 0, 1, 9, 10, 19, 20, 21 ].each do | idx |
assert_equal( expChar( idx ), s.get( idx ), idx.to_s )
end
[ 22, 23 ].each do | idx |
assert_equal( nil, s.get( idx ), idx.to_s )
end
assert_equal( nil, s.get( -1 ))
end
def test_StreamBuf_get_n
s = setupInputStream( 22, 1024 )
[ 0, 1, 9, 10, 19, 20, 21 ].each do | idx |
assert_equal( expStr( idx, 1 ), s.get( idx, 1 ), idx.to_s )
end
assert_equal( "", s.get( 22, 1 ))
assert_equal( nil, s.get( 23, 1 ))
assert_equal( nil, s.get( -1, 1 ))
assert_equal( nil, s.get( -1, -1 ))
end
def test_StreamBuf_drop
s = setupInputStream( 22, 1024 )
assert_equal( expChar( 0 ), s[ 0 ] )
assert_equal( expChar( 21 ), s[ 21 ] )
assert_equal( nil, s[ 22 ] )
dropped = s.drop( -1 )
assert_equal( 0, dropped )
assert_equal( expChar( 0 ), s[ 0 ] )
dropped = s.drop( 0 )
assert_equal( 0, dropped )
assert_equal( expChar( 0 ), s[ 0 ] )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( expChar( 1 ), s[ 0 ] )
assert_equal( expChar( 2 ), s[ 1 ] )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( expChar( 2 ), s[ 0 ] )
assert_equal( expChar( 3 ), s[ 1 ] )
s = setupInputStream( 4, 2 )
dropped = s.drop( 2 )
assert_equal( 2, dropped )
assert_equal( expChar( 2 ), s[ 0 ] )
assert_equal( expChar( 3 ), s[ 1 ] )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( expChar( 3 ), s[ 0 ] )
assert_equal( nil, s[ 1 ] )
dropped = s.drop( 1 )
assert_equal( 1, dropped )
assert_equal( nil, s[ 0 ] )
assert_equal( nil, s[ 1 ] )
dropped = s.drop( 0 )
assert_equal( 0, dropped )
assert_equal( nil, s[ 0 ] )
assert_equal( nil, s[ 1 ] )
s = setupInputStream( 6, 3 )
dropped = s.drop( 2 )
assert_equal( 2, dropped )
dropped = s.drop( 2 )
assert_equal( 2, dropped )
assert_equal( expChar( 4 ), s[ 0 ] )
assert_equal( expChar( 5 ), s[ 1 ] )
dropped = s.drop( 3 )
assert_equal( 2, dropped )
assert_equal( nil, s[ 0 ] )
assert_equal( nil, s[ 1 ] )
end
def test_StreamBuf_isEOS?
s = setupInputStream( 3, 1024 )
assert( !s.isEOS? )
s.drop( 1 )
assert( !s.isEOS? )
s.drop( 1 )
assert( !s.isEOS? )
s.drop( 1 )
assert( s.isEOS? )
s.drop( 1 )
assert( s.isEOS? )
s = setupInputStream( 3, 2 )
assert( !s.isEOS? )
s.drop( 1 )
assert( !s.isEOS? )
s.drop( 1 )
assert( !s.isEOS? )
s.drop( 1 )
assert( s.isEOS? )
s.drop( 1 )
assert( s.isEOS? )
end
def test_StreamBuf_s_new
# NotImplementedError should be raised from StreamBuf#read.
assert_exception( NotImplementedError ) do
CSV::StreamBuf.new
end
end
def test_IOBuf_close
f = File.open( @@outfile, "wb" )
f << "tst"
f.close
f = File.open( @@outfile, "rb" )
iobuf = CSV::IOBuf.new( f )
assert_no_exception do
iobuf.close
end
end
def test_IOBuf_s_new
iobuf = CSV::IOBuf.new( Tempfile.new( "in.csv" ))
assert_instance_of( CSV::IOBuf, iobuf )
end
#### CSV functional test
# sample data
#
# 1 2 3 4 5 6 7 8
# +------+-------+---------+-------+--------+------+----+------+
# | foo | "foo" | foo,bar | "" |(empty) |(null)| \r | \r\n |
# +------+-------+---------+-------+--------+------+----+------+
# | NaHi | "Na" | Na,Hi | \r.\n | \r\n\n | " | \n | \r\n |
# +------+-------+---------+-------+--------+------+----+------+
#
def test_s_parseAndCreate
colSize = 8
csvStr = "foo,!!!foo!!!,!foo,bar!,!!!!!!,!!,,!\r!,!\r\n!\r\nNaHi,!!!Na!!!,!Na,Hi!,!\r.\n!,!\r\n\n!,!!!!,!\n!,!\r\n!".gsub!( '!', '"' )
csvStrTerminated = csvStr + "\r\n"
myStr = csvStr.dup
res1 = []; res2 = []
idx = 0
col, idx = CSV::parseLine( myStr, 0, res1 )
col, idx = CSV::parseLine( myStr, idx, res2 )
buf = ''
col = CSV::createLine( res1, colSize, buf )
col = CSV::createLine( res2, colSize, buf )
assert_equal( csvStrTerminated, buf )
parsed = []
CSV::Reader.parse( csvStrTerminated ) do | row |
parsed << row
end
buf = ''
CSV::Writer.generate( buf ) do | writer |
parsed.each do | row |
writer.addRow( row )
end
end
assert_equal( csvStrTerminated, buf )
buf = ''
CSV::Writer.generate( buf ) do | writer |
parsed.each do | row |
writer << row.collect { | e | e.isNull ? nil : e.data }
end
end
assert_equal( csvStrTerminated, buf )
end
end
if $0 == __FILE__
testrunner = RUNIT::CUI::TestRunner.new
if ARGV.size == 0
suite = TestCSV.suite
else
suite = RUNIT::TestSuite.new
ARGV.each do |testmethod|
suite.add_test(TestCSV.new(testmethod))
end
end
testrunner.run(suite)
end
|