-
Notifications
You must be signed in to change notification settings - Fork 66
/
test_count.rs
205 lines (182 loc) · 4.9 KB
/
test_count.rs
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
use crate::{qcheck, workdir::Workdir, CsvData};
#[test]
fn count_simple() {
let wrk = Workdir::new("count_simple");
wrk.create_indexed(
"in.csv",
vec![
svec!["letter", "number"],
svec!["alpha", "13"],
svec!["beta", "24"],
svec!["gamma", "37"],
],
);
let mut cmd = wrk.command("count");
cmd.arg("in.csv");
let got: String = wrk.stdout(&mut cmd);
let expected = "3";
assert_eq!(got, expected.to_string());
}
#[test]
fn count_width() {
let wrk = Workdir::new("count_width");
wrk.create_indexed(
"in.csv",
vec![
svec!["letter", "number", "flag"],
svec!["alphabetic", "13", "true"],
svec!["beta", "24", "false"],
svec!["gamma", "37.1", "true"],
svec!("delta", "42.5", "false"),
],
);
let mut cmd = wrk.command("count");
cmd.arg("--width").arg("in.csv");
let got: String = wrk.stdout(&mut cmd);
let expected = "4;18";
assert_eq!(got, expected.to_string());
}
#[test]
fn count_flexible() {
let wrk = Workdir::new("count_flexible");
wrk.create_from_string(
"in.csv",
r#"letter,number,flag
alphabetic,13,true,extra column
beta,24,false
gamma,37.1
delta,42.5,false
"#,
);
let mut cmd = wrk.command("count");
cmd.arg("--flexible").arg("in.csv");
let got: String = wrk.stdout(&mut cmd);
let expected = "4";
assert_eq!(got, expected.to_string());
}
#[test]
fn count_comments() {
let wrk = Workdir::new("count_comments");
wrk.create(
"in.csv",
vec![
svec!["# this is a comment", ""],
svec!["# next comment", ""],
svec!["letter", "number"],
svec!["alpha", "13"],
svec!["beta", "24"],
svec!["# comment here too!", "24"],
],
);
let mut cmd = wrk.command("count");
cmd.arg("in.csv").env("QSV_COMMENT_CHAR", "#");
let got: String = wrk.stdout(&mut cmd);
let expected = "2";
assert_eq!(got, expected.to_string());
}
/// This tests whether `qsv count` gets the right answer.
///
/// It does some simple case analysis to handle whether we want to test counts
/// in the presence of headers and/or indexes.
fn prop_count_len(
name: &str,
rows: CsvData,
headers: bool,
idx: bool,
noheaders_env: bool,
human_readable: bool,
) -> bool {
let mut expected_count = rows.len();
if headers && expected_count > 0 {
expected_count -= 1;
}
let wrk = Workdir::new(name);
if idx {
wrk.create_indexed("in.csv", rows);
} else {
wrk.create("in.csv", rows);
}
let mut cmd = wrk.command("count");
if !headers {
cmd.arg("--no-headers");
}
if noheaders_env {
cmd.env("QSV_NO_HEADERS", "1");
}
if human_readable {
cmd.arg("--human-readable");
}
cmd.arg("in.csv");
if human_readable {
use indicatif::HumanCount;
let got_count: String = wrk.stdout(&mut cmd);
let expected_count_commas = HumanCount(expected_count as u64).to_string();
rassert_eq!(got_count, expected_count_commas)
} else {
let got_count: usize = wrk.stdout(&mut cmd);
rassert_eq!(got_count, expected_count)
}
}
#[test]
fn prop_count() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count", rows, false, false, false, false)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_human_readable() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count", rows, false, false, false, true)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_headers() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count_headers", rows, true, false, false, false)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_headers_human_readable() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count_headers", rows, true, false, false, true)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_indexed() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count_indexed", rows, false, true, false, false)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_indexed_headers() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count_indexed_headers", rows, true, true, false, false)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_noheaders_env() {
fn p(rows: CsvData) -> bool {
prop_count_len("prop_count_noheaders_env", rows, false, false, true, false)
}
qcheck(p as fn(CsvData) -> bool);
}
#[test]
fn prop_count_noheaders_indexed_env() {
fn p(rows: CsvData) -> bool {
prop_count_len(
"prop_count_noheaders_indexed_env",
rows,
false,
true,
true,
false,
)
}
qcheck(p as fn(CsvData) -> bool);
}