|
8 | 8 | set-header! |
9 | 9 | add-suite-under-test! |
10 | 10 | check-suite-refactors |
11 | | - check-suite-does-not-refactor) |
| 11 | + check-suite-does-not-refactor |
| 12 | + check-suite-analysis) |
12 | 13 |
|
13 | 14 |
|
14 | 15 | (require racket/logging |
|
34 | 35 | resyntax/private/source |
35 | 36 | resyntax/private/string-indent |
36 | 37 | resyntax/private/string-replacement |
| 38 | + resyntax/private/syntax-path |
| 39 | + resyntax/private/syntax-property-bundle |
| 40 | + resyntax/private/syntax-traversal |
37 | 41 | syntax/modread |
38 | 42 | syntax/parse |
39 | 43 | (except-in racket/list range)) |
|
156 | 160 | (with-handlers |
157 | 161 | ([exn:fail? |
158 | 162 | (λ (e) |
159 | | - (with-check-info (['logs (build-logs-info)] |
160 | | - ['original (string-block-info (code-block-raw-string original-program))] |
161 | | - ['expected (string-block-info expected-program)] |
162 | | - ['exception e]) |
| 163 | + (with-check-info |
| 164 | + (['logs (build-logs-info)] |
| 165 | + ['original (string-block-info (code-block-raw-string original-program))] |
| 166 | + ['expected (string-block-info expected-program)] |
| 167 | + ['exception e]) |
163 | 168 | (fail-check "an error occurred while processing refactoring results")))]) |
164 | 169 | (call-with-logs-captured |
165 | 170 | (λ () (modified-source-contents (refactoring-result-set-updated-source result-set)))))) |
|
212 | 217 | (fail-check "the program was not changed, but no-op fixes were suggested")))))) |
213 | 218 |
|
214 | 219 |
|
| 220 | +(define-check (check-suite-analysis program context-list target property-key expected-value) |
| 221 | + (define suite (current-suite-under-test)) |
| 222 | + (set! program (code-block-append (current-header) program)) |
| 223 | + (define program-src (string-source (code-block-raw-string program))) |
| 224 | + (define-values (call-with-logs-captured build-logs-info) (make-log-capture-utilities)) |
| 225 | + |
| 226 | + (define actual-props |
| 227 | + (call-with-logs-captured |
| 228 | + (λ () (reysntax-analyze-for-properties-only program-src)))) |
| 229 | + |
| 230 | + (define target-src (string-source (string-trim (code-block-raw-string target)))) |
| 231 | + (define context-src-list |
| 232 | + (for/list ([ctx (in-list context-list)]) |
| 233 | + (string-source (string-trim (code-block-raw-string ctx))))) |
| 234 | + |
| 235 | + (define target-path (source-find-path-of program-src target-src #:contexts context-src-list)) |
| 236 | + |
| 237 | + (unless target-path |
| 238 | + (with-check-info (['logs (build-logs-info)] |
| 239 | + ['program (string-block-info (string-source-contents program-src))] |
| 240 | + ['target (string-block-info (string-source-contents target-src))]) |
| 241 | + (fail-check "could not locate target subform within the given program"))) |
| 242 | + |
| 243 | + (define (fail-property-lookup) |
| 244 | + (define target-properties |
| 245 | + (syntax-property-bundle-get-immediate-properties actual-props target-path)) |
| 246 | + (with-check-info (['logs (build-logs-info)] |
| 247 | + ['program (string-block-info (string-source-contents program-src))] |
| 248 | + ['target (string-block-info (string-source-contents target-src))] |
| 249 | + ['target-properties target-properties] |
| 250 | + ['property-key property-key]) |
| 251 | + (fail-check "analysis did not assign a value for the given syntax property key"))) |
| 252 | + |
| 253 | + (define actual-value |
| 254 | + (syntax-property-bundle-get-property actual-props target-path property-key fail-property-lookup)) |
| 255 | + |
| 256 | + (unless (equal? actual-value expected-value) |
| 257 | + (with-check-info (['logs (build-logs-info)] |
| 258 | + ['program (string-block-info (string-source-contents program-src))] |
| 259 | + ['target (string-block-info (string-source-contents target-src))] |
| 260 | + ['property-key property-key] |
| 261 | + ['actual actual-value] |
| 262 | + ['expected expected-value]) |
| 263 | + (fail-check "analysis assigned an incorrect value for the given syntax property key")))) |
| 264 | + |
| 265 | + |
| 266 | +(define (source-find-path-of src target-src #:contexts [context-srcs '()]) |
| 267 | + (define stx (syntax-label-paths (source-read-syntax src) 'source-path)) |
| 268 | + (define target-as-string (string-source-contents target-src)) |
| 269 | + |
| 270 | + (define target-stx |
| 271 | + (let loop ([stx stx] [context-srcs context-srcs]) |
| 272 | + (match context-srcs |
| 273 | + ['() |
| 274 | + (syntax-find-first stx subform |
| 275 | + #:when (equal? (source-text-of src (attribute subform)) target-as-string))] |
| 276 | + [(cons next-context remaining-contexts) |
| 277 | + (define next-as-string (string-source-contents next-context)) |
| 278 | + (define substx |
| 279 | + (syntax-find-first stx subform |
| 280 | + #:when (equal? (source-text-of src (attribute subform)) next-as-string))) |
| 281 | + (and substx (loop substx remaining-contexts))]))) |
| 282 | + |
| 283 | + (and target-stx (syntax-property target-stx 'source-path))) |
| 284 | + |
| 285 | + |
| 286 | +(module+ test |
| 287 | + (test-case "source-find-path-of" |
| 288 | + |
| 289 | + (test-case "no #lang" |
| 290 | + (define src (string-source "(+ a b c)")) |
| 291 | + (define target (string-source "b")) |
| 292 | + (check-equal? (source-find-path-of src target) (syntax-path (list 2)))) |
| 293 | + |
| 294 | + (test-case "simple #lang" |
| 295 | + (define src (string-source "#lang racket (define a 1)")) |
| 296 | + (define target (string-source "a")) |
| 297 | + (check-equal? (source-find-path-of src target) (syntax-path (list 3 1 1)))) |
| 298 | + |
| 299 | + (test-case "single context" |
| 300 | + (define src (string-source "(list (+ a) (* a))")) |
| 301 | + (define target (string-source "a")) |
| 302 | + (define contexts (list (string-source "(* a)"))) |
| 303 | + (check-equal? (source-find-path-of src target #:contexts contexts) (syntax-path (list 2 1)))) |
| 304 | + |
| 305 | + (test-case "multiple contexts" |
| 306 | + (define src (string-source "(+ a (+ a (+ a (+ a))))")) |
| 307 | + (define target (string-source "a")) |
| 308 | + (define contexts |
| 309 | + (list (string-source "(+ a (+ a (+ a)))") |
| 310 | + (string-source "(+ a (+ a))") |
| 311 | + (string-source "(+ a)"))) |
| 312 | + |
| 313 | + (define actual-path (source-find-path-of src target #:contexts contexts)) |
| 314 | + |
| 315 | + (check-equal? actual-path (syntax-path (list 2 2 2 1)))))) |
| 316 | + |
| 317 | + |
215 | 318 | (define (refactoring-result-set-matched-rules-info result-set) |
216 | 319 | (define matches |
217 | 320 | (transduce (refactoring-result-set-results result-set) |
|
0 commit comments