SimpleTestResourceTestCase.st
changeset 222 8e6f482297fa
parent 108 7509607479f5
child 611 1eecc860f4a5
child 664 e31b2e7b658d
--- a/SimpleTestResourceTestCase.st	Wed Jun 29 20:38:32 2011 +0200
+++ b/SimpleTestResourceTestCase.st	Wed Jun 29 21:15:49 2011 +0200
@@ -7,48 +7,15 @@
 	category:'SUnit-Tests'
 !
 
-TestResource subclass:#SimpleTestResource
-	instanceVariableNames:'runningState hasRun hasSetup hasRanOnce'
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:SimpleTestResourceTestCase
-!
 
-
-!SimpleTestResourceTestCase class methodsFor:'Not categorized'!
+!SimpleTestResourceTestCase class methodsFor:'accessing'!
 
 resources
-	^Set new add: SimpleTestResource; yourself
-			
+	^Array with: SimpleTestResource
 ! !
 
 !SimpleTestResourceTestCase methodsFor:'Not categorized'!
 
-dummy
-	self assert: true
-			
-!
-
-error
-	'foo' odd
-			
-!
-
-fail
-	self assert: false
-			
-!
-
-setRun
-	resource setRun
-			
-!
-
-setUp
-	resource := SimpleTestResource current
-			
-!
-
 testRan
 	| case |
 
@@ -56,7 +23,7 @@
 	case run.
 	self assert: resource hasSetup.
 	self assert: resource hasRun
-			
+
 !
 
 testResourceInitRelease
@@ -67,82 +34,103 @@
 	suite addTest: (self class selector: #dummy).
 	result := suite run.
 	self assert: resource hasSetup
-			
-!
 
-testResourcesCollection
-	| collection |
-	collection := self resources.
-	self assert: collection size = 1
-			
 ! !
 
-!SimpleTestResourceTestCase::SimpleTestResource methodsFor:'accessing'!
+!SimpleTestResourceTestCase methodsFor:'private'!
 
-runningState
-
-	^runningState
-			
+dummy
+	self assert: resource hasSetup
+		description: 'This test uses a resource but it was not set up'.
+	self setRun.
+	self assert: resource hasRun
+		description: 'This test uses a resource but we could not interact with it'.
 !
 
-runningState: aSymbol
+error
+	'foo' odd
+!
 
-	runningState := aSymbol
-			
+fail
+	self assert: false
+!
+
+setRun
+	resource setRun
 ! !
 
-!SimpleTestResourceTestCase::SimpleTestResource methodsFor:'running'!
-
-setRun
-	hasRun := true
-			
-!
+!SimpleTestResourceTestCase methodsFor:'running'!
 
 setUp
-	
-	self runningState: self startedStateSymbol.
-	hasSetup := true
-			
+	"Ensure that we error, not just fail, if resource is nil so that #should:raise: checks cannot mistake such an error for what they are trapping."
+
+	resource := SimpleTestResource rawCurrentForTest.
+	self deny: resource isNil
+		description: 'SimpleTestResource has no current value in test'.
+	self assert: resource class == SimpleTestResource
+		description: 'SimpleTestResource current is not an instance of itself'.
+	self assert: resource hasSetup
+		description: 'This test uses a resource but it was not set up'.
 !
 
-startedStateSymbol
+testDebugTestWithResource
+	"The debug will raise an error if the resource is not set up properly."
 
-	^#started
-			
+	self clearOuterResourceStateDuring:
+		[(self class selector: #setRun) debug].
 !
 
-stoppedStateSymbol
-
-	^#stopped
-			
+testResourceCollection
+	self assert: self class buildSuiteFromSelectors resources size = self resources size
+		description: 'The suite should have the same number of resources as its test'.
+	self class buildSuiteFromSelectors resources do:
+		[:each |
+		self assert: (self resources includes: each)
+			description: each name, ':  I have this resource but my suite does not'].
 !
 
-tearDown
+testRunSuiteWithResource
+	| suite |
+	suite := TestSuite new.
+	suite addTest: (SimpleTestResourceTestCase selector: #error).
+	suite addTest: (SimpleTestResourceTestCase selector: #fail).
+	suite addTest: (self class selector: #dummy).
+	self clearOuterResourceStateDuring:
+		[self assert: suite run printString = '3 run, 1 passed, 1 failed, 1 error'
+			description: 'A suite of tests needing SimpleTestResource did not run as expected'].
+!
 
-	self runningState: self stoppedStateSymbol
-			
+testRunTestWithResource
+	self clearOuterResourceStateDuring:
+		[self assert: (self class selector: #dummy) run printString
+					= '1 run, 1 passed, 0 failed, 0 errors'
+			description: 'A dummy test that needed a resource did not pass'].
 ! !
 
-!SimpleTestResourceTestCase::SimpleTestResource methodsFor:'testing'!
+!SimpleTestResourceTestCase methodsFor:'utility'!
 
-hasRun
-	^hasRun
-			
-!
+clearOuterResourceStateDuring: aBlock
+	"This self-testing test must clear the outer state of its resources before starting and after finishing, so that it can construct test cases and suites of itself and test them."
 
-hasSetup
-	^hasSetup
-			
-!
-
-isAvailable
-	
-	^self runningState == self startedStateSymbol
-			
+	self assert: SimpleTestResource isAlreadyAvailable
+		description: 'The resource was not set up for the test'.
+	SimpleTestResource reset.
+	self deny: SimpleTestResource isAlreadyAvailable
+		description: 'The resource was still set up before we began the run'.
+	^aBlock sunitEnsure:
+		[self deny: SimpleTestResource isAlreadyAvailable
+			description: 'The resource was still set up after we finished the run'.
+		SimpleTestResource isAvailable.
+		self assert: SimpleTestResource isAlreadyAvailable
+			description: 'The resource was not set up again after the test'].
 ! !
 
 !SimpleTestResourceTestCase class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/goodies/sunit/SimpleTestResourceTestCase.st,v 1.1 2003-09-26 16:01:01 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/goodies/sunit/SimpleTestResourceTestCase.st,v 1.2 2011-06-29 19:15:49 cg Exp $'
+!
+
+version_SVN
+    ^ '§Id: SimpleTestResourceTestCase.st 214 2011-03-14 12:22:21Z vranyj1 §'
 ! !