Skip to content

Commit e61ac1c

Browse files
committed
sgf-parsing: Add individual tests of escaping/whitespace behaviour
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
1 parent ee75dbc commit e61ac1c

File tree

2 files changed

+24
-4
lines changed
  • exercises/practice/sgf-parsing

2 files changed

+24
-4
lines changed

exercises/practice/sgf-parsing/.meta/examples/success-standard/src/Sgf.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Sgf (
55

66
import Control.Applicative (many)
77
import Data.Attoparsec.Text (Parser, anyChar, char, many1, parseOnly, satisfy)
8-
import Data.Char (isUpper, isSpace)
8+
import Data.Char (isUpper)
99
import Data.Map (Map)
1010
import qualified Data.Map as Map
1111
import Data.Tree (Tree(..))
@@ -51,7 +51,7 @@ val = char '[' *> worker [] False
5151
']' | not bs -> return . T.pack . reverse $ acc
5252
'\\' | not bs -> worker acc True
5353
'\n' | bs -> worker acc False -- remove soft newline
54-
_ | isSpace c -> worker (' ' : acc) False
54+
'\t' -> worker (' ' : acc) False
5555
_ -> worker (c : acc) False
5656

5757
-- | Create an 'SgfTree' from a list of nodes and subtrees.

exercises/practice/sgf-parsing/test/Tests.hs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,27 @@ specs = describe "parseSgf" $ for_ cases test
4444
, Node [("C", ["D"])] [] ] )
4545
-- multiple property values
4646
, ("(;A[b][c][d])" , Just $ Node [("A", ["b", "c", "d" ])] [] )
47-
-- escaped property value
48-
, ("(;A[\\]b\nc\\\nd\t\te\\\\ \\\n\\]])", Just $ Node [("A", ["]b cd e\\ ]"])] [] ) ]
47+
-- Within property values, whitespace characters such as tab are converted to spaces
48+
, ("(;A[hello\t\tworld])" , Just $ Node [("A", ["hello world"])] [] )
49+
-- Within property values, newlines remain as newlines
50+
, ("(;A[hello\n\nworld])" , Just $ Node [("A", ["hello\n\nworld"])] [] )
51+
-- Escaped closing bracket within property value becomes just a closing bracket
52+
, ("(;A[\\]])" , Just $ Node [("A", ["]"])] [] )
53+
-- Escaped backslash in property value becomes just a backslash
54+
, ("(;A[\\\\])" , Just $ Node [("A", ["\\"])] [] )
55+
-- Opening bracket within property value doesn't need to be escaped
56+
, ("(;A[x[y\\]z]B[foo];C[bar])" , Just $ Node [("A", ["x[y]z"]), ("B", ["foo"])] [ Node [("C", ["bar"])] [] ] )
57+
-- Semicolon in property value doesn't need to be escaped
58+
, ("(;A[a;b]B[foo];C[bar])" , Just $ Node [("A", ["a;b"]), ("B", ["foo"])] [ Node [("C", ["bar"])] [] ] )
59+
-- Parentheses in property value doesn't need to be escaped
60+
, ("(;A[x(y)z]B[foo];C[bar])" , Just $ Node [("A", ["x(y)z"]), ("B", ["foo"])] [ Node [("C", ["bar"])] [] ] )
61+
-- Escaped tab in property value is converted to space
62+
, ("(;A[hello\\\tworld])" , Just $ Node [("A", ["hello world"])] [] )
63+
-- Escaped newline in property value is converted to nothing at all
64+
, ("(;A[hello\\\nworld])" , Just $ Node [("A", ["helloworld"])] [] )
65+
-- Escaped t and n in property value are just letters, not whitespace
66+
, ("(;A[\\t = t and \\n = n])" , Just $ Node [("A", ["t = t and n = n"])] [] )
67+
-- mixing various kinds of whitespace and escaped characters in property value
68+
, ("(;A[\\]b\nc\\\nd\t\te\\\\ \\\n\\]])", Just $ Node [("A", ["]b\ncd e\\ ]"])] [] ) ]
4969

5070
-- b74debc3be24b5c81650189935c9bbfa019b367e

0 commit comments

Comments
 (0)