Skip to content

Commit

Permalink
Merge pull request #768 from melkiyasser/BookTesterVersion2
Browse files Browse the repository at this point in the history
BookTester Tests
  • Loading branch information
Ducasse authored Jun 7, 2024
2 parents beec7ec + 0d2e852 commit 8c097dd
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 78 deletions.
191 changes: 191 additions & 0 deletions src/Microdown-BookTester-Tests/MicBookTesterVisitorTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
Class {
#name : 'MicBookTesterVisitorTest',
#superclass : 'TestCase',
#category : 'Microdown-BookTester-Tests',
#package : 'Microdown-BookTester-Tests'
}

{ #category : 'tests' }
MicBookTesterVisitorTest >> parseAndTest: docText [
| doc bTester |
doc := Microdown parse: docText.
bTester := MicBookTesterVisitor new.
bTester start: doc.
^ bTester
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExampleCodeblock [
| docText bTester |
docText := '```example=true
3 + 4
>>> 7
``` '.
bTester := self parseAndTest: docText.
self assertEmpty: bTester failedTests.
self assert: bTester validTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExampleCodeblockWithFailingTest [
| docText bTester |
docText := '```example=true
3 + ''12''
>>> 8
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester validTests.
self assert: bTester failedTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExampleCodeblockWithFalseTest [
| docText bTester |
docText := '```example=true
3 + 4
>>> 8
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester validTests.
self assert: bTester failedTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExampleCodeblockWithNoBrakets [
| docText bTester |
docText := '```example=true
3 + 12
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester validTests.
self assert: bTester failedTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExampleCodeblockWithTwoBrackets [
">> instead of > > > should not work so there is one failed test"
| docText bTester |
docText := '```example=true
3 + 12
>> 15
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester validTests.
self assert: bTester failedTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExpectedFailureForAFailure [
| docText bTester |
docText := '```example=true&expectedFailure=true
3 + 4
>>> 12
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester failedTests.
self assert: bTester validTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExpectedFailureForARaisedException [
| docText bTester |
docText := '```example=true&expectedFailure=true
3 + ''a''
>>> 12
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester failedTests.
self assert: bTester validTests size equals: 1
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExplanationIsExceptionCatchedInFailingTest [
| docText doc bTester |
docText := '```example=true
3 + ''12''
>>> 8
```
'.
doc := Microdown parse: docText.
bTester := MicBookTesterVisitor new.
bTester start: doc.
self assertEmpty: bTester validTests.
self assert: bTester failedTests size equals: 1.
self assert: bTester failedTests first explanation equals: 'Instance of Character did not understand #adaptToNumber:andSend:'
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExplanationIsTestFailedWithoutException [
| docText doc bTester |
docText := '```example=true
3 + 13
>>> 15
```
'.
doc := Microdown parse: docText.
bTester := MicBookTesterVisitor new.
bTester start: doc.
self assertEmpty: bTester validTests.
self assert: bTester failedTests size equals: 1.
self assert: bTester failedTests first explanation equals: 'Test failed without raising an exception'
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExplanationIsTestPassed [
| docText doc bTester |
docText := '```example=true
3 + 12
>>> 15
```
'.
doc := Microdown parse: docText.
bTester := MicBookTesterVisitor new.
bTester start: doc.
self assert: bTester validTests size equals: 1.
self assertEmpty: bTester failedTests.
self assert: bTester validTests first explanation equals: 'Test passed'
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testNoExampleCodeblock [
| docText bTester |
docText := '```
3 + 4
>>> 7
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester failedTests.
self assertEmpty: bTester validTests
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testThreeCodeBlocksWithTwoExamples [

| docText bTester |
docText :=
'```example=true
3 + 4
>>> 7
```
```example=true
3 + 4
>>> 8
```
```
3 + ''4''
>>> 7
```
'.
bTester := self parseAndTest: docText.
self assert: bTester failedTests size equals: 1.
self assert: bTester validTests size equals: 1.
]
1 change: 1 addition & 0 deletions src/Microdown-BookTester-Tests/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : 'Microdown-BookTester-Tests' }
23 changes: 15 additions & 8 deletions src/Microdown-BookTester/MicBookTesterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ MicBookTesterVisitor >> allTestResults: anObject [
{ #category : 'visiting' }
MicBookTesterVisitor >> checkAssociation: aCodeBlockObject [
"Asserts the association created in any codeBlock "
^ (self class compiler evaluate: aCodeBlockObject text ) isPaired .
|association|

association := (self class compiler evaluate: aCodeBlockObject text) .
^ (association key = association value )
]

{ #category : 'visiting' }
Expand Down Expand Up @@ -87,12 +90,16 @@ MicBookTesterVisitor >> visitCode: aCodeBlock [
textForTestcase
executeAndReportExample:
Note that there is no fragmentedText here, no need for it b/ecause the codeblock text contains the whole text and is the equivalent of Playground execution"
| result |
result := MicBookTestResult new.
result text: aCodeBlock code.
allTestResults add: result .
self excuteAndReportExample: result.
aCodeBlock isExpectedFailure
ifTrue: [ result status: result status not]
aCodeBlock isExample
ifFalse: [ ]
ifTrue: [
| result |
result := MicBookTestResult new.
result text: aCodeBlock code.
allTestResults add: result .
self excuteAndReportExample: result.
aCodeBlock isExpectedFailure
ifTrue: [ result status: result status not]
]

]
9 changes: 7 additions & 2 deletions src/Microdown-Evaluator/MicCodeBlock.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ MicCodeBlock >> isEvaluated [
]

{ #category : '*Microdown-Evaluator' }
MicCodeBlock >> isExpectedFailure [
MicCodeBlock >> isExample [
arguments at: #example ifAbsent: [ ^ false ].
^ (arguments at: #example) = 'true'
]

{ #category : '*Microdown-Evaluator' }
MicCodeBlock >> isExpectedFailure [
arguments at: #expectedFailure ifAbsent: [ ^ false ].
^ (arguments at: #expectedFailure) = 'true'

]
Loading

0 comments on commit 8c097dd

Please sign in to comment.