<commit_before>(ns test.immutant.messaging.codecs
  (:use [immutant.messaging.codecs])
  (:use [clojure.test]))

(defn session-mock []
  (proxy [javax.jms.Session] []
    (createTextMessage [text]
      (let [properties (java.util.HashMap.)]
        (proxy [javax.jms.TextMessage] []
          (getText [] text)
          (setStringProperty [k,v]
            (.put properties k v))
          (getStringProperty [k]
            (.get properties k)))))))

(defmacro test-for [name message encoding]
  (list 'deftest name
        `(let [~'message ~message
               ~'encoded (encode (session-mock) ~'message {:encoding ~encoding})]
           (is (= (decode ~'encoded) ~'message)))))

(test-for simple-json     "a random text message"       :json)
(test-for simple-clojure  "a simple text message"       :clojure)
(test-for complex-json    {:a "b" :c [1 2 3 {:foo 42}]} :json)
(test-for complex-clojure {:a "b" :c [1 2 3 {:foo 42}]} :clojure)

(deftest complex-json-encoding
  (let [message {:a "b" :c [1 2 3 {:foo 42}]}
        encoded (.getText (encode (session-mock) message {:encoding :json}))]
    (is (= encoded "{\"a\":\"b\",\"c\":[1,2,3,{\"foo\":42}]}"))))
<commit_msg>Never use a macro when a function will do<commit_after>(ns test.immutant.messaging.codecs
  (:use [immutant.messaging.codecs])
  (:use [clojure.test]))

(defn session-mock []
  (proxy [javax.jms.Session] []
    (createTextMessage [text]
      (let [properties (java.util.HashMap.)]
        (proxy [javax.jms.TextMessage] []
          (getText [] text)
          (setStringProperty [k,v]
            (.put properties k v))
          (getStringProperty [k]
            (.get properties k)))))))

(defn test-codec [message encoding]
  (is (= (decode (encode (session-mock) message {:encoding encoding})))))

(deftest simple-json
  (test-codec "a random text message" :json))

(deftest simple-clojure
  (test-codec "a simple text message" :clojure))

(deftest complex-json
  (test-codec {:a "b" :c [1 2 3 {:foo 42}]} :json))

(deftest complex-clojure
  (test-codec {:a "b" :c [1 2 3 {:foo 42}]} :clojure))

(deftest complex-json-encoding
  (let [message {:a "b" :c [1 2 3 {:foo 42}]}
        encoded (.getText (encode (session-mock) message {:encoding :json}))]
    (is (= encoded "{\"a\":\"b\",\"c\":[1,2,3,{\"foo\":42}]}"))))

{'fraction_non_alphanumeric': 0.1918402777777778, 'fraction_numerical': 0.017361111111111112, 'mean_word_length': 3.043859649122807, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '17242', 'n_tokens_mistral': 862, 'n_tokens_neox': 805, 'n_words': 220}