@@ -37,42 +37,39 @@ a valid code consists only of letters. So let's rewrite our example:
3737
3838code := 'AR'.
3939
40- AssertionCheckerBuilder new
41- checking : [ :asserter |
40+ AssertionChecker
41+ check : [ :asserter |
4242 asserter
4343 enforce: [ code size = 2 ]
4444 because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters';
4545 enforce: [ code allSatisfy: #isLetter ]
4646 because: 'ISO 3166-1 Alpha-2 codes must only contain letters'
47- ];
48- buildAndCheck
47+ ]
4948```
5049
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 ` .
5755
5856If 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:
6058
6159``` smalltalk
6260| code |
6361
6462code := 'AR3'.
6563
66- AssertionCheckerBuilder new
67- failFast;
68- checking: [ :asserter |
64+ AssertionChecker
65+ check: [ :asserter |
6966 asserter
7067 enforce: [ code size = 2 ]
7168 because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters';
7269 enforce: [ code allSatisfy: #isLetter ]
7370 because: 'ISO 3166-1 Alpha-2 codes must only contain letters'
74- ];
75- buildAndCheck
71+ ]
72+ configuredBy: [ :checker | checker failFast ]
7673```
7774
7875If 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:
9087code := 'AA'.
9188officiallyAssignedCodes := #('AR' 'BR' 'US').
9289
93- AssertionCheckerBuilder new
94- checking : [ :asserter |
90+ AssertionChecker
91+ check : [ :asserter |
9592 asserter
9693 enforce: [ code size = 2 and: [ code allSatisfy: #isLetter ]]
9794 because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters'
98- onSuccess: [ :sucessAsserter |
99- sucessAsserter
95+ onSuccess: [ :successAsserter |
96+ successAsserter
10097 enforce: [ officiallyAssignedCodes includes: code ]
10198 because: [ '<1s> is not an officially assigned code'
10299 expandMacrosWith: code ]
103100 ]
104- ];
105- buildAndCheck
101+ ]
106102```
107103
108104Here we are introducing two new features:
@@ -112,8 +108,7 @@ Here we are introducing two new features:
112108 is satisfied. So we can make assumptions about what ` code ` looks like at this point.
113109- Second, using a block as the ` because: ` argument. This avoids creating
114110 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.
117112
118113## Refusing
119114
@@ -126,26 +121,25 @@ Sometimes it's easier to explain a condition using negative logic, so
126121code := 'AR'.
127122unassignedCodes := #('LO' 'LP' 'OU').
128123
129- AssertionCheckerBuilder new
130- checking : [ :asserter |
124+ AssertionChecker
125+ check : [ :asserter |
131126 asserter
132127 enforce: [ code size = 2 and: [ code allSatisfy: #isLetter ]]
133128 because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters'
134- onSuccess: [ :sucessAsserter |
135- sucessAsserter
129+ onSuccess: [ :successAsserter |
130+ successAsserter
136131 refuse: [ unassignedCodes includes: code ]
137132 because: [ '<1s> is an unassigned code' expandMacrosWith: code ]
138133 ]
139- ];
140- buildAndCheck
134+ ]
141135```
142136
143137## Configuring the error to raise
144138
145139If not specified the library will raise ` AssertionFailed ` when some check fails.
146140If you want to raise a different kind of error there are two ways to configure it:
147141
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: `
149143
150144``` smalltalk
151145| code |
@@ -158,24 +152,25 @@ AssertionChecker
158152 raising: Error
159153```
160154
161- When using the builder you should use:
155+ For multiple condition checks, use:
162156
163157``` smalltalk
164158| code |
165159
166160code := 'AR'.
167161
168- AssertionCheckerBuilder new
169- raising: InstanceCreationFailed;
170- checking: [ :asserter |
162+ AssertionChecker
163+ check: [ :asserter |
171164 asserter
172165 enforce: [ code size = 2 ]
173166 because: 'ISO 3166-1 Alpha-2 codes must have exactly two letters';
174167 enforce: [ code allSatisfy: #isLetter ]
175168 because: 'ISO 3166-1 Alpha-2 codes must only contain letters'
176- ];
177- buildAndCheck
169+ ]
170+ configuredBy: [ :checker |
171+ checker raising: InstanceCreationFailed
172+ ]
178173```
179174
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
181176` signalAll: ` in order to work.
0 commit comments