-
Notifications
You must be signed in to change notification settings - Fork 29
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
Feature request: specs for constant values #68
Comments
I've been thinking about this a bit and discussing options with @wojtekmach. I've held off on implementing Constants are a bit trickier. Its possible to implement something like a constant now, but its verbose and not very elegant: Even with all of this, I don't think we have good support yet for your specific example. You could make it work, but again, its inelegant. The problem is that we don't have a good way to describe sequences of values. We can describe maps and generic collections. But we can't say, "this is a list where the first element should be 'users', the second element should be a UUID, and the third element should be 'posts'". I've been thinking for a bit that we need to support something like So with all of these ideas together, we could re-write your example like so: def id_spec(), do: spec(is_string())
def posts_show_route(), do: cat([users: "users", user_id: id_spec(), posts: "posts", post_id: id_spec()])
def posts_route(), do: cat([users: "users", user_id: id_spec(), posts: "posts"])
def user_show_route(), do: cat([users: "users", user_id: id_spec()])
def users_route(), do: cat([users: "user"])
def routes(), do: one_of([posts_show_route(), posts_route(), users_show_route(), users_route()]) There's a lot of individual work to do here. But what do you think about these ideas? |
Thank you for your response!
Yes. Besides being verbose/inelegant it will also not give you a usable property-checking generator. If you want that, the even more verbose spec(&(&1 == val))
|> with_gen(StreamData.constant(val)) is currently required.
👍
You are right. One idea would be to e.g. recognize the use of users_route(), do: spec(["users", id_spec()])
posts_route(), do: spec(users_route() ++ ["posts", id_spec()]) 🤔 ... on top of introducing |
Currently, tuples and atoms already are converted to specs automatically (They implement the
Norm.Conformer.Conformable
protocol). I think it would make sense if that same protocol is implemented for other values that are often 'on their own':one_of
in a list of possible options.For other datatypes, I'd like to see a wrapper function
&constant/1
whose implementation is something likeOne question that remains is what should happen if we want to mix constants with other specs.
For instance, if I have a web-application with a 'posts' route nested under users, we end up with something like:
The problem in above example is passing a list immediately to
spec()
. One could write e.g.constant(["uses", id_spec()])
instead, but what would that mean?What do you think?
The text was updated successfully, but these errors were encountered: