|
| 1 | +module FileSpec (spec) where |
| 2 | + |
| 3 | +import Data.Char (isSpace) |
| 4 | +import System.IO |
| 5 | +import Test.Hspec (Spec, describe, it, shouldBe, shouldReturn) |
| 6 | + |
| 7 | +import File |
| 8 | +import Option |
| 9 | + |
| 10 | +spec :: Spec |
| 11 | +spec = do |
| 12 | + readFromFileSpec |
| 13 | + detectSplitterSpec |
| 14 | + splitFixedSizeSpec |
| 15 | + |
| 16 | +readFromFileSpec :: Spec |
| 17 | +readFromFileSpec = |
| 18 | + describe "readFromFile" $ do |
| 19 | + let opts = Option { skipHeader = True, |
| 20 | + outputHeader = False, |
| 21 | + delimiter = Nothing, |
| 22 | + tabDelimited = False, |
| 23 | + outputDelimiter = Nothing, |
| 24 | + tabDelimitedOutput = False, |
| 25 | + keepLeadingWhiteSpace = False, |
| 26 | + gzipped = False, |
| 27 | + queryFile = Nothing, |
| 28 | + query = Nothing } |
| 29 | + |
| 30 | + it "should read from a test file" $ do |
| 31 | + handle <- openFile "test/tests/basic.csv" ReadMode |
| 32 | + let expected = (["foo", "bar", "baz"], [["a0", "1", "a2"], ["b0", "3", "b2"], ["c0", "", "c2"]]) |
| 33 | + readFromFile opts handle `shouldReturn` expected |
| 34 | + hClose handle |
| 35 | + |
| 36 | + it "should read from a gzipped file" $ do |
| 37 | + handle <- openFile "test/tests/basic.csv.gz" ReadMode |
| 38 | + let expected = (["foo", "bar", "baz"], [["a0", "1", "a2"], ["b0", "3", "b2"], ["c0", "", "c2"]]) |
| 39 | + readFromFile (opts { gzipped = True }) handle `shouldReturn` expected |
| 40 | + hClose handle |
| 41 | + |
| 42 | + it "should read from a test file which contains a multiline cell" $ do |
| 43 | + handle <- openFile "test/tests/multiline.csv" ReadMode |
| 44 | + let expected = (["foo", "bar", "baz", "qux", "quux"], [["a0", "1", "a2\nb0\",3,\"b2\nc0", "", "c2"]]) |
| 45 | + readFromFile opts handle `shouldReturn` expected |
| 46 | + hClose handle |
| 47 | + |
| 48 | +detectSplitterSpec :: Spec |
| 49 | +detectSplitterSpec = |
| 50 | + describe "detectSplitter" $ do |
| 51 | + |
| 52 | + it "should detect the column splitter space" $ do |
| 53 | + let (headLine, secondLine) = ("c0 c1 c2 c3 c4", "0 1 2 3 4") |
| 54 | + detectSplitter headLine secondLine ' ' `shouldBe` True |
| 55 | + detectSplitter headLine secondLine '\t' `shouldBe` True |
| 56 | + |
| 57 | + it "should detect the column splitter comma" $ do |
| 58 | + let (headLine, secondLine) = ("c0,c1,c2,c3,c4", "0,1,2,3,4") |
| 59 | + detectSplitter headLine secondLine ',' `shouldBe` True |
| 60 | + |
| 61 | + it "should detect the column splitter comma even if the column title has spaces" $ do |
| 62 | + let (headLine, secondLine) = ("foo bar baz,qux quux,hoge huga,cmd", "100,200,300,foo bar baz qux") |
| 63 | + detectSplitter headLine secondLine ',' `shouldBe` True |
| 64 | + |
| 65 | +splitFixedSizeSpec :: Spec |
| 66 | +splitFixedSizeSpec = |
| 67 | + describe "splitFixedSize" $ do |
| 68 | + |
| 69 | + it "should split the String with isSpace" $ do |
| 70 | + let (input, expected) = ("c0 c1 c2", [ "c0", "c1", "c2" ]) |
| 71 | + splitFixedSize isSpace 0 input `shouldBe` expected |
| 72 | + |
| 73 | + it "should ignore the successive spaces when splitting with isSpace" $ do |
| 74 | + let (input, expected) = ("c0 c1 c2 \t\t c3", [ "c0", "c1", "c2", "c3" ]) |
| 75 | + splitFixedSize isSpace 0 input `shouldBe` expected |
| 76 | + |
| 77 | + it "should take the column size into account when splitting with isSpace" $ do |
| 78 | + let (input, (n1, expected1), (n2, expected2), (n3, expected3), (n4, expected4)) |
| 79 | + = ("c0 c1 c2 \t\t c3 c4 c5 ", |
| 80 | + (1, [ "c0 c1 c2 \t\t c3 c4 c5 " ]), |
| 81 | + (4, [ "c0", "c1", "c2", "c3 c4 c5 " ]), |
| 82 | + (6, [ "c0", "c1", "c2", "c3", "c4", "c5 " ]), |
| 83 | + (9, [ "c0", "c1", "c2", "c3", "c4", "c5", "", "", "" ])) |
| 84 | + splitFixedSize isSpace n1 input `shouldBe` expected1 |
| 85 | + splitFixedSize isSpace n2 input `shouldBe` expected2 |
| 86 | + splitFixedSize isSpace n3 input `shouldBe` expected3 |
| 87 | + splitFixedSize isSpace n4 input `shouldBe` expected4 |
| 88 | + |
| 89 | + it "should split the String with (==',')" $ do |
| 90 | + let (input, expected) = ("c0,c1,c2", [ "c0", "c1", "c2" ]) |
| 91 | + splitFixedSize (==',') 0 input `shouldBe` expected |
| 92 | + |
| 93 | + it "should not ignore the successive commas when splitting with (==',')" $ do |
| 94 | + let (input, expected) = ("c0,c1,c2,,c3,,,c4", [ "c0", "c1", "c2", "", "c3", "", "", "c4" ]) |
| 95 | + splitFixedSize (==',') 0 input `shouldBe` expected |
| 96 | + |
| 97 | + it "should take the column size into account when splitting with (==',')" $ do |
| 98 | + let (input, (n1, expected1), (n2, expected2), (n3, expected3), (n4, expected4)) |
| 99 | + = ("c0,c1,,c2,foo bar baz,c4", |
| 100 | + (1, [ "c0,c1,,c2,foo bar baz,c4" ]), |
| 101 | + (4, [ "c0", "c1", "", "c2,foo bar baz,c4" ]), |
| 102 | + (6, [ "c0", "c1", "", "c2", "foo bar baz", "c4" ]), |
| 103 | + (9, [ "c0", "c1", "", "c2", "foo bar baz", "c4", "", "", "" ])) |
| 104 | + splitFixedSize (==',') n1 input `shouldBe` expected1 |
| 105 | + splitFixedSize (==',') n2 input `shouldBe` expected2 |
| 106 | + splitFixedSize (==',') n3 input `shouldBe` expected3 |
| 107 | + splitFixedSize (==',') n4 input `shouldBe` expected4 |
0 commit comments