2017-06-10 20:30:59 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2017-06-04 16:32:07 +03:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.HUnit
|
|
|
|
import Data.Ord
|
2017-06-10 20:30:59 +03:00
|
|
|
import Text.Megaparsec
|
|
|
|
import Text.Megaparsec.Text
|
|
|
|
import Data.Either (isRight, either)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Monoid ((<>))
|
2017-06-10 22:06:37 +03:00
|
|
|
import System.FilePath((</>))
|
2017-06-10 20:30:59 +03:00
|
|
|
import HackageArchive
|
|
|
|
import Stackage
|
|
|
|
|
|
|
|
import qualified Data.Text.IO as TIO
|
2017-06-04 16:32:07 +03:00
|
|
|
|
2017-06-10 20:30:59 +03:00
|
|
|
testWorkingDir :: String
|
|
|
|
testWorkingDir = "testworkdir"
|
2017-06-04 16:32:07 +03:00
|
|
|
|
2017-06-10 20:30:59 +03:00
|
|
|
parseTests = testGroup "Hackage archive parsing tests"
|
2017-06-04 16:32:07 +03:00
|
|
|
[
|
2017-06-10 20:30:59 +03:00
|
|
|
testPath "filecollection/0.1.1.9/filecollection.cabal" "filecollection" True
|
|
|
|
, testPath "filecollection/0.1.1.9/filecollection.cabal" "filecollectionz" False
|
|
|
|
, testPath "file-collection/0.1.1.9/file-collection.cabal" "file-collection" True
|
|
|
|
, testPath "file-collection/0.1.1.9/file-collection.cabal" "filecollection" False
|
2017-06-04 16:32:07 +03:00
|
|
|
]
|
|
|
|
|
2017-06-10 20:30:59 +03:00
|
|
|
testPath :: T.Text -> T.Text -> Bool -> TestTree
|
|
|
|
testPath text val match = testCase (T.unpack ("Parsing " <> expect match <> " \'" <> text <> "\'")) $
|
|
|
|
assertBool "Failed" $ ((fst <$> parsePath (T.unpack text)) == Just (T.unpack val)) == match
|
|
|
|
|
|
|
|
parseStackageTests = testGroup "Stackage parsing tests"
|
|
|
|
[
|
2017-06-10 22:06:37 +03:00
|
|
|
{-
|
2017-06-10 20:30:59 +03:00
|
|
|
testParse parsePackageLine "constraints: abstract-deque ==0.3," True
|
|
|
|
, testParse parsePackageLine "constraints: abstract-deque ==0.3" True
|
|
|
|
, testParse parsePackageLine "constraints: abstract-deque ==0." False
|
|
|
|
, testParse parsePackageLine "constraints: abstract-deque ==" False
|
|
|
|
, testParse parsePackageLine "constraints: abst3453#$%#ract-deque ==0.3" False
|
|
|
|
, testParse parsePackageLine "constraints: abstract-deque ==0.3," True
|
2017-06-10 22:06:37 +03:00
|
|
|
-}
|
|
|
|
testParse parsePackageLine " ztail ==1.2" True
|
2017-06-10 20:30:59 +03:00
|
|
|
, testParse parsePackageLine " adjunctions ==4.3," True
|
|
|
|
, testParse parsePackageLine "ztail ==1.2" True
|
|
|
|
, testParse parsePackageLine "adjunctions ==4.3," True
|
|
|
|
|
2017-06-10 22:06:37 +03:00
|
|
|
, testParse parseLTS "-- Stackage snapshot from: http://www.stackage.org/snapshot/lts-2.10" True
|
|
|
|
, testParse parseLTS "-- Stackage snapshot from: http://www.stackage.org/snapshot/lts-2.$10" True
|
|
|
|
, testParse parseLTS "-- Please place this file next to your .cabal file as cabal.config" False
|
|
|
|
, testParse parseLTS "-- To only use tested packages, uncomment the following line:" False
|
|
|
|
, testParse parseLTS "-- remote-repo: stackage-lts-2.10:http://www.stackage.org/lts-2.10" False
|
|
|
|
, testParse parseLTS "constraints: abstract-deque ==0.3," False
|
|
|
|
, testParse parseLTS "abstract-par ==0.3.3," False
|
|
|
|
, testParse parseLTS "zlib-lens ==0.1.2" False
|
|
|
|
, testParse parseLTS "-- Stackage snapshot from: http://www.stackage.org/snapshot/nightly-2017-06-10" True
|
2017-06-10 20:30:59 +03:00
|
|
|
]
|
2017-06-10 22:06:37 +03:00
|
|
|
|
|
|
|
parseCabalConfig = testGroup "Cabal config parsing tests"
|
2017-06-10 20:30:59 +03:00
|
|
|
[
|
2017-06-10 22:06:37 +03:00
|
|
|
testStackagePackageLines "sometestfile.cnf"
|
2017-06-04 16:32:07 +03:00
|
|
|
]
|
2017-06-10 22:06:37 +03:00
|
|
|
|
|
|
|
testStackagePackageLines file = testFileParse (testWorkingDir </> file)
|
|
|
|
parseStackageLTS countPackageLines matchWithStackageLTS
|
|
|
|
|
|
|
|
|
|
|
|
-- refactor isComment
|
|
|
|
countPackageLines :: T.Text -> Int
|
|
|
|
countPackageLines text = length $ filter isComment lns
|
|
|
|
where lns = T.lines text
|
|
|
|
isComment ln = not ("--" `T.isInfixOf` ln)
|
|
|
|
|
|
|
|
matchWithStackageLTS :: Int -> StackageLTS -> Bool
|
|
|
|
matchWithStackageLTS count1 stackage = count1 == (length.snd) stackage
|
2017-06-04 16:32:07 +03:00
|
|
|
|
2017-06-10 20:30:59 +03:00
|
|
|
expect :: Bool -> T.Text
|
|
|
|
expect True = "expect success"
|
|
|
|
expect False = "expect fail"
|
2017-06-04 16:32:07 +03:00
|
|
|
|
2017-06-10 20:30:59 +03:00
|
|
|
testParse :: Parser a -> T.Text -> Bool -> TestTree
|
|
|
|
testParse p text match = testCase (T.unpack ("Parsing " <> expect match <> " \'" <> text <> "\'")) $
|
|
|
|
assertBool "Failed" (isRight (runParser p "" text) == match)
|
|
|
|
|
|
|
|
testFileParse :: FilePath -> Parser a -> (T.Text -> b) -> (b -> a -> Bool) -> TestTree
|
|
|
|
testFileParse file p textFunc matchFunc =
|
|
|
|
testCase ("Testing file: " ++ file) $ do
|
|
|
|
fileText <- TIO.readFile file -- got the file
|
|
|
|
let textVal = textFunc fileText
|
|
|
|
let eVal = matchFunc textVal <$> runParser p "" fileText
|
|
|
|
assertBool "Failed" (either (const False) id eVal)
|
|
|
|
|
|
|
|
tests :: TestTree
|
2017-06-10 22:06:37 +03:00
|
|
|
tests = testGroup "REPL tests" [parseStackageTests, parseTests, parseCabalConfig]
|
2017-06-04 16:32:07 +03:00
|
|
|
|
2017-06-10 20:30:59 +03:00
|
|
|
main = defaultMain tests
|