@@ -37,42 +37,39 @@ a valid code consists only of letters. So let's rewrite our example:
37
37
38
38
code := 'AR'.
39
39
40
- AssertionCheckerBuilder new
41
- checking : [ :asserter |
40
+ AssertionChecker
41
+ check : [ :asserter |
42
42
asserter
43
43
enforce: [ code size = 2 ]
44
44
because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters';
45
45
enforce: [ code allSatisfy: #isLetter ]
46
46
because: 'ISO 3166-1 Alpha-2 codes must only contain letters'
47
- ];
48
- buildAndCheck
47
+ ]
49
48
```
50
49
51
- Note that in this case we are creating an ` AssertionCheckerBuilder ` and
52
- configuring all the conditions to enforce. Let's try now replacing ` code ` with
53
- ` 'AR3' ` and ` Do it ` again. By default all the conditions to enforce are checked
54
- so you should get an error message combining both explanations, and if you
55
- handle the raised exception you can get all the failures by sending it the
56
- message ` failures ` .
50
+ Note that in this case we are configuring all the conditions to enforce. Let's
51
+ try now replacing ` code ` with ` 'AR3' ` and ` Do it ` again. By default all the
52
+ conditions to enforce are checked so you should get an error message combining
53
+ both explanations, and if you handle the raised exception you can get all the
54
+ failures by sending it the message ` failures ` .
57
55
58
56
If you want the more usual behavior of stopping after the first failure you can
59
- configure the builder to fail fast:
57
+ configure it to fail fast:
60
58
61
59
``` smalltalk
62
60
| code |
63
61
64
62
code := 'AR3'.
65
63
66
- AssertionCheckerBuilder new
67
- failFast;
68
- checking: [ :asserter |
64
+ AssertionChecker
65
+ check: [ :asserter |
69
66
asserter
70
67
enforce: [ code size = 2 ]
71
68
because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters';
72
69
enforce: [ code allSatisfy: #isLetter ]
73
70
because: 'ISO 3166-1 Alpha-2 codes must only contain letters'
74
- ];
75
- buildAndCheck
71
+ ]
72
+ configuredBy: [ :checker | checker failFast ]
76
73
```
77
74
78
75
If you ` Do it ` you will get only the first failure, the next conditions won't
@@ -90,19 +87,18 @@ valid code. Now we will consider only the officially assigned codes as valid:
90
87
code := 'AA'.
91
88
officiallyAssignedCodes := #('AR' 'BR' 'US').
92
89
93
- AssertionCheckerBuilder new
94
- checking : [ :asserter |
90
+ AssertionChecker
91
+ check : [ :asserter |
95
92
asserter
96
93
enforce: [ code size = 2 and: [ code allSatisfy: #isLetter ]]
97
94
because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters'
98
- onSuccess: [ :sucessAsserter |
99
- sucessAsserter
95
+ onSuccess: [ :successAsserter |
96
+ successAsserter
100
97
enforce: [ officiallyAssignedCodes includes: code ]
101
98
because: [ '<1s> is not an officially assigned code'
102
99
expandMacrosWith: code ]
103
100
]
104
- ];
105
- buildAndCheck
101
+ ]
106
102
```
107
103
108
104
Here we are introducing two new features:
@@ -112,8 +108,7 @@ Here we are introducing two new features:
112
108
is satisfied. So we can make assumptions about what ` code ` looks like at this point.
113
109
- Second, using a block as the ` because: ` argument. This avoids creating
114
110
unnecessary objects because the explanation will only be evaluated if the
115
- condition is not met. In this case the argument is a literal String, so it
116
- makes no difference.
111
+ condition is not met.
117
112
118
113
## Refusing
119
114
@@ -126,26 +121,25 @@ Sometimes it's easier to explain a condition using negative logic, so
126
121
code := 'AR'.
127
122
unassignedCodes := #('LO' 'LP' 'OU').
128
123
129
- AssertionCheckerBuilder new
130
- checking : [ :asserter |
124
+ AssertionChecker
125
+ check : [ :asserter |
131
126
asserter
132
127
enforce: [ code size = 2 and: [ code allSatisfy: #isLetter ]]
133
128
because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters'
134
- onSuccess: [ :sucessAsserter |
135
- sucessAsserter
129
+ onSuccess: [ :successAsserter |
130
+ successAsserter
136
131
refuse: [ unassignedCodes includes: code ]
137
132
because: [ '<1s> is an unassigned code' expandMacrosWith: code ]
138
133
]
139
- ];
140
- buildAndCheck
134
+ ]
141
135
```
142
136
143
137
## Configuring the error to raise
144
138
145
139
If not specified the library will raise ` AssertionFailed ` when some check fails.
146
140
If you want to raise a different kind of error there are two ways to configure it:
147
141
148
- For single condition checks you can use ` enforce:because:raising: ` or ` refuse:because:raising: ` .
142
+ For single condition checks, use ` enforce:because:raising: ` or ` refuse:because:raising: `
149
143
150
144
``` smalltalk
151
145
| code |
@@ -158,24 +152,25 @@ AssertionChecker
158
152
raising: Error
159
153
```
160
154
161
- When using the builder you should use:
155
+ For multiple condition checks, use:
162
156
163
157
``` smalltalk
164
158
| code |
165
159
166
160
code := 'AR'.
167
161
168
- AssertionCheckerBuilder new
169
- raising: InstanceCreationFailed;
170
- checking: [ :asserter |
162
+ AssertionChecker
163
+ check: [ :asserter |
171
164
asserter
172
165
enforce: [ code size = 2 ]
173
166
because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters';
174
167
enforce: [ code allSatisfy: #isLetter ]
175
168
because: 'ISO 3166-1 Alpha-2 codes must only contain letters'
176
- ];
177
- buildAndCheck
169
+ ]
170
+ configuredBy: [ :checker |
171
+ checker raising: InstanceCreationFailed
172
+ ]
178
173
```
179
174
180
- but keep in mind when using the builder that the error to raise must understand
175
+ but keep in mind when using multiple conditions, that the error to raise must understand
181
176
` signalAll: ` in order to work.
0 commit comments