Skip to content

Commit

Permalink
Convert tests in DomTest (#27)
Browse files Browse the repository at this point in the history
Converting some tests from TS
  • Loading branch information
nlam-atlassian authored Oct 8, 2024
1 parent f6f3e5b commit 653e3a8
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 139 deletions.
66 changes: 44 additions & 22 deletions model/config/ktlint/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -703,25 +703,50 @@
</file>
<file name="src/commonTest/kotlin/com/atlassian/prosemirror/model/DomTest.kt">
<error line="3" column="1" source="standard:import-ordering" />
<error line="14" column="25" source="standard:multiline-expression-wrapping" />
<error line="16" column="17" source="standard:multiline-expression-wrapping" />
<error line="16" column="41" source="standard:multiline-expression-wrapping" />
<error line="19" column="17" source="standard:multiline-expression-wrapping" />
<error line="19" column="41" source="standard:multiline-expression-wrapping" />
<error line="20" column="26" source="standard:multiline-expression-wrapping" />
<error line="37" column="1" source="standard:no-empty-first-line-in-class-body" />
<error line="41" column="25" source="standard:function-signature" />
<error line="41" column="35" source="standard:function-signature" />
<error line="41" column="54" source="standard:function-signature" />
<error line="41" column="68" source="standard:function-signature" />
<error line="56" column="14" source="standard:function-signature" />
<error line="56" column="25" source="standard:function-signature" />
<error line="56" column="37" source="standard:function-signature" />
<error line="187" column="19" source="standard:multiline-expression-wrapping" />
<error line="259" column="17" source="standard:function-signature" />
<error line="259" column="31" source="standard:function-signature" />
<error line="259" column="42" source="standard:function-signature" />
<error line="259" column="84" source="standard:function-signature" />
<error line="23" column="25" source="standard:function-signature" />
<error line="23" column="35" source="standard:function-signature" />
<error line="23" column="54" source="standard:function-signature" />
<error line="23" column="68" source="standard:function-signature" />
<error line="28" column="47" source="standard:colon-spacing" />
<error line="38" column="14" source="standard:function-signature" />
<error line="38" column="25" source="standard:function-signature" />
<error line="38" column="37" source="standard:function-signature" />
<error line="75" column="51" source="standard:function-signature" />
<error line="75" column="73" source="standard:function-signature" />
<error line="75" column="116" source="standard:function-signature" />
<error line="75" column="119" source="standard:function-signature" />
<error line="84" column="59" source="standard:curly-spacing" />
<error line="181" column="33" source="standard:multiline-expression-wrapping" />
<error line="183" column="25" source="standard:multiline-expression-wrapping" />
<error line="183" column="49" source="standard:multiline-expression-wrapping" />
<error line="186" column="25" source="standard:multiline-expression-wrapping" />
<error line="186" column="49" source="standard:multiline-expression-wrapping" />
<error line="187" column="34" source="standard:multiline-expression-wrapping" />
<error line="197" column="104" source="standard:function-signature" />
<error line="200" column="19" source="standard:multiline-expression-wrapping" />
<error line="211" column="29" source="standard:multiline-expression-wrapping" />
<error line="214" column="25" source="standard:multiline-expression-wrapping" />
<error line="214" column="49" source="standard:multiline-expression-wrapping" />
<error line="215" column="34" source="standard:multiline-expression-wrapping" />
<error line="217" column="36" source="standard:multiline-expression-wrapping" />
<error line="223" column="1" source="standard:indent" />
<error line="227" column="1" source="standard:indent" />
<error line="240" column="19" source="standard:multiline-expression-wrapping" />
<error line="242" column="23" source="standard:multiline-expression-wrapping" />
<error line="245" column="31" source="standard:multiline-expression-wrapping" />
<error line="248" column="37" source="standard:multiline-expression-wrapping" />
<error line="249" column="1" source="standard:indent" />
<error line="250" column="1" source="standard:indent" />
<error line="251" column="1" source="standard:indent" />
<error line="267" column="26" source="standard:multiline-expression-wrapping" />
<error line="270" column="25" source="standard:multiline-expression-wrapping" />
<error line="270" column="49" source="standard:multiline-expression-wrapping" />
<error line="271" column="31" source="standard:multiline-expression-wrapping" />
<error line="281" column="101" source="standard:function-signature" />
<error line="314" column="17" source="standard:function-signature" />
<error line="314" column="31" source="standard:function-signature" />
<error line="314" column="42" source="standard:function-signature" />
<error line="314" column="84" source="standard:function-signature" />
</file>
<file name="src/commonTest/kotlin/com/atlassian/prosemirror/model/MarkTest.kt">
<error line="19" column="1" source="standard:blank-line-before-declaration" />
Expand Down Expand Up @@ -796,7 +821,4 @@
<error line="13" column="41" source="standard:function-signature" />
<error line="13" column="53" source="standard:function-signature" />
</file>
<file name="src/jvmTest/kotlin/com/atlassian/prosemirror/model/JvmPlatformTest.jvm.kt">
<error line="7" column="1" source="standard:no-empty-first-line-in-class-body" />
</file>
</baseline>
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class DOMParser(
// or has an '=' sign after the prop, followed by the given
// value.
style.length > prop.length &&
(style[prop.length] != 61.toChar() || style.slice(prop.length + 1 until style.length) != value)
(style[prop.length] != '=' || style.slice(prop.length + 1 until style.length) != value)
) {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ open class DOMSerializer(
if (space > 0) {
dom.attr(
// name.slice(0 until space),
name.slice(space + 1..space + 1),
name.substring(space + 1),
attrs[name].toString()
)
} else {
Expand Down
215 changes: 141 additions & 74 deletions model/src/commonTest/kotlin/com/atlassian/prosemirror/model/DomTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,30 @@ import com.atlassian.prosemirror.testbuilder.NodeBuildCompanion
import com.atlassian.prosemirror.testbuilder.NodeBuilder
import com.atlassian.prosemirror.testbuilder.NodeSpecImpl
import com.atlassian.prosemirror.testbuilder.PMNodeBuilder.Companion.doc
import kotlin.test.Ignore
import kotlin.test.Test
import com.atlassian.prosemirror.testbuilder.schema as testSchema

val schemaWithComment = Schema(
SchemaSpec(
nodes = testSchema.spec.nodes + mapOf(
"doc" to (testSchema.spec.nodes["doc"] as NodeSpecImpl).copy(marks = "comment")
),
marks = testSchema.spec.marks + mapOf(
"comment" to MarkSpecImpl(
parseDOM = listOf(ParseRuleImpl(tag = "div.comment")),
toDOM = { _, _ ->
DOMOutputSpec.ArrayDOMOutputSpec(listOf("div", mapOf("class" to "comment"), 0))
}
)
)
)
)

fun NodeBuilder<CommentNodeBuilder>.comment(func: NodeBuilder<CommentNodeBuilder>.() -> Unit) = mark("comment", func)
import com.atlassian.prosemirror.testbuilder.AttributeSpecImpl
import com.atlassian.prosemirror.testbuilder.PMNodeBuilder

class CommentNodeBuilder(
pos: Int = 0,
marks: List<Mark> = emptyList(),
override val schema: Schema = schemaWithComment
override val schema: Schema = testSchema
) : NodeBuilder<CommentNodeBuilder>(pos, marks, schema) {

override val checked: Boolean
get() = false

override fun create(pos: Int, marks: List<Mark>, schema: Schema): NodeBuilder<CommentNodeBuilder> {
return CommentNodeBuilder(pos, marks, schema)
}
}

companion object : NodeBuildCompanion<CommentNodeBuilder>(schemaWithComment) {
override val checked: Boolean
get() = false
class CustomNodeBuildCompanion(schema: Schema): NodeBuildCompanion<CommentNodeBuilder>(schema) {
override val checked: Boolean
get() = false

override fun create(): CommentNodeBuilder {
return CommentNodeBuilder()
}
override fun create(): CommentNodeBuilder {
return CommentNodeBuilder(schema = schema)
}
}

Expand Down Expand Up @@ -87,11 +69,23 @@ class DomTest {
)
}

@Ignore // "This test is failing - fix code"
@Test
fun `can represent links`() {
// custom link mark that has a title=null attribute
fun NodeBuilder<PMNodeBuilder>.aWithTitle(href: String = "foo", func: NodeBuilder<PMNodeBuilder>.() -> Unit) =
mark("link", func, attrs = mapOf("href" to href, "title" to null))

test(
doc { p { +"a " + a(href = "foo") { +"big " + a(href = "bar") { +"nested" } + " link" } } },
// TypeScript code: doc(p("a ", a({href: "foo"}, "big ", a({href: "bar"}, "nested"), " link")))
// converts to the code below because each node cannot have more than 1 Link mark
doc {
p {
+"a " +
aWithTitle(href = "foo") { +"big "} +
aWithTitle(href = "bar") { +"nested" } +
aWithTitle(href = "foo") { +" link" }
}
},
"<p>a <a href=\"foo\">big </a><a href=\"bar\">nested</a><a href=\"foo\"> link</a></p>"
)
}
Expand Down Expand Up @@ -184,7 +178,26 @@ class DomTest {

@Test
fun `can parse marks on block nodes`() {
val doc = CommentNodeBuilder.doc {
val schemaWithComment = Schema(
SchemaSpec(
nodes = testSchema.spec.nodes + mapOf(
"doc" to (testSchema.spec.nodes["doc"] as NodeSpecImpl).copy(marks = "comment")
),
marks = testSchema.spec.marks + mapOf(
"comment" to MarkSpecImpl(
parseDOM = listOf(ParseRuleImpl(tag = "div.comment")),
toDOM = { _, _ ->
DOMOutputSpec.ArrayDOMOutputSpec(listOf("div", mapOf("class" to "comment"), 0))
}
)
)
)
)

fun NodeBuilder<CommentNodeBuilder>.comment(func: NodeBuilder<CommentNodeBuilder>.() -> Unit) =
mark("comment", func)

val doc = CustomNodeBuildCompanion(schemaWithComment).doc {
p { +"one" } + this.comment { p { +"two" } + p { strong { +"three" } } } + p { +"four" }
}
test(
Expand All @@ -193,46 +206,88 @@ class DomTest {
)
}

// TODO convert tests below
// it("parses unique, non-exclusive, same-typed marks", () => {
// let commentSchema = new Schema({
// nodes: schema.spec.nodes,
// marks: schema.spec.marks.update("comment", {
// attrs: { id: { default: null }},
// parseDOM: [{
// tag: "span.comment",
// getAttrs(dom) { return { id: parseInt((dom as HTMLElement).getAttribute('data-id')!, 10) } }
// }],
// excludes: '',
// toDOM(mark: Mark) { return ["span", {class: "comment", "data-id": mark.attrs.id }, 0] }
// })
// })
// let b = builders(commentSchema)
// test(b.schema.nodes.doc.createAndFill(undefined, [
// b.schema.nodes.paragraph.createAndFill(undefined, [
// b.schema.text('double comment', [
// b.schema.marks.comment.create({ id: 1 }),
// b.schema.marks.comment.create({ id: 2 })
// ])!
// ])!
// ])!,
// "<p><span class=\"comment\" data-id=\"1\"><span class=\"comment\" data-id=\"2\">double comment</span></span></p>")()
// })
//
// it("serializes non-spanning marks correctly", () => {
// let markSchema = new Schema({
// nodes: schema.spec.nodes,
// marks: schema.spec.marks.update("test", {
// parseDOM: [{tag: "test"}],
// toDOM() { return ["test", 0] },
// spanning: false
// })
// })
// let b = builders(markSchema) as any
// test(b.doc(b.paragraph(b.test("a", b.image({src: "x"}), "b"))),
// "<p><test>a</test><test><img src=\"x\"></test><test>b</test></p>")()
// })
//
@Test
fun `parses unique non-exclusive same-typed marks`() {
val commentSchema = Schema(
SchemaSpec(
nodes = testSchema.spec.nodes,
marks = testSchema.spec.marks + mapOf(
"comment" to MarkSpecImpl(
attrs = mapOf("id" to AttributeSpecImpl(default = null)),
parseDOM = listOf(
ParseRuleImpl(
tag = "span.comment",
getNodeAttrs = { dom ->
val id = dom.attribute("data-id")?.int() ?: 10
ParseRuleMatch(mapOf("id" to id))
},
)
),
excludes = "",
toDOM = { mark, _ ->
DOMOutputSpec.ArrayDOMOutputSpec(
listOf(
"span",
mapOf("class" to "comment", "data-id" to mark.attrs["id"]),
0
)
)
}
)
)
)
)
val doc = commentSchema.nodes["doc"]!!.createAndFill(
attrs = null,
content = listOf(
commentSchema.nodes["paragraph"]!!.createAndFill(
attrs = null,
content = listOf(
commentSchema.text(
text = "double comment",
marks = listOf(
commentSchema.marks["comment"]!!.create(mapOf("id" to 1)),
commentSchema.marks["comment"]!!.create(mapOf("id" to 2))
)
)
),
marks = null
)!!
),
marks = null
)!!
test(
doc,
"<p><span class=\"comment\" data-id=\"1\"><span class=\"comment\" data-id=\"2\">double comment</span></span></p>"
)
}

@Test
fun `serializes non-spanning marks correctly`() {
val markSchema = Schema(
SchemaSpec(
nodes = testSchema.spec.nodes,
marks = testSchema.spec.marks + mapOf(
"test" to MarkSpecImpl(
parseDOM = listOf(ParseRuleImpl(tag = "test")),
toDOM = { _, _ -> DOMOutputSpec.ArrayDOMOutputSpec(listOf("test", 0)) },
spanning = false
)
)
)
)
val b = CustomNodeBuildCompanion(markSchema)

fun NodeBuilder<CommentNodeBuilder>.test(func: NodeBuilder<CommentNodeBuilder>.() -> Unit) =
mark("test", func)

test(
b.doc { p { test { +"a" + img(mapOf("src" to "x")) {} + "b" } } },
"<p><test>a</test><test><img src=\"x\"></test><test>b</test></p>"
)
}

// Skipping the following tests because we don't support them yet
// it("serializes an element and an attribute with XML namespace", () => {
// let xmlnsSchema = new Schema({
// nodes: {
Expand Down Expand Up @@ -430,12 +485,16 @@ class DomTest {
doc { p { em { +"Hello" } + "!" } }
)
}

@Test
fun `interprets font-weight bold as strong`() {
recover(
"<p style='font-weight: bold'>Hello</p>",
doc { p { strong { +"Hello" } } }
)
}
}

// it("interprets font-weight: bold as strong",
// recover("<p style='font-weight: bold'>Hello</p>",
// doc(p(strong("Hello")))))
//
// it("allows clearing of pending marks",
// recover("<blockquote style='font-style: italic'><p style='font-style: normal'>One</p><p>Two</p></blockquote",
// doc(blockquote(p("One"), p(em("Two"))))))
Expand Down Expand Up @@ -822,3 +881,11 @@ class DomTest {
// "<strong>foo<code>bar</code></strong><em><i data-emphasis=\"true\"><strong><code>baz</code></strong>quux</i></em>xyz")
// })
// })

fun com.fleeksoft.ksoup.nodes.Attribute?.int(default: Int? = null): Int? {
return try {
this?.value?.takeIf { it.isNotBlank() }?.toInt()
} catch (ex: NumberFormatException) {
default
}
}
Loading

0 comments on commit 653e3a8

Please sign in to comment.