From d1cff46ba774ed9bed2aa2eebb9d9e4a0b09217b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 3 Sep 2018 15:30:52 -0700 Subject: [PATCH] tests: port CsvReader tests to easytest --- hledger-lib/Hledger/Read.hs | 5 +- hledger-lib/Hledger/Read/CsvReader.hs | 81 +++++++-------------------- 2 files changed, 21 insertions(+), 65 deletions(-) diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index 3a28750f6..387adf95b 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -59,7 +59,7 @@ import Hledger.Read.JournalReader as JournalReader -- import qualified Hledger.Read.LedgerReader as LedgerReader import qualified Hledger.Read.TimedotReader as TimedotReader import qualified Hledger.Read.TimeclockReader as TimeclockReader -import qualified Hledger.Read.CsvReader as CsvReader +import Hledger.Read.CsvReader as CsvReader import Hledger.Utils import Prelude hiding (getContents, writeFile) @@ -345,8 +345,6 @@ samplejournal = readJournal' $ T.unlines tests_Hledger_Read = TestList $ tests_readJournal' ++ [ - CsvReader.tests_Hledger_Read_CsvReader, - "journal" ~: do r <- runExceptT $ parseWithState mempty JournalReader.journalp "" assertBool "journalp should parse an empty file" (isRight $ r) @@ -357,5 +355,6 @@ tests_Hledger_Read = TestList $ easytests_Read = tests "Read" [ easytests_Common + ,easytests_CsvReader ,easytests_JournalReader ] diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index 9f0f785ec..66d1581a7 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -25,7 +25,7 @@ module Hledger.Read.CsvReader ( expandIncludes, transactionFromCsvRecord, -- * Tests - tests_Hledger_Read_CsvReader + easytests_CsvReader, ) where import Prelude () @@ -832,67 +832,24 @@ parseDateWithFormatOrDefaultFormats mformat s = firstJust $ map parsewith format -------------------------------------------------------------------------------- -- tests -tests_Hledger_Read_CsvReader = TestList (test_parser) - -- ++ test_description_parsing) +easytests_CsvReader = tests "CsvReader" [ + tests "rulesp" [ + test "empty file" $ + expectEq' (Right rules) (parseCsvRules "unknown" "") + + ,test "trailing comments" $ + expectEq' (Right rules{rdirectives = [("skip","")]}) (parseWithState' rules rulesp "skip\n# \n#\n") + + ,test "trailing blank lines" $ + expectEq' (Right rules{rdirectives = [("skip","")]}) (parseWithState' rules rulesp "skip\n\n \n") + + ,test "no final newline" $ + expectEq' (Right rules{rdirectives=[("skip","")]}) (parseWithState' rules rulesp "skip") --- test_description_parsing = [ --- "description-field 1" ~: assertParseDescription "description-field 1\n" [FormatField False Nothing Nothing (FieldNo 1)] --- , "description-field 1 " ~: assertParseDescription "description-field 1 \n" [FormatField False Nothing Nothing (FieldNo 1)] --- , "description-field %(1)" ~: assertParseDescription "description-field %(1)\n" [FormatField False Nothing Nothing (FieldNo 1)] --- , "description-field %(1)/$(2)" ~: assertParseDescription "description-field %(1)/%(2)\n" [ --- FormatField False Nothing Nothing (FieldNo 1) --- , FormatLiteral "/" --- , FormatField False Nothing Nothing (FieldNo 2) --- ] --- ] --- where --- assertParseDescription string expected = do assertParseEqual (parseDescription string) (rules {descriptionField = expected}) --- parseDescription :: String -> Either ParseError CsvRules --- parseDescription x = runParser descriptionfieldWrapper rules "(unknown)" x --- descriptionfieldWrapper :: GenParser Char CsvRules CsvRules --- descriptionfieldWrapper = do --- descriptionfield --- r <- getState --- return r - -test_parser = [ - - "convert rules parsing: empty file" ~: do - -- let assertMixedAmountParse parseresult mixedamount = - -- (either (const "parse error") showMixedAmountDebug parseresult) ~?= (showMixedAmountDebug mixedamount) - assertParseEqual (parseCsvRules "unknown" "") rules - - -- ,"convert rules parsing: accountrule" ~: do - -- assertParseEqual (parseWithState rules accountrule "A\na\n") -- leading blank line required - -- ([("A",Nothing)], "a") - - ,"convert rules parsing: trailing comments" ~: do - assertParse (parseWithState' rules rulesp "skip\n# \n#\n") - - ,"convert rules parsing: trailing blank lines" ~: do - assertParse (parseWithState' rules rulesp "skip\n\n \n") - - ,"convert rules parsing: empty field value" ~: do - assertParse (parseWithState' rules rulesp "account1 \nif foo\n account2 foo\n") - - -- not supported - -- ,"convert rules parsing: no final newline" ~: do - -- assertParse (parseWithState rules csvrulesfile "A\na") - -- assertParse (parseWithState rules csvrulesfile "A\na\n# \n#") - -- assertParse (parseWithState rules csvrulesfile "A\na\n\n ") - - -- (rules{ - -- -- dateField=Maybe FieldPosition, - -- -- statusField=Maybe FieldPosition, - -- -- codeField=Maybe FieldPosition, - -- -- descriptionField=Maybe FieldPosition, - -- -- amountField=Maybe FieldPosition, - -- -- currencyField=Maybe FieldPosition, - -- -- baseCurrency=Maybe String, - -- -- baseAccount=AccountName, - -- accountRules=[ - -- ([("A",Nothing)], "a") - -- ] - -- }) + ,test "assignment with empty value" $ + expectEq' + (Right rules{rassignments = [("account1","")], rconditionalblocks = [([["foo"]],[("account2","foo")])]}) + (parseWithState' rules rulesp "account1 \nif foo\n account2 foo\n") + ] ]