
import Test.HUnit
import Text.ParserCombinators.Parsec

import Parse

genTestParse :: (Show a, Eq a) => Parser a -> String -> a -> Test
genTestParse parser input expected = TestCase $
    case parse parser "test" input of
        Left error -> assertFailure (show error)
        Right result -> assertEqual "parse" expected result

testParse = genTestParse parseWiki

main = runTestTT $ TestList [
    testParse "" [],
    testParse "foo" [Text "foo"],
    testParse "[[foo]]" [Link "foo" "foo"],
    testParse "[[bar|foo]]" [Link "foo" "bar"],
    testParse "[[bar|]]" [Link "" "bar"],
    testParse "[[foo" [Text "[[foo"],
    testParse "foo]]" [Text "foo]]"],
    testParse "foo [[bar]]" [Text "foo ", Link "bar" "bar"],
    testParse "{{·}}" [Template "·" []],
    testParse "{{a|b|c|d}}"
        [Template "a" [
            (Nothing, [Text "b"]),
            (Nothing, [Text "c"]),
            (Nothing, [Text "d"])]],
    testParse "{{a\n|b\n|c\n|d}}"
        [Template "a" [
            (Nothing, [Text "b\n"]),
            (Nothing, [Text "c\n"]),
            (Nothing, [Text "d"])]],
    testParse "{{a|foo = one|bar = two}}"
        [Template "a" [(Just "foo", [Text "one"]),
                       (Just "bar", [Text "two"])]],
    testParse "{{foo|[[bar|baz]]}}"
        [Template "foo" [(Nothing, [Link "baz" "bar"])]]
    ]

