Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_csv.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import csv
16
17 var words = "../examples/rosettacode/unixdict.txt".to_path.read_all.split('\n')
18
19 var doc = new CsvDocument
20 for i in [0 .. 8[ do doc.header.add("Col{i}")
21
22 var del = doc.delimiter.to_s
23 var eol = doc.eol
24 var sep = doc.separator.to_s
25 for i in [0 .. 10000[ do
26 var ln = new Array[String]
27 for j in [0 .. 8[ do
28 var add_sep = 100.rand >= 70
29 var add_eol = 100.rand >= 70
30 var add_del = 100.rand >= 70
31 var el = "{words.rand}"
32 if add_sep then el += sep
33 if add_eol then el += eol
34 if add_del then el += del
35 ln.add el
36 end
37 doc.records.add ln
38 end
39
40 var refst = new StringWriter
41 doc.write_to(refst)
42
43 var csvd = new CsvReader.from_string(refst.to_s).read_all
44
45 var prodst = new StringWriter
46 csvd.write_to(prodst)
47
48 assert refst.to_s.trim == prodst.to_s.trim