tester.nim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Small program that runs the test cases
  2. import strutils, os, sequtils
  3. const
  4. dir = "nimpretty/tests/"
  5. outputdir = dir / "outputdir"
  6. var
  7. failures = 0
  8. when defined(develop):
  9. const nimp = "bin" / "nimpretty".addFileExt(ExeExt)
  10. if execShellCmd("nim c -o:$# nimpretty/nimpretty.nim" % [nimp]) != 0:
  11. quit("FAILURE: compilation of nimpretty failed")
  12. else:
  13. const nimp = "nimpretty"
  14. proc test(infile, ext: string) =
  15. if execShellCmd("$# -o:$# --backup:off $#" % [nimp, infile.changeFileExt(ext), infile]) != 0:
  16. echo "FAILURE: nimpretty cannot prettify ", infile
  17. failures += 1
  18. return
  19. let nimFile = splitFile(infile).name
  20. let expected = dir / "expected" / nimFile & ".nim"
  21. let produced = dir / nimFile.changeFileExt(ext)
  22. if readFile(expected) != readFile(produced):
  23. echo "FAILURE: files differ: ", nimFile
  24. discard execShellCmd("diff -uNdr " & expected & " " & produced)
  25. failures += 1
  26. else:
  27. echo "SUCCESS: files identical: ", nimFile
  28. proc testTogether(infiles: seq[string]) =
  29. if execShellCmd("$# --outDir:$# --backup:off $#" % [nimp, outputdir, infiles.join(" ")]) != 0:
  30. echo "FAILURE: nimpretty cannot prettify files: ", $infiles
  31. failures += 1
  32. return
  33. for infile in infiles:
  34. let nimFile = splitFile(infile).name
  35. let expected = dir / "expected" / nimFile & ".nim"
  36. let produced = dir / "outputdir" / infile
  37. if readFile(expected) != readFile(produced):
  38. echo "FAILURE: files differ: ", nimFile
  39. discard execShellCmd("diff -uNdr " & expected & " " & produced)
  40. failures += 1
  41. else:
  42. echo "SUCCESS: files identical: ", nimFile
  43. let allFiles = toSeq(walkFiles(dir / "*.nim"))
  44. for t in allFiles:
  45. test(t, "pretty")
  46. # also test that pretty(pretty(x)) == pretty(x)
  47. test(t.changeFileExt("pretty"), "pretty2")
  48. removeFile(t.changeFileExt("pretty"))
  49. removeFile(t.changeFileExt("pretty2"))
  50. testTogether(allFiles)
  51. removeDir(outputdir)
  52. if failures > 0: quit($failures & " failures occurred.")