From 362d8e2a9fb9ced3cca91098040c8acd39197d85 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Tue, 1 Jun 2021 09:39:12 -0700 Subject: [PATCH 1/3] Add fuzz tester to catch cases where parser fails. --- tests/ParserRecoveryTests.elm | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/ParserRecoveryTests.elm diff --git a/tests/ParserRecoveryTests.elm b/tests/ParserRecoveryTests.elm new file mode 100644 index 0000000..007ea46 --- /dev/null +++ b/tests/ParserRecoveryTests.elm @@ -0,0 +1,68 @@ +module ParserRecoveryTests exposing (suite) + +import Expect exposing (Expectation) +import Fuzz +import Markdown.Block as Block exposing (..) +import Markdown.Parser as Markdown exposing (..) +import Parser +import Parser.Advanced as Advanced exposing ((|.), (|=)) +import Test exposing (..) + + +type alias Parser a = + Advanced.Parser String Parser.Problem a + + +parse : String -> Result (List (Advanced.DeadEnd String Parser.Problem)) (List Block) +parse markdown = + markdown + |> Markdown.parse + + +suite : Test +suite = + describe "parser errors all have fallbacks so parsing always succeeds" + [ fuzz Fuzz.string "random strings are parseable" <| + \randomString -> + randomString + |> Markdown.parse + |> Expect.ok + ] + + +expectOk : List Block -> String -> Expectation +expectOk expected input = + case input |> parse of + Ok actual -> + actual + |> Expect.equal expected + + Err error -> + Expect.fail (Debug.toString error) + + +plainListItem : String -> Block.ListItem Block.Inline +plainListItem body = + Block.ListItem Block.NoTask [ Block.Text body ] + + +unstyledText : String -> List Inline +unstyledText body = + [ Block.Text body ] + + +emphasisText : String -> List Inline +emphasisText body = + [ Block.Emphasis <| + [ Block.Text body ] + ] + + +parserError : String -> Expect.Expectation +parserError markdown = + case parse markdown of + Ok _ -> + Expect.fail "Expected a parser failure!" + + Err _ -> + Expect.pass From fd67e4403f6fc34587dc2ca4119d6afd9f719f7b Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Tue, 1 Jun 2021 10:14:00 -0700 Subject: [PATCH 2/3] Add another failing test case. --- tests/ParserRecoveryTests.elm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/ParserRecoveryTests.elm b/tests/ParserRecoveryTests.elm index 007ea46..1814df3 100644 --- a/tests/ParserRecoveryTests.elm +++ b/tests/ParserRecoveryTests.elm @@ -3,6 +3,7 @@ module ParserRecoveryTests exposing (suite) import Expect exposing (Expectation) import Fuzz import Markdown.Block as Block exposing (..) +import Markdown.Inline import Markdown.Parser as Markdown exposing (..) import Parser import Parser.Advanced as Advanced exposing ((|.), (|=)) @@ -27,6 +28,18 @@ suite = randomString |> Markdown.parse |> Expect.ok + , test "recover from invalid html" <| + \() -> + " Markdown.parse + |> Expect.equal + (Ok + [ Block.Paragraph + [ Block.Text + " Date: Tue, 1 Jun 2021 10:20:46 -0700 Subject: [PATCH 3/3] Add a passing test case for <| and << fallbacks. --- tests/ParserRecoveryTests.elm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/ParserRecoveryTests.elm b/tests/ParserRecoveryTests.elm index 1814df3..765eadd 100644 --- a/tests/ParserRecoveryTests.elm +++ b/tests/ParserRecoveryTests.elm @@ -40,6 +40,18 @@ suite = ] ] ) + , test "<< is treated as regular text, not HTML" <| + \() -> + "In Elm, there are two ways to pipe: <| or <<" + |> Markdown.parse + |> Expect.equal + (Ok + [ Block.Paragraph + [ Block.Text + "In Elm, there are two ways to pipe: <| or <<" + ] + ] + ) ]