TestAsserter.st
changeset 687 e8513ab11939
parent 674 ddf31755804e
child 692 781228855c60
equal deleted inserted replaced
686:7e6f03915ad9 687:e8513ab11939
    14 
    14 
    15 
    15 
    16 !TestAsserter class methodsFor:'asserting'!
    16 !TestAsserter class methodsFor:'asserting'!
    17 
    17 
    18 assert: aBoolean description: aString
    18 assert: aBoolean description: aString
    19 	"Minimal clone of the instance-side assert protocol so that class-side methods can use it."
    19         "Minimal clone of the instance-side assert protocol so that class-side methods can use it."
    20 
    20 
    21 	aBoolean ifFalse:
    21         aBoolean ifFalse: [
    22 		[self logFailure: aString.
    22             self logFailure: aString.
    23 		TestResult failure sunitSignalWith: aString].
    23             TestResult failure sunitSignalWith: aString
       
    24         ].
       
    25 
       
    26     "Modified (format): / 13-07-2017 / 15:03:51 / cg"
    24 ! !
    27 ! !
    25 
    28 
    26 !TestAsserter class methodsFor:'logging'!
    29 !TestAsserter class methodsFor:'logging'!
    27 
    30 
    28 failureLog
    31 failureLog
    43 logSkipped: aString
    46 logSkipped: aString
    44         self isLogging ifTrue:
    47         self isLogging ifTrue:
    45                 [self failureLog cr; nextPutAll: aString; flush].
    48                 [self failureLog cr; nextPutAll: aString; flush].
    46 ! !
    49 ! !
    47 
    50 
       
    51 !TestAsserter class methodsFor:'private'!
       
    52 
       
    53 comparingCollectionBetween: left and: right
       
    54     "helper to generate a nice description, if 
       
    55      a collection is not the expected one."
       
    56      
       
    57     | additionalLeft additionalRight sortBlock|
       
    58 
       
    59     "use a very slow sort block"
       
    60     sortBlock := [ :a :b | a asString <= b asString ].
       
    61     additionalLeft := (left difference: right) sorted: sortBlock.
       
    62     additionalRight := (right difference: left) sorted: sortBlock. 
       
    63 
       
    64     ^ String 
       
    65         streamContents: [:stream |
       
    66             stream
       
    67                 nextPutAll: 'Given Collections do not match.'; lf;
       
    68                 tab; nextPutAll: 'Got left := '; print: left; nextPut: $.; lf;
       
    69                 nextPutAll: ' instead of '; lf;
       
    70                 tab; nextPutAll: 'right :='; print: right; nextPut: $.; lf.
       
    71 
       
    72             left size = right size ifFalse: [ 
       
    73                 stream 
       
    74                     nextPutAll: 'Collection size does not match: left='; 
       
    75                     print: left size;
       
    76                     nextPutAll: ' vs. right=';
       
    77                     print: right size; lf 
       
    78             ].
       
    79             additionalLeft notEmpty ifTrue:[
       
    80                   stream
       
    81                       nextPutAll:'Got ';
       
    82                       print:additionalLeft size;
       
    83                       nextPutAll:' additional element(s) in the left collection: ';
       
    84                       tab;
       
    85                       print:additionalLeft
       
    86             ].
       
    87             additionalRight notEmpty ifTrue:[
       
    88                   stream
       
    89                       nextPutAll:'Got ';
       
    90                       print:additionalRight size;
       
    91                       nextPutAll:' additional element(s) in the right collection: ';
       
    92                       tab;
       
    93                       print:additionalRight
       
    94             ]
       
    95         ]
       
    96 
       
    97     "
       
    98      self basicNew comparingCollectionBetween:#(1 2 3) and:#(1 2 3 4)
       
    99     "
       
   100 
       
   101     "Modified: / 13-07-2017 / 14:13:15 / cg"
       
   102 ! !
       
   103 
    48 !TestAsserter methodsFor:'asserting'!
   104 !TestAsserter methodsFor:'asserting'!
    49 
   105 
    50 assert: aBoolean
   106 assert: aBooleanOrBlock
    51     "fail the testcase if aBoolean is false"
   107     "fail the testcase if aBooleanOrBlock evaluates to false"
    52 
   108 
    53     <resource: #skipInDebuggersWalkBack>
   109     <resource: #skipInDebuggersWalkBack>
    54 
   110 
    55     aBoolean ifFalse:
   111     aBooleanOrBlock value ifFalse:[
    56         [self logFailure: 'Assertion failed'.
   112         self logFailure: 'Assertion failed'.
    57         TestResult failure sunitSignalWith: 'Assertion failed'].
   113         TestResult failure sunitSignalWith: 'Assertion failed'
       
   114     ].
    58 
   115 
    59     "Modified: / 05-12-2009 / 18:14:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   116     "Modified: / 05-12-2009 / 18:14:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    60 !
   117     "Modified: / 13-07-2017 / 13:50:13 / cg"
    61 
   118     "Modified (comment): / 13-07-2017 / 15:05:33 / cg"
    62 assert:aBoolean description:aString
   119 !
    63     "fail the testCase if aBoolean is false, 
   120 
    64      and report aString as failure-description."
   121 assert:aBooleanOrBlock description:aString
    65 
   122     "fail the testCase if aBooleanOrBlock evaluates to false, 
    66     <resource: #skipInDebuggersWalkBack>
   123      and report aStringOrBlock's value as failure-description."
    67 
   124 
    68     ^self assert:aBoolean description:aString resumable: false.
   125     <resource: #xxskipInDebuggersWalkBack>
    69 
   126 
    70     "Modified: / 06-08-2006 / 22:56:27 / cg"
   127     ^self assert:aBooleanOrBlock description:aString resumable: false.
       
   128 
    71     "Modified: / 11-09-2010 / 15:34:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   129     "Modified: / 11-09-2010 / 15:34:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    72 !
   130     "Modified (comment): / 13-07-2017 / 15:06:02 / cg"
    73 
   131 !
    74 assert: aBoolean description: aString resumable: resumableBoolean
   132 
    75 
   133 assert:aBooleanOrBlock description:aStringOrBlock resumable:resumableBoolean
    76     <resource: #skipInDebuggersWalkBack>
   134     "fail the testCase if aBooleanOrBlock evaluates to false, 
       
   135      and report aStringOrBlock's value as failure-description.
       
   136      If resumableBoolean is true, the test can be resumed in the debugger (if it was started by 'Debug')"
       
   137 
       
   138     <resource: #skipInDebuggersWalkBack>
       
   139 
    77     | exception |
   140     | exception |
    78     aBoolean ifFalse:
   141 
    79 	[self logFailure: aString.
   142     aBooleanOrBlock value ifFalse:[
    80 		exception := resumableBoolean
   143         |string|
    81 			ifTrue: [TestResult resumableFailure]
   144 
    82 			ifFalse: [TestResult failure].
   145         string := aStringOrBlock value.
    83 		exception sunitSignalWith: aString].
   146         self logFailure: string.
       
   147         exception := resumableBoolean
       
   148                         ifTrue: [TestResult resumableFailure]
       
   149                         ifFalse: [TestResult failure].
       
   150         exception sunitSignalWith: string
       
   151     ].
    84 
   152 
    85     "Modified: / 05-12-2009 / 18:15:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   153     "Modified: / 05-12-2009 / 18:15:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    86 !
   154     "Modified: / 13-07-2017 / 13:49:38 / cg"
    87 
   155     "Modified (comment): / 13-07-2017 / 15:07:33 / cg"
    88 deny: aBoolean
   156 !
    89     "fail the testcase if aBoolean is true"
   157 
    90 
   158 deny: aBooleanOrBlock
    91     <resource: #skipInDebuggersWalkBack>
   159     "fail the testcase if aBooleanOrBlock evaluates to true"
    92 
   160 
    93     self assert: aBoolean not.
   161     <resource: #skipInDebuggersWalkBack>
       
   162 
       
   163     self assert:(aBooleanOrBlock value not).
    94 
   164 
    95     "Modified: / 05-12-2009 / 18:16:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   165     "Modified: / 05-12-2009 / 18:16:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    96 !
   166     "Modified (comment): / 13-07-2017 / 15:06:17 / cg"
    97 
   167 !
    98 deny: aBoolean description: aString
   168 
    99     "fail the testCase if aBoolean is true, 
   169 deny: aBooleanOrBlock description: aString
   100      and report aString as failure-description."
   170     "fail the testCase if aBooleanOrBlock evaluates to true, 
   101 
   171      and report aStringOrBlock's value as failure-description."
   102     <resource: #skipInDebuggersWalkBack>
   172 
   103 
   173     <resource: #skipInDebuggersWalkBack>
   104     self assert: aBoolean not description: aString.
   174 
       
   175     self assert:(aBooleanOrBlock value not) description:aString.
   105 
   176 
   106     "Modified: / 05-12-2009 / 18:17:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   177     "Modified: / 05-12-2009 / 18:17:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   107 !
   178     "Modified (comment): / 13-07-2017 / 15:06:30 / cg"
   108 
   179 !
   109 deny: aBoolean description: aString resumable: resumableBoolean
   180 
   110 
   181 deny:aBooleanOrBlock description:aString resumable:resumableBoolean
   111     <resource: #skipInDebuggersWalkBack>
   182     "fail the testCase if aBooleanOrBlock evaluates to true, 
   112 
   183      and report aStringOrBlock's value as failure-description.
   113     self assert: aBoolean not description: aString resumable: resumableBoolean.
   184      If resumableBoolean is true, the test can be resumed in the debugger (if it was started by 'Debug')"
       
   185 
       
   186     <resource: #skipInDebuggersWalkBack>
       
   187 
       
   188     self assert:(aBooleanOrBlock value not) description:aString resumable:resumableBoolean.
   114 
   189 
   115     "Modified: / 05-12-2009 / 18:17:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   190     "Modified: / 05-12-2009 / 18:17:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   191     "Modified (comment): / 13-07-2017 / 15:07:38 / cg"
   116 !
   192 !
   117 
   193 
   118 should: aBlock raise: anExceptionalEvent
   194 should: aBlock raise: anExceptionalEvent
   119     "during the execution of aBlock, the anExceptionalEvent should be raised"
   195     "during the execution of aBlock, the anExceptionalEvent should be raised"
   120 
   196 
   235     "Modified (format): / 12-02-2017 / 22:44:17 / cg"
   311     "Modified (format): / 12-02-2017 / 22:44:17 / cg"
   236 ! !
   312 ! !
   237 
   313 
   238 !TestAsserter methodsFor:'convenience'!
   314 !TestAsserter methodsFor:'convenience'!
   239 
   315 
   240 assert: anObject equals: anotherObject
   316 assert:anObject equals:anotherObject
   241 	self assert: anObject = anotherObject
   317     ^ self 
   242 		description: anObject printString, ' is not equal to ', anotherObject printString.
   318         assert: anObject = anotherObject
       
   319         description: [ anObject printString, ' is not equal to ', anotherObject printString ].
       
   320 
       
   321     "Modified: / 13-07-2017 / 15:12:26 / cg"
       
   322 !
       
   323 
       
   324 assertCollection:actual equals:expected
       
   325     "specialized test method that generates a proper error message for collection"
       
   326 
       
   327     ^ self 
       
   328         assert:(expected = actual)
       
   329         description:[ self class comparingCollectionBetween:actual and:expected ]
       
   330 
       
   331     "
       
   332      self basicNew assertCollection:#(1 2 3) equals:#(1 2 3)
       
   333      self basicNew assertCollection:#(1 2 3) equals:#(1 2 3 4)
       
   334     "
       
   335 
       
   336     "Created: / 13-07-2017 / 13:46:51 / cg"
       
   337     "Modified (comment): / 13-07-2017 / 15:03:28 / cg"
   243 ! !
   338 ! !
   244 
   339 
   245 !TestAsserter methodsFor:'logging'!
   340 !TestAsserter methodsFor:'logging'!
   246 
   341 
   247 logFailure: aString
   342 logFailure: aString