forked from lifepillar/ASUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASUnit.applescript
More file actions
1984 lines (1789 loc) · 56 KB
/
ASUnit.applescript
File metadata and controls
1984 lines (1789 loc) · 56 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*!
@header ASUnit
An AppleScript testing framework.
@abstract License: GNU GPL, see COPYING for details.
@author Nir Soffer, Lifepillar
@copyright 2013-2023 Lifepillar, 2006 Nir Soffer
@version 1.2.4
@charset utf-8
*)
(*! @abstract <em>[text]</em> ASUnit's name. *)
property name : "ASUnit"
(*! @abstract <em>[text]</em> ASUnit's version. *)
property version : "1.2.4"
(*! @abstract <em>[text]</em> ASUnit's id. *)
property id : "com.lifepillar.ASUnit"
(*! @abstract Error number signalling a failed test. *)
property TEST_FAILED : 1000
(*! @abstract Error number signalling a skipped test. *)
property TEST_SKIPPED : 1001
(*! @abstract Error number used inside @link failIf() @/link. *)
property TEST_SUCCEEDED_BUT_SHOULD_HAVE_FAILED : 1002
(*! @abstract A property that refers to the top-level script. *)
property TOP_LEVEL : me
(*!
@abstract
Base class for observers.
@discussion
Observers are objects that may get notified by visitors.
Concrete observers are supposed to inherit from this script.
*)
script Observer
property parent : AppleScript
(*! @abstract Sets the object observed by this observer. *)
on setNotifier(aNotifier)
end setNotifier
end script -- Observer
(*!
@abstract
Base class for visitors.
@discussion
This script defines the interface for a @link Visitor @/link object.
Subclasses are supposed to override some handlers.
To operate on a suite, you call the suite @link accept() @/link with a visitor.
ASUnit defines only one visitor, @link TestResult @/link, which runs all the tests in a suite.
You may create other visitors to do filtered testing, custom reporting and like.
Your custom visitor should inherit from one of the framework visitors or from @link Visitor @/link.
*)
script Visitor
property parent : AppleScript
(*! @abstract @see visitTestSuite() *)
on visitTestSuite(aTestSuite)
end visitTestSuite
(*! @abstract @see visitTestCase() *)
on visitTestCase(TestCase)
end visitTestCase
end script -- Visitor
(*! @abstract Builds and returns a @link TestResult @/link object. *)
on makeTestResult(aName)
(*! @abstract Runs test cases and collects the results. *)
script TestResult
property parent : Visitor
property name : aName
(*! @abstract An observer will be notified on visiting progress. *)
property observers : {}
property startDate : missing value
property stopDate : missing value
property passed : {}
property skips : {}
property failures : {}
property errors : {}
property assertions : 0
(*!
@abstract
Makes the given object an observer of TestResult.
@discussion
Observers of TestResult are sent notifications whenever
certain events occur, like starting a test, completing a test, etcÉ
An observer should be an object that inherits from @link Observer @/link,
or at least conforms to its interface.
@param
anObject <em>[script]</em> The observer.
*)
on addObserver(anObject)
anObject's setNotifier(me)
set the end of my observers to anObject
end addObserver
(*!
@abstract
Runs the given test case or test suite.
@param
aTest <em>[script]</em> May be a test case or a test suite.
*)
on runTest(aTest)
set assertions to 0
try
startTest()
aTest's accept(me)
stopTest()
on error msg number n
stopTest()
error msg number n
end try
end runTest
(*! @abstract Sets the start time of the test and notifies the observers. *)
on startTest()
set my startDate to current date
notify({name:"start"})
end startTest
(*! @abstract Sets the end time of the test and notifies the observers. *)
on stopTest()
set my stopDate to current date
notify({name:"stop"})
end stopTest
(*!
@abstract
Notifies the observers that the given test has started.
@param
aTestCase <em>[script]</em> A test case.
*)
on startTestCase(aTestCase)
notify({name:"start test case", test:aTestCase})
end startTestCase
(*!
@abstract
Runs a test case and collects results.
@param
aTestCase <em>[script]</em> A test case.
*)
on visitTestCase(aTestCase)
startTestCase(aTestCase)
try
aTestCase's runCase()
addSuccess(aTestCase)
set my assertions to (my assertions) + (aTestCase's numberOfAssertions())
on error message number errorNumber
set my assertions to (my assertions) + (aTestCase's numberOfAssertions())
if errorNumber is TEST_SKIPPED then
addSkip(aTestCase, message)
else if errorNumber is TEST_FAILED then
addFailure(aTestCase, message)
else
addError(aTestCase, message & " (" & errorNumber & ")")
end if
end try
end visitTestCase
(*!
@abstract
Registers the fact that the given test has succeeded and notifies the observers.
@param
aTestCase <em>[script]</em> A test case.
*)
on addSuccess(aTestCase)
set end of my passed to aTestCase
notify({name:"success", test:aTestCase})
end addSuccess
(*!
@abstract
Registers the fact that the given test was skipped and notifies the observers.
@param
aTestCase <em>[script]</em> A test case.
@param
message <em>[text]</em> The message to be shown to the user.
*)
on addSkip(aTestCase, message)
set end of my skips to {test:aTestCase, message:message}
notify({name:"skip", test:aTestCase})
end addSkip
(*!
@abstract
Registers the fact that the given test has failed and notifies the observers.
@param
aTestCase <em>[script]</em> A test case.
@param
message <em>[text]</em> The message to be shown to the user.
*)
on addFailure(aTestCase, message)
set end of my failures to {test:aTestCase, message:message}
notify({name:"fail", test:aTestCase})
end addFailure
(*!
@abstract
Registers the fact that the given test raised an error and notifies the observers.
@param
aTestCase <em>[script]</em> A test case.
@param
message <em>[text]</em> The message to be shown to the user.
*)
on addError(aTestCase, message)
set end of my errors to {test:aTestCase, message:message}
notify({name:"error", test:aTestCase})
end addError
(*!
@abstract
Sends the given event to all the observers.
@param
anEvent <em>[record]</em> the event that must be sent to the observers.
An event contains two fields: the <tt>name</tt> of the event
and the <tt>test</tt> object.
*)
on notify(anEvent)
repeat with obs in (a reference to my observers)
obs's update(anEvent)
end repeat
end notify
(*!
@abstract
Returns true if and only if the test suite completes successfully, that is,
without errors or failures.
*)
on hasPassed()
return (my failures's length) + (my errors's length) = 0
end hasPassed
(*! @abstract Returns the number of tests run. *)
on runCount()
return (my passed's length) + (my skips's length) + (my failures's length) + (my errors's length)
end runCount
(*! @abstract Returns the number of successful tests. *)
on passCount()
return count of my passed
end passCount
(*! @abstract Returns the total number of successful assertions. *)
on assertionCount()
return assertions
end assertionCount
(*! @abstract Returns the number of skipped test. *)
on skipCount()
return count of my skips
end skipCount
(*! @abstract Returns the number of tests that generated an error. *)
on errorCount()
return count of my errors
end errorCount
(*! @abstract Returns the number of failed tests. *)
on failureCount()
return count of my failures
end failureCount
(*! @abstract Returns the time spent to run the test suite, in seconds. *)
on runSeconds()
return (my stopDate) - (my startDate)
end runSeconds
end script -- TestResult
return TestResult
end makeTestResult
(*!
@abstract
Factory handler to generate a test script.
@discussion
This handler is used to create a script that inherits
from <code>theParent</code> and implements testing assertions.
@param
theParent <em>[script]</em> The script to inherit from.
@return
A script inheriting from the given script and implementing assertions.
*)
on makeAssertions(theParent)
(*! @abstract The script defining all the test assertions. *)
script TestAssertions
property parent : theParent
(*!
@abstract
Determines whether invisible characters should be made visible.
@discussion
When this property is set to true (which is the default), invisible
characters (spaces, tabulations, linefeeds, and returns) are printed
as visible characters.
This is especially useful when a test like @link assertEqual() @/link fails
because the expected value and the actual value differ, say, just
because the actual value has a trailing new line that the
expected value does not have.
*)
property showInvisibles : true
(*! @abstract The maximum recursion depth for @link pp() @/link. *)
property maxRecursionDepth : 50
on test_failed_error_number()
return TEST_FAILED
end test_failed_error_number
on test_skipped_error_number()
return TEST_SKIPPED
end test_skipped_error_number
(*!
@abstract
Helper handler that returns a textual representation of an inheritance chain.
*)
on formatInheritanceChain(chain)
local n
set n to the length of the chain
if n = 0 then return "(The inheritance chain is empty)"
if n > 0 then
local s
set s to "Inheritance chain: " & pp(item 1 of chain)
repeat with i from 2 to n
set s to s & linefeed & " -> " & pp(item i of chain)
end repeat
return s
end if
end formatInheritanceChain
(*!
@abstract
Raises a TEST_SKIPPED error.
@param
why <em>[text]</em> A message.
@throws
A @link TEST_SKIPPED @/link error.
*)
on skip(why)
error why number TEST_SKIPPED
end skip
(*!
@abstract
Raises a TEST_FAILED error.
@param
why <em>[text]</em> A message.
@throws
A @link TEST_FAILED @/link error.
*)
on fail(why)
error why number TEST_FAILED
end fail
(*!
@abstract
Succeeds when its argument is true.
@param
expr <em>[boolean]</em> An expression that evaluates to true or false.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on ok(expr)
if not expr then fail("The given expression did not evaluate to true.")
countAssertion()
end ok
(*!
@abstract
Succeeds when its argument is false.
@param
expr <em>[boolean]</em> An expression that evaluates to true or false.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on notOk(expr)
if expr then fail("The given expression did not evaluate to false.")
countAssertion()
end notOk
(*!
@abstract
Succeeds when the given expression is true.
@param
expr <em>[boolean]</em> An expression that evaluates to true or false.
@param
message <em>[text]</em> A message that is printed when the test fails.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assert(expr, message)
if not expr then fail(message)
countAssertion()
end assert
(*! @abstract A synonym for @link assert() @/link. *)
on should(expr, message)
assert(expr, message)
end should
(*!
@abstract
Succeeds when the given expression is false.
@param
expr <em>[boolean]</em> An expression that evaluates to true or false.
@param
message <em>[text]</em> A message that is printed when the test fails.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on refute(expr, message)
if expr then fail(message)
countAssertion()
end refute
(*! @abstract A synonym for @link refute() @/link. *)
on shouldnt(expr, message)
refute(expr, message)
end shouldnt
(*!
@abstract
Succeeds when the two given expressions have the same value.
@param
expected <em>[anything]</em> The expected value.
@param
value <em>[anything]</em> Some other value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertEqual(expected, value)
local msg, got, wanted, errMsg
considering case, diacriticals, hyphens, punctuation and white space
if (value is not expected) then
fail("Expected: " & pp(expected) & linefeed & " Actual: " & pp(value))
end if
end considering
countAssertion()
end assertEqual
(*! @abstract A synonym for @link assertEqual() @/link. *)
on shouldEqual(expected, value)
assertEqual(expected, value)
end shouldEqual
(*!
@abstract
Succeeds when the two given expressions are not equal.
@param
unexpected <em>[anything]</em> The unexpected value.
@param
value <em>[anything]</em> Some other value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertNotEqual(unexpected, value)
local msg, unwanted, errMsg
considering case, diacriticals, hyphens, punctuation and white space
if value is equal to unexpected then
fail("Expected a value different from " & pp(unexpected) & ".")
end if
end considering
countAssertion()
end assertNotEqual
(*! @abstract A synonym for @link assertNotEqual() @/link. *)
on refuteEqual(unexpected, value)
assertNotEqual(unexpected, value)
end refuteEqual
(*! @abstract A synonym for @link assertNotEqual() @/link. *)
on shouldNotEqual(unexpected, value)
assertNotEqual(unexpected, value)
end shouldNotEqual
(*!
@abstract
Fails unless <tt>e1</tt> and <tt>e2</tt> are within <tt>delta</tt> from each other.
@discussion
This assertion succeeds if and only if |e1-e2| <= delta.
@param
e1 <em>[number]</em> A number.
@param
e2 <em>[number]</em> A number.
@param
delta <em>[number]</em> The absolute error.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertEqualAbsError(e1, e2, delta)
if delta < 0.0 then fail("The absolute error cannot be negative.")
local n
set n to e1 - e2
if n < 0.0 then set n to -n
if n > delta then fail("The arguments differ by " & asText(n) & " > " & asText(delta))
countAssertion()
end assertEqualAbsError
(*!
@abstract
Fails unless <tt>e1</tt> and <tt>e2</tt> have a relative error less than <tt>eps</tt>.
@discussion
This assertion succeeds if and only if |e1-e2| <= min(|e1|,|e2|) * eps.
@param
e1 <em>[number]</em> A number.
@param
e2 <em>[number]</em> A number.
@param
eps <em>[number]</em> The relative error.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertEqualRelError(e1, e2, eps)
if eps < 0.0 then fail("The relative error cannot be negative.")
local min
local n
set n to e1 - e2
if n < 0.0 then set n to -n
if e1 < 0.0 then set e1 to -e1
if e2 < 0.0 then set e2 to -e2
if e1 < e2 then
set min to e1
else
set min to e2
end if
if n > min * eps then Â
fail("The relative error is " & asText(n / min) & " > " & asText(eps))
countAssertion()
end assertEqualRelError
(*! @abstract A shortcut for @link assertEqual @/link(missing value, expr). *)
on assertMissing(expr)
assertEqual(missing value, expr)
end assertMissing
(*! @abstract A shortcut for @link refuteEqual @/link(missing value, expr). *)
on refuteMissing(expr)
refuteEqual(missing value, expr)
end refuteMissing
(*! @abstract Deprecated. @see assertMissing() *)
on assertNil(expr)
assertMissing(expr)
end assertNil
(*! @abstract Deprecated. @see refuteMissing() *)
on refuteNil(expr)
refuteMissing(expr)
end refuteNil
(*! @abstract A shortcut for @link assertEqual @/link(null, expr). *)
on assertNull(expr)
assertEqual(null, expr)
end assertNull
(*! @abstract A shortcut for @link refuteEqual @/link(null, expr). *)
on refuteNull(expr)
refuteEqual(null, expr)
end refuteNull
(*!
@abstract
Tests whether the given expression belongs to the given class.
@param
klass <em>[class]</em> A class name.
@param
expr <em>[anything]</em> A value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertInstanceOf(klass, expr)
local k
try
set k to class of expr
on error
fail("Can't get class of" & space & pp(expr) & ".")
end try
if k is not klass then
fail("Expected class: " & pp(klass) & linefeed & Â
" Actual class: " & pp(k))
end if
countAssertion()
end assertInstanceOf
(*!
@abstract
Succeeds when the given expression is not of the given class.
@param
klass <em>[class]</em> A class name.
@param
expr <em>[anything]</em> A value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on refuteInstanceOf(klass, expr)
local k
try
set k to class of expr
on error
countAssertion()
return
end try
if k is klass then Â
fail("Expected class of " & pp(expr) & linefeed & "to be different from " & pp(klass) & ".")
countAssertion()
end refuteInstanceOf
(*!
@abstract
Tests whether the given object or any of its ancestors belongs to the given class.
@discussion
This is mainly useful for user-defined scripts and user-defined
inheritance hierarchies. For built-in types, it is almost equivalent
to @link assertInstanceOf() @/link. The main difference is that it can be
used to test whether an expression is a <tt>number</tt>,
but it does not matter if it is an <tt>integer</tt> or <tt>real</tt>
(you cannot do that with @link assertInstanceOf() @/link).
@param
klass <em>[class]</em> A class name.
@param
expr <em>[anything]</em> A value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertKindOf(klass, expr)
local curr, k, inheritanceChain
set curr to expr
set inheritanceChain to {}
repeat
try
set k to class of curr
on error
set the end of inheritanceChain to curr
exit repeat
end try
if k is klass then
countAssertion()
return
end if
if klass is number and k is in {integer, real} then
countAssertion()
return
end if
set the end of the inheritanceChain to curr
try
set curr to curr's parent
if curr is in inheritanceChain then -- cycle
set the end of inheritanceChain to curr
exit repeat
end if
on error errMsg number errNum
if errNum is -1728 then exit repeat -- Can't get parent (end of inheritance chain)
error "Unexpected error: " & errMsg number errNum
end try
end repeat
fail(pp(expr) & space & "is not a kind of" & space & pp(klass) & "." & linefeed & Â
formatInheritanceChain(inheritanceChain))
end assertKindOf
(*!
@abstract
Verifies that neither the given object nor any of its ancestors belong to the given class.
@discussion
See @link assertKindOf() @/link.
@param
klass <em>[class]</em> A class name.
@param
expr <em>[anything]</em> A value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on refuteKindOf(klass, expr)
local curr, k, inheritanceChain
set curr to expr
set inheritanceChain to {}
repeat
try
set k to class of curr
on error
countAssertion()
return
end try
set the end of the inheritanceChain to curr
if k is klass then exit repeat
if klass is number and k is in {integer, real} then exit repeat
try
set curr to curr's parent
if curr is in inheritanceChain then -- cycle
countAssertion()
return
end if
on error errMsg number errNum
if errNum is -1728 then -- Can't get parent (end of inheritance chain)
countAssertion()
return
end if
error "Unexpected error: " & errMsg number errNum
end try
end repeat
fail(pp(expr) & space & "is a kind of" & space & pp(klass) & "." & linefeed & Â
formatInheritanceChain(inheritanceChain))
end refuteKindOf
(*!
@abstract
Tests whether an object inherits from another object.
@discussion
This test walks up the inheritance chain of <tt>descendantObject</tt>
until it finds <tt>obj</tt>, reaches the end of the inheritance
chain, or detects a cycle in the inheritance chain.
@param
ancestor <em>[anything]</em> A value.
@param
descendant <em>[anything]</em> A value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertInheritsFrom(ancestor, descendant)
local currObj, inheritanceChain
set currObj to descendant
set inheritanceChain to {}
repeat
set the end of the inheritanceChain to currObj
try
set currObj to currObj's parent
if currObj is equal to ancestor then
countAssertion()
return
end if
if currObj is in inheritanceChain then -- cycle
set the end of inheritanceChain to currObj
exit repeat
end if
on error errMsg number errNum
if errNum is -1728 then exit repeat -- Can't get parent (end of inheritance chain)
error "Unexpected error: " & errMsg number errNum
end try
end repeat
fail(pp(descendant) & space & "does not inherit from" & space & pp(ancestor) & "." & linefeed & Â
formatInheritanceChain(inheritanceChain))
end assertInheritsFrom
(*!
@abstract
Succeeds when <tt>anotherObj</tt> does not inherit
(directly on indirectly) from <tt>obj</tt>.
@param
obj <em>[anything]</em> A value.
@param
anotherObj <em>[anything]</em> A value.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on refuteInheritsFrom(obj, anotherObj)
local currObj, inheritanceChain
set currObj to anotherObj
set inheritanceChain to {} -- To detect cycles
repeat
set the end of inheritanceChain to currObj
try
set currObj to currObj's parent
if currObj is equal to obj then
set the end of inheritanceChain to currObj
exit repeat
end if
if currObj is in inheritanceChain then -- cycle
countAssertion()
return
end if
on error errMsg number errNum
if errNum is -1728 then -- Can't get parent (end of inheritance chain)
countAssertion()
return
end if
error "Unexpected error: " & errMsg number errNum
end try
end repeat
fail(pp(anotherObj) & space & "inherits from" & space & pp(obj) & "." & linefeed & Â
formatInheritanceChain(inheritanceChain))
end refuteInheritsFrom
(*!
@abstract
Tests whether a variable is a reference.
@param
anObject <em>[anything]</em> An expression.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertReference(anObject)
try
anObject as reference -- Try to coerce to reference class
on error
fail(pp(anObject) & space & "is not a reference.")
end try
countAssertion()
end assertReference
(*! @abstract A synonym for @link assertReference() @/link. *)
on shouldBeReference(anObject)
assertReference(anObject)
end shouldBeReference
(*!
@abstract
Fails when a variable is a reference.
@param
anObject <em>[anything]</em> An expression.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertNotReference(anObject)
try
anObject as reference -- Try to coerce to reference class
on error
countAssertion()
return
end try
fail("Got a reference to " & pp(anObject) & ".")
end assertNotReference
(*! @abstract A synonym for @link assertNotReference() @/link. *)
on shouldNotBeReference(anObject)
assertNotReference(anObject)
end shouldNotBeReference
(*!
@abstract
Succeeds when the given argument is a reference to a Cocoa object.
Fails otherwise.
@param
anObject <em>[anything]</em> An expression.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on assertCocoaReference(anObject)
if isCocoaRef(anObject) then
countAssertion()
else
fail(pp(anObject) & space & "is not a reference to a Cocoa object.")
end if
end assertCocoaReference
(*! @abstract A synonym for @link assertCocoaReference() @/link. *)
on shouldBeCocoaReference(anObject)
assertCocoaReference(anObject)
end shouldBeCocoaReference
(*!
@abstract
Succeeds when the given argument is not a reference to a Cocoa object.
Fails otherwise.
@param
anObject <em>[anything]</em> An expression.
@throws
A @link TEST_FAILED @/link error if the assertion fails.
*)
on refuteCocoaReference(anObject)
if isCocoaRef(anObject) then
fail(pp(anObject) & space & "is a reference to a Cocoa object.")
else
countAssertion()
end if
end refuteCocoaReference
(*! @abstract A synonym for @link refuteCocoaReference() @/link. *)
on shouldNotBeCocoaReference(anObject)
refuteCocoaReference(anObject)
end shouldNotBeCocoaReference
(*!
@abstract
Fails when the given assertion succeeds.
@discussion
This is mostly a convenience for testing ASUnit itself, since for every
positive assertion (<tt>assert...</tt>, <tt>should...</tt>), ASUnit already
defines a corresponding negative assertion (<tt>refute...</tt>, <tt>shouldnt...</tt>).
@param
assertion <em>[handler]</em> An assertion handler.
@param
args <em>[list]</em> A list of arguments to be passed to the handler.
The length of the list must match the number of arguments of the assertion.
@param
msg <em>[text]</em> A message to print when this test fails.
@throws
A @link TEST_FAILED @/link error when <tt>assertion(args)</tt> succeeds.
*)
on failIf(assertion, args, msg)
local n
script AssertionFunctor
property call : assertion
end script
if args's class is not list then set args to {args}
set n to length of args
try
if n = 1 then
AssertionFunctor's call(item 1 of args)
else if n = 2 then
AssertionFunctor's call(item 1 of args, item 2 of args)
else if n = 3 then
AssertionFunctor's call(item 1 of args, item 2 of args, item 3 of args)
else
error "Wrong number of arguments to assertion handler" number -1721
end if
error number TEST_SUCCEEDED_BUT_SHOULD_HAVE_FAILED
on error errMsg number errNum
if errNum is TEST_FAILED then
countAssertion()
return
end if
if errNum is TEST_SUCCEEDED_BUT_SHOULD_HAVE_FAILED then
set my nAssertions to (my nAssertions) - 1
fail(msg)
end if
if errNum is TEST_SKIPPED then skip(msg)
error errMsg number errNum
end try
end failIf
(*!
@abstract
Utility handler to check whether a given expression is a reference to a Cocoa object.
@discussion
See <a href="http://macscripter.net/viewtopic.php?pid=177998">this MacScripter's thread</a>.
*)
on isCocoaRef(x)
try
(class of x) as reference
(contents of class of x is class of x)
on error
false
end try
end isCocoaRef
(*!
@abstract
Returns a textual representation of an object.
@param
anObject <em>[anything]</em> An expression.
*)
on pp(anObject)
_pp(anObject, 0)
end pp
on _pp(anObject, depth)
local res, klass, referencedObject
if depth > my maxRecursionDepth then return "..."
try -- Is it a reference?
anObject as reference
try
set referencedObject to contents of anObject
on error
return "Çundefined referenceÈ"
end try
if anObject is not equal to referencedObject then
return "a reference to" & space & _pp(contents of anObject, depth + 1)
end if
-- Is it an Objective-C reference?
if isCocoaRef(anObject) then return "Çclass ocidÈ"
-- Is it a file reference?
try
if class of anObject is alias then
return "alias" & space & asText(anObject)
end if
end try
try
anObject as Çclass furlÈ
return "file" & space & asText(anObject)
end try
-- Is it a date?
try
if class of anObject is date then return asText(anObject)
end try
-- Is it a unit type?
try
set klass to class of anObject
if klass is in {centimeters, feet, inches, kilometers, meters, miles, yards, square feet, square kilometers, square meters, square miles, square yards, cubic centimeters, cubic feet, cubic inches, cubic meters, cubic yards, gallons, liters, quarts, grams, kilograms, ounces, pounds, degrees Celsius, degrees Fahrenheit, degrees Kelvin} then
return asText(anObject) & space & asText(klass)
end if
end try
try