-
Notifications
You must be signed in to change notification settings - Fork 410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable explicit API mode #3139
Merged
Merged
Enable explicit API mode #3139
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ import kotlin.test.assertEquals | |
import kotlin.test.asserter | ||
|
||
// entry point: | ||
fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) { | ||
public fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) { | ||
val matcher = ContentMatcherBuilder(ContentComposite::class).apply(block).build() | ||
try { | ||
matcher.tryMatch(this) | ||
|
@@ -24,123 +24,161 @@ fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> | |
|
||
// DSL: | ||
@DslMarker | ||
annotation class ContentMatchersDsl | ||
public annotation class ContentMatchersDsl | ||
|
||
@ContentMatchersDsl | ||
class ContentMatcherBuilder<T : ContentComposite> @PublishedApi internal constructor(private val kclass: KClass<T>) { | ||
public class ContentMatcherBuilder<T : ContentComposite> @PublishedApi internal constructor(private val kclass: KClass<T>) { | ||
@PublishedApi | ||
internal val children = mutableListOf<MatcherElement>() | ||
internal val children: MutableList<MatcherElement> = mutableListOf() | ||
internal val assertions = mutableListOf<T.() -> Unit>() | ||
|
||
fun build() = CompositeMatcher(kclass, childrenOrSkip()) { assertions.forEach { it() } } | ||
public fun build(): CompositeMatcher<T> = CompositeMatcher(kclass, childrenOrSkip()) { assertions.forEach { it() } } | ||
|
||
// part of DSL that cannot be defined as an extension | ||
operator fun String.unaryPlus() { | ||
public operator fun String.unaryPlus() { | ||
children += TextMatcher(this) | ||
} | ||
|
||
private fun childrenOrSkip() = if (children.isEmpty() && assertions.isNotEmpty()) listOf(Anything) else children | ||
} | ||
|
||
fun <T : ContentComposite> ContentMatcherBuilder<T>.check(assertion: T.() -> Unit) { | ||
public fun <T : ContentComposite> ContentMatcherBuilder<T>.check(assertion: T.() -> Unit) { | ||
assertions += assertion | ||
} | ||
|
||
private val ContentComposite.extractedText | ||
get() = withDescendants().filterIsInstance<ContentText>().joinToString(separator = "") { it.text } | ||
|
||
fun <T : ContentComposite> ContentMatcherBuilder<T>.hasExactText(expected: String) { | ||
public fun <T : ContentComposite> ContentMatcherBuilder<T>.hasExactText(expected: String) { | ||
assertions += { | ||
assertEquals(expected, this.extractedText) | ||
} | ||
} | ||
|
||
inline fun <reified S : ContentComposite> ContentMatcherBuilder<*>.composite( | ||
public inline fun <reified S : ContentComposite> ContentMatcherBuilder<*>.composite( | ||
block: ContentMatcherBuilder<S>.() -> Unit | ||
) { | ||
children += ContentMatcherBuilder(S::class).apply(block).build() | ||
} | ||
|
||
inline fun <reified S : ContentNode> ContentMatcherBuilder<*>.node(noinline assertions: S.() -> Unit = {}) { | ||
public inline fun <reified S : ContentNode> ContentMatcherBuilder<*>.node(noinline assertions: S.() -> Unit = {}) { | ||
children += NodeMatcher(S::class, assertions) | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.skipAllNotMatching() { | ||
public fun ContentMatcherBuilder<*>.skipAllNotMatching() { | ||
children += Anything | ||
} | ||
|
||
|
||
// Convenience functions: | ||
fun ContentMatcherBuilder<*>.group(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = composite(block) | ||
public fun ContentMatcherBuilder<*>.group(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.tabbedGroup( | ||
public fun ContentMatcherBuilder<*>.tabbedGroup( | ||
block: ContentMatcherBuilder<ContentGroup>.() -> Unit | ||
) = composite<ContentGroup> { | ||
block() | ||
check { assertContains(this.style, ContentStyle.TabbedContent) } | ||
) { | ||
composite<ContentGroup> { | ||
block() | ||
check { assertContains(this.style, ContentStyle.TabbedContent) } | ||
} | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.tab( | ||
public fun ContentMatcherBuilder<*>.tab( | ||
tabbedContentType: TabbedContentType, block: ContentMatcherBuilder<ContentGroup>.() -> Unit | ||
) = composite<ContentGroup> { | ||
block() | ||
check { | ||
assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) | ||
) { | ||
composite<ContentGroup> { | ||
block() | ||
check { | ||
assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) | ||
} | ||
} | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder<ContentHeader>.() -> Unit) = | ||
public fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder<ContentHeader>.() -> Unit) { | ||
composite<ContentHeader> { | ||
block() | ||
check { if (expectedLevel != null) assertEquals(expectedLevel, this.level) } | ||
} | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = | ||
public fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { | ||
composite<ContentGroup> { | ||
block() | ||
check { assertContains(this.style, TextStyle.Paragraph) } | ||
} | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder<ContentLink>.() -> Unit) = composite(block) | ||
public fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder<ContentLink>.() -> Unit) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder<ContentTable>.() -> Unit) = composite(block) | ||
public fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder<ContentTable>.() -> Unit) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = | ||
public fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { | ||
composite<PlatformHintedContent> { group(block) } | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.list(block: ContentMatcherBuilder<ContentList>.() -> Unit) = composite(block) | ||
public fun ContentMatcherBuilder<*>.list(block: ContentMatcherBuilder<ContentList>.() -> Unit) { | ||
composite(block) | ||
} | ||
Comment on lines
-111
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of specifying the |
||
|
||
fun ContentMatcherBuilder<*>.codeBlock(block: ContentMatcherBuilder<ContentCodeBlock>.() -> Unit) = composite(block) | ||
public fun ContentMatcherBuilder<*>.codeBlock(block: ContentMatcherBuilder<ContentCodeBlock>.() -> Unit) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder<ContentCodeInline>.() -> Unit) = composite(block) | ||
public fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder<ContentCodeInline>.() -> Unit) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = composite<ContentGroup> { | ||
block() | ||
check { assertContains(this.style, ContentStyle.Caption) } | ||
public fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { | ||
composite<ContentGroup> { | ||
block() | ||
check { assertContains(this.style, ContentStyle.Caption) } | ||
} | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.br() = node<ContentBreakLine>() | ||
public fun ContentMatcherBuilder<*>.br() { | ||
node<ContentBreakLine>() | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) { | ||
public fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) { | ||
skipAllNotMatching() | ||
block() | ||
skipAllNotMatching() | ||
} | ||
|
||
fun ContentMatcherBuilder<*>.divergentGroup(block: ContentMatcherBuilder<ContentDivergentGroup>.() -> Unit) = | ||
public fun ContentMatcherBuilder<*>.divergentGroup( | ||
block: ContentMatcherBuilder<ContentDivergentGroup>.() -> Unit | ||
) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<ContentDivergentGroup>.divergentInstance(block: ContentMatcherBuilder<ContentDivergentInstance>.() -> Unit) = | ||
public fun ContentMatcherBuilder<ContentDivergentGroup>.divergentInstance( | ||
block: ContentMatcherBuilder<ContentDivergentInstance>.() -> Unit | ||
) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<ContentDivergentInstance>.before(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = | ||
public fun ContentMatcherBuilder<ContentDivergentInstance>.before( | ||
block: ContentMatcherBuilder<ContentComposite>.() -> Unit | ||
) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<ContentDivergentInstance>.divergent(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = | ||
public fun ContentMatcherBuilder<ContentDivergentInstance>.divergent( | ||
block: ContentMatcherBuilder<ContentComposite>.() -> Unit | ||
) { | ||
composite(block) | ||
} | ||
|
||
fun ContentMatcherBuilder<ContentDivergentInstance>.after(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = | ||
public fun ContentMatcherBuilder<ContentDivergentInstance>.after( | ||
block: ContentMatcherBuilder<ContentComposite>.() -> Unit | ||
) { | ||
composite(block) | ||
} | ||
|
||
/* | ||
* TODO replace with kotlin.test.assertContains after migrating to Kotlin language version 1.5+ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
explicitApi
was set intasks.withType<KotlinCompile>()
, it would be applied to the test sources as well, which we don't want. But setting it inkotlin {}
fixes it, so I did that and moved other compiler arguments into it as well.