You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- [x] `params gen all` can decostruction destination, seems like `gen all` (parity feature).
- [x] added `:label` in `params gen all` before decostruction, to hooks the param. It wins over variable name.
- [x] refactoring.
- [x] documentation.
Signed-off-by: Gabriele Ghio <gabriele.ghio@secomind.com>
@@ -33,18 +33,20 @@ defmodule Astarte.Generators.Utilities.ParamsGen do
33
33
- **Seamless Integration:** Built on top of ExUnitProperties, it integrates smoothly with your property-based tests.
34
34
- **Flexible Customization:** Accepts a keyword list for generator overrides, ensuring that only the generators you specify are replaced while the rest remain untouched.
35
35
- **Improved Test Clarity:** By explicitly defining custom generators, tests become easier to understand and maintain.
36
+
- **Labeled Clauses:** Bind overrides to a specific clause using labels to avoid conflicts and improve readability.
37
+
- **Destructuring Parity:** Supports the same destructuring on the left-hand side as `gen all` (e.g., `%{k: v} = var <- ...`). The captured variable (e.g., `var`) acts as the hook for overrides when no label is used.
36
38
37
39
## Usage Examples
38
40
39
41
### Example
40
42
41
-
In this example, we override the default `a` integer generator with a tuple {2, 3, 4}.
43
+
In this example, we override the default `a` integer generator with a generator that picks from [2, 3, 4].
42
44
43
45
defmodule MyGenerators do
44
46
use Astarte.Generators.Utilities.ParamsGen
45
47
46
48
# Override the default integer generator using params gen all
@@ -53,14 +55,53 @@ defmodule Astarte.Generators.Utilities.ParamsGen do
53
55
end
54
56
end
55
57
58
+
### Labeling a clause for overrides
59
+
60
+
Labels let you target a specific clause for overrides via `params:`. This is useful when the left side is a pattern (not a single variable), or when you want explicit names.
61
+
62
+
Supported form:
63
+
64
+
- Leading atom label (classic):
65
+
66
+
params gen all :payload, %{v: v} <- integer(), params: [payload: 42] do
67
+
v
68
+
end
69
+
70
+
With labels, the override uses the label name (e.g., `:payload`) instead of a variable name.
71
+
72
+
### Destructuring and variable hooks
73
+
74
+
`params gen all` fully supports destructuring on the left-hand side, mirroring `gen all`. When using a pattern match with an assignment, the variable on the right side of `=` becomes the hook name for the override (if no explicit label is provided).
75
+
76
+
params gen all %{b: b} = var <- string(?a..?a, length: 1),
77
+
params: [var: %{b: 10}] do
78
+
{b, var}
79
+
end
80
+
81
+
In the example above, `var` is the hook name, so `params: [var: %{b: 10}]` overrides the generator for that clause; `b` will be `10` and `var` will be `%{b: 10}`.
82
+
83
+
### Label precedence over variable/destructuring
84
+
85
+
If you provide both a clause label and a variable (either a plain variable or a destructured one), the label wins. This ensures clear and explicit targeting of the clause, regardless of the variable name used in the pattern.
86
+
87
+
params gen all :payload,
88
+
%{b: b} = captured <- string(?a..?a, length: 1),
89
+
params: [payload: %{b: 10}] do
90
+
{b, captured}
91
+
end
92
+
93
+
In this case, the override uses the `:payload` label and not the `captured` variable name; `b` will be `10` and `captured` will be `%{b: 10}`.
94
+
56
95
## Notes
57
96
58
97
- **Integration with ExUnitProperties:** This macro leverages the existing functionality of ExUnitProperties,
59
98
making it easy to adopt if you are already using property-based testing in your project.
60
99
- **Macro Syntax:** The macro expects a keyword list under the `params:` key, where each key corresponds to
61
-
a generator name (e.g., `a`, `b`) and each value is the custom generator or fixed params to be used.
100
+
a generator name (e.g., `a`, `b`), the variable captured by a destructuring (e.g., `var` in `%{k: v} = var <- ...`),
101
+
or a clause label (e.g., `:payload`). Each value is the custom generator or fixed params to be used.
62
102
- **Fallback Behavior:** For generators not specified in the override list, the macro will default to using
63
103
the original generator from ExUnitProperties.
104
+
- **Precedence:** If both a label and a variable name are present for the same clause, the label takes precedence when resolving the override target.
64
105
- **Compile-Time Verification:** Misuse or incorrect configuration will be flagged at compile time, helping
65
106
you catch errors early in the development process.
66
107
@@ -79,16 +120,37 @@ defmodule Astarte.Generators.Utilities.ParamsGen do
0 commit comments