Skip to content

figure out a way to add the index predicate shortcut to strings (gets shadowed b... #52

@github-actions

Description

@github-actions

figure out a way to add the index predicate shortcut to strings (gets shadowed by the default implementation)

https://github.com/DetachHead/xpath-builder/blob/a67ceb2dc666e4234991e31d0aa5268074539cb9/src/commonTest/kotlin/LocationPathTests.kt#L79

import functions.position
import kotlin.test.Test
import kotlin.test.assertEquals

class LocationPathTests {
    @Test
    fun attributeAndChild() =
        assertEquals(
            "descendant-or-self::node()/child::div[attribute::id = '1']/child::span",
            xpath { any / div[{ attr("id") equal "1" }] / span }.toString()
        )

    @Test
    fun indexTest() =
        assertEquals(
            "descendant-or-self::node()/child::div[position() = '1']", xpath { any / div[1] }.toString()
        )

    @Test
    fun innertext() =
        assertEquals(
            "descendant-or-self::node()/child::div[attribute::id = 'thing' and translate(normalize-space(self::node()),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'sdfg']",
            xpath {
                any / div[{ attr("id") equal "thing" and textIs("sdfg") }]
            }.toString()
        )

    @Test
    fun child() =
        assertEquals(
            "descendant-or-self::node()/child::div/child::p[attribute::class = 'asdf']", xpath {
                any / div / p[{ attr("class") equal "asdf" }]
            }.toString()
        )

    @Test
    fun nested() =
        assertEquals(
            "descendant-or-self::node()/child::div[self::node()/child::div[translate(normalize-space(self::node()),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'asdf']]/child::div/child::p[position() = '1']",
            xpath {
                any / div[self / div[textIs("asdf")]] / div / p[1]
            }.toString()
        )

    @Test
    fun escapequotes() =
        //https://stackoverflow.com/questions/14822153/escape-single-quote-in-xpath-with-nokogiri
        assertEquals(
            "descendant-or-self::node()/child::*[translate(normalize-space(self::node()),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = concat('\"That',\"'\",'s mine\", he said.')]",
            xpath {
                anyNode[textIs("\"That's mine\", he said.")]
            }.toString()
        )

    @Test
    fun stringNodeTest() =
        assertEquals(
            "descendant-or-self::node()/child::asdf[attribute::id = 'thing' and translate(normalize-space(self::node()),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'sdfg']",
            xpath {
                any / "asdf"[{ attr("id") equal "thing" and textIs("sdfg") }]
            }.toString()
        )

    @Test
    fun twoStringNodeTests() =
        assertEquals(
            "descendant-or-self::node()/child::div/child::p[attribute::class = 'asdf']",
            xpath {
                any / "div" / "p"[{ attr("class") equal "asdf" }]
            }.toString()
        )

    @Test
    fun nestedstring() =
        assertEquals(
            "descendant-or-self::node()/child::div[self::node()/child::div[translate(normalize-space(self::node()),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'asdf']]/child::div/child::p[position() = '1']",
            xpath {
                //TODO: figure out a way to add the index predicate shortcut to strings (gets shadowed by the default implementation)
                //TODO: fix inconsistency with predicates, some of them require blocks and others dont. and messing them up causes a runtime error
                any / "div"[self / "div"[textIs("asdf")]] / "div" / "p"[{ position() equal "1" }]
            }.toString()
        )

    @Test
    fun classContains() =
        assertEquals(
            "descendant-or-self::node()/child::*[contains(concat(' ',attribute::class,' '),' foo ')]",
            xpath { anyNode[hasClass("foo")] }.toString()
        )
}
 No newline at end of file
ew file mode 100644
ndex 0000000..ec62627
++ b/src/commonTest/kotlin/ShimTests.kt

23562e12539c713daea3a44078ecd831464f8ae6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions