-------------------------------------------------------------------------------- -- $Id: CSVParserTest.hs,v 1.2 2004/04/20 14:50:12 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 CSVParser ( ParseResult(..) , parseCSVfromString ) import N3Parser ( ParseResult(..) , parseN3fromString ) import ErrorM ( ErrorM(..) ) 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 TraceHelpers ( trace, traceShow, traceVal ) ------------------------------------------------------------ -- CSV parsing test ------------------------------------------------------------ -- Simple graph test case csvText01 :: [String] csvText01 = [ "# comment1" , "# , comment2" , "\"# comment3\", and more" , "# NOTE, comments are allowed only before the @data section" , ",,,," , "@prefix,ex:,,,,," , "" , "@rowtype,ex:RowType,,,,," , "" , "@columns,,,,,," , "ex:col1,ex:col2,@about,ex:col3,ex:col4,," , "@coltypes,,,,,," , ",xsd:integer,@resource,@resource,@string,," , "" , "@data,,,,,," , "\"row1,col1\",12,ex:row1,ex:row1col3,row1col4,,named subject" , "@end,,,,,,end of data" ] csvGraph01 :: [String] csvGraph01 = [ "@prefix ex: ." , "" , "# \"row1,col1\",12,ex:row1,ex:row1col3,row1col4,,named subject" , "ex:row1 a ex:RowType ;" , " ex:col1 \"row1,col1\" ;" , " ex:col2 \"12\"^^xsd:integer ;" , " ex:col3 ex:row1col3 ;" , " ex:col4 \"row1col4\" ." ] -- Fuller graph test case csvText02 :: [String] csvText02 = [ "@prefix,ex:,,,,," , "" , "@rowtype,ex:RowType,,,,," , "" , "@columns,,,,,," , "ex:col1,ex:col2,@about,ex:col3,ex:col4,," , "@coltypes,,,,,," , ",xsd:integer,@resource,@resource,@string,," , "" , "@data,,,,,," , "\"row1,col1\",12,ex:row1,ex:row1col3,row1col4,,named subject" , "\"row2,col1\",22,,ex:row2col3,row2col4,,blank node subject" , "\"row3,col1\",,ex:row3,,,,full URI for resource" , ",,ex:row4,,,,no properties -- no resource" , "\"row5,col1\",,ex:row5,,,,just 1 property" , "\"row6,col1\",,ex:row6,,,," , "\"row7,col1\",,ex:***,,,,syntax error in subject name" , "\"row8,col1\",,ex:row8,ex:***,,,syntax error in object qname" , "\"row9,col1\",,ex:row9,,,,syntax error in object uri" , ",,,,,,blank row" , "@end,,,,,,end of data" , "foo,,,,,,ignore this" ] csvGraph02 :: [String] csvGraph02 = [ "@prefix ex: ." , "" , "# \"row1,col1\",12,ex:row1,ex:row1col3,row1col4,,named subject" , "ex:row1 a ex:RowType ;" , " ex:col1 \"row1,col1\" ;" , " ex:col2 \"12\"^^xsd:integer ;" , " ex:col3 ex:row1col3 ;" , " ex:col4 \"row1col4\" ." , "" , "# \"row2,col1\",22,,ex:row2col3,row2col4,,blank node subject" , "[ a ex:RowType ;" , " ex:col1 \"row2,col1\" ;" , " ex:col2 \"22\"^^xsd:integer ;" , " ex:col3 ex:row2col3 ;" , " ex:col4 \"row2col4\" ] ." , "" , "# \"row3,col1\",,ex:row3,,,,full URI for resource" , "ex:row3 a ex:RowType ;" , " ex:col1 \"row3,col1\" ;" , " ex:col3 ." , "" , "# ,,ex:row4,,,,no properties -- no resource" , "" , "# \"row5,col1\",,ex:row5,,,,just 1 property" , "ex:row5 a ex:RowType ;" , " ex:col1 \"row5,col1\" ." , "" , "# \"row6,col1\",,ex:row6,,,," , "ex:row6 a ex:RowType ;" , " ex:col1 \"row6,col1\" ." , "" , "# \"row7,col1\",,ex:***,,,,syntax error in subject name" , "[ a ex:RowType ;" , " ex:col1 \"row7,col1\" ] ." , "" , "# \"row8,col1\",,ex:row8,ex:***,,,syntax error in object qname" , "ex:row8 a ex:RowType ;" , " ex:col1 \"row8,col1\" ." , "" , "# \"row9,col1\",,ex:row9,<***>,,,syntax error in object uri" , "ex:row9 a ex:RowType ;" , " ex:col1 \"row9,col1\" ." ] testCsvN3 :: String -> [String] -> [String] -> Test testCsvN3 lab n3 csv = testEq lab n3gr csvgr where n3gr = case parseN3fromString n3str of Error er -> error er Result g -> g n3str = concatMap (++"\n") n3 csvgr = case parseCSVfromString lab csvstr of Error er -> error er Result g -> g csvstr = concatMap (++"\n") csv csvTest01 = testCsvN3 "csvTest01" csvGraph01 csvText01 csvTest02 = testCsvN3 "csvTest02" csvGraph02 csvText02 testCSVSuite = TestList $ [ csvTest01 , csvTest02 ] ------------------------------------------------------------ -- All tests ------------------------------------------------------------ allTests = TestList [ testCSVSuite ] 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/HaskellRDF/CSVParserTest.hs,v $ -- $Author: graham $ -- $Revision: 1.2 $ -- $Log: CSVParserTest.hs,v $ -- Revision 1.2 2004/04/20 14:50:12 graham -- Fix some bugs in the CSV parser -- -- Revision 1.1 2004/03/10 16:01:04 graham -- Add CSV parser to Swish, for scraping RDF from exported -- spreadsheet and database files. --