[?] Spec error: please tell me the reason.

33 views
Skip to first unread message

Philos Kim

unread,
Oct 30, 2019, 10:44:46 PM10/30/19
to Clojure
I don't know why the following spec error occurred.

Could anyone tell me why?

(ns spec-test 
   (:require [clojure.spec.alpha :as s]
             [clojure.spec.test.alpha :as stest]))

;;; spec definitions

(s/def :msds.fontspec/tag #{:fontspec})

(s/def ::id string?)
(s/def ::size int?)
(s/def ::family string?)
(s/def ::color string?)

(s/def :msds.fontspec/attrs 
  (s/keys :req-un [::id ::size ::family ::color]))

(s/def :msds.fontspec/content (s/cat))

(s/def ::fontspec
  (s/keys :req-un [:msds.fontspec/tag :msds.fontspec/attrs :msds.fontspec/content]))

(s/def ::fontspecs (s/* ::fontspec))


;;; spec application

(defn transform-fontspecs
  "fontspecs를 사용하기 편리한 형태로 변환한다."
  [fontspecs]
  (->> fontspecs
       (map (fn [{:keys [attrs] :as fontspec}]
              [(get attrs :id) (dissoc attrs :id)] ))
       (into {}) ))

(s/fdef transform-fontspecs
  :args ::fontspecs)

(stest/instrument `transform-fontspecs)


;;; test code

(def test-sample
  '({:tag :fontspec,
     :attrs {:id "283f1EV", :size 21, :family "c2S6", :color ""},
     :content ()}
    {:tag :fontspec,
     :attrs
     {:id "BzYMt4eEm", :size -5, :family "q0xhOE3", :color "bhV3PsVh"},
     :content ()}
    {:tag :fontspec,
     :attrs {:id "uA2i", :size -5, :family "HHGoD", :color "J47ZX935"},
     :content ()}))

(transform-fontspecs test-sample)
2. Unhandled clojure.lang.Compiler$CompilerException
   Error compiling src/spec_test.clj at (55:1)
   #:clojure.error{:phase :compile-syntax-check,
                   :line 55,
                   :column 1,
                   :source "/home/philos/work/metapdf/src/spec_test.clj"}
             Compiler.java: 7648  clojure.lang.Compiler/load
                      REPL:    1  metapdf.core/eval19243
                      REPL:    1  metapdf.core/eval19243
             Compiler.java: 7177  clojure.lang.Compiler/eval
             Compiler.java: 7132  clojure.lang.Compiler/eval
                  core.clj: 3214  clojure.core/eval
                  core.clj: 3210  clojure.core/eval
                  main.clj:  437  clojure.main/repl/read-eval-print/fn
                  main.clj:  437  clojure.main/repl/read-eval-print
                  main.clj:  458  clojure.main/repl/fn
                  main.clj:  458  clojure.main/repl
                  main.clj:  368  clojure.main/repl
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   79  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   55  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  142  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  171  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  170  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  748  java.lang.Thread/run

1. Caused by clojure.lang.ExceptionInfo
   Spec assertion failed.

         Spec: #object[clojure.spec.alpha$regex_spec_impl$reify__2509 0x3660c4ee "clojure.spec.alpha$regex_spec_impl$reify__2509@3660c4ee"]
        Value: (({:tag :fontspec,
                  :attrs {:id "283f1EV", :size 21, :family "c2S6", :color ""},
                  :content ()}
                 {:tag :fontspec,
                  :attrs
                  {:id "BzYMt4eEm", :size -5, :family "q0xhOE3", :color "bhV3PsVh"},
                  :content ()}
                 {:tag :fontspec,
                  :attrs {:id "uA2i", :size -5, :family "HHGoD", :color "J47ZX935"},
                  :content ()}))

     Problems: 

            val: ({:tag :fontspec,
                   :attrs {:id "283f1EV", :size 21, :family "c2S6", :color ""},
                   :content ()}
                  {:tag :fontspec,
                   :attrs
                   {:id "BzYMt4eEm", :size -5, :family "q0xhOE3", :color "bhV3PsVh"},
                   :content ()}
                  {:tag :fontspec,
                   :attrs {:id "uA2i", :size -5, :family "HHGoD", :color "J47ZX935"},
                   :content ()})
             in: [0]
         failed: map?
           spec: :spec-test/fontspec

                 alpha.clj:  132  clojure.spec.test.alpha/spec-checking-fn/conform!
                 alpha.clj:  140  clojure.spec.test.alpha/spec-checking-fn/fn
               RestFn.java:  408  clojure.lang.RestFn/invoke
             spec_test.clj:   55  spec-test/eval19293
             spec_test.clj:   55  spec-test/eval19293
             Compiler.java: 7177  clojure.lang.Compiler/eval
             Compiler.java: 7636  clojure.lang.Compiler/load
                      REPL:    1  metapdf.core/eval19243
                      REPL:    1  metapdf.core/eval19243
             Compiler.java: 7177  clojure.lang.Compiler/eval
             Compiler.java: 7132  clojure.lang.Compiler/eval
                  core.clj: 3214  clojure.core/eval
                  core.clj: 3210  clojure.core/eval
                  main.clj:  437  clojure.main/repl/read-eval-print/fn
                  main.clj:  437  clojure.main/repl/read-eval-print
                  main.clj:  458  clojure.main/repl/fn
                  main.clj:  458  clojure.main/repl
                  main.clj:  368  clojure.main/repl
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   79  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   55  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  142  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  171  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  170  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  748  java.lang.Thread/run

Sean Corfield

unread,
Oct 30, 2019, 11:51:33 PM10/30/19
to Clojure Mailing List
s/fdef's :args should be a sequence of the arguments to the function. In this case it should be :args (s/cat :fontspecs ::fontspecs) which indicates it is a sequence with one element, named :fontspecs, whose spec is ::fontspecs

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/4254452b-74af-4741-9772-ee892f57166f%40googlegroups.com.


--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Philos Kim

unread,
Oct 31, 2019, 12:02:15 AM10/31/19
to Clojure
Thanks for your answer!

I corrected the code like the following according to your advice.

(s/fdef transform-fontspecs
  :args (s/cat :fontspecs ::fontspecs))

However, the same error occurs again.

2019년 10월 31일 목요일 오전 11시 44분 46초 UTC+9, Philos Kim 님의 말:

Sean Corfield

unread,
Oct 31, 2019, 12:52:06 AM10/31/19
to Clojure Mailing List
(s/def ::fontspecs (s/coll-of ::fontspec))

Sorry, I missed that error first time around. s/* is a regex spec but your argument should be a collection of ::fontspec

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Sean Corfield

unread,
Oct 31, 2019, 12:52:45 AM10/31/19
to Clojure Mailing List
With that correction, you should see:

spec-test=> (transform-fontspecs test-sample)
{"283f1EV" {:size 21, :family "c2S6", :color ""}, "BzYMt4eEm" {:size -5, :family "q0xhOE3", :color "bhV3PsVh"}, "uA2i" {:size -5, :family "HHGoD", :color "J47ZX935"}}
spec-test=>
Reply all
Reply to author
Forward
0 new messages