2020-04-21 01:02:55 +03:00
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-04-21 01:02:55 +03:00
|
|
|
row_count++
|
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'email'
|
|
|
|
assert row[2] == 'phone'
|
|
|
|
assert row[3] == 'other'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-04-21 01:02:55 +03:00
|
|
|
assert row[0] == 'joe'
|
|
|
|
assert row[1] == 'joe@blow.com'
|
|
|
|
assert row[2] == '0400000000'
|
|
|
|
assert row[3] == 'test'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 3 {
|
2020-04-21 01:02:55 +03:00
|
|
|
assert row[0] == 'sam'
|
|
|
|
assert row[1] == 'sam@likesham.com'
|
|
|
|
assert row[2] == '0433000000'
|
|
|
|
// quoted field
|
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-04-21 01:02:55 +03:00
|
|
|
row_count++
|
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'email'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-04-21 01:02:55 +03:00
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-04-21 01:02:55 +03:00
|
|
|
row_count++
|
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'email'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-04-21 01:02:55 +03:00
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-04-21 01:02:55 +03:00
|
|
|
row_count++
|
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'email'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-04-21 01:02:55 +03:00
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
csv_reader.read() or { break }
|
2020-04-21 01:02:55 +03:00
|
|
|
row_count++
|
|
|
|
}
|
|
|
|
assert row_count == 2
|
2020-04-25 15:37:32 +03:00
|
|
|
}
|
2020-04-29 17:50:02 +03:00
|
|
|
|
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-07-22 17:26:26 +03:00
|
|
|
row_count++
|
2020-04-29 17:50:02 +03:00
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'description'
|
|
|
|
assert row[2] == 'value'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-04-29 17:50:02 +03:00
|
|
|
assert row[0] == 'one'
|
|
|
|
assert row[1] == 'first'
|
|
|
|
assert row[2] == '1'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 3 {
|
2020-04-29 17:50:02 +03:00
|
|
|
assert row[0] == 'two'
|
|
|
|
assert row[1] == 'second'
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 05:53:02 +03:00
|
|
|
assert row_count == 3
|
2020-04-29 17:50:02 +03:00
|
|
|
}
|
2020-05-09 18:35:03 +03:00
|
|
|
|
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-07-22 17:26:26 +03:00
|
|
|
row_count++
|
2020-05-09 18:35:03 +03:00
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'description'
|
|
|
|
assert row[2] == 'value'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-05-09 18:35:03 +03:00
|
|
|
assert row[0] == 'one'
|
|
|
|
assert row[1] == 'first'
|
|
|
|
assert row[2] == '1'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 3 {
|
2020-05-09 18:35:03 +03:00
|
|
|
assert row[0] == 'two'
|
|
|
|
assert row[1] == 'second'
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 05:53:02 +03:00
|
|
|
assert row_count == 3
|
2020-05-09 18:35:03 +03:00
|
|
|
}
|
2020-05-10 15:19:26 +03:00
|
|
|
|
|
|
|
fn test_field_multiple_line() {
|
|
|
|
data := '"name","multiple
|
|
|
|
|
2021-03-09 05:53:02 +03:00
|
|
|
line","value"\n"one","first","1"\n'
|
2020-05-10 15:19:26 +03:00
|
|
|
mut csv_reader := csv.new_reader(data)
|
|
|
|
mut row_count := 0
|
|
|
|
for {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2020-07-22 17:26:26 +03:00
|
|
|
row_count++
|
2020-05-10 15:19:26 +03:00
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'name'
|
|
|
|
assert row[1] == 'multiple\n\n line'
|
|
|
|
assert row[2] == 'value'
|
2020-07-22 17:26:26 +03:00
|
|
|
} else if row_count == 2 {
|
2020-05-10 15:19:26 +03:00
|
|
|
assert row[0] == 'one'
|
|
|
|
assert row[1] == 'first'
|
|
|
|
assert row[2] == '1'
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 05:53:02 +03:00
|
|
|
assert row_count == 2
|
2020-05-10 15:19:26 +03:00
|
|
|
}
|
2021-03-08 17:57:02 +03:00
|
|
|
|
|
|
|
fn test_field_quotes_for_parts() {
|
|
|
|
data := 'a1,"b1",c1\n"a2",b2,c2\na3,b3,"c3"\na4,b4,c4\n'
|
|
|
|
mut csv_reader := csv.new_reader(data)
|
|
|
|
mut row_count := 0
|
|
|
|
for {
|
2021-05-08 13:32:29 +03:00
|
|
|
row := csv_reader.read() or { break }
|
2021-03-08 17:57:02 +03:00
|
|
|
row_count++
|
|
|
|
if row_count == 1 {
|
|
|
|
assert row[0] == 'a1'
|
|
|
|
assert row[1] == 'b1'
|
|
|
|
assert row[2] == 'c1'
|
|
|
|
} else if row_count == 2 {
|
|
|
|
assert row[0] == 'a2'
|
|
|
|
assert row[1] == 'b2'
|
|
|
|
assert row[2] == 'c2'
|
|
|
|
} else if row_count == 3 {
|
|
|
|
assert row[0] == 'a3'
|
|
|
|
assert row[1] == 'b3'
|
|
|
|
assert row[2] == 'c3'
|
|
|
|
} else if row_count == 4 {
|
|
|
|
assert row[0] == 'a4'
|
|
|
|
assert row[1] == 'b4'
|
|
|
|
assert row[2] == 'c4'
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 05:53:02 +03:00
|
|
|
assert row_count == 4
|
2021-03-08 17:57:02 +03:00
|
|
|
}
|