-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.zd
More file actions
324 lines (271 loc) · 10.6 KB
/
Copy pathassert.zd
File metadata and controls
324 lines (271 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// ---------------------------------------------------------------------------
// assert -- iddialar, ve *neyin* beklendiğini söyleyen bir başarısızlık.
//
// Bundan önce bir testin söyleyebildiği tek şey `returned false` idi. Hangi
// değerin beklendiği, hangisinin geldiği, hatta hangi satırın düştüğü -- hiçbiri
// yoktu. Bir sınama takımının en çok okunan çıktısı başarısızlık mesajıdır, ve
// o mesaj boştu.
//
// Yeni bir mekanizma gerekmedi. Koşucu, bir testin *fırlattığı* mesajı zaten
// taşıyor:
//
// FAIL test_raises uncaught error: nth: index 9 outside 0..2
//
// Yani dilin halihazırdaki efekt kanalı "beklenen 5, gelen 4"ü taşımaya yeter.
// Bir iddia başarısız olduğunda `exn.raise` ile çıkıyor; başarılı olduğunda
// `true` döndürüyor, ve testin gövdesi olağan bir `Bool` ifadesi olarak kalıyor.
//
// Bir testin imzası yalnız `exn.raise` ve `div.loop` taşıyabildiğine göre, bu
// kütüphane de o ikisinin dışına çıkmıyor -- yetki isteyen bir iddia, testi
// hermetik olmaktan çıkarırdı.
//
// use assert
//
// fn test_adds() -> Bool !{exn.raise} { assert.eq_int(add(2, 2), 4) }
//
// $ zdc test t.zd
// FAIL test_adds uncaught error: expected 5, got 4
//
// `dtoa`ya bağlı: bir `Float`un metni, dilin kendi yazıcısıyla yazılmalı --
// başka türlü mesajdaki sayı, programın başka yerde bastığı sayıdan farklı
// görünürdü.
// ---------------------------------------------------------------------------
use dtoa
module !{exn.raise, div.loop}
// --- çekirdek ---------------------------------------------------------------
// Bir iddianın düşmesi. Doğrudan da çağrılabilir: koşulu kendin yazdığında
// mesajı da kendin yazarsın.
pub fn fail(why: Str) -> Bool !{exn.raise} {
exn.raise(why)
}
// Etiketli ve etiketsiz mesajın tek yeri. Her iddianın iki yüzü var --
// `eq_int(got, want)` ve `eq_int_at("port", got, want)` -- ve ikisi arasındaki
// tek fark bu satır.
fn said(what: Str, msg: Str) -> Str !{exn.raise} {
if what == "" { msg } else { what + ": " + msg }
}
fn quoted(s: Str) -> Str !{exn.raise} {
"\"" + s + "\""
}
// --- Int --------------------------------------------------------------------
pub fn eq_int(got: Int, want: Int) -> Bool !{exn.raise} {
eq_int_at("", got, want)
}
pub fn eq_int_at(what: Str, got: Int, want: Int) -> Bool !{exn.raise} {
if got == want {
true
} else {
fail(said(what, "expected " + show(want) + ", got " + show(got)))
}
}
pub fn ne_int(got: Int, unwanted: Int) -> Bool !{exn.raise} {
if got != unwanted { true } else { fail("expected anything but " + show(unwanted)) }
}
pub fn lt_int(got: Int, bound: Int) -> Bool !{exn.raise} {
if got < bound { true } else { fail("expected less than " + show(bound) + ", got " + show(got)) }
}
pub fn le_int(got: Int, bound: Int) -> Bool !{exn.raise} {
if got <= bound { true } else { fail("expected at most " + show(bound) + ", got " + show(got)) }
}
pub fn gt_int(got: Int, bound: Int) -> Bool !{exn.raise} {
if got > bound { true } else { fail("expected more than " + show(bound) + ", got " + show(got)) }
}
pub fn ge_int(got: Int, bound: Int) -> Bool !{exn.raise} {
if got >= bound { true } else { fail("expected at least " + show(bound) + ", got " + show(got)) }
}
// Kapalı aralık. Bir ölçümün "makul" olduğunu söylemenin en kısa yolu.
pub fn between(got: Int, low: Int, high: Int) -> Bool !{exn.raise} {
if got >= low && got <= high {
true
} else {
fail("expected " + show(low) + ".." + show(high) + ", got " + show(got))
}
}
// --- Str --------------------------------------------------------------------
pub fn eq_str(got: Str, want: Str) -> Bool !{exn.raise} {
eq_str_at("", got, want)
}
pub fn eq_str_at(what: Str, got: Str, want: Str) -> Bool !{exn.raise} {
if got == want {
true
} else {
fail(said(what, "expected " + quoted(want) + ", got " + quoted(got)))
}
}
pub fn ne_str(got: Str, unwanted: Str) -> Bool !{exn.raise} {
if got != unwanted { true } else { fail("expected anything but " + quoted(unwanted)) }
}
pub fn contains(hay: Str, needle: Str) -> Bool !{exn.raise, div.loop} {
if at_index(hay, needle, 0) >= 0 {
true
} else {
fail("expected " + quoted(hay) + " to contain " + quoted(needle))
}
}
pub fn starts_with(s: Str, want: Str) -> Bool !{exn.raise} {
if str_len(s) >= str_len(want) && substr(s, 0, str_len(want)) == want {
true
} else {
fail("expected " + quoted(s) + " to start with " + quoted(want))
}
}
pub fn ends_with(s: Str, want: Str) -> Bool !{exn.raise} {
if str_len(s) >= str_len(want) && substr(s, str_len(s) - str_len(want), str_len(s)) == want {
true
} else {
fail("expected " + quoted(s) + " to end with " + quoted(want))
}
}
pub fn str_empty(s: Str) -> Bool !{exn.raise} {
if str_len(s) == 0 { true } else { fail("expected an empty string, got " + quoted(s)) }
}
// `contains`ın aradığı yer. Bulamazsa -1.
fn at_index(hay: Str, needle: Str, i: Int) -> Int !{exn.raise, div.loop} {
if i + str_len(needle) > str_len(hay) {
0 - 1
} else if substr(hay, i, i + str_len(needle)) == needle {
i
} else {
at_index(hay, needle, i + 1)
}
}
// --- Bool -------------------------------------------------------------------
pub fn is_true(got: Bool) -> Bool !{exn.raise} {
is_true_at("", got)
}
pub fn is_true_at(what: Str, got: Bool) -> Bool !{exn.raise} {
if got { true } else { fail(said(what, "expected true, got false")) }
}
pub fn is_false(got: Bool) -> Bool !{exn.raise} {
is_false_at("", got)
}
pub fn is_false_at(what: Str, got: Bool) -> Bool !{exn.raise} {
if got { fail(said(what, "expected false, got true")) } else { true }
}
pub fn eq_bool(got: Bool, want: Bool) -> Bool !{exn.raise} {
if got == want {
true
} else {
fail("expected " + show(want) + ", got " + show(got))
}
}
// --- Float ------------------------------------------------------------------
// Kayan noktada eşitlik sorulmaz, yakınlık sorulur. `eps` çağıranın kararı:
// hangi farkın önemsiz olduğunu kütüphane bilemez.
pub fn near(got: Float, want: Float, eps: Float) -> Bool !{exn.raise, div.loop} {
near_at("", got, want, eps)
}
pub fn near_at(what: Str, got: Float, want: Float, eps: Float)
-> Bool !{exn.raise, div.loop}
{
if abs_f(got - want) <= eps {
true
} else {
fail(said(what, "expected " + dtoa.render(want) + " +/- " + dtoa.render(eps)
+ ", got " + dtoa.render(got)))
}
}
fn abs_f(x: Float) -> Float {
if x < 0.0 { 0.0 - x } else { x }
}
// --- List -------------------------------------------------------------------
// Uzunluk her eleman tipinde aynı soru, o yüzden tip değişkeniyle.
pub fn len_is(xs: List a, want: Int) -> Bool !{exn.raise} {
if len(xs) == want {
true
} else {
fail("expected a list of " + show(want) + ", got " + show(len(xs)))
}
}
pub fn list_empty(xs: List a) -> Bool !{exn.raise} {
if len(xs) == 0 { true } else { fail("expected an empty list, got " + show(len(xs)) + " items") }
}
pub fn eq_list_int(got: List Int, want: List Int) -> Bool !{exn.raise, div.loop} {
if len(got) != len(want) {
fail("expected a list of " + show(len(want)) + ", got " + show(len(got)))
} else {
same_ints(got, want, 0)
}
}
fn same_ints(got: List Int, want: List Int, i: Int) -> Bool !{exn.raise, div.loop} {
if i >= len(want) {
true
} else if nth(got, i) == nth(want, i) {
same_ints(got, want, i + 1)
} else {
fail("at " + show(i) + ": expected " + show(nth(want, i)) + ", got " + show(nth(got, i)))
}
}
pub fn eq_list_str(got: List Str, want: List Str) -> Bool !{exn.raise, div.loop} {
if len(got) != len(want) {
fail("expected a list of " + show(len(want)) + ", got " + show(len(got)))
} else {
same_strs(got, want, 0)
}
}
fn same_strs(got: List Str, want: List Str, i: Int) -> Bool !{exn.raise, div.loop} {
if i >= len(want) {
true
} else if nth(got, i) == nth(want, i) {
same_strs(got, want, i + 1)
} else {
fail("at " + show(i) + ": expected " + quoted(nth(want, i)) + ", got " + quoted(nth(got, i)))
}
}
pub fn has_int(xs: List Int, want: Int) -> Bool !{exn.raise, div.loop} {
if found_int(xs, want, 0) { true } else { fail("expected the list to contain " + show(want)) }
}
fn found_int(xs: List Int, want: Int, i: Int) -> Bool !{exn.raise, div.loop} {
if i >= len(xs) { false } else if nth(xs, i) == want { true } else { found_int(xs, want, i + 1) }
}
pub fn has_str(xs: List Str, want: Str) -> Bool !{exn.raise, div.loop} {
if found_str(xs, want, 0) { true } else { fail("expected the list to contain " + quoted(want)) }
}
fn found_str(xs: List Str, want: Str, i: Int) -> Bool !{exn.raise, div.loop} {
if i >= len(xs) { false } else if nth(xs, i) == want { true } else { found_str(xs, want, i + 1) }
}
// --- fırlatma ---------------------------------------------------------------
// Bir şeyin *düşmesi* gerektiğini sınamak. Efektli bir dilde bu, mock'suz
// yapılabilen bir şey: fırlatmayı karşılıyoruz ve karşılandı mı diye bakıyoruz.
pub fn raises(f: () !{exn.raise, div.loop} -> Bool) -> Bool !{exn.raise, div.loop} {
if caught(f) == "" { fail("expected a raise, none happened") } else { true }
}
pub fn does_not_raise(f: () !{exn.raise, div.loop} -> Bool) -> Bool !{exn.raise, div.loop} {
let m: Str = caught(f);
if m == "" { true } else { fail("expected no raise, got " + quoted(m)) }
}
// Yalnız "düştü mü" değil, *ne diyerek* düştü. Yanlış nedenle düşen bir test,
// geçen bir testtir -- ve bu onu yakalar.
pub fn raises_with(f: () !{exn.raise, div.loop} -> Bool, needle: Str)
-> Bool !{exn.raise, div.loop}
{
let m: Str = caught(f);
if m == "" {
fail("expected a raise containing " + quoted(needle) + ", none happened")
} else if at_index(m, needle, 0) >= 0 {
true
} else {
fail("expected a raise containing " + quoted(needle) + ", got " + quoted(m))
}
}
// Fırlatılan mesaj, ya da düşmediyse boş dizgi. Boş bir mesajla fırlatan bir
// fonksiyon burada "düşmedi" sayılır; bunu belgeliyoruz, gizlemiyoruz.
fn caught(f: () !{exn.raise, div.loop} -> Bool) -> Str !{div.loop} {
handle { f(); "" } with { exn.raise(m) => m }
}
// --- birleştirme ------------------------------------------------------------
// Tablo sürücülü testler için: hepsinin doğru olması. Bir iddia zaten düşünce
// fırlattığı için bu çoğu zaman gerekmiyor; kendi koşullarını yazdığında
// gerekiyor.
pub fn all(bs: List Bool) -> Bool !{exn.raise, div.loop} {
every(bs, 0)
}
fn every(bs: List Bool, i: Int) -> Bool !{exn.raise, div.loop} {
if i >= len(bs) {
true
} else if nth(bs, i) {
every(bs, i + 1)
} else {
fail("case " + show(i) + " is false")
}
}