Skip to content

Commit

Permalink
Squash NPE - throw parse exception on empty input: fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-hull committed Jun 2, 2016
1 parent d7ebb33 commit 2df7802
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/jasentaa/position.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
(apply str (map :char augmented-text)))

(defn parse-exception [location]
(java.text.ParseException.
(str "Failed to parse text at line: " (:line location) ", col: " (:col location))
(int (:offset location))))
(if (nil? location)
(java.text.ParseException. (str "Unable to parse empty text") 0)
(java.text.ParseException.
(str "Failed to parse text at line: " (:line location) ", col: " (:col location))
(int (:offset location)))))
7 changes: 5 additions & 2 deletions test/jasentaa/position_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@

(deftest check-exception
(is (thrown-with-msg? java.text.ParseException
#"Failed to parse text at line: 6, col: 31"
(throw (parse-exception (Location. \Y 6 31 321))))))
#"Unable to parse empty text"
(throw (parse-exception nil))))
(is (thrown-with-msg? java.text.ParseException
#"Failed to parse text at line: 6, col: 31"
(throw (parse-exception (Location. \Y 6 31 321))))))
7 changes: 6 additions & 1 deletion test/jasentaa/worked_example_1.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@
(is (thrown-with-msg?
java.text.ParseException
#"Failed to parse text at line: 1, col: 7"
(parse-all search-expr "steel iron"))))
(parse-all search-expr "steel iron")))

(is (thrown-with-msg?
java.text.ParseException
#"Unable to parse empty text"
(parse-all search-expr ""))))

0 comments on commit 2df7802

Please sign in to comment.