-------------------------------------------------------------------------------- -- $Id: ParseCSVTest.hs,v 1.2 2004/04/20 14:50:47 graham Exp $ -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- See end of this file for licence information. -------------------------------------------------------------------------------- -- | -- Module : ParseCSVTest -- Copyright : (c) 2003, Graham Klyne -- License : GPL V2 -- -- Maintainer : Graham Klyne -- Stability : provisional -- Portability : H98 -- -- This module contains test cases for parsing CSV files. -- -------------------------------------------------------------------------------- module Main where import ParseCSV ( parseCSV ) import ListHelpers ( equiv ) import TestHelpers ( test, testEq, testNe, testLe, testGe, testElem , testJust, testNothing , testEqv, testNotEqv, testEqv2, testHasEqv, testMaybeEqv ) import HUnit ( Test(TestCase,TestList,TestLabel) , Assertion , assertBool, assertEqual, assertString, assertFailure , runTestTT, runTestText, putTextToHandle ) import IO ( Handle, IOMode(WriteMode) , openFile, hClose, hPutStr, hPutStrLn ) import Monad ( unless ) import List ( sort, union, intersect ) import Maybe ( isJust, fromJust, fromMaybe ) ------------------------------------------------------------ -- CSV parsing test ------------------------------------------------------------ -- Following MS Excel, quotes are used around strings containing commas, -- Quotes within quoted strings are doubled csvString :: String csvString = concatMap (++"\n") csvList csvList :: [String] csvList = [ "a,b,c,d" , "a a,b b, \"c,c c\", d d d d d , " , "a a,b b, 'c,c c', d d d d d ," , ",b,,d" , "a,,c" , ",,," , "" , "a, \" b \", \" \", " , "a, \" \"\"b\"\" \", ' ''c'' ', " , "a,b,'c'" , "a,b,'d' " ] csvParsed :: [[String]] csvParsed = [ ["a", "b", "c", "d"] , ["a a","b b","c,c c","d d d d d",""] , ["a a","b b","c,c c","d d d d d"] , ["", "b", "", "d"] , ["a", "", "c"] , ["", "", ""] , [] , ["a", " b ", " ", ""] , ["a", " \"b\" ", " 'c' ", ""] , ["a", "b", "c"] , ["a", "b", "d"] ] csvTests = zip [1::Int ..] (zip csvList csvParsed) csvTestCase :: (Int,(String,[String])) -> Test csvTestCase (n,(s,vs)) = testEq ("testParseCSV"++show n) vals (parseCSV s) where vals = if null vs then [] else [vs] csvTestCases = map csvTestCase csvTests testParseCSVSuite = TestList $ csvTestCases ++ [ testEq "testParseCSV99" csvParsed (parseCSV csvString) ] ------------------------------------------------------------ -- All tests ------------------------------------------------------------ allTests = TestList [ testParseCSVSuite ] main = runTestTT allTests runTestFile t = do h <- openFile "a.tmp" WriteMode runTestText (putTextToHandle h False) t hClose h tf = runTestFile tt = runTestTT -------------------------------------------------------------------------------- -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- -- This is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This software is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with Swish; if not, write to: -- The Free Software Foundation, Inc., -- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- -------------------------------------------------------------------------------- -- $Source: /file/cvsdev/HaskellUtils/ParseCSVTest.hs,v $ -- $Author: graham $ -- $Revision: 1.2 $ -- $Log: ParseCSVTest.hs,v $ -- Revision 1.2 2004/04/20 14:50:47 graham -- Fix some bugs in the CSV parser -- -- Revision 1.1 2004/03/10 16:05:14 graham -- Add CSV parser to Swish, for scraping RDF from exported -- spreadsheet and database files. --