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
use std::fmt::Write;
pub(super) fn string_value_expression(field: &str, is_pointer: bool, is_data_interface: bool) -> String {
if is_data_interface {
format!("jsonString(t, {field})")
} else if is_pointer {
format!("string(*{field})")
} else {
format!("string({field})")
}
}
pub(super) fn contains_value_expression(
field: &str,
is_pointer: bool,
is_array: bool,
is_data_interface: bool,
) -> String {
if is_data_interface || is_array {
format!("jsonString(t, {field})")
} else {
string_value_expression(field, is_pointer, false)
}
}
pub(super) fn render_guarded_scalar_comparison(
out: &mut String,
guard: Option<&str>,
field_expr: &str,
operator: &str,
comparison_value: &str,
expected_message: &str,
) -> bool {
let Some(guard) = guard else {
return false;
};
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tif {field_expr} {operator} {comparison_value} {{");
let _ = writeln!(
out,
"\t\t\tt.Errorf(\"expected {expected_message}, got %v\", {field_expr})"
);
let _ = writeln!(out, "\t\t}}");
let _ = writeln!(out, "\t}}");
true
}
/// Grouping for `render_count_assertion`'s boolean options, kept out of the parameter list
/// itself so the function stays under the 6-parameter lint ceiling.
pub(super) struct CountAssertionShape {
pub(super) is_slice: bool,
pub(super) exact: bool,
/// Whether a sibling `not_empty` assertion on the same field already fails the test
/// when the field is nil -- see `AssertionRenderContext::presence_checked_fields`.
pub(super) has_sibling_presence_check: bool,
}
pub(super) fn render_count_assertion(
out: &mut String,
field: &str,
count: u64,
nullable_guard: Option<&str>,
shape: CountAssertionShape,
) {
let CountAssertionShape {
is_slice,
exact,
has_sibling_presence_check,
} = shape;
let (method, message) = if exact {
("Equal", format!("expected exactly {count} elements"))
} else {
("GreaterOrEqual", format!("expected at least {count} elements"))
};
let is_length = field.starts_with("len(");
let length = if is_length {
field.to_string()
} else if nullable_guard.is_some() && !is_slice {
format!("len(*{field})")
} else {
format!("len({field})")
};
match nullable_guard {
// `is_length` means `field` is already a `len(...)`/pseudo `.length`/`.count`/`.size`
// expression measuring a derived scalar off `guard` (e.g. a string's length behind an
// optional pointer) -- `guard` here is NOT the collection this assertion is about, it
// is an unrelated optional value the measurement happens to be taken through. Nil is a
// legitimate "not populated" state with no presence claim attached, the same semantics
// `render_guarded_scalar_comparison` already applies to optional scalars like
// `QualityScore`. Only a *named* collection field (`is_length == false`, e.g.
// `elements`, `chunks`, `detected_languages`) is the thing a count assertion exists to
// guarantee produced something, so only that shape gets the failing `else` below. ~keep
Some(guard) if is_length => {
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tassert.{method}(t, {length}, {count}, \"{message}\")");
let _ = writeln!(out, "\t}}");
}
// A sibling `not_empty` assertion on the same field already fails the test when
// `guard` is nil (`render_not_empty`), so an `else` here would only duplicate
// that failure -- stay guard-only. ~keep
Some(guard) if has_sibling_presence_check => {
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tassert.{method}(t, {length}, {count}, \"{message}\")");
let _ = writeln!(out, "\t}}");
}
// No sibling presence assertion covers this field, and it is a real named collection
// (not a pseudo-length measurement): without an `else`, a nil guard would make the
// count assertion silently not run and the test would pass on exactly the regression
// it exists to catch. Fail instead. ~keep
Some(guard) => {
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tassert.{method}(t, {length}, {count}, \"{message}\")");
let _ = writeln!(out, "\t}} else {{");
let _ = writeln!(out, "\t\tt.Errorf(\"{message}, got nil\")");
let _ = writeln!(out, "\t}}");
}
None => {
let _ = writeln!(out, "\tassert.{method}(t, {length}, {count}, \"{message}\")");
}
}
}
/// Grouping for `render_length_assertion`'s boolean options -- mirrors `CountAssertionShape`,
/// kept out of the parameter list itself so the function stays under the 6-parameter lint
/// ceiling.
pub(super) struct LengthAssertionShape {
pub(super) is_pointer: bool,
pub(super) minimum: bool,
/// Whether a sibling `not_empty` assertion on the same field already fails the test
/// when the field is nil -- see `AssertionRenderContext::presence_checked_fields`.
pub(super) has_sibling_presence_check: bool,
}
pub(super) fn render_length_assertion(
out: &mut String,
field: &str,
length: u64,
nullable_guard: Option<&str>,
shape: LengthAssertionShape,
) {
let LengthAssertionShape {
is_pointer,
minimum,
has_sibling_presence_check,
} = shape;
let (method, relation) = if minimum {
("GreaterOrEqual", ">=")
} else {
("LessOrEqual", "<=")
};
let is_length = field.starts_with("len(");
let expression = if is_length {
field.to_string()
} else if is_pointer {
format!("len(*{field})")
} else {
format!("len({field})")
};
let message = format!("expected length {relation} {length}");
match nullable_guard {
// See `render_count_assertion`'s identical arm: `is_length` means `field` is already
// a `len(...)`/pseudo `.length`/`.count`/`.size` expression measuring a derived
// scalar off `guard`, not a named collection field in its own right -- nil there is a
// legitimate "not populated" state with no presence claim, so it stays guard-only. ~keep
Some(guard) if is_length => {
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tassert.{method}(t, {expression}, {length}, \"{message}\")");
let _ = writeln!(out, "\t}}");
}
// A sibling `not_empty` assertion on the same field already fails the test when
// `guard` is nil (`render_not_empty`), so an `else` here would only duplicate that
// failure -- stay guard-only. ~keep
Some(guard) if has_sibling_presence_check => {
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tassert.{method}(t, {expression}, {length}, \"{message}\")");
let _ = writeln!(out, "\t}}");
}
// No sibling presence assertion covers this field, and it is a real named field (not
// a pseudo-length measurement): without an `else`, a nil guard would make the length
// assertion silently not run and the test would pass on exactly the regression it
// exists to catch. Fail instead. ~keep
Some(guard) => {
let _ = writeln!(out, "\tif {guard} != nil {{");
let _ = writeln!(out, "\t\tassert.{method}(t, {expression}, {length}, \"{message}\")");
let _ = writeln!(out, "\t}} else {{");
let _ = writeln!(out, "\t\tt.Errorf(\"{message}, got nil\")");
let _ = writeln!(out, "\t}}");
}
None => {
let _ = writeln!(out, "\tassert.{method}(t, {expression}, {length}, \"{message}\")");
}
}
}