Skip to content

Commit

Permalink
sgf-parsing: Add individual tests of escaping/whitespace behaviour
Browse files Browse the repository at this point in the history
previous case "escaped property value" crams too much into one test
case.  Split it up into multiple ones.

The reimplemented case is mostly as it was when the exercise was
originally implemented in
exercism/exercism@7a5075b

Note that the original case also got it wrong in that newlines should
remain newlines; this is corrected in the new case.

exercism/problem-specifications#1889
  • Loading branch information
petertseng committed Nov 28, 2021
1 parent ee75dbc commit d02c5de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Sgf (

import Control.Applicative (many)
import Data.Attoparsec.Text (Parser, anyChar, char, many1, parseOnly, satisfy)
import Data.Char (isUpper, isSpace)
import Data.Char (isUpper)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Tree (Tree(..))
Expand Down Expand Up @@ -51,7 +51,7 @@ val = char '[' *> worker [] False
']' | not bs -> return . T.pack . reverse $ acc
'\\' | not bs -> worker acc True
'\n' | bs -> worker acc False -- remove soft newline
_ | isSpace c -> worker (' ' : acc) False
'\t' -> worker (' ' : acc) False
_ -> worker (c : acc) False

-- | Create an 'SgfTree' from a list of nodes and subtrees.
Expand Down
24 changes: 22 additions & 2 deletions exercises/practice/sgf-parsing/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,27 @@ specs = describe "parseSgf" $ for_ cases test
, Node [("C", ["D"])] [] ] )
-- multiple property values
, ("(;A[b][c][d])" , Just $ Node [("A", ["b", "c", "d" ])] [] )
-- escaped property value
, ("(;A[\\]b\nc\\\nd\t\te\\\\ \\\n\\]])", Just $ Node [("A", ["]b cd e\\ ]"])] [] ) ]
-- Within property values, whitespace characters such as tab are converted to spaces
, ("(;A[hello\t\tworld])" , Just $ Node [("A", ["hello world"])] [] )
-- Within property values, newlines remain as newlines
, ("(;A[hello\n\nworld])" , Just $ Node [("A", ["hello\n\nworld"])] [] )
-- Escaped closing bracket within property value becomes just a closing bracket
, ("(;A[\\]])" , Just $ Node [("A", ["]"])] [] )
-- Escaped backslash in property value becomes just a backslash
, ("(;A[\\\\])" , Just $ Node [("A", ["\\"])] [] )
-- Opening bracket within property value doesn't need to be escaped
, ("(;A[x[y\\]z][foo]B[bar];C[baz])" , Just $ Node [("A", ["x[y]z", "foo"]), ("B", ["bar"])] [ Node [("C", ["baz"])] [] ] )
-- Semicolon in property value doesn't need to be escaped
, ("(;A[a;b][foo]B[bar];C[baz])" , Just $ Node [("A", ["a;b", "foo"]), ("B", ["bar"])] [ Node [("C", ["baz"])] [] ] )
-- Parentheses in property value doesn't need to be escaped
, ("(;A[x(y)z][foo]B[bar];C[baz])" , Just $ Node [("A", ["x(y)z", "foo"]), ("B", ["bar"])] [ Node [("C", ["baz"])] [] ] )
-- Escaped tab in property value is converted to space
, ("(;A[hello\\\tworld])" , Just $ Node [("A", ["hello world"])] [] )
-- Escaped newline in property value is converted to nothing at all
, ("(;A[hello\\\nworld])" , Just $ Node [("A", ["helloworld"])] [] )
-- Escaped t and n in property value are just letters, not whitespace
, ("(;A[\\t = t and \\n = n])" , Just $ Node [("A", ["t = t and n = n"])] [] )
-- mixing various kinds of whitespace and escaped characters in property value
, ("(;A[\\]b\nc\\\nd\t\te\\\\ \\\n\\]])", Just $ Node [("A", ["]b\ncd e\\ ]"])] [] ) ]

-- b74debc3be24b5c81650189935c9bbfa019b367e

0 comments on commit d02c5de

Please sign in to comment.