summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2014-04-20 23:40:29 +0200
committerAndreas Rumpf <rumpf_a@web.de>2014-04-20 23:40:29 +0200
commit23621857bb2ced3eb473bf3131169e7215f39b95 (patch)
tree566fa700d8eed6615ee11f072d9f3fb6eacfa2e1 /tests
parent726c709f975f72de71ed974a2dce7c3a062e156b (diff)
parent9ce0ac38e5816910730843f4ef0c565e3c839ad0 (diff)
downloadNim-23621857bb2ced3eb473bf3131169e7215f39b95.tar.gz
Merge pull request #1099 from Varriount/os/getFileInfo
Added os.getFileInfo procedure.
Diffstat (limited to 'tests')
-rw-r--r--tests/stdlib/tgetfileinfo.nim93
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/stdlib/tgetfileinfo.nim b/tests/stdlib/tgetfileinfo.nim
new file mode 100644
index 000000000..49a019061
--- /dev/null
+++ b/tests/stdlib/tgetfileinfo.nim
@@ -0,0 +1,93 @@
+discard """
+  output: ""
+"""
+
+import os, strutils
+# Cases
+#  1 - String : Existing File : Symlink true
+#  2 - String : Existing File : Symlink false
+#  3 - String : Non-existing File : Symlink true
+#  4 - String : Non-existing File : Symlink false
+#  5 - Handle : Valid File
+#  6 - Handle : Invalid File
+#  7 - Handle : Valid Handle
+#  8 - Handle : Invalid Handle
+
+proc genBadFileName(limit = 100): string =
+    ## Generates a filename of a nonexistant file.
+    ## Returns "" if generation fails.
+    result = "a"
+    var hitLimit = true
+
+    for i in 0..100:
+      if existsFile(result):
+        result.add("a")
+      else:
+        hitLimit = false
+        break
+    if hitLimit:
+      result = ""
+
+proc caseOneAndTwo(followLink: bool) =
+  try:
+    discard getFileInfo(getAppFilename(), followLink)
+    #echo("String : Existing File : Symlink $# : Success" % $followLink)
+  except EOS:
+    echo("String : Existing File : Symlink $# : Failure" % $followLink)
+
+proc caseThreeAndFour(followLink: bool) =
+  var invalidName = genBadFileName()
+  try:
+    discard getFileInfo(invalidName, true)
+    echo("String : Non-existing File : Symlink $# : Failure" % $followLink)
+  except EOS:
+    #echo("String : Non-existing File : Symlink $# : Success" % $followLink)
+
+proc testGetFileInfo =
+  # Case 1
+  caseOneAndTwo(true)
+
+  # Case 2
+  caseOneAndTwo(false)
+
+  # Case 3
+  caseThreeAndFour(true)
+
+  # Case 4
+  caseThreeAndFour(false)
+
+  # Case 5 and 7
+  block:
+    let
+      testFile = open(getAppFilename())
+      testHandle = fileHandle(testFile)
+    try:
+      discard getFileInfo(testFile)
+      #echo("Handle : Valid File : Success")
+    except EIO:
+      echo("Handle : Valid File : Failure")
+
+    try:
+      discard getFileInfo(testHandle)
+      #echo("Handle : Valid File : Success")
+    except EIO:
+      echo("Handle : Valid File : Failure")
+
+  # Case 6 and 8
+  block:
+    let
+      testFile: TFile = nil
+      testHandle = TFileHandle(-1)
+    try:
+      discard getFileInfo(testFile)
+      echo("Handle : Invalid File : Failure")
+    except EIO, EOS:
+      #echo("Handle : Invalid File : Success")
+
+    try:
+      discard getFileInfo(testHandle)
+      echo("Handle : Invalid File : Failure")
+    except EIO, EOS:
+      #echo("Handle : Invalid File : Success")
+
+testGetFileInfo()
\ No newline at end of file