(##import _test)
(##import pawn)

;; SUCCESS

(test-equal
 '((build
    (cmds
     "echo 'Hello'"))) 
 (get-tasks '((tasks
	      (build
	       (cmds
		"echo 'Hello'"))))))

(test-equal
 '((cmds
    "echo 'Hello'")) 
 (get-task '((build
	      (cmds
	       "echo 'Hello'"))
	     (foobar
	      (cmds
	       "yes")))
	   "build"))

(test-equal
 '("echo 'Hello'"
   "echo 'Foobar'") 
 (get-cmds '((cmds
	      "echo 'Hello'"
	      "echo 'Foobar'"))))

(test-equal
 48
 (fold-result-or-pawn-ex
  42
  (list
   (lambda (x) (+ x 2))
   (lambda (x) (+ x 4)))
  identity))

(setup-task '((dir "example")))
(test-equal "example" (path-strip-directory (path-strip-trailing-directory-separator (current-directory))))

;; ERROR

(test-error (pawn-ex->error (make-pawn-ex "foobar" '())))

(test-equal
 (make-pawn-ex
  "Missing tasks field in Pawnfile"
  '())
 (get-tasks '()))

(test-equal
 (make-pawn-ex
  "Missing tasks field in Pawnfile"
  '((foobar (build (cmds "foobar")))))
 (get-tasks
  '((foobar (build (cmds "foobar"))))))

(test-equal
 (make-pawn-ex
  "Empty tasks is not allowed"
  '(tasks))
 (get-tasks
  '((tasks))))

(test-equal
 (make-pawn-ex
  "Empty task build is not allowed"
  '(build))
 (get-task
  '((build)) "build"))

(test-equal
 (make-pawn-ex
  "Missing build command in tasks"
  "build")
 (get-task
  '((foobar (cmds "echo 'Foobar'")))
  "build"))

(test-equal
 (make-pawn-ex
  "Empty cmds is not allowed"
  '((cmds)))
 (get-cmds '((cmds))))

(test-equal
 (make-pawn-ex
  "Missing cmds field"
  '((foobar "foobar")))
 (get-cmds '((foobar "foobar"))))

(test-error
 (fold-result-or-pawn-ex
  (make-pawn-ex
   "oops"
   '())
  (list)
  pawn-ex->error))

(test-error
 (fold-result-or-pawn-ex
  42
  (list
   (lambda (x) (+ x 2))
   (lambda (x) (make-pawn-ex "oops" '())))
  pawn-ex->error))