Public Vars

Back

with-out-str (clj)

(source)

macro

(with-out-str & body)
Evaluates exprs in a context in which *out* is bound to a fresh StringWriter. Returns the string created by any nested printing calls.

Examples

logseq/logseq
(ns frontend.pubsub
  "All mults and pubs are collected to this ns.
  vars with suffix '-mult' is a/Mult, use a/tap and a/untap on them. used by event subscribers
  vars with suffix '-pub' is a/Pub, use a/sub and a/unsub on them. used by event subscribers
  vars with suffix '-ch' is chan used by event publishers."
  {:clj-kondo/config {:linters {:unresolved-symbol {:level :off}}}}
  #?(:cljs (:require-macros [frontend.pubsub :refer [def-mult-or-pub chan-of]]))
  (:require [clojure.core.async :as a :refer [chan mult pub]]
            [clojure.core.async.impl.protocols :as ap]
            [malli.core :as m]
            [malli.dev.pretty :as mdp]
            [clojure.pprint :as pp]))

(defmacro def-mult-or-pub
  "define following vars:
  - `symbol-name`-ch for event publisher.
  - `symbol-name`-mult or `symbol-name`-pub for event subscribers.
  - `symbol-name`-validator is malli schema validator
  def -pub var when `:topic-fn` exists otherwise -mult var"
  [symbol-name doc-string malli-schema & {:keys [ch-buffer topic-fn]
                                          :or   {ch-buffer 1}}]
  (let [schema-validator-name (symbol (str symbol-name "-validator"))
        schema-name           (symbol (str symbol-name "-schema"))
        ch-name               (symbol (str symbol-name "-ch"))
        mult-or-pub-name      (if topic-fn
                                (symbol (str symbol-name "-pub"))
                                (symbol (str symbol-name "-mult")))
        doc-string*           (str doc-string "\nMalli-schema:\n" (with-out-str (pp/pprint malli-schema)))]
    `(do
       (def ~schema-name ~malli-schema)
       (def ~schema-validator-name (m/validator ~malli-schema))
       (def ~ch-name ~doc-string* (chan-of ~malli-schema ~schema-validator-name ~ch-buffer))
       ~(if topic-fn
          `(def ~mult-or-pub-name ~doc-string* (pub ~ch-name ~topic-fn))
          `(def ~mult-or-pub-name ~doc-string* (mult ~ch-name))))))
originrose/cortex
(ns cortex.util-test
  (:refer-clojure :exclude [defonce])
  (:require
    #?(:cljs [cljs.test :refer-macros [deftest is testing]]
       :clj [clojure.test :refer [deftest is testing]])
    [clojure.core.matrix :as m]
    [clojure.string :as str]
    [cortex.util :refer :all]))

(deftest sprintf-test
  (is (= (with-out-str (sprintf "%(d%n" [[1 -3 5] [-7 3]]))
         (format "[[1 (3) 5] [(7) 3]]%n"))))
originrose/cortex
(ns cortex.optimise.util-test
  (:refer-clojure :exclude [defonce])
  (:require [cortex.optimise.util :refer :all]
            [clojure.core.matrix :as m]
            [clojure.test :refer :all]))

(deftest sprintf-test
  (is (= (with-out-str (sprintf "%(d%n" [[1 -3 5] [-7 3]]))
         (format "[[1 (3) 5] [(7) 3]]%n"))))
hyperfiddle/rcf
(ns hyperfiddle.rcf.example-test
  (:require [clojure.core.async :refer [chan >! go go-loop <! timeout close!]]
            [hyperfiddle.rcf :as rcf :refer [tests tap % with]]
            [missionary.core :as m]))

(tests
 "Macros are supported"
 (with-out-str 
   "print is captured"
   (print "hello")
   1 := 1)
 := "hello")
marick/structural-typing
(ns structural-typing.use.condensed-type-descriptions.f-explain-with
  (:require [structural-typing.assist.oopsie :as oopsie])
  (:use midje.sweet
        structural-typing.type
        structural-typing.global-type
        structural-typing.clojure.core
        structural-typing.assist.testutil)
  (:refer-clojure :except [any?]))

(type! :Account {:password good-password?})
(fact
  (with-out-str (built-like :Account {:password "foo"}))
  => #":password should contain a digit")