summary refs log tree commit diff stats
path: root/testament/lib/stdtest
diff options
context:
space:
mode:
authorflywind <xzsflywind@gmail.com>2021-04-05 04:47:28 +0800
committerGitHub <noreply@github.com>2021-04-04 13:47:28 -0700
commit70a30317f7b0a5711e248e0653b50d1c057bd16b (patch)
tree80c3b1f52264cabe83c0ba28627a0b8d89f09ad5 /testament/lib/stdtest
parentf02e159b56aaa63713991c0a7f8e7125e91c832e (diff)
downloadNim-70a30317f7b0a5711e248e0653b50d1c057bd16b.tar.gz
fix #16693: testament spec nimout too lax (#16698)
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Diffstat (limited to 'testament/lib/stdtest')
-rw-r--r--testament/lib/stdtest/testutils.nim33
1 files changed, 23 insertions, 10 deletions
diff --git a/testament/lib/stdtest/testutils.nim b/testament/lib/stdtest/testutils.nim
index 36f951272..58d136696 100644
--- a/testament/lib/stdtest/testutils.nim
+++ b/testament/lib/stdtest/testutils.nim
@@ -1,5 +1,4 @@
 import std/private/miscdollars
-import std/strutils
 from std/os import getEnv
 
 template flakyAssert*(cond: untyped, msg = "", notifySuccess = true) =
@@ -26,15 +25,29 @@ template flakyAssert*(cond: untyped, msg = "", notifySuccess = true) =
     msg2.add $expr & " " & msg
     echo msg2
 
-proc greedyOrderedSubsetLines*(lhs, rhs: string): bool =
-  ## returns true if each stripped line in `lhs` appears in rhs, using a greedy matching.
-  let rhs = rhs.strip
-  var currentPos = 0
-  for line in lhs.strip.splitLines:
-    currentPos = rhs.find(line.strip, currentPos)
-    if currentPos < 0:
-      return false
-  return true
+when not defined(js):
+  import std/strutils
+
+  proc greedyOrderedSubsetLines*(lhs, rhs: string): bool =
+    ## Returns true if each stripped line in `lhs` appears in rhs, using a greedy matching.
+    iterator splitLinesClosure(): string {.closure.} =
+      for line in splitLines(rhs.strip):
+        yield line
+
+    var rhsIter = splitLinesClosure
+    var currentLine = strip(rhsIter())
+
+    for line in lhs.strip.splitLines:
+      let line = line.strip
+      if line.len != 0:
+        while line != currentLine:
+          currentLine = strip(rhsIter())
+          if rhsIter.finished:
+            return false
+
+      if rhsIter.finished:
+        return false
+    return true
 
 template enableRemoteNetworking*: bool =
   ## Allows contolling whether to run some test at a statement-level granularity.
n211' href='#n211'>211 212 213 214 215 216 217 218 219 220 221