Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec-assertion-thrown? test helper #55

Merged
merged 4 commits into from
Jun 7, 2019
Merged

Conversation

thumbnail
Copy link
Member

Migrated from nedap/utils.test#2

@thumbnail thumbnail requested a review from vemv May 27, 2019 13:14
@vemv
Copy link
Contributor

vemv commented May 27, 2019

Seems unlikely to conflict, but just in case I'll review after the bigger PR

@thumbnail
Copy link
Member Author

but just in case I'll review after the bigger PR

If you're referencing to #45, this'll probably break compatibility because of the import for example. happy to update after #45 tho.

@vemv
Copy link
Contributor

vemv commented Jun 2, 2019

It was https://github.com/nedap/utils.spec/pull/54

Yours is the only PR now, feel free to rebase :)

@thumbnail thumbnail force-pushed the add-check-violated branch from 48b47f4 to 9cbd456 Compare June 4, 2019 14:10
@thumbnail
Copy link
Member Author

I can't seem to get the cljs case working. for some reason the assert-expr-multimethod isn't defined at the time of loading the file so the compiler warns (and the impl crashes on undefined).

according to this page it should be possible; but I can't seem to get it to work.

@vemv would you care to take a look some time?

@vemv
Copy link
Contributor

vemv commented Jun 4, 2019

The defmethod should work indeed,
https://github.com/weavejester/cljfmt/blob/3c6fbde59cd931972feafe73917866064c9d7b08/cljfmt/test/cljfmt/test_util/cljs.cljc is a known-good example

Will review

README.md Outdated Show resolved Hide resolved
src/nedap/utils/spec/api.cljc Outdated Show resolved Hide resolved
src/nedap/utils/spec/api.cljc Outdated Show resolved Hide resolved
@@ -27,3 +30,24 @@
(let [m (coerce/coerce spec m)]
(cond-> m
(not (spec/valid? spec m)) (assoc ::invalid? true))))

; note that cljs.test/assert-expr expects 3 params, clojure.test/assert-expr 2
(defmethod test/assert-expr 'check-violated? [& params]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason of your issue appears to be that you should only target the JVM:

https://github.com/weavejester/cljfmt/blob/3c6fbde59cd931972feafe73917866064c9d7b08/cljfmt/test/cljfmt/test_util/cljs.cljc

Note the #?(:clj wrapping at L5

It kind of makes sense: I don't expect something that uses syntax-quote, unquote-splicing, etc to work in vanilla cljs (similarly to how you don't have defmacro in vanilla cljs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did find a very similar example in cljs.test.cljc as well; so while I'm happy to just target the jvm for this helper, I figured it would just be possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the cljs compiler can host itself, but we aren't targeting self-hosted cljs - only vanilla one.

So afaict that's an inaccurate example.

cljfmt can be run from JVM-compiled ("vanilla") clojurescript, so when I said "target the JVM", you are not excluding vanilla cljs.

Sorry for the confusion!

test/unit/nedap/utils/api.cljc Outdated Show resolved Hide resolved
test/unit/nedap/utils/api.cljc Outdated Show resolved Hide resolved
test/unit/nedap/utils/api.cljc Outdated Show resolved Hide resolved
test/unit/nedap/utils/api.cljc Outdated Show resolved Hide resolved
src/nedap/utils/spec/api.cljc Outdated Show resolved Hide resolved
@thumbnail
Copy link
Member Author

Had some trouble because of the 'expansion' of specs in speced/defn.

It'd require the user of spec-assertion-thrown? to know internals of speced/defn, because:

  (is (spec-assertion-thrown? 'number? (accepts-number "1234")))

Would fail ('number? != (clojure.spec.alpha/and number? (instance ....))).

To mitigate this, I check for either an exact match; or a derived match (using the same fn as defn does; parsing/infer-spec-from-symbol). This works quite well actually :)

@thumbnail thumbnail force-pushed the add-check-violated branch from e290250 to a66f9da Compare June 6, 2019 07:21
@@ -0,0 +1,19 @@
(ns unit.nedap.utils.speced.spec-assertion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this test be a .cljc one and added to the runner ns?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have had little luck getting any of this run on cljs. If i convert to cljc the jvm / cljs compiler throws the following error;

Syntax error (IllegalAccessError) compiling at (nedap/utils/spec/impl/spec_assertion.cljc:1:1).
infer-spec-from-symbol does not exist

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I managed to narrow this behavior down to a local issue. This suggestion has been addressed.

note that the assertions only run in clj.

Copy link
Contributor

@vemv vemv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have had little luck getting any of this run on cljs.

Can give it a shot after a last round of feedback

src/nedap/utils/spec/impl/spec_assertion.cljc Outdated Show resolved Hide resolved
[nedap.utils.spec.impl.parsing :refer [infer-spec-from-symbol]])
#?(:clj (:import (clojure.lang ExceptionInfo))))

(defn assert-spec-failure [msg spec-sym body]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a check! for these args

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use speced/defn because it'll be a circular dep; but neither does manually using check! seem to work; after importing like this:

[nedap.utils.spec.impl.check #?(:clj :refer :cljs :refer-macros) [check!]]

it throws;

Syntax error compiling at (nedap/utils/spec/impl/spec_assertion.cljc:9:1).
Can't take value of a macro: #'nedap.utils.spec.impl.check/check!

src/nedap/utils/spec/impl/spec_assertion.cljc Outdated Show resolved Hide resolved
@nedap nedap deleted a comment from thumbnail Jun 6, 2019
thumbnail added 2 commits June 6, 2019 19:48
 - support expanded defn specs
 - rename fns
 - remove import aliases
@thumbnail thumbnail force-pushed the add-check-violated branch from f38e163 to 03bf52b Compare June 6, 2019 17:52
Copy link
Contributor

@vemv vemv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm after final coverage. Thanks for this one!

I'm ok with having this feature as JVM only for now (which should be cleanly offered: no cljs version should be provided at all). A few other things aren't offered for cljs either currently.

Accordingly please create an issue for cljs compat, and include as part of that task adding the check! that was tricky

@thumbnail thumbnail changed the title Add check-violated test helper Add spec-assertion-thrown? test helper Jun 7, 2019
@thumbnail thumbnail merged commit 484d5ab into master Jun 7, 2019
@thumbnail thumbnail deleted the add-check-violated branch June 7, 2019 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants