Skip to content

Commit 9212eeb

Browse files
Copilotjackfirth
andcommitted
Add comment-only-test statement for testing warning-only rules
Co-authored-by: jackfirth <[email protected]>
1 parent 0f321f7 commit 9212eeb

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

test-warning-suite.rkt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#lang racket/base
2+
3+
(require resyntax/base)
4+
5+
(provide test-warning-suite)
6+
7+
;; Define a warning-only rule that matches any (equal? x y)
8+
(define-refactoring-rule test-warning-rule
9+
#:description "Test warning rule for equal?"
10+
#:suggested-fixes 'none
11+
#:literals (equal?)
12+
(equal? x y)
13+
(void))
14+
15+
(define test-warning-suite
16+
(refactoring-suite #:rules (list test-warning-rule)))

test.rkt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
header
99
test
1010
no-change-test
11-
analysis-test)
11+
analysis-test
12+
comment-only-test)
1213

1314

1415
(require (for-syntax racket/base
@@ -228,6 +229,25 @@
228229
'expected-value)))]))))
229230

230231

232+
(define-syntax comment-only-test
233+
(statement-transformer
234+
(λ (stx)
235+
(syntax-parse stx
236+
#:track-literals
237+
#:datum-literals (option @within @inspect @assertMatch)
238+
[(#:statement _ name:str
239+
code:literal-code
240+
(~seq (#:option #:within context-block:literal-code) ...
241+
(#:option #:inspect target-block:literal-code)
242+
(#:option #:assertMatch rule-name:id)))
243+
#`(test-case 'name
244+
#,(syntax/loc this-syntax
245+
(check-suite-comment-only code
246+
(list context-block ...)
247+
target-block
248+
'rule-name)))]))))
249+
250+
231251
;; Helper function to check if any require: statements are present
232252
(begin-for-syntax
233253
(define (has-require-statements? body-stxs)
@@ -400,7 +420,7 @@
400420

401421
(define (add-uts-properties stx)
402422
(syntax-traverse stx
403-
#:datum-literals (require header test no-change-test analysis-test)
423+
#:datum-literals (require header test no-change-test analysis-test comment-only-test)
404424

405425
[:id
406426
(define as-string (symbol->string (syntax-e this-syntax)))
@@ -425,7 +445,7 @@
425445
(add-uts-properties (attribute code))))
426446
(datum->syntax #false new-datum this-syntax this-syntax)]
427447

428-
[((~and tag #:statement) (~and test-id (~or test no-change-test analysis-test)) arg ...)
448+
[((~and tag #:statement) (~and test-id (~or test no-change-test analysis-test comment-only-test)) arg ...)
429449
(define separators (append (list "" ": " "\n") (make-list (length (attribute arg)) "")))
430450
(define tag-with-prop (syntax-property (attribute tag) 'uts-separators separators))
431451
(define new-datum

test/comment-only-test-demo.rkt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#lang resyntax/test
2+
3+
4+
require: resyntax/test-warning-suite test-warning-suite
5+
6+
7+
comment-only-test: "warning-only rule should produce a comment"
8+
--------------------
9+
#lang racket/base
10+
11+
(define a 5)
12+
(equal? a a)
13+
--------------------
14+
@inspect - (equal? a a)
15+
@assertMatch test-warning-rule

test/private/rackunit.rkt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
add-suite-under-test!
1313
check-suite-refactors
1414
check-suite-does-not-refactor
15-
check-suite-analysis)
15+
check-suite-analysis
16+
check-suite-comment-only)
1617

1718

1819
(require racket/logging
@@ -312,6 +313,47 @@
312313
(fail-check "analysis assigned an incorrect value for the given syntax property key"))))
313314

314315

316+
(define-check (check-suite-comment-only program context-list target rule-name)
317+
(define suite (current-suite-under-test))
318+
(set! program (code-block-append (current-header) program))
319+
(define program-src (string-source (code-block-raw-string program)))
320+
(define-values (call-with-logs-captured build-logs-info) (make-log-capture-utilities))
321+
322+
(define result-set
323+
(call-with-logs-captured
324+
(λ () (resyntax-analyze program-src
325+
#:suite suite
326+
#:timeout-ms (current-analyzer-timeout-millis)))))
327+
328+
(define results (refactoring-result-set-results result-set))
329+
330+
;; Find target location
331+
(define target-src (string-source (string-trim (code-block-raw-string target))))
332+
(define context-src-list
333+
(for/list ([ctx (in-list context-list)])
334+
(string-source (string-trim (code-block-raw-string ctx)))))
335+
336+
;; Try to find a result that matches the target location and rule name
337+
(define matching-results
338+
(for/list ([result (in-list results)]
339+
#:when (and (equal? (refactoring-result-rule-name result) rule-name)
340+
(not (refactoring-result-has-fix? result))))
341+
result))
342+
343+
(with-check-info (['logs (build-logs-info)]
344+
['program (string-block-info (string-source-contents program-src))]
345+
['target (string-block-info (string-source-contents target-src))]
346+
['rule-name rule-name])
347+
(when (empty? matching-results)
348+
(fail-check "no warning-only result found for the specified rule"))
349+
350+
(when (> (length matching-results) 1)
351+
(fail-check (format "found ~a warning-only results, expected exactly 1" (length matching-results))))
352+
353+
;; Success - we found exactly one matching warning-only result
354+
(void)))
355+
356+
315357
(define (source-find-path-of src target-src #:contexts [context-srcs '()])
316358
(define stx (syntax-label-paths (source-read-syntax src) 'source-path))
317359
(define target-as-string (string-source-contents target-src))

0 commit comments

Comments
 (0)