;; partial.hs — exercises Phase 9 partial functions caught at the top level.
;;
;; Each program calls a partial function on bad input; hk-run-io catches the
;; raise and appends the error message to io-lines so tests can inspect.

(hk-test
  "partial.hs — main = print (head [])"
  (let
    ((lines (hk-run-io "main = print (head [])")))
    (>= (index-of (str lines) "Prelude.head: empty list") 0))
  true)

(hk-test
  "partial.hs — main = print (tail [])"
  (let
    ((lines (hk-run-io "main = print (tail [])")))
    (>= (index-of (str lines) "Prelude.tail: empty list") 0))
  true)

(hk-test
  "partial.hs — main = print (fromJust Nothing)"
  (let
    ((lines (hk-run-io "main = print (fromJust Nothing)")))
    (>= (index-of (str lines) "Maybe.fromJust: Nothing") 0))
  true)

(hk-test
  "partial.hs — putStrLn before error preserves prior output"
  (let
    ((lines (hk-run-io "main = do { putStrLn \"step 1\"; putStrLn (show (head [])); putStrLn \"never\" }")))
    (and
      (>= (index-of (str lines) "step 1") 0)
      (>= (index-of (str lines) "Prelude.head: empty list") 0)
      (= (index-of (str lines) "never") -1)))
  true)

(hk-test
  "partial.hs — undefined as IO action"
  (let
    ((lines (hk-run-io "main = print undefined")))
    (>= (index-of (str lines) "Prelude.undefined") 0))
  true)

(hk-test
  "partial.hs — catches error from a user-thrown error"
  (let
    ((lines (hk-run-io "main = error \"boom from main\"")))
    (>= (index-of (str lines) "boom from main") 0))
  true)

;; Negative case: when no error is raised, io-lines doesn't contain
;; "Prelude" prefixes from our error path.
(hk-test
  "partial.hs — happy path: head [42] succeeds, no error in output"
  (hk-run-io "main = print (head [42])")
  (list "42"))

{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
