Christian Dräger
04/22/2021, 7:09 PMI am writing a DSL that allows to parse HTML in kotlin.
(example: <https://github.com/skrapeit/skrape.it#parse-and-verify-html-from-string>)
since * is common to use in this context (e.g. when it comes to css selectors) it would feel natural if the DSL could do as well. Therefore it would be handy if it would be possible to overload a unaryTimes operator.
since kotlin already supports unaryPlus and unaryMinus overload, why there isn't a unaryTimes?
here is an example what it looks like right now:
@Test
fun `can parse html from String`() {
htmlDocument("""
<html>
<body>
<h1>welcome</h1>
<div>
<p>first p-element</p>
<p class="foo">some p-element</p>
<p class="foo">last p-element</p>
</div>
</body>
</html>""") {
h1 {
findFirst {
text toBe "welcome"
}
p {
withClass = "foo"
findSecond {
text toBe "some p-element"
className toBe "foo"
}
}
p {
findAll {
text toContain "p-element"
}
findLast {
text toBe "last p-element"
}
}
}
}
}
i think this should be possible right now by making * an infix operator?
@Test
fun `can parse html from String`() {
htmlDocument("""
<html>
<body>
<h1>welcome</h1>
<div>
<p>first p-element</p>
<p class="foo">some p-element</p>
<p class="foo">last p-element</p>
</div>
</body>
</html>""") {
h1 {
find 0 {
text toBe "welcome"
}
p {
withClass = "foo"
find 1 {
text toBe "some p-element"
className toBe "foo"
}
}
p {
find * {
text toContain "p-element"
}
findLast {
text toBe "last p-element"
}
}
}
}
}
what would be possible due to the proposal of having unaryTimes operator to overload:
@Test
fun `can parse html from String`() {
htmlDocument("""
<html>
<body>
<h1>welcome</h1>
<div>
<p>first p-element</p>
<p class="foo">some p-element</p>
<p class="foo">last p-element</p>
</div>
</body>
</html>""") {
h1 {
0 { // already possible by invoke operator
text toBe "welcome"
}
p {
withClass = "foo"
1 {
text toBe "some p-element"
className toBe "foo"
}
}
p {
* { // would need unaryTimes
text toContain "p-element"
}
findLast {
text toBe "last p-element"
}
}
}
}
}
jw
04/22/2021, 7:22 PM.inv()
function for the same purpose on custom types. Their primary use seems to be DSLs, and I would rather see some kind of DSL-specific escape hatch for opening up the use of wider sigils than adding one more (seemingly) mathematically nonsensical operation solely for DSL usage.Christian Dräger
04/22/2021, 7:26 PMjw
04/22/2021, 7:28 PM@DslMarker
which modifies the language behavior to remove your ability to reference enclosing `this`'s in the function hierarchy. Perhaps there could be something with further signaled that you were stepping outside the bounds of the normal language so that more characters could be used like your *
. Basically something between what we have today and macros, without going full macro.nanodeath
04/22/2021, 8:09 PM+"foo"
to append a string does seem bizarre to me. what would replace it?edrd
04/22/2021, 10:34 PMedrd
04/22/2021, 10:36 PMunaryMinus
, on the other hand, never seen (actually I didn't even know it existed)jw
04/22/2021, 10:37 PMedrd
04/22/2021, 10:42 PMnanodeath
04/22/2021, 10:53 PMunaryMinus
is probably useful for negating BigDecimal
jw
04/23/2021, 12:10 AMjw
04/23/2021, 12:10 AMvladimirsitnikov
11/24/2021, 7:49 AMwithClass = "foo" and "bar" and "fizz" and "buzz"
, and UI designers removed class fizz
to optimize CSS. How do you keep the tests up to date?
Have you explored something like https://approvaltests.com/ that is tailored to Kotlin?
Approvaltests (and other snapshot-like tools) allow automatic re-generation of the "expected" outcomes, so I incline that fancy withClass
assertions are not that suitable for 7+ line HTML outputs :-/
It would be great if the failing test could read the original test code, and update it with the actual outputs (see https://github.com/kotest/kotest/issues/395 ).Christian Dräger
11/25/2021, 1:35 PM