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

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

Open
github-actions bot opened this issue Oct 11, 2020 · 0 comments
Labels

Comments

@github-actions
Copy link

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

@github-actions github-actions bot added the todo label Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants