Skip to content

Commit

Permalink
Fixed BookTester Tests For Code Example
Browse files Browse the repository at this point in the history
  • Loading branch information
melkiyasser committed Jun 6, 2024
1 parent beec7ec commit b258c53
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 77 deletions.
18 changes: 11 additions & 7 deletions src/Microdown-BookTester/MicBookTesterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,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'

]
216 changes: 148 additions & 68 deletions src/Microdown-Tests-BookTester/MicBookTesterVisitorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Class {
MicBookTesterVisitorTest >> parseAndTest: docText [
| doc bTester |
doc := Microdown parse: docText.
bTester := MicrodownBookTesterVisitor new.
bTester := MicBookTesterVisitor new.
bTester start: doc.
^ bTester
]
Expand All @@ -27,85 +27,165 @@ MicBookTesterVisitorTest >> testExampleCodeblock [
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testMultipleCodeAllCorrectBlock [
|content doc visitor|

content := '
```langage=pharo
4+4
>>>
8
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
```
```langage=pharo
4+2
>>>
6
'.
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
```
```langage=pharo
4+0
>>>
4
``` ' .
doc := Microdown parse: content .
visitor := MicrodownBookTesterVisitor new .
doc accept: visitor .
self assert: visitor isOk .
'.
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 >> testMultipleCodeIncorrectBlock [
|content doc visitor|

content := '
```langage=pharo
4+4
>>>
8
MicBookTesterVisitorTest >> testExplanationIsTestFailedWithoutException [
| docText doc bTester |
docText := '```example=true
3 + 13
>>> 15
```
```langage=pharo
4+2
>>>
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: 'Test failed without raising an exception'
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testExplanationIsTestPassed [
| docText doc bTester |
docText := '```example=true
3 + 12
>>> 15
```
```langage=pharo
4+0
>>>
9
``` ' .
doc := Microdown parse: content .
visitor := MicrodownBookTesterVisitor new .
doc accept: visitor .
self deny: visitor isOk .
'.
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 >> testOneCorrectCodeBlock [
|content doc visitor|
content := '
```langage=pharo
4+4
>>>
8
``` ' .
doc := Microdown parse: content .
visitor := MicrodownBookTesterVisitor new .
doc accept: visitor .
self assert: visitor isOk .
MicBookTesterVisitorTest >> testNoExampleCodeblock [
| docText bTester |
docText := '```
3 + 4
>>> 7
```
'.
bTester := self parseAndTest: docText.
self assertEmpty: bTester failedTests.
self assertEmpty: bTester validTests
]

{ #category : 'tests' }
MicBookTesterVisitorTest >> testOneIncorrectcCodeBlock [
|content doc visitor|

content := '
```langage=pharo
4+4
>>>
9
MicBookTesterVisitorTest >> testThreeCodeBlocksWithTwoExamples [

| docText bTester |
docText :=
'```example=true
3 + 4
>>> 7
```
```example=true
3 + 4
>>> 8
```
' .
doc := Microdown parse: content .
visitor := MicrodownBookTesterVisitor new .
doc accept: visitor .
self deny: visitor isOk .
```
3 + ''4''
>>> 7
```
'.
bTester := self parseAndTest: docText.
self assert: bTester failedTests size equals: 1.
self assert: bTester validTests size equals: 1.
]

0 comments on commit b258c53

Please sign in to comment.