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

Capturing of printed lines in tests #15

Closed
matthewdowney opened this issue Jan 20, 2023 · 1 comment
Closed

Capturing of printed lines in tests #15

matthewdowney opened this issue Jan 20, 2023 · 1 comment

Comments

@matthewdowney
Copy link
Owner

Sometimes I'll sprinkle printlns throughout a comment to demonstrate side effects or order of events.

Like

(comment
  (try
    (println "Happens first") 
    (throw (ex-info "" {}))
    (catch Exception e (println "Happens after exception"))
    (finally (println "Happens last no matter what")))
  )

(Or more realistically, see this comment from ptaoussanis.)

It'd be nice to have a helper to assert this behavior with rct, something like:

^:rct/test
(comment
  (with-printlns
    (try
      (println "Happens first")
      (throw (ex-info "" {}))
      (catch Exception e (println "Happens after exception"))
      (finally (println "Happens last no matter what"))))
  ;=>> ["Happens first" 
  ;     "Happens after exception" 
  ;     "Happens last no matter what"]
  )

I want something different than with-out-str because (1) I want it to still print, and (2) sometimes weird binding conveyance stuff happens with with-out-str.

E.g. using the promesa library:

(with-out-str
  (let [p (-> (p/future (Thread/sleep 50) (println "Future"))
              (p/handle (fn [_ __] (println "Finally"))))]
    (println "Built")
    @p))

; Finally
;=> "Built\nFuture\n"

The "Finally" is printed and the rest is captured by with-out-str.

@matthewdowney
Copy link
Owner Author

The downside would be that if this goes in a util namespace or something, anyone who wants to use it would want to depend on rct within the codebase under test.

But it would be optional, so no forced dependency.

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

No branches or pull requests

1 participant