Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuzz tester to catch cases where parser fails #83

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions tests/ParserRecoveryTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 ((|.), (|=))
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
, test "recover from invalid html" <|
\() ->
"<no-closing-tag maybe this can just be rendered as a regular paragraph and not as HTML"
|> Markdown.parse
|> Expect.equal
(Ok
[ Block.Paragraph
[ Block.Text
"<no-closing-tag maybe this can just be rendered as a regular paragraph and not as HTML"
]
]
)
, 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 <<"
]
]
)
]


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