Skip to content

Commit f1c3060

Browse files
refactor(linter): Factor some common logic out of unit tests
1 parent 5a6509c commit f1c3060

File tree

3 files changed

+39
-86
lines changed

3 files changed

+39
-86
lines changed

components/clarity-repl/src/analysis/lints/noop.rs

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ impl Lint for NoopChecker<'_> {
175175
#[cfg(test)]
176176
mod tests {
177177
use clarity::vm::diagnostic::Level;
178+
use clarity::vm::ExecutionResult;
178179
use indoc::indoc;
179180

180181
use super::NoopChecker;
181182
use crate::analysis::linter::Lint;
182183
use crate::repl::session::Session;
183184
use crate::repl::SessionSettings;
184185

185-
fn get_session() -> Session {
186+
fn run_snippet(snippet: String) -> (Vec<String>, ExecutionResult) {
186187
let mut settings = SessionSettings::default();
187188
settings.repl_settings.analysis.disable_all_lints();
188189
settings
@@ -191,12 +192,12 @@ mod tests {
191192
.enable_lint(NoopChecker::get_name(), Level::Warning);
192193

193194
Session::new(settings)
195+
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
196+
.expect("Invalid code snippet")
194197
}
195198

196199
#[test]
197200
fn single_operand_equals() {
198-
let mut session = get_session();
199-
200201
#[rustfmt::skip]
201202
let snippet = indoc!("
202203
(define-public (test-func)
@@ -207,20 +208,15 @@ mod tests {
207208
)
208209
").to_string();
209210

210-
match session.formatted_interpretation(snippet, Some("checker".to_string()), false, None) {
211-
Ok((output, result)) => {
212-
assert_eq!(result.diagnostics.len(), 1);
213-
assert!(output[0].contains("warning:"));
214-
assert!(output[0].contains("`is-eq` with fewer than 2 operands has no effect"));
215-
}
216-
_ => panic!("Expected warning"),
217-
};
211+
let (output, result) = run_snippet(snippet);
212+
213+
assert_eq!(result.diagnostics.len(), 1);
214+
assert!(output[0].contains("warning:"));
215+
assert!(output[0].contains("`is-eq` with fewer than 2 operands has no effect"));
218216
}
219217

220218
#[test]
221219
fn single_operand_add() {
222-
let mut session = get_session();
223-
224220
#[rustfmt::skip]
225221
let snippet = indoc!("
226222
(define-public (test-func)
@@ -231,20 +227,15 @@ mod tests {
231227
)
232228
").to_string();
233229

234-
match session.formatted_interpretation(snippet, Some("checker".to_string()), false, None) {
235-
Ok((output, result)) => {
236-
assert_eq!(result.diagnostics.len(), 1);
237-
assert!(output[0].contains("warning:"));
238-
assert!(output[0].contains("`+` with fewer than 2 operands has no effect"));
239-
}
240-
_ => panic!("Expected warning"),
241-
};
230+
let (output, result) = run_snippet(snippet);
231+
232+
assert_eq!(result.diagnostics.len(), 1);
233+
assert!(output[0].contains("warning:"));
234+
assert!(output[0].contains("`+` with fewer than 2 operands has no effect"));
242235
}
243236

244237
#[test]
245238
fn single_operand_logical() {
246-
let mut session = get_session();
247-
248239
#[rustfmt::skip]
249240
let snippet = indoc!("
250241
(define-public (test-func)
@@ -255,20 +246,15 @@ mod tests {
255246
)
256247
").to_string();
257248

258-
match session.formatted_interpretation(snippet, Some("checker".to_string()), false, None) {
259-
Ok((output, result)) => {
260-
assert_eq!(result.diagnostics.len(), 1);
261-
assert!(output[0].contains("warning:"));
262-
assert!(output[0].contains("`and` with fewer than 2 operands has no effect"));
263-
}
264-
_ => panic!("Expected warning"),
265-
};
249+
let (output, result) = run_snippet(snippet);
250+
251+
assert_eq!(result.diagnostics.len(), 1);
252+
assert!(output[0].contains("warning:"));
253+
assert!(output[0].contains("`and` with fewer than 2 operands has no effect"));
266254
}
267255

268256
#[test]
269257
fn allow_noop_annotation() {
270-
let mut session = get_session();
271-
272258
#[rustfmt::skip]
273259
let snippet = indoc!("
274260
(define-public (test-func)
@@ -280,11 +266,8 @@ mod tests {
280266
)
281267
").to_string();
282268

283-
match session.formatted_interpretation(snippet, Some("checker".to_string()), false, None) {
284-
Ok((_, result)) => {
285-
assert_eq!(result.diagnostics.len(), 0);
286-
}
287-
_ => panic!("Expected warning"),
288-
};
269+
let (_, result) = run_snippet(snippet);
270+
271+
assert_eq!(result.diagnostics.len(), 0);
289272
}
290273
}

components/clarity-repl/src/analysis/lints/unused_const.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,15 @@ impl Lint for UnusedConst<'_> {
160160
#[cfg(test)]
161161
mod tests {
162162
use clarity::vm::diagnostic::Level;
163+
use clarity::vm::ExecutionResult;
163164
use indoc::indoc;
164165

165166
use super::UnusedConst;
166167
use crate::analysis::linter::Lint;
167168
use crate::repl::session::Session;
168169
use crate::repl::SessionSettings;
169170

170-
fn get_session() -> Session {
171+
fn run_snippet(snippet: String) -> (Vec<String>, ExecutionResult) {
171172
let mut settings = SessionSettings::default();
172173
settings.repl_settings.analysis.disable_all_lints();
173174
settings
@@ -176,12 +177,12 @@ mod tests {
176177
.enable_lint(UnusedConst::get_name(), Level::Warning);
177178

178179
Session::new(settings)
180+
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
181+
.expect("Invalid code snippet")
179182
}
180183

181184
#[test]
182185
fn const_used() {
183-
let mut session = get_session();
184-
185186
#[rustfmt::skip]
186187
let snippet = indoc!("
187188
(define-constant MINUTES_PER_HOUR u60)
@@ -190,17 +191,13 @@ mod tests {
190191
(* hours MINUTES_PER_HOUR))
191192
").to_string();
192193

193-
let (_, result) = session
194-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
195-
.expect("Invalid code snippet");
194+
let (_, result) = run_snippet(snippet);
196195

197196
assert_eq!(result.diagnostics.len(), 0);
198197
}
199198

200199
#[test]
201200
fn const_used_before_declaration() {
202-
let mut session = get_session();
203-
204201
#[rustfmt::skip]
205202
let snippet = indoc!("
206203
(define-read-only (hours-to-minutes (hours uint))
@@ -209,17 +206,13 @@ mod tests {
209206
(define-constant MINUTES_PER_HOUR u60)
210207
").to_string();
211208

212-
let (_, result) = session
213-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
214-
.expect("Invalid code snippet");
209+
let (_, result) = run_snippet(snippet);
215210

216211
assert_eq!(result.diagnostics.len(), 0);
217212
}
218213

219214
#[test]
220215
fn const_not_used() {
221-
let mut session = get_session();
222-
223216
#[rustfmt::skip]
224217
let snippet = indoc!("
225218
(define-constant MINUTES_PER_HOUR u60)
@@ -228,9 +221,7 @@ mod tests {
228221
(* hours u60))
229222
").to_string();
230223

231-
let (output, result) = session
232-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
233-
.expect("Invalid code snippet");
224+
let (output, result) = run_snippet(snippet);
234225

235226
let const_name = "MINUTES_PER_HOUR";
236227
let expected_message = UnusedConst::make_diagnostic_message(&const_name.into());
@@ -243,8 +234,6 @@ mod tests {
243234

244235
#[test]
245236
fn allow_with_comment() {
246-
let mut session = get_session();
247-
248237
#[rustfmt::skip]
249238
let snippet = indoc!("
250239
;; #[allow(unused_const)]
@@ -254,9 +243,7 @@ mod tests {
254243
(* hours u60))
255244
").to_string();
256245

257-
let (_, result) = session
258-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
259-
.expect("Invalid code snippet");
246+
let (_, result) = run_snippet(snippet);
260247

261248
assert_eq!(result.diagnostics.len(), 0);
262249
}

components/clarity-repl/src/analysis/lints/unused_data_var.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,15 @@ impl Lint for UnusedDataVar<'_> {
224224
#[cfg(test)]
225225
mod tests {
226226
use clarity::vm::diagnostic::Level;
227+
use clarity::vm::ExecutionResult;
227228
use indoc::indoc;
228229

229230
use super::UnusedDataVar;
230231
use crate::analysis::linter::Lint;
231232
use crate::repl::session::Session;
232233
use crate::repl::SessionSettings;
233234

234-
fn get_session() -> Session {
235+
fn run_snippet(snippet: String) -> (Vec<String>, ExecutionResult) {
235236
let mut settings = SessionSettings::default();
236237
settings.repl_settings.analysis.disable_all_lints();
237238
settings
@@ -240,12 +241,12 @@ mod tests {
240241
.enable_lint(UnusedDataVar::get_name(), Level::Warning);
241242

242243
Session::new(settings)
244+
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
245+
.expect("Invalid code snippet")
243246
}
244247

245248
#[test]
246249
fn data_var_used() {
247-
let mut session = get_session();
248-
249250
#[rustfmt::skip]
250251
let snippet = indoc!("
251252
(define-data-var counter uint u0)
@@ -254,17 +255,13 @@ mod tests {
254255
(ok (var-set counter (+ (var-get counter) u1))))
255256
").to_string();
256257

257-
let (_, result) = session
258-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
259-
.expect("Invalid code snippet");
258+
let (_, result) = run_snippet(snippet);
260259

261260
assert_eq!(result.diagnostics.len(), 0);
262261
}
263262

264263
#[test]
265264
fn data_var_not_set() {
266-
let mut session = get_session();
267-
268265
#[rustfmt::skip]
269266
let snippet = indoc!("
270267
(define-data-var counter uint u0)
@@ -273,9 +270,7 @@ mod tests {
273270
(ok (var-get counter)))
274271
").to_string();
275272

276-
let (output, result) = session
277-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
278-
.expect("Invalid code snippet");
273+
let (output, result) = run_snippet(snippet);
279274

280275
let var_name = "counter";
281276
let (expected_message, _) = UnusedDataVar::make_diagnostic_strings_unset(&var_name.into());
@@ -288,8 +283,6 @@ mod tests {
288283

289284
#[test]
290285
fn data_var_not_read() {
291-
let mut session = get_session();
292-
293286
#[rustfmt::skip]
294287
let snippet = indoc!("
295288
(define-data-var counter uint u0)
@@ -298,9 +291,7 @@ mod tests {
298291
(ok (var-set counter val)))
299292
").to_string();
300293

301-
let (output, result) = session
302-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
303-
.expect("Invalid code snippet");
294+
let (output, result) = run_snippet(snippet);
304295

305296
let var_name = "counter";
306297
let (expected_message, _) = UnusedDataVar::make_diagnostic_strings_unread(&var_name.into());
@@ -313,8 +304,6 @@ mod tests {
313304

314305
#[test]
315306
fn data_var_not_used() {
316-
let mut session = get_session();
317-
318307
#[rustfmt::skip]
319308
let snippet = indoc!("
320309
(define-data-var counter uint u0)
@@ -323,9 +312,7 @@ mod tests {
323312
(ok u5))
324313
").to_string();
325314

326-
let (output, result) = session
327-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
328-
.expect("Invalid code snippet");
315+
let (output, result) = run_snippet(snippet);
329316

330317
let var_name = "counter";
331318
let (expected_message, _) = UnusedDataVar::make_diagnostic_strings_unused(&var_name.into());
@@ -338,8 +325,6 @@ mod tests {
338325

339326
#[test]
340327
fn allow_with_comment() {
341-
let mut session = get_session();
342-
343328
#[rustfmt::skip]
344329
let snippet = indoc!("
345330
;; #[allow(unused_data_var)]
@@ -349,9 +334,7 @@ mod tests {
349334
(ok u5))
350335
").to_string();
351336

352-
let (_, result) = session
353-
.formatted_interpretation(snippet, Some("checker".to_string()), false, None)
354-
.expect("Invalid code snippet");
337+
let (_, result) = run_snippet(snippet);
355338

356339
assert_eq!(result.diagnostics.len(), 0);
357340
}

0 commit comments

Comments
 (0)