Skip to content
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

Add assertion for tag #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/assertj-jsoup.api
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class io/github/ulfs/assertj/jsoup/DocumentAssert : org/assertj/core/api/
public final fun elementHasClass (Ljava/lang/String;Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
public final fun elementHasHtml (Ljava/lang/String;Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
public final fun elementHasHtml (Ljava/lang/String;[Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
public final fun elementHasTag (Ljava/lang/String;Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
public final fun elementHasText (Ljava/lang/String;Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
public final fun elementHasText (Ljava/lang/String;[Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
public final fun elementMatchesHtml (Ljava/lang/String;Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/DocumentAssert;
Expand Down Expand Up @@ -90,6 +91,7 @@ public final class io/github/ulfs/assertj/jsoup/NodeAssertionsSpec {
public final fun hasHtml (Ljava/lang/Object;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
public final fun hasHtml (Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
public final fun hasHtml ([Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
public final fun hasTag (Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
public final fun hasText (Ljava/lang/Object;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
public final fun hasText (Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
public final fun hasText ([Ljava/lang/String;)Lio/github/ulfs/assertj/jsoup/NodeAssertionsSpec;
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/io/github/ulfs/assertj/jsoup/DocumentAssert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,33 @@ public open class DocumentAssert(
}
}

public fun elementHasTag(cssSelector: String, tagName: String): DocumentAssert = apply {
isNotNull

val selection = actual.select(cssSelector)
if (selection.isEmpty()) {
failWithElementNotFound(cssSelector)
return this
}

selection.forEach {
if (it.tagName() != tagName) {
failWithMessage(
"%nExpecting element for%n" +
" <%s>%n" +
"to be of tag%n" +
" <%s>%n" +
"but was%n" +
" <%s>",
cssSelector,
tagName,
it.tagName(),
maskSelection(selection)
)
}
}
}

public fun elementContainsText(cssSelector: String, substring: String): DocumentAssert = apply {
isNotNull

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ public data class NodeAssertionsSpec(
spec.assert()
}

public fun hasTag(tagName: String): NodeAssertionsSpec = apply {
softAssertions.assertThat(document).elementHasTag(cssSelector, tagName)
}

public fun attribute(attributeName: String): NodeAssertionsSpec = attribute(attributeName) { exists() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.github.ulfs.assertj.jsoup

import io.github.ulfs.assertj.jsoup.Assertions.assertThat
import io.github.ulfs.assertj.jsoup.test.hasErrorWithMessage
import io.github.ulfs.assertj.jsoup.test.hasOneError
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.error.AssertJMultipleFailuresError
import org.assertj.core.util.FailureMessages.actualIsNull
import org.jsoup.nodes.Document
import kotlin.test.Test

class DocumentAssertElementHasTagTest {

@Test
fun `should fail if element is null`() {
// given
val nullDocument: Document? = null

// when / then
assertThatThrownBy {
assertThat(nullDocument).elementHasTag(".class", "div")
}
.isInstanceOf(AssertionError::class.java)
.hasMessage(actualIsNull())
}

@Test
fun `should fail if element does not exist`() {
// given
val document = JsoupUtils.parse("")

// when / then
assertThatThrownBy {
assertThat(document, true) {
elementHasTag(".class", "div")
}
}
.isInstanceOf(AssertJMultipleFailuresError::class.java)
.hasOneError()
.hasErrorWithMessage(
"""

Expecting element for
<.class>
but found nothing
""".trimIndent()
)
}

@Test
fun `should pass if element has tag`() {
// given
val document: Document = JsoupUtils.parse("""<div class="class">text</div>""")

// when
assertThat(document, true) {
elementHasTag(".class", "div")
}

// then
// no exception is thrown
}

@Test
fun `should fail if element is of a different tag`() {
// given
val document: Document = JsoupUtils.parse("""<span class="class">different</span>""")

// when / then
assertThatThrownBy {
assertThat(document, true) {
elementHasTag(".class", "div")
}
}
.isInstanceOf(AssertionError::class.java)
.hasOneError()
.hasErrorWithMessage(
"""

Expecting element for
<.class>
to be of tag
<div>
but was
<span>
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ class NodeAssertionsSpecTest {
verify { softAssertions.assertThat(any<Document>()).elementAttributeExists("selector", "attr") }
}

@Test
fun `should call elementHasTag`() {
// given
every { softAssertions.assertThat(any<Document>()).elementHasTag(any(), any()) } returns dummySoftAssertions()

val spec = spec()

// when
spec.hasTag("tag")

// then
verify { softAssertions.assertThat(any<Document>()).elementHasTag("selector", "tag") }
}

private fun spec() = NodeAssertionsSpec(
softAssertions = softAssertions,
Expand Down