-
Notifications
You must be signed in to change notification settings - Fork 222
WIP: delay/future/promise schemas #1171
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
base: master
Are you sure you want to change the base?
Changes from all commits
cd7af02
216a709
e433f8c
a17fa56
dcf6909
96b46f5
6c90e3a
2c8cecc
84e7e49
881da11
b4d6f48
e381c80
167617e
0e0eb62
c79ad8f
4027f7d
c195dca
cfd89dc
1d73c5e
f8207b0
937e2b2
2789044
cbdb3a1
1b30f53
516a300
9aab145
3e10318
0b882ce
279938c
49d90d3
26dc44d
7008469
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3581,3 +3581,93 @@ | |
(is (not (m/validate [:sequential {:min 11} :int] (eduction identity (range 10))))) | ||
(is (not (m/validate [:seqable {:min 11} :int] (eduction identity (range 10))))) | ||
(is (nil? (m/explain [:sequential {:min 9} :int] (eduction identity (range 10)))))) | ||
|
||
(deftest deref-test | ||
(testing "schema matches delay" | ||
(is (m/validate [:deref :int] (delay 1))) | ||
(is (m/validate [:deref :int] (doto (delay 1) deref))) | ||
(is (m/validate [:deref {:force true} :int] (delay 1))) | ||
(is (m/validate [:deref {:force true} :int] (doto (delay 1) deref))) | ||
(is (nil? (m/explain [:deref :int] (delay 1)))) | ||
(is (nil? (m/explain [:deref :int] (doto (delay 1) deref)))) | ||
(is (nil? (m/explain [:deref {:force true} :int] (delay 1))))) | ||
(testing "schema does not match delay" | ||
(is (m/validate [:deref :boolean] (delay 1))) | ||
(is (not (m/validate [:deref :boolean] (doto (delay 1) deref)))) | ||
(is (not (m/validate [:deref {:force true} :boolean] (delay 1)))) | ||
(is (not (m/validate [:deref {:force true} :boolean] (doto (delay 1) deref)))) | ||
(is (not (m/validate [:deref :boolean] 1))) | ||
(is (nil? (m/explain [:deref :boolean] (delay 1)))) | ||
(is (m/explain [:deref :boolean] (doto (delay 1) deref))) | ||
(is (m/explain [:deref {:force true} :boolean] (delay 1))) | ||
(is (m/explain [:deref {:force true} :boolean] (doto (delay 1) deref))) | ||
(is (m/explain [:deref :boolean] 1)))) | ||
|
||
(deftest delay-test | ||
(testing "schema matches delay" | ||
(is (m/validate [:delay :int] (delay 1))) | ||
(is (m/validate [:delay :int] (doto (delay 1) deref))) | ||
(is (m/validate [:delay {:force true} :int] (delay 1))) | ||
(is (m/validate [:delay {:force true} :int] (doto (delay 1) deref))) | ||
(is (nil? (m/explain [:delay :int] (delay 1)))) | ||
(is (nil? (m/explain [:delay :int] (doto (delay 1) deref)))) | ||
(is (nil? (m/explain [:delay {:force true} :int] (delay 1))))) | ||
(testing "schema does not match delay" | ||
(is (m/validate [:delay :boolean] (delay 1))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a bit off: it should fail right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main decision here is whether we should force delays during validation. Given that they often contain blocking code, it might not be the best idea. Here, I thought I'd make the default to only check if realized, and allow the user to tell malli when it's ok to force the delay via a property. |
||
(is (not (m/validate [:delay :boolean] (doto (delay 1) deref)))) | ||
(is (not (m/validate [:delay {:force true} :boolean] (delay 1)))) | ||
(is (not (m/validate [:delay {:force true} :boolean] (doto (delay 1) deref)))) | ||
(is (not (m/validate [:delay :boolean] 1))) | ||
(is (nil? (m/explain [:delay :boolean] (delay 1)))) | ||
(is (m/explain [:delay :boolean] (doto (delay 1) deref))) | ||
(is (m/explain [:delay {:force true} :boolean] (delay 1))) | ||
(is (m/explain [:delay {:force true} :boolean] (doto (delay 1) deref))) | ||
(is (m/explain [:delay :boolean] 1)))) | ||
|
||
#?(:clj | ||
(deftest future-test | ||
(testing "schema matches future" | ||
(is (m/validate [:future :int] (future 1))) | ||
(is (m/validate [:future :int] (doto (future 1) deref))) | ||
(is (m/validate [:future {:force true} :int] (future 1))) | ||
(is (m/validate [:future {:force true} :int] (doto (future 1) deref))) | ||
(is (nil? (m/explain [:future :int] (future 1)))) | ||
(is (nil? (m/explain [:future :int] (doto (future 1) deref)))) | ||
(is (nil? (m/explain [:future {:force true} :int] (future 1))))) | ||
(testing "schema does not match future" | ||
(let [p (promise) | ||
f (future @p 1)] | ||
(is (m/validate [:future :boolean] f)) | ||
(is (nil? (m/explain [:future :boolean] f))) | ||
(deliver p true)) | ||
(is (not (m/validate [:future :boolean] (doto (future 1) deref)))) | ||
(is (not (m/validate [:future {:force true} :boolean] (future 1)))) | ||
(is (not (m/validate [:future {:force true} :boolean] (doto (future 1) deref)))) | ||
(is (not (m/validate [:future :boolean] 1))) | ||
(is (m/explain [:future :boolean] (doto (future 1) deref))) | ||
(is (m/explain [:future {:force true} :boolean] (future 1))) | ||
(is (m/explain [:future {:force true} :boolean] (doto (future 1) deref))) | ||
(is (m/explain [:future :boolean] 1))))) | ||
|
||
#?(:clj | ||
(deftest promise-test | ||
(testing "schema matches promise" | ||
(is (m/validate [:promise :int] (doto (promise) (deliver 1)))) | ||
(is (m/validate [:promise {:force true} :int] (doto (promise) (deliver 1)))) | ||
(is (m/validate [:promise {:force true} :int] (doto (promise) (deliver 1)))) | ||
(is (nil? (m/explain [:promise :int] (doto (promise) (deliver 1))))) | ||
(is (nil? (m/explain [:promise :int] (doto (promise) (deliver 1))))) | ||
(is (nil? (m/explain [:promise {:force true} :int] (doto (promise) (deliver 1)))))) | ||
(testing "schema does not match promise" | ||
(is (m/validate [:promise :boolean] (promise))) | ||
(is (not (m/validate [:promise :boolean] (doto (promise) (deliver 1))))) | ||
(is (not (m/validate [:promise {:force true} :boolean] (doto (promise) (deliver 1))))) | ||
(is (not (m/validate [:promise {:force true} :boolean] (doto (promise) (deliver 1))))) | ||
(is (not (m/validate [:promise :boolean] 1))) | ||
(is (not (m/validate [:promise :boolean] (future)))) | ||
(is (not (m/validate [:promise :boolean] (delay)))) | ||
(is (nil? (m/explain [:promise :boolean] (promise)))) | ||
(is (m/explain [:promise :boolean] (doto (promise) (deliver 1)))) | ||
(is (m/explain [:promise {:force true} :boolean] (doto (promise) (deliver 1)))) | ||
(is (m/explain [:promise {:force true} :boolean] (doto (promise) (deliver 1)))) | ||
(is (m/explain [:promise :boolean] 1))))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider adding tests for get-in / walk behaviour, there's been some bugs recently in those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the naming, but I fear confusion with
m/deref
. Not sure what to do about that. Docstring?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My original intention for this name was to have an overarching
[:deref S]
schema. I couldn't decide whether it should be a simplereify
or a disjunction of:delay
/:promise
/:future
.I'm not sure if providing a schema called
:deref
increases or reduces clarity here.