Skip to content

Commit

Permalink
Fix broken error handling in Minitests for broken promises
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Mar 14, 2017
1 parent be327e6 commit 79e293c
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions core-lib/TestSuite/Minitest.som
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ OTHER DEALINGS IN THE SOFTWARE. *)
private IdentitySet = platform collections IdentitySet.
private ObjectMirror = platform mirrors ObjectMirror.
private ClassMirror = platform mirrors ClassMirror.
private actors = platform actors.
|)(
class TestCase env: testEnvironment selector: selector = (
Expand Down Expand Up @@ -427,21 +428,27 @@ OTHER DEALINGS IN THE SOFTWARE. *)
promisePair := actors createPromisePair.
[[| promise testSuccess |
promise := sendTestMessage.
promise whenResolved: [:r |
(* At this point the test has been successful, so create the TestSuccess before it is removed. *)
testSuccess := testContextInstance createSuccessResultFor: self.
(* Clean up immediately after a successful test. This makes the test context instance
immediately garbage-collectable, which is an important property for large test suites. *)
cleanUp.
promisePair resolve: testSuccess ].
promise on: TestFailureException do: [:ex |
promisePair resolve: (testContextInstance
ifNil: [ TestFailure case: self description: ex messageText]
ifNotNil: [:it | it createFailureResultFor: self description: ex messageText])].
promise on: Exception do: [:ex |
promisePair resolve: (testContextInstance
ifNil: [ TestError case: self exception: ex ]
ifNotNil: [:it | it createErrorResultFor: self exception: ex ])]
promise
whenResolved: [:r |
(* At this point the test has been successful, so create the TestSuccess before it is removed. *)
testSuccess := testContextInstance createSuccessResultFor: self.
(* Clean up immediately after a successful test. This makes the test context instance
immediately garbage-collectable, which is an important property for large test suites. *)
cleanUp.
promisePair resolve: testSuccess ]
onError: [:e |
| exClass |
exClass := (ClassMirror reflecting: e) classObject.
exClass == TestFailureException ifTrue: [
promisePair resolve: (testContextInstance
ifNil: [ TestFailure case: self description: ex messageText]
ifNotNil: [:it | it createFailureResultFor: self description: ex messageText])
] ifFalse: [
promisePair resolve: (testContextInstance
ifNil: [ TestError case: self exception: ex ]
ifNotNil: [:it | it createErrorResultFor: self exception: ex ])
]
]
] on: TestFailureException
do: [:ex |
(* testContextInstance should never be nil at this point, but just in case. *)
Expand Down Expand Up @@ -643,7 +650,7 @@ OTHER DEALINGS IN THE SOFTWARE. *)
^TestSuccess case: testCase
)

private failWithMessage: messageText = (
protected failWithMessage: messageText = (
TestFailureException signal: messageText
)

Expand Down Expand Up @@ -766,18 +773,24 @@ OTHER DEALINGS IN THE SOFTWARE. *)
)
assert: promise erroredWith: expectedErrorClass = (
| state actualResolution |
(* | state actualResolution |
state := #unresolved.
(withTimeout: promise inMSecs: 300)
whenResolved: [:r | state := #resolved. actualResolution := r ]
catch: [:e | state := #error. actualResolution := e ].
onError: [:e | state := #error. actualResolution := e ].
state = #resolved
ifTrue: [ ^ failWithMessage: 'Promise resolved with: ', actualResolution printString ].
state = #error
ifTrue: [ ^ assert: actualResolution class equals: expectedErrorClass ].
failWithMessage: 'Invalid state blocking on a promise: ', state.
ifTrue: [ ^ assert: (ClassMirror reflecting: actualResolution) classObject is: expectedErrorClass ].
failWithMessage: 'Invalid state blocking on a promise: ', state.*)
^ promise
whenResolved: [:actualResolution |
failWithMessage: 'Promise succeeded unexpectedly with: ' + actualResolution asString ]
onError: [:actualResolution |
assert: (ClassMirror reflecting: actualResolution) classObject is: expectedErrorClass ]
)
)
Expand Down

0 comments on commit 79e293c

Please sign in to comment.