property_helper.pony

  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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
interface val _PropertyRunNotify
  """
  Simple callback for notifying the runner
  that a run completed.
  """
  fun apply(round: _Round, success: Bool)

interface tag _IPropertyRunner
  """
  Interface for a PropertyRunner without the generic type parameter,
  and only with the behaviours we are interested in.
  """

  be expect_action(name: String, round: _Round)

  be complete_action(name: String, round: _Round, ph: PropertyHelper)

  be fail_action(name: String, round: _Round, ph: PropertyHelper)

  be dispose_when_done(disposable: DisposableActor, round: _Round)

  be log(msg: String, verbose: Bool = false)


class val PropertyHelper
  """
  Helper for PonyCheck properties.

  Mirrors the [TestHelper](pony_test-TestHelper.md) API as closely as possible.

  Contains assertion functions and functions for completing asynchronous
  properties, for expecting and completing or failing actions.

  Internally a new PropertyHelper will be created for each call to
  a property with a new sample and also for every shrink run.
  So don't assume anything about the identity of the PropertyHelper inside of
  your Properties.

  This class is `val` by default so it can be safely passed around to other
  actors.

  It exposes the process [Env](builtin-Env.md) as public `env` field in order to
  give access to the root authority and other stuff.
  """
  let _runner: _IPropertyRunner
  let _run_notify: _PropertyRunNotify
  let _run: _Round
  let _params: String

  let env: Env

  new val create(
    env': Env,
    runner: _IPropertyRunner,
    run_notify: _PropertyRunNotify,
    run: _Round,
    params: String
  ) =>
    env = env'
    _runner = runner
    _run_notify = run_notify
    _run = run
    _params = params

/****** START DUPLICATION FROM TESTHELPER ********/

  fun log(msg: String, verbose: Bool = false) =>
    """
    Log the given message.

    The verbose parameter allows messages to be printed only when the
    `--verbose` command line option is used. For example, by default assert
    failures are logged, but passes are not. With `--verbose`, both passes and
    fails are reported.

    Logs are printed one test at a time to avoid interleaving log lines from
    concurrent tests.
    """
    _runner.log(msg, verbose)

  fun fail(msg: String = "Test failed") =>
    """
    Flag the test as having failed.
    """
    _fail(msg)

  fun assert_false(
    predicate: Bool,
    msg: String val = "",
    loc: SourceLoc val = __loc)
    : Bool val
  =>
    """
    Assert that the given expression is false.
    """
    if predicate then
      _fail(_fmt_msg(loc, "Assert false failed. " + msg))
      return false
    end
    _runner.log(_fmt_msg(loc, "Assert false passed. " + msg))
    true

  fun assert_true(
    predicate: Bool,
    msg: String val = "",
    loc: SourceLoc val = __loc)
    : Bool val
  =>
    """
    Assert that the given expression is true.
    """
    if not predicate then
      _fail(_fmt_msg(loc, "Assert true failed. " + msg))
      return false
    end
    _runner.log(_fmt_msg(loc, "Assert true passed. " + msg))
    true

  fun assert_error(
    test: {(): None ?} box,
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the given test function throws an error when run.
    """
    try
      test()?
      _fail(_fmt_msg(loc, "Assert error failed. " + msg))
      false
    else
      _runner.log(_fmt_msg(loc, "Assert error passed. " + msg), true)
      true
    end

  fun assert_no_error(
    test: {(): None ?} box,
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the given test function does not throw an error when run.
    """
    try
      test()?
      _runner.log(_fmt_msg(loc, "Assert no error passed. " + msg), true)
      true
    else
      _fail(_fmt_msg(loc, "Assert no error failed. " + msg))
      false
    end

  fun assert_is[A](
    expect: A,
    actual: A,
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the 2 given expressions resolve to the same instance.
    """
    if expect isnt actual then
      _fail(_fmt_msg(loc, "Assert is failed. " + msg
        + " Expected (" + (digestof expect).string() + ") is ("
        + (digestof actual).string() + ")"))
      return false
    end

    _runner.log(
      _fmt_msg(loc, "Assert is passed. " + msg
        + " Got (" + (digestof expect).string() + ") is ("
        + (digestof actual).string() + ")"),
      true)
    true

  fun assert_isnt[A](
    not_expect: A,
    actual: A,
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the 2 given expressions resolve to different instances.
    """
    if not_expect is actual then
      _fail(_fmt_msg(loc, "Assert isn't failed. " + msg
        + " Expected (" + (digestof not_expect).string() + ") isnt ("
        + (digestof actual).string() + ")"))
      return false
    end

    _runner.log(
      _fmt_msg(loc, "Assert isn't passed. " + msg
        + " Got (" + (digestof not_expect).string() + ") isnt ("
        + (digestof actual).string() + ")"),
      true)
    true

  fun assert_eq[A: (Equatable[A] #read & Stringable #read)](
    expect: A,
    actual: A,
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the 2 given expressions are equal.
    """
    if expect != actual then
      _fail(_fmt_msg(loc,  "Assert eq failed. " + msg
        + " Expected (" + expect.string() + ") == (" + actual.string() + ")"))
      return false
    end

    _runner.log(_fmt_msg(loc, "Assert eq passed. " + msg
      + " Got (" + expect.string() + ") == (" + actual.string() + ")"),
      true)
    true

  fun assert_ne[A: (Equatable[A] #read & Stringable #read)](
    not_expect: A,
    actual: A,
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the 2 given expressions are not equal.
    """
    if not_expect == actual then
      _fail(_fmt_msg(loc, "Assert ne failed. " + msg
        + " Expected (" + not_expect.string() + ") != (" + actual.string()
        + ")"))
      return false
    end

    _runner.log(
      _fmt_msg(loc, "Assert ne passed. " + msg
        + " Got (" + not_expect.string() + ") != (" + actual.string() + ")"),
      true)
    true

  fun assert_array_eq[A: (Equatable[A] #read & Stringable #read)](
    expect: ReadSeq[A],
    actual: ReadSeq[A],
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the contents of the 2 given ReadSeqs are equal.
    """
    var ok = true

    if expect.size() != actual.size() then
      ok = false
    else
      try
        var i: USize = 0
        while i < expect.size() do
          if expect(i)? != actual(i)? then
            ok = false
            break
          end

          i = i + 1
        end
      else
        ok = false
      end
    end

    if not ok then
      _fail(_fmt_msg(loc, "Assert EQ failed. " + msg + " Expected ("
        + _print_array[A](expect) + ") == (" + _print_array[A](actual) + ")"))
      return false
    end

    _runner.log(
      _fmt_msg(loc, "Assert EQ passed. " + msg + " Got ("
        + _print_array[A](expect) + ") == (" + _print_array[A](actual) + ")"),
      true)
    true

  fun assert_array_eq_unordered[A: (Equatable[A] #read & Stringable #read)](
    expect: ReadSeq[A],
    actual: ReadSeq[A],
    msg: String = "",
    loc: SourceLoc = __loc)
    : Bool
  =>
    """
    Assert that the contents of the 2 given ReadSeqs are equal ignoring order.
    """
    try
      let missing = Array[box->A]
      let consumed = Array[Bool].init(false, actual.size())
      for e in expect.values() do
        var found = false
        var i: USize = -1
        for a in actual.values() do
          i = i + 1
          if consumed(i)? then continue end
          if e == a then
            consumed.update(i, true)?
            found = true
            break
          end
        end
        if not found then
          missing.push(e)
        end
      end

      let extra = Array[box->A]
      for (i, c) in consumed.pairs() do
        if not c then extra.push(actual(i)?) end
      end

      if (extra.size() != 0) or (missing.size() != 0) then
        _fail(
          _fmt_msg(loc, "Assert EQ_UNORDERED failed. " + msg
            + " Expected (" + _print_array[A](expect) + ") == ("
            + _print_array[A](actual) + "):"
            + "\nMissing: " + _print_array[box->A](missing)
            + "\nExtra: " + _print_array[box->A](extra)
          )
        )
        return false
      end
      _runner.log(
        _fmt_msg(
          loc,
          "Assert EQ_UNORDERED passed. "
          + msg
          + " Got ("
          + _print_array[A](expect)
          + ") == ("
          + _print_array[A](actual)
          + ")"
        ),
        true
      )
      true
    else
      _fail("Assert EQ_UNORDERED failed from an internal error.")
      false
    end

  fun _print_array[A: Stringable #read](array: ReadSeq[A]): String =>
    """
    Generate a printable string of the contents of the given readseq to use in
    error messages.
    """
    "[len=" + array.size().string() + ": " + ", ".join(array.values()) + "]"


/****** END DUPLICATION FROM TESTHELPER *********/

  fun expect_action(name: String) =>
    """
    Expect some action of the given name to complete
    for the property to hold.

    If all expected actions are completed successfully,
    the property is considered successful.

    If 1 action fails, the property is considered failing.

    Call `complete_action(name)` or `fail_action(name)`
    to mark some action as completed.

    Example:

    ```pony
      actor AsyncActor

        let _ph: PropertyHelper

        new create(ph: PropertyHelper) =>
          _ph = ph

        be complete(s: String) =>
          if (s.size() % 2) == 0 then
            _ph.complete_action("is_even")
          else
            _ph.fail_action("is_even")

      class EvenStringProperty is Property1[String]
        fun name(): String => "even_string"

        fun gen(): Generator[String] =>
          Generators.ascii()

      fun property(arg1: String, ph: PropertyHelper) =>
        ph.expect_action("is_even")
        AsyncActor(ph).check(arg1)
    ```

    """
    _runner.expect_action(name, _run)

  fun val complete_action(name: String) =>
    """
    Complete an expected action successfully.

    If all expected actions are completed successfully,
    the property is considered successful.

    If 1 action fails, the property is considered failing.

    If the action `name` was not expected, i.e. was not registered using
    `expect_action`, nothing happens.
    """
    _runner.complete_action(name, _run, this)

  fun val fail_action(name: String) =>
    """
    Mark an expected action as failed.

    If all expected actions are completed successfully,
    the property is considered successful.

    If 1 action fails, the property is considered failing.
    """
    _runner.fail_action(name, _run, this)

  fun complete(success: Bool) =>
    """
    Complete an asynchronous property successfully.

    Once this method is called the property
    is considered successful or failing
    depending on the value of the parameter `success`.

    For more fine grained control over completing or failing
    a property that consists of many steps, consider using
    `expect_action`, `complete_action` and `fail_action`.
    """
    _run_notify.apply(_run, success)

  fun dispose_when_done(disposable: DisposableActor) =>
    """
    Dispose the actor after a property run / a shrink is done.
    """
    _runner.dispose_when_done(disposable, _run)

  fun _fail(msg: String) =>
    _runner.log(msg)
    _run_notify.apply(_run, false)

  fun _fmt_msg(loc: SourceLoc, msg: String): String =>
    let msg_prefix = _params + " " + _run.string() + " " + _format_loc(loc)
    if msg.size() > 0 then
      msg_prefix + ": " + msg
    else
      msg_prefix
    end

  fun _format_loc(loc: SourceLoc): String =>
    loc.file() + ":" + loc.line().string()