test-check 2019-04-15

@defndaines has joined the channel

Recently learned that prn-str and edn/read-string are not symmetric, thanks to playing around with test.check.

(defspec prop-symmetric
  ;; NOTE This actually fails. prn-str and read-string are not symmetric.
  100
  (prop/for-all [s gen/any]
    (let [encoded (prn-str s)]
      (and (string? encoded)
           (= s (edn/read-string encoded))))))

this sounds like a good trivia question

👍🏽 1

(Note, there isn’t any official documentation that they are supposed to be, if you exclude the comment here https://clojuredocs.org/clojure.edn/read-string#example-542dc112e4b05f4d257a2993 from phreed)

the first thing I can think of is things like (keyword " ")

I can't remember if gen/any tries to generate those though; I think it doesn't

I'm 99.7% sure it doesn't after thinking about it for two more seconds

(symbol "true") would be problematic; I don't think it avoids that, but it's also probably highly unlikely to generate it by chance

I know that a Double/NAN is one thing that isn’t symmetric.

I'm sure there's something much more basic

oh, well that probably reads back in as the correct object but = won't be true

that says more about = than it does about prn-str and edn/read-string

Ah, true. Would have to use a specialized = to see what else pops up.

(symbol "nil") is easier to generate than (symbol "true") I guess

there's a lot more stuff that would come up if gen/any was more ambitious

e.g., queues, sorted sets, sorted maps, ...