From 686caaaf770f59a066cefe3e9ec9cc0f16b346a7 Mon Sep 17 00:00:00 2001 From: Roman Vishnevskii Date: Sat, 8 Apr 2017 15:17:19 +0700 Subject: [PATCH] DOM diff problem: tests write verdict to console --- diffDomTests.nim | 59 +++++++++++++++++++++++++++++++++--------------- karax.nim | 11 ++++----- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/diffDomTests.nim b/diffDomTests.nim index e3014f8..2fa9ccf 100644 --- a/diffDomTests.nim +++ b/diffDomTests.nim @@ -11,18 +11,23 @@ proc reset() = redraw() kout cstring"reset finished" -proc checkOrder(order : seq[int]) = +proc checkOrder(order : seq[int]): bool = var ul = getElementById("ul") - assert ul != nil - assert len(ul.children) == len(order) + if ul == nil or len(ul.children) != len(order): + return false var pos = 0 for child in ul.children: - assert child.id == $order[pos] + if child.id != $order[pos]: + return false inc pos + return true proc check1() = - checkOrder(@[0, 1, 2, 3, 4, 7, 5]) - kout cstring"test1 finished" + let result = checkOrder(@[0, 1, 2, 3, 4, 7, 5]) + if result: + kout cstring"test1 - OK" + else: + kout cstring"test1 - FAIL" # result: 0 1 2 3 4 7 5 proc test1() = @@ -33,8 +38,11 @@ proc test1() = timeout = setTimeout(check1, 20) proc check2() = - checkOrder(@[8, 0, 1, 2, 3, 4, 7, 5]) - kout cstring"test2 finished" + let result = checkOrder(@[8, 0, 1, 2, 3, 4, 7, 5]) + if result: + kout cstring"test1 - OK" + else: + kout cstring"test2 - FAIL" # result: 8 0 1 2 3 4 7 5 proc test2() = @@ -46,8 +54,11 @@ proc test2() = timeout = setTimeout(check2, 20) proc check3() = - checkOrder(@[2, 3, 4, 1]) - kout cstring"test3 finished" + let result = checkOrder(@[2, 3, 4, 1]) + if result: + kout cstring"test3 - OK" + else: + kout cstring"test3 - FAIL" # result: 2 3 4 1 proc test3() = @@ -57,8 +68,11 @@ proc test3() = timeout = setTimeout(check3, 20) proc check4() = - checkOrder(@[5, 6, 7, 8]) - kout cstring"test4 finished" + let result = checkOrder(@[5, 6, 7, 8]) + if result: + kout cstring"test4 - OK" + else: + kout cstring"test4 - FAIL" # result: 5 6 7 8 proc test4() = @@ -68,8 +82,11 @@ proc test4() = timeout = setTimeout(check4, 20) proc check5() = - checkOrder(@[0, 1, 3, 5, 4, 5]) - kout cstring"test5 finished" + let result = checkOrder(@[0, 1, 3, 5, 4, 5]) + if result: + kout cstring"test5 - OK" + else: + kout cstring"test5 - FAIL" # result: 0 1 3 5 4 5 proc test5() = @@ -79,8 +96,11 @@ proc test5() = timeout = setTimeout(check5, 20) proc check6() = - checkOrder(@[]) - kout cstring"test6 finished" + let result = checkOrder(@[]) + if result: + kout cstring"test6 - OK" + else: + kout cstring"test6 - FAIL" # result: empty proc test6() = @@ -90,8 +110,11 @@ proc test6() = timeout = setTimeout(check6, 20) proc check7() = - checkOrder(@[2]) - kout cstring"test7 finished" + let result = checkOrder(@[2]) + if result: + kout cstring"test7 - OK" + else: + kout cstring"test7 - FAIL" # result: 2 proc test7() = diff --git a/karax.nim b/karax.nim index 16c453c..d4e60d6 100644 --- a/karax.nim +++ b/karax.nim @@ -132,7 +132,7 @@ proc updateElement(parent, current: Node, newNode, oldNode: VNode) = replaceById("ROOT", n) else: parent.replaceChild(n, current) - kout cstring("---- replaceChild") + #kout cstring("---- replaceChild") #kout cstring("ReplaceChild: (" & current.nodeName & " = " & newNode.id & ") -> (" & # n.nodeName & " = " & oldNode.id & ")") elif newNode.kind != VNodeKind.text: @@ -156,9 +156,6 @@ proc updateElement(parent, current: Node, newNode, oldNode: VNode) = dec oldPos dec newPos - if commonPrefix + newLength - newPos > 1000: - kout cstring($(commonPrefix + newLength - newPos)) - var pos = min(oldPos, newPos) + 1 # kout cstring("commonPrefix = " & $commonPrefix) @@ -186,7 +183,7 @@ proc updateElement(parent, current: Node, newNode, oldNode: VNode) = while pos <= newPos: if nextChildPos == oldLength: current.appendChild(vnodeToDom(newNode[pos])) - kout cstring"---- appendChild" + #kout cstring"---- appendChild" # kout cstring("commonPrefix = " & $commonPrefix) # kout cstring("oldPos = " & $oldPos) # kout cstring("newPos = " & $newPos) @@ -199,14 +196,14 @@ proc updateElement(parent, current: Node, newNode, oldNode: VNode) = #kout cstring("pos = " & $pos) #kout cstring("newLength = " & $newLength) else: - kout cstring"---- insertBefore" + #kout cstring"---- insertBefore" current.insertBefore(vnodeToDom(newNode[pos]), current.childNodes[nextChildPos]) inc oldLength inc pos inc nextChildPos for i in 0..oldPos-pos: - kout cstring"---- removeChild" + #kout cstring"---- removeChild" current.removeChild(current.childNodes[pos]) # var itNew = newPos + 1