namespace-tests.ss
(module namespace-tests mzscheme

  (require (planet "test.ss" ("schematics" "schemeunit.plt" 1 1)))
  (require "namespace.ss"
           "test.ss")

  (provide namespace-tests)

  (define namespace-tests
    (make-test-suite
     "All tests for namespace"
     (make-test-case
      "file-name->module-name with valid file names"
      (begin
        (assert-eq?
         (file-name->module-name "file.ss")
         'file)
        (assert-eq?
         (file-name->module-name "/foo/bar/file.ss")
         'file)
        (assert-eq?
         (file-name->module-name "/../bar/file.scm")
         'file)
        (assert-eq?
         (file-name->module-name "/bar/foo/file.ss.bak")
         'file)))
     (make-test-case
      "file-name->module-name with invalid file names"
      (begin
        (assert-exn
         exn:namespace?
         (lambda () (file-name->module-name "/foo/bar/")))
        (assert-exn
         exn:namespace?
         (lambda () (file-name->module-name "/foo/bar/.login")))
        (assert-exn
         exn:namespace?
         (lambda () (file-name->module-name "/foo/../bar/")))
        (assert-exn
         exn:namespace?
         (lambda () (file-name->module-name ".")))))

     (make-test-case
      "require-spec with valid specs"
      (map
       (lambda (require-spec expected)
         (assert string=?
                 (require-spec-file require-spec)
                 expected))
       ;; actual
       '("file.ss" "foo/bar/file.ss"
         (file "file.ss") (file "foo/bar/file.ss")
         (lib "file.ss" "schematics")
         (prefix foo: "file.ss")
         (prefix foo: (file "file.ss"))
         (prefix foo: (lib "file.ss" "schematics"))
         (all-except "file.ss" foo bar)
         (all-except (file "file.ss") foo)
         (all-except (lib "file.ss" "schematics") foo bar baz)
         (prefix-all-except foo: "file.ss" foo bar)
         (prefix-all-except foo: (file "file.ss") foo)
         (prefix-all-except foo: (lib "file.ss" "schematics") foo bar baz)
         (rename "file.ss" foo bar)
         (rename (file "file.ss") foo bar)
         (rename (lib "file.ss" "schematics") foo bar))
       ;; expected
       '("file.ss" "foo/bar/file.ss"
         "file.ss" "foo/bar/file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss"
         "file.ss")))

     (make-test-case
      "require-spec-file with invalid specs"
      ;; should have more tests
      (map
       (lambda (require-spec)
         (assert-exn exn:namespace?
                     (lambda ()
                       (require-spec-file require-spec))))
       '(rubbish.ss)))

     (make-test-case
      "namespace-attach/require shares module"
      (let ((namespace (make-namespace 'initial)))
        (assert-exn
         exn?
         (lambda ()
           (with-namespace
            namespace
            (eval '(set-foo-bar 2))))
         "Module not initially imported")
        (namespace-attach/require
         namespace
         '(file "test.ss"))
        (set-foo-bar 4)
        (assert = 4 (get-foo-bar))
        (with-namespace
         namespace
         (assert = 4 (eval '(get-foo-bar)))
         (eval '(set-foo-bar 2))
         (assert = 2 (eval '(get-foo-bar))))
        (assert = 2 (get-foo-bar))))

     (make-test-case
      "namespace-require doesn't share module"
      (let ((namespace (make-namespace 'initial)))
        (set-foo-bar 4)
        (assert = 4 (get-foo-bar))
        (with-namespace
         namespace
         (namespace-require '(file "test.ss"))
         (assert-false (eval '(get-foo-bar)))
         (eval '(set-foo-bar 2))
         (assert = 2 (eval '(get-foo-bar))))
        (assert = 4 (get-foo-bar))))

     ))
  )