nimsuggest tester supports editing of files
This commit is contained in:
parent
499ae7297b
commit
162f5ddb7d
2 changed files with 62 additions and 16 deletions
|
|
@ -7,7 +7,7 @@ import os, osproc, strutils, streams, re
|
||||||
|
|
||||||
type
|
type
|
||||||
Test = object
|
Test = object
|
||||||
cmd: string
|
cmd, dest: string
|
||||||
script: seq[(string, string)]
|
script: seq[(string, string)]
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
@ -17,17 +17,17 @@ const
|
||||||
proc parseTest(filename: string): Test =
|
proc parseTest(filename: string): Test =
|
||||||
const cursorMarker = "#[!]#"
|
const cursorMarker = "#[!]#"
|
||||||
let nimsug = curDir & addFileExt("nimsuggest", ExeExt)
|
let nimsug = curDir & addFileExt("nimsuggest", ExeExt)
|
||||||
let dest = getTempDir() / extractFilename(filename)
|
result.dest = getTempDir() / extractFilename(filename)
|
||||||
result.cmd = nimsug & " --tester " & dest
|
result.cmd = nimsug & " --tester " & result.dest
|
||||||
result.script = @[]
|
result.script = @[]
|
||||||
var tmp = open(dest, fmWrite)
|
var tmp = open(result.dest, fmWrite)
|
||||||
var specSection = 0
|
var specSection = 0
|
||||||
var markers = newSeq[string]()
|
var markers = newSeq[string]()
|
||||||
var i = 1
|
var i = 1
|
||||||
for x in lines(filename):
|
for x in lines(filename):
|
||||||
let marker = x.find(cursorMarker)+1
|
let marker = x.find(cursorMarker)+1
|
||||||
if marker > 0:
|
if marker > 0:
|
||||||
markers.add "\"" & filename & "\";\"" & dest & "\":" & $i & ":" & $marker
|
markers.add "\"" & filename & "\";\"" & result.dest & "\":" & $i & ":" & $marker
|
||||||
tmp.writeLine x.replace(cursorMarker, "")
|
tmp.writeLine x.replace(cursorMarker, "")
|
||||||
else:
|
else:
|
||||||
tmp.writeLine x
|
tmp.writeLine x
|
||||||
|
|
@ -36,6 +36,8 @@ proc parseTest(filename: string): Test =
|
||||||
elif specSection == 1:
|
elif specSection == 1:
|
||||||
if x.startsWith("$nimsuggest"):
|
if x.startsWith("$nimsuggest"):
|
||||||
result.cmd = x % ["nimsuggest", nimsug, "file", filename]
|
result.cmd = x % ["nimsuggest", nimsug, "file", filename]
|
||||||
|
elif x.startsWith("!edit"):
|
||||||
|
result.script.add((x, ""))
|
||||||
elif x.startsWith(">"):
|
elif x.startsWith(">"):
|
||||||
# since 'markers' here are not complete yet, we do the $substitutions
|
# since 'markers' here are not complete yet, we do the $substitutions
|
||||||
# afterwards
|
# afterwards
|
||||||
|
|
@ -50,6 +52,18 @@ proc parseTest(filename: string): Test =
|
||||||
for a in mitems(result.script):
|
for a in mitems(result.script):
|
||||||
a[0] = a[0] % markers
|
a[0] = a[0] % markers
|
||||||
|
|
||||||
|
proc edit(tmpfile, cmd: string) =
|
||||||
|
let x = cmd.splitWhitespace()
|
||||||
|
let f = if x.len >= 4: x[3] else: tmpfile
|
||||||
|
try:
|
||||||
|
let content = readFile(f)
|
||||||
|
let newcontent = content.replace(x[1], x[2])
|
||||||
|
if content == newcontent:
|
||||||
|
quit "wrong test case: edit had no effect"
|
||||||
|
writeFile(f, newcontent)
|
||||||
|
except IOError:
|
||||||
|
quit "cannot edit file " & tmpfile
|
||||||
|
|
||||||
proc smartCompare(pattern, x: string): bool =
|
proc smartCompare(pattern, x: string): bool =
|
||||||
if pattern.contains('*'):
|
if pattern.contains('*'):
|
||||||
result = match(x, re(escapeRe(pattern).replace("\\x2A","(.*)"), {}))
|
result = match(x, re(escapeRe(pattern).replace("\\x2A","(.*)"), {}))
|
||||||
|
|
@ -69,6 +83,9 @@ proc runTest(filename: string): int =
|
||||||
while outp.readLine(a):
|
while outp.readLine(a):
|
||||||
if a == DummyEof: break
|
if a == DummyEof: break
|
||||||
for req, resp in items(s.script):
|
for req, resp in items(s.script):
|
||||||
|
if req.startsWith("!edit"):
|
||||||
|
edit(s.dest, req)
|
||||||
|
else:
|
||||||
inp.writeLine(req)
|
inp.writeLine(req)
|
||||||
inp.flush()
|
inp.flush()
|
||||||
var answer = ""
|
var answer = ""
|
||||||
|
|
|
||||||
29
tools/nimsuggest/tests/tdot2.nim
Normal file
29
tools/nimsuggest/tests/tdot2.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Test that basic editing. We replace the 'false' by 'true' to
|
||||||
|
# see whether then the z field is suggested.
|
||||||
|
|
||||||
|
const zField = 0i32
|
||||||
|
|
||||||
|
type
|
||||||
|
Foo = object
|
||||||
|
x, y: int
|
||||||
|
when zField == 1i32:
|
||||||
|
z: string
|
||||||
|
|
||||||
|
proc main(f: Foo) =
|
||||||
|
f.#[!]#
|
||||||
|
|
||||||
|
# the tester supports the spec section at the bottom of the file and
|
||||||
|
# this way, the line numbers more often stay the same
|
||||||
|
discard """
|
||||||
|
$nimsuggest --tester $file
|
||||||
|
>sug $1
|
||||||
|
sug;;skField;;x;;int;;$file;;8;;4;;"";;100
|
||||||
|
sug;;skField;;y;;int;;$file;;8;;7;;"";;100
|
||||||
|
sug;;skProc;;tdot2.main;;proc (f: Foo);;$file;;12;;5;;"";;100
|
||||||
|
!edit 0i32 1i32
|
||||||
|
>sug $1
|
||||||
|
sug;;skField;;x;;int;;$file;;8;;4;;"";;100
|
||||||
|
sug;;skField;;y;;int;;$file;;8;;7;;"";;100
|
||||||
|
sug;;skField;;z;;string;;$file;;10;;6;;"";;100
|
||||||
|
sug;;skProc;;tdot2.main;;proc (f: Foo);;$file;;12;;5;;"";;100
|
||||||
|
"""
|
||||||
Loading…
Add table
Add a link
Reference in a new issue