8000 Fix :map-of :min and unreachable generator, explain such-that failures by frenchy64 · Pull Request #1029 · metosin/malli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix :map-of :min and unreachable generator, explain such-that failures #1029

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

Merged
merged 4 commits into from
Apr 14, 2024

Conversation

frenchy64
Copy link
Collaborator
@frenchy64 frenchy64 commented Mar 30, 2024
  1. The :map-of generator should use (gen/vector-distinct-by first ..) in order to satisfy :min, otherwise it can generate less than the minimum number of entries by generating the same key multiple times with different vals.

  2. such-that and functions built on it accept a custom error message on generation failures. It's very helpful to have the precise schema that failed in the error message.

  3. The unreachable elements case for :map-of disallows generating the empty map if :max was non-zero. It's perfectly fine to generate empty regardless of :max. Other collection generators almost made this mistake by checking (<= (or min 0) 0 (or max 0)), which is technically correct but simplifiable to (= 0 (or min 0)).

  4. coll-distinct-gen nested generators incorrectly in the unreachable elements case. We want (-never-gen options), not (gen/fmap f (-never-gen options)) which defeats our simplification logic of checking for -never-gen's metadata.

@frenchy64 frenchy64 force-pushed the such-that-error-msg branch 2 times, most recently from fa56647 to 4540b39 Compare March 30, 2024 18:14
@frenchy64 frenchy64 force-pushed the such-that-error-msg branch from 4540b39 to 9e947ff Compare March 30, 2024 18:14
@frenchy64 frenchy64 marked this pull request as ready for review March 30, 2024 18:18
@frenchy64 frenchy64 changed the title Fix :map-of :min generator, explain such-that failures WIP: Fix :map-of :min generator, explain such-that failures Mar 30, 2024
@frenchy64 frenchy64 marked this pull request as draft March 30, 2024 18:31
@frenchy64 frenchy64 changed the title WIP: Fix :map-of :min generator, explain such-that failures WIP: Fix :map-of :min generator, explain such-that failures, fix unreachable child cases Mar 30, 2024
@frenchy64 frenchy64 changed the title WIP: Fix :map-of :min generator, explain such-that failures, fix unreachable child cases WIP: Fix :map-of :min generator, explain such-that failures, fix unreachable elements cases Mar 30, 2024
@frenchy64 frenchy64 changed the title WIP: Fix :map-of 10000 :min generator, explain such-that failures, fix unreachable elements cases Fix :map-of :min generator, explain such-that failures, fix unreachable elements cases Mar 30, 2024
@frenchy64 frenchy64 marked this pull request as ready for review March 30, 2024 18:59
@frenchy64 frenchy64 changed the title Fix :map-of :min generator, explain such-that failures, fix unreachable elements cases Fix :map-of :min generator, explain such-that failures, fix unreachable :map-of case Mar 30, 2024
@frenchy64 frenchy64 changed the title Fix :map-of :min generator, explain such-that failures, fix unreachable :map-of case Fix :map-of :min and unreachable generator, explain such-that failures Mar 30, 2024
Copy link
Member
@ikitommi ikitommi left a comment

Choose a reason for hiding this comment

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

Looks good, comment about throwing & errors.

(gen/return (f []))
(-never-gen options))
(gen/fmap f (gen/vector-distinct gen {:min-elements min, :max-elements max, :max-tries 100
:ex-fn #(ex-info (str "Could not generate enough distinct elements for schema "
Copy link
Member

Choose a reason for hiding this comment

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

Malli follow a "fail fast with data, pretty errors in dev-mode" philosophy. This enables both smaller bundle sizes on ClojureScript and fast runtime in prod (things like pr-str are really slow).

e.g.

(m/schema :enum)

fails with just enough data, while:

((requiring-resolve 'malli.dev/start!))

(m/schema :enum)

pretty prints the proper text of what happened.

Would it be possible to do same here? Currently, the malli.dev/start! hi-jacks actual throwing of error, I guess you can't control that here, just generating the exception, to be throwed by test.check?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

I think it's just a matter of calling m/-exception, I'll look into it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you take another look?

@@ -412,11 +427,21 @@
(defmethod -schema-generator :< [schema options] (-double-gen {:max (-> schema (m/children options) first dec)}))
(defmethod -schema-generator :<= [schema options] (-double-gen {:max (-> schema (m/children options) first)}))
(defmethod -schema-generator := [schema options] (gen/return (first (m/children schema options))))
(defmethod -schema-generator :not= [schema options] (gen/such-that #(not= % (-> schema (m/children options) first)) gen/any-printable 100))
(defmethod -schema-generator :not= [schema options] (gen/such-that #(not= % (-> schema (m/children options) first)) gen/any-printable
{:max-tries 100
Copy link
Member

Choose a reason for hiding this comment

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

some of these have :max-tries, but not all, is there a reason for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My best guess is that the ones without :max-tries always succeed in practice.

@ikitommi ikitommi merged commit 9858157 into metosin:master Apr 14, 2024
@ikitommi
Copy link
Member
ikitommi commented 8869 Apr 14, 2024

Thanks! Calling the m/-exception doesn't currently enable the pretty printing, but will figure how to do that separate of this.

@ikitommi ikitommi mentioned this pull request Apr 20, 2024
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
0