Skip to content

Commit ae4c9aa

Browse files
reformat SgfParsingTest
1 parent fe574a3 commit ae4c9aa

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

exercises/practice/sgf-parsing/SgfParsingTest.lean

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ def sgfParsingTests : TestSuite :=
3535
|>.addTest "upper and lowercase property" (do
3636
return assertEqual (.error "property must be in uppercase") (SgfParsing.parse "(;Aa[b])"))
3737
|>.addTest "two nodes" (do
38-
return assertEqual (.ok ⟨.ofArray #[("A", #["B"])], #[⟨.ofArray #[("B", #["C"])], #[]⟩]⟩) (SgfParsing.parse "(;A[B];B[C])"))
38+
return assertEqual (.ok ⟨.ofArray #[("A", #["B"])], #[
39+
⟨.ofArray #[("B", #["C"])], #[]⟩
40+
]⟩) (SgfParsing.parse "(;A[B];B[C])"))
3941
|>.addTest "two child trees" (do
40-
return assertEqual (.ok ⟨.ofArray #[("A", #["B"])], #[⟨.ofArray #[("B", #["C"])], #[]⟩, ⟨.ofArray #[("C", #["D"])], #[]⟩]⟩) (SgfParsing.parse "(;A[B](;B[C])(;C[D]))"))
42+
return assertEqual (.ok ⟨.ofArray #[("A", #["B"])], #[
43+
⟨.ofArray #[("B", #["C"])], #[]⟩,
44+
⟨.ofArray #[("C", #["D"])], #[]⟩
45+
]⟩) (SgfParsing.parse "(;A[B](;B[C])(;C[D]))"))
4146
|>.addTest "multiple property values" (do
4247
return assertEqual (.ok ⟨.ofArray #[("A", #["b", "c", "d"])], #[]⟩) (SgfParsing.parse "(;A[b][c][d])"))
4348
|>.addTest "within property values, whitespace characters such as tab are converted to spaces" (do
@@ -49,11 +54,17 @@ def sgfParsingTests : TestSuite :=
4954
|>.addTest "escaped backslash in property value becomes just a backslash" (do
5055
return assertEqual (.ok ⟨.ofArray #[("A", #["\\"])], #[]⟩) (SgfParsing.parse "(;A[\\\\])"))
5156
|>.addTest "opening bracket within property value doesn't need to be escaped" (do
52-
return assertEqual (.ok ⟨.ofArray #[("A", #["x[y]z", "foo"]), ("B", #["bar"])], #[⟨.ofArray #[("C", #["baz"])], #[]⟩]⟩) (SgfParsing.parse "(;A[x[y\\]z][foo]B[bar];C[baz])"))
57+
return assertEqual (.ok ⟨.ofArray #[("A", #["x[y]z", "foo"]), ("B", #["bar"])], #[
58+
⟨.ofArray #[("C", #["baz"])], #[]⟩
59+
]⟩) (SgfParsing.parse "(;A[x[y\\]z][foo]B[bar];C[baz])"))
5360
|>.addTest "semicolon in property value doesn't need to be escaped" (do
54-
return assertEqual (.ok ⟨.ofArray #[("A", #["a;b", "foo"]), ("B", #["bar"])], #[⟨.ofArray #[("C", #["baz"])], #[]⟩]⟩) (SgfParsing.parse "(;A[a;b][foo]B[bar];C[baz])"))
61+
return assertEqual (.ok ⟨.ofArray #[("A", #["a;b", "foo"]), ("B", #["bar"])], #[
62+
⟨.ofArray #[("C", #["baz"])], #[]⟩
63+
]⟩) (SgfParsing.parse "(;A[a;b][foo]B[bar];C[baz])"))
5564
|>.addTest "parentheses in property value don't need to be escaped" (do
56-
return assertEqual (.ok ⟨.ofArray #[("A", #["x(y)z", "foo"]), ("B", #["bar"])], #[⟨.ofArray #[("C", #["baz"])], #[]⟩]⟩) (SgfParsing.parse "(;A[x(y)z][foo]B[bar];C[baz])"))
65+
return assertEqual (.ok ⟨.ofArray #[("A", #["x(y)z", "foo"]), ("B", #["bar"])], #[
66+
⟨.ofArray #[("C", #["baz"])], #[]⟩
67+
]⟩) (SgfParsing.parse "(;A[x(y)z][foo]B[bar];C[baz])"))
5768
|>.addTest "escaped tab in property value is converted to space" (do
5869
return assertEqual (.ok ⟨.ofArray #[("A", #["hello world"])], #[]⟩) (SgfParsing.parse "(;A[hello\\\u0009world])"))
5970
|>.addTest "escaped newline in property value is converted to nothing at all" (do
@@ -63,7 +74,14 @@ def sgfParsingTests : TestSuite :=
6374
|>.addTest "mixing various kinds of whitespace and escaped characters in property value" (do
6475
return assertEqual (.ok ⟨.ofArray #[("A", #["]b\ncd e\\ ]"])], #[]⟩) (SgfParsing.parse "(;A[\\]b\nc\\\nd\u0009\u0009e\\\\ \\\n\\]])"))
6576
|>.addTest "complex child trees" (do
66-
return assertEqual (.ok ⟨.ofArray #[("FF", #["4"])], #[⟨.ofArray #[("B", #["aa"])], #[⟨.ofArray #[("W", #["ab"])], #[]⟩]⟩, ⟨.ofArray #[("B", #["dd"])], #[⟨.ofArray #[("W", #["ee"])], #[]⟩]⟩]⟩) (SgfParsing.parse "(;FF[4](;B[aa];W[ab])(;B[dd];W[ee]))"))
77+
return assertEqual (.ok ⟨.ofArray #[("FF", #["4"])], #[
78+
⟨.ofArray #[("B", #["aa"])], #[
79+
⟨.ofArray #[("W", #["ab"])], #[]⟩
80+
]⟩,
81+
⟨.ofArray #[("B", #["dd"])], #[
82+
⟨.ofArray #[("W", #["ee"])], #[]⟩
83+
]⟩
84+
]⟩) (SgfParsing.parse "(;FF[4](;B[aa];W[ab])(;B[dd];W[ee]))"))
6785

6886
def main : IO UInt32 := do
6987
runTestSuitesWithExitCode [sgfParsingTests]

generators/Generator/Generator/SgfParsingGenerator.lean

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ def extractProperties (json : Json) : Array (String × Array String) :=
3030
|> Std.TreeMap.Raw.toArray
3131
|> Array.map (λ (k, v) => (s!"\"{k}\"", v))
3232

33-
partial def getResult (json : Json) : String :=
33+
partial def getResult (json : Json) (separator : String) : String :=
3434
let propertiesMap := extractProperties json
3535
let properties := if propertiesMap.isEmpty then "{}"
3636
else s!"{propertiesMap}" |> (".ofArray " ++ ·)
3737
let children := getOk (json.getObjValD "children").getArr?
3838
if children.isEmpty then
3939
s!"⟨{properties}, #[]⟩"
4040
else
41-
let right := String.intercalate ", " (children.toList.map (getResult ·))
42-
s!"⟨{properties}, #[{right}]⟩"
41+
let right := String.intercalate ("," ++ separator ++ " ") (children.toList.map (getResult · (separator ++ " ")))
42+
s!"⟨{properties}, #[{separator} {right}{separator}]⟩"
4343

4444
def genTestCase (exercise : String) (case : TreeMap.Raw String Json) : String :=
4545
let input := case.get! "input"
@@ -48,9 +48,10 @@ def genTestCase (exercise : String) (case : TreeMap.Raw String Json) : String :=
4848
|> (·.compress)
4949
let funName := getFunName (case.get! "property")
5050
let call := s!"({exercise}.{funName} {insertAllInputs input})"
51+
let separator := "\n "
5152
let result := match expected.getObjVal? "error" with
5253
| .ok error => s!"(.error {error})"
53-
| .error _ => s!"(.ok {getResult expected})"
54+
| .error _ => s!"(.ok {getResult expected separator})"
5455
s!"
5556
|>.addTest {description} (do
5657
return assertEqual {result} {call})"

0 commit comments

Comments
 (0)