csv: cleanup reader_test.v

pull/5924/head
Carlos Esquerdo Bernat 2020-07-22 16:26:26 +02:00 committed by GitHub
parent 1476602f99
commit f09e14a2b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 48 deletions

View File

@ -3,7 +3,6 @@ import encoding.csv
fn test_encoding_csv_reader() {
data := 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"test quoted field"\n#chris,chris@nomail.com,94444444,"commented row"\n'
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
@ -15,14 +14,12 @@ fn test_encoding_csv_reader() {
assert row[1] == 'email'
assert row[2] == 'phone'
assert row[3] == 'other'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'joe'
assert row[1] == 'joe@blow.com'
assert row[2] == '0400000000'
assert row[3] == 'test'
}
if row_count == 3 {
} else if row_count == 3 {
assert row[0] == 'sam'
assert row[1] == 'sam@likesham.com'
assert row[2] == '0433000000'
@ -30,15 +27,12 @@ fn test_encoding_csv_reader() {
assert row[3] == 'test quoted field'
}
}
assert row_count == 3
}
fn test_line_break_lf() {
lf_data := 'name,email\njoe,joe@blow.com\n'
mut csv_reader := csv.new_reader(lf_data)
mut row_count := 0
for {
row := csv_reader.read() or {
@ -48,21 +42,17 @@ fn test_line_break_lf() {
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'email'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'joe'
assert row[1] == 'joe@blow.com'
}
}
assert row_count == 2
}
fn test_line_break_cr() {
cr_data := 'name,email\rjoe,joe@blow.com\r'
mut csv_reader := csv.new_reader(cr_data)
mut row_count := 0
for {
row := csv_reader.read() or {
@ -72,21 +62,17 @@ fn test_line_break_cr() {
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'email'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'joe'
assert row[1] == 'joe@blow.com'
}
}
assert row_count == 2
}
fn test_line_break_crlf() {
crlf_data := 'name,email\r\njoe,joe@blow.com\r\n'
mut csv_reader := csv.new_reader(crlf_data)
mut row_count := 0
for {
row := csv_reader.read() or {
@ -96,20 +82,17 @@ fn test_line_break_crlf() {
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'email'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'joe'
assert row[1] == 'joe@blow.com'
}
}
assert row_count == 2
}
fn test_no_line_ending() {
data := 'name,email,phone,other\njoe,joe@blow.com,0400000000,test'
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
csv_reader.read() or {
@ -117,31 +100,27 @@ fn test_no_line_ending() {
}
row_count++
}
assert row_count == 2
}
fn test_last_field_empty() {
data := '"name","description","value"\n"one","first","1"\n"two","second",\n'
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row_count++
row := csv_reader.read() or {
break
}
row_count++
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'description'
assert row[2] == 'value'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'one'
assert row[1] == 'first'
assert row[2] == '1'
}
if row_count == 3 {
} else if row_count == 3 {
assert row[0] == 'two'
assert row[1] == 'second'
}
@ -151,24 +130,21 @@ fn test_last_field_empty() {
fn test_empty_line() {
data := '"name","description","value"\n\n\n"one","first","1"\n\n"two","second",\n'
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row_count++
row := csv_reader.read() or {
break
}
row_count++
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'description'
assert row[2] == 'value'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'one'
assert row[1] == 'first'
assert row[2] == '1'
}
if row_count == 3 {
} else if row_count == 3 {
assert row[0] == 'two'
assert row[1] == 'second'
}
@ -180,19 +156,17 @@ fn test_field_multiple_line() {
line","value"\n"one","first","1"'
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row_count++
row := csv_reader.read() or {
break
}
row_count++
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'multiple\n\n line'
assert row[2] == 'value'
}
if row_count == 2 {
} else if row_count == 2 {
assert row[0] == 'one'
assert row[1] == 'first'
assert row[2] == '1'