@@ -67,13 +67,12 @@ open class AsciiDocTestSuite(
6767 private var currentDocumentLevel: DocumentLevel ? = null
6868 private var currentDepth = 0
6969
70- private val globalCodeBlocks = mutableMapOf<String , ParsedBlock >()
71- private var codeBlocksOfTest = mutableMapOf<String , ParsedBlock >()
70+ private val globalCodeBlocks = mutableMapOf<String , MutableList < ParsedBlock > >()
71+ private var codeBlocksOfTest = mutableMapOf<String , MutableList < ParsedBlock > >()
7272
7373 fun parse (): Stream <DynamicNode > {
74- val file = File (AsciiDocTestSuite ::class .java.getResource(" /$fileName " ).toURI())
74+ val file = File (AsciiDocTestSuite ::class .java.getResource(" /$fileName " )? .toURI()!! )
7575 val lines = file.readLines()
76- val terminatorElement = testCaseMarkers.lastOrNull()
7776
7877 var title: String? = null
7978 var currentBlock: ParsedBlock ? = null
@@ -92,11 +91,17 @@ open class AsciiDocTestSuite(
9291 val headlineMatcher = HEADLINE_PATTERN .matcher(line)
9392
9493 when {
95- ! globalDone && globalMarkers.contains(line) -> currentBlock = startBlock(line, lineNr, globalCodeBlocks)
94+ ! globalDone && globalMarkers.contains(line) -> currentBlock =
95+ startBlock(line, lineNr, globalCodeBlocks)
9696 testCaseMarkers.contains(line) -> {
9797 globalDone = true
9898 currentBlock = startBlock(line, lineNr, codeBlocksOfTest)
9999 }
100+ line == " '''" -> {
101+ createTests(title, lineNr, ignore)
102+ currentBlock = null
103+ ignore = false
104+ }
100105 line == " ----" -> {
101106 inside = ! inside
102107 if (inside) {
@@ -111,16 +116,10 @@ open class AsciiDocTestSuite(
111116 SCHEMA_MARKER -> {
112117 val schemaTests = schemaTestFactory(currentBlock.code())
113118 currentDocumentLevel?.tests?.add(schemaTests)
114- if (terminatorElement == null ) {
119+ if (testCaseMarkers.isEmpty()) {
115120 break @loop
116121 }
117122 }
118-
119- terminatorElement -> {
120- createTests(title, lineNr, ignore)
121- currentBlock = null
122- ignore = false
123- }
124123 }
125124
126125 }
@@ -143,15 +142,22 @@ open class AsciiDocTestSuite(
143142
144143 if (UPDATE_TEST_FILE ) {
145144 // this test prints out the adjusted test file
146- root?.afterTests?.add(DynamicTest .dynamicTest(" Write updated Testfile" , srcLocation, this @AsciiDocTestSuite::writeAdjustedTestFile))
145+ root?.afterTests?.add(
146+ DynamicTest .dynamicTest(" Write updated Testfile" , srcLocation, this @AsciiDocTestSuite::writeAdjustedTestFile)
147+ )
147148 } else if (GENERATE_TEST_FILE_DIFF ) {
148149 // this test prints out the adjusted test file
149- root?.afterTests?.add(DynamicTest .dynamicTest(" Adjusted Tests" , srcLocation, this @AsciiDocTestSuite::printAdjustedTestFile))
150+ root?.afterTests?.add(
151+ DynamicTest .dynamicTest(" Adjusted Tests" , srcLocation, this @AsciiDocTestSuite::printAdjustedTestFile)
152+ )
150153 }
151154 return root?.generateTests() ? : Stream .empty()
152155 }
153156
154157 private fun createTests (title : String? , lineNr : Int , ignore : Boolean ) {
158+ if (codeBlocksOfTest.isEmpty()) {
159+ throw IllegalStateException (" no code blocks for tests (line $lineNr )" )
160+ }
155161 val tests = testFactory(
156162 title ? : throw IllegalStateException (" Title should be defined (line $lineNr )" ),
157163 globalCodeBlocks,
@@ -211,33 +217,34 @@ open class AsciiDocTestSuite(
211217 return rebuildTest.toString()
212218 }
213219
214- fun startBlock (marker : String , lineIndex : Int , blocks : MutableMap <String , ParsedBlock >): ParsedBlock {
220+ fun startBlock (marker : String , lineIndex : Int , blocks : MutableMap <String , MutableList < ParsedBlock > >): ParsedBlock {
215221 val uri = UriBuilder .fromUri(srcLocation).queryParam(" line" , lineIndex + 1 ).build()
216222 val block = ParsedBlock (marker, uri)
217223 knownBlocks + = block
218- blocks[ marker] = block
224+ blocks.computeIfAbsent( marker) { mutableListOf () }.add( block)
219225 return block
220226 }
221227
222- protected open fun testFactory (title : String , globalBlocks : Map <String , ParsedBlock >, codeBlocks : Map <String , ParsedBlock >, ignore : Boolean ): List <DynamicNode > {
228+ protected open fun testFactory (title : String , globalBlocks : Map <String , List < ParsedBlock >> , codeBlocks : Map <String , List < ParsedBlock > >, ignore : Boolean ): List <DynamicNode > {
223229 return emptyList()
224230 }
225231
226232 protected open fun schemaTestFactory (schema : String ): List <DynamicNode > {
227233 return emptyList()
228234 }
229235
230- protected fun getOrCreateBlock (codeBlocks : Map <String , ParsedBlock >, marker : String , headline : String ): ParsedBlock ? {
231- var block = codeBlocks[marker]
232- if (block == null && (GENERATE_TEST_FILE_DIFF || UPDATE_TEST_FILE )) {
236+ protected fun getOrCreateBlocks (codeBlocks : Map <String , List < ParsedBlock >> , marker : String , headline : String ): List < ParsedBlock > {
237+ val blocks = codeBlocks[marker]?.toMutableList() ? : mutableListOf ()
238+ if (blocks.isEmpty() && (GENERATE_TEST_FILE_DIFF || UPDATE_TEST_FILE )) {
233239 val insertPoints = testCaseMarkers.indexOf(marker).let { testCaseMarkers.subList(0 , it).asReversed() }
234- val insertPoint = insertPoints.mapNotNull { codeBlocks[it] }.firstOrNull()
235- ? : throw IllegalArgumentException (" none of the insert points $insertPoints found" )
236- block = ParsedBlock (marker, insertPoint.uri, headline)
240+ val insertPoint = insertPoints.mapNotNull { codeBlocks[it]?.firstOrNull() }.firstOrNull()
241+ ? : throw IllegalArgumentException (" none of the insert points $insertPoints found in $fileName " )
242+ val block = ParsedBlock (marker, insertPoint.uri, headline)
237243 block.start = (insertPoint.end ? : throw IllegalStateException (" no start for block defined" )) + 6
238- knownBlocks + = block
244+ knownBlocks + = blocks
245+ blocks + = block
239246 }
240- return block
247+ return blocks
241248 }
242249
243250 companion object {
0 commit comments