Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whitespace sensitive testing for aligned multi-line strings #37

Merged
merged 6 commits into from
May 23, 2024

Conversation

crisptrutski
Copy link
Collaborator

@crisptrutski crisptrutski commented May 23, 2024

Unfortunately when you visually align multi-line strings in Clojure, you will accumulate an additional margin due to the current indentation level:

(println "A
            B
          C")

;; A
;;            B
;;          C

Things get worse if you want to define before and after strings with different indentations:

(is (= "A
          X
        C"
       (str/replace "A
                       B
                     C"
                    "B" "X"))) ;; NOPE T_T

Our workaround for this was to collapse contiguous whitespace, but in that case we could have false negatives where we don't detect whitespace being mangled.

This change brings in a method that strips the common indentation from the extra lines, so that the alignment in the actual strings can match how it looks in one's code, emulating the way that multi-line strings are defined in modern languages like Rust.

It also introduces the ws= macro, which strips the common indentation from all string literals within the given form before performing an equality check. This gives great ergonomics for whitespace-sensitive tests. This is a better solution than test-replacements as well, which prevented one from evaluating the inner replacement result form.

@crisptrutski crisptrutski changed the title Fix whitespace sensitive testing for aligned multi-line strings. Fix whitespace sensitive testing for aligned multi-line strings May 23, 2024
tables (into {} (for [t (:tables comps)
i (:instances (:component t))]
[i (:component t)]))
columns (index-by-instances (:columns comps))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't resist sneaking this in as well 🧼

@crisptrutski crisptrutski requested a review from piranha May 23, 2024 15:19
Copy link
Contributor

@tsmacdonald tsmacdonald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand by my statements, but am giving you a ✔️ since I trust you to do them without supervision, haha

@crisptrutski crisptrutski merged commit 831821b into master May 23, 2024
4 checks passed
@crisptrutski crisptrutski deleted the clean-ups branch May 23, 2024 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants