test/testfiles/testfile.txt

#lang racket

#lang racket
(provide file-utils-tests)

(require rackunit
         rackunit/gui
         "../file-utils.rkt")

(define file-utils-tests
  (define testfolder (build-path "testfiles"))
  (define testfile (build-path "testfiles" "testfile.txt"))
  (test-suite 
   "File-Utils Library"
   (test-case
    "get-file-or-folder-name of folder"
    (check-equal? "testfiles" (get-file-or-folder-name (build-path "testfiles"))))
   (test-case
    "get-file-or-folder-name of file"
    (check-equal? "picture.tif" (get-file-or-folder-name (build-path "testfiles" "picture.tif"))))
   (test-case
    "get-file-or-folder-name taking string"
    (check-equal? "testfiles" (get-file-or-folder-name (path->string (build-path "testfiles")))))
   (test-case
    "name-as-string of folder"
    (check-equal? "testfiles" (name-as-string (build-path "testfiles"))))
   (test-case
    "name-as-string of file"
    (check-equal? "picture.tif" (name-as-string (build-path "testfiles" "picture.tif"))))
   (test-case
    "name-as-string taking string"
    (check-equal? "testfiles" (name-as-string (path->string (build-path "testfiles")))))
   (test-case
    "filename-main"
    (check-equal? "picture" (filename-main (build-path "testfiles" "picture.tif"))))
   (test-case 
    "filename-main without suffix"
    (check-equal? "testfiles" (filename-main (build-path "testfiles"))))
   (test-case
    "filename-main empty name"
    (check-equal? "" (filename-main (build-path "testfiles" ".mp3"))))
   (test-case
    "filename-main empty string"
    (check-equal? "" (filename-main "")))
   (test-case
    "filename-suffix"
    (check-equal? "tif" (filename-suffix (build-path "testfiles" "picture.tif"))))
   (test-case
    "filename-suffix string"
    (check-equal? "mp3" (filename-suffix "hello.mp3")))
   (test-case
    "filename-suffix empty suffix"
    (check-equal? "" (filename-suffix "hello")))
   (test-case
    "filename-suffix empty malformed suffix"
    (check-equal? "" (filename-suffix "hello.")))
   (test-case 
    "filename-suffix empty"
    (check-equal? "" (filename-suffix "")))
   (test-case 
    "filename-suffix multiple suffixes"
    (check-equal? "mp3" (filename-suffix (build-path "testfiles" "picture.tif.mp3"))))
   (test-case
    "compose-name"
    (check-equal? "test-23.txt" (compose-name "test" 23 "txt")))
   (test-case
    "compose-name no number"
    (check-equal? "test.txt" (compose-name "test" #f "txt")))
   (test-case
    "compose-name empty suffix"
    (check-equal? "test-23" (compose-name "test" 23 "")))
   (test-case
    "compose-name no number, empty suffix"
    (check-equal? "test" (compose-name "test" #f "")))
   (test-case
    "compose-name empty name"
    (check-equal? "-23.txt" (compose-name "" 23 "txt")))
   (test-case
    "compose-name everything empty"
    (check-equal? "" (compose-name "" #f "")))
   (test-case
    "make-unique-name"
    (check-false (file-exists? (make-unique-name "picture.tif"
                                                 (build-path "testfiles")))))
   (test-case
    "make-unique-name nonexistent file"
    (check-equal? (build-path "testfiles" "humpty.bla") (make-unique-name (build-path "testfiles" "humpty.bla")
                                                 (build-path "testfiles"))))
   (test-case
    "parent-directory"
    (check-equal? (parent-directory (build-path "testfiles" "picture.tif")) (path->complete-path (build-path "testfiles"))))
   (test-case
    "make-unique-path"
    (check-false (file-exists? (make-unique-path (build-path "testfiles" "picture.tif")))))
   (test-case
    "make-unique-path returns path"
    (check-true (path? (make-unique-path (build-path "testfiles" "picture.tif")))))
   (test-case
    "path-equal?"
    (check-true (path-equal? (build-path "testfiles" "picture.tif") (path->complete-path (build-path "testfiles" "pictures.tif")))))
   (test-case
    "path-equal? does not depend on file existence"
    (check-true (path-equal? (build-path "testfiles" "foobar.txt") (path->complete-path (build-path "testfiles" "foobar.txt")))))
   (test-case
    "file=?"
    (check-true (file=? (build-path "testfiles" "picture.tif") (build-path "testfiles" "picture.tif"))))
   (test-case
    "file=? requires existent file"
    (check-false (file=? (build-path "testfiles" "foobar.txt") (build-path "testfiles" "foobar.txt") )))
   (test-case
    "move-folder-to"
    (let ((from (build-path "test-received"))
          (to (build-path "testfiles"))
          (chk (build-path "testfiles" "test-received")))
      (move-folder-to from to)
      (check-true (directory-exists? chk))))
   (test-case
    "move-folder-to revert changes"
    (let ((from (build-path "testfiles" "test-received"))
          (to (current-directory))
          (chk (build-path "test-received")))
      (move-folder-to from to)
      (check-true (directory-exists? chk))))
   (test-case
    "copy-folders/renaming"
    (let ((from (current-directory))
          (to (build-path "testfiles"))
          (chk (build-path "testfiles" "test-received")))
      (copy-folders/renaming from to (lambda (p) (equal? (filename-main p) "test-received")))
      (let ((result (directory-exists? chk)))
        (delete-directory chk)
        (check-true result))))
   (test-case 
    "is-visible? visible file"
    (check-true (file-is-visible? (build-path "testfiles" "picture.tif"))))
   (test-case
    "is-visible? invisible file"
    (check-false (file-is-visible? (build-path "testfiles" ".info"))))
   (test-case
    "count-lines mixed"
    (let ((in (open-input-string "\n\rhello\n\rworld\n")))
      (check-equal? (count-lines in) 3)))
   (test-case
    "count-lines empty"
    (let ((in (open-input-string "")))
      (check-equal? (count-lines in) 0)))
   (test-case
    "count-lines LF"
    (let ((in (open-input-string "\n\n\n\n")))
      (check-equal? (count-lines in) 4)))
   (test-case
    "count-lines one line"
    (let ((in (open-input-string "\n\rhello")))
      (check-equal? (count-lines in) 2)))
   (test-case
    "count-lines one line"
    (let ((in (open-input-string "\n\r\n\n\nhello")))
      (check-equal? (count-lines in) 5)))
   (test-case
    "move-file-to"
    (let ((source (build-path "testfiles" "picture.tif"))
          (target (build-path "test-received" "picture.tif")))
                 (move-file-to source target)
      (check-true (file-exists? target))))
   (test-case
    "move-file-to source moved"
    (check-false (file-exists? (build-path "testfiles" "picture.tif"))))
   (test-case
    "move-file-to move back"
      (let ((source (build-path "test-received" "picture.tif"))
          (target (build-path "testfiles" "picture.tif")))
         (move-file-to source target)
         (check-true (file-exists? target))))
   (test-case
    "move-file-to original moved (2)"
    (check-false (file-exists? (build-path "test-received" "picture.tif"))))
   (test-case
    "file-equal? identical file"
    (check-true (file-equal? (build-path "testfiles" "picture.tif")(build-path "testfiles" "picture.tif"))))
   (test-case
    "file-equal? two copies"
    (let ((source (build-path "testfiles" "picture.tif"))
          (target (build-path "test-received" "picture.tif")))
      (copy-file source target)
      (check-true (file-equal? source target))
      (delete-file target)))
   (test-case 
    "file-equal? files do not exist"
    (check-exn exn:fail? (lambda () (file-equal? (build-path "testfiles" "nonexistent")
                                                 (build-path "testfiles" "nonexistent")))))
   (test-case 
    "file-equal? different files, same size"
     (let ((source (build-path "testfiles" "picture.tif"))
          (target (build-path "test-received" "picture.tif")))
      (copy-file source target)
       (let ((out (open-output-file target #:mode 'binary #:exists 'update)))
         (file-position out 10)
         (write-byte 72 out)
         (close-output-port out))
       (check-false (file-equal? source target))
       (delete-file target)))
   ))
        
(define (setup)
  (delete-directory/files "test-received")
  (make-directory "test-received"))

(define (test-all)
  (setup)
  (test/gui file-utils-tests))