Fork me on GitHub
#test-check
<
2019-04-15
>
defndaines14:04:38

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))))))

gfredericks14:04:30

this sounds like a good trivia question

4
defndaines14:04:34

(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)

gfredericks14:04:19

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

gfredericks14:04:50

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

gfredericks14:04:59

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

gfredericks14:04:37

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

defndaines14:04:44

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

gfredericks14:04:44

I'm sure there's something much more basic

gfredericks14:04:03

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

gfredericks14:04:19

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

defndaines14:04:49

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

gfredericks14:04:59

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

gfredericks14:04:24

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

gfredericks14:04:56

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