check emptiness
This commit is contained in:
parent
570411b702
commit
7f3973890d
@ -32,6 +32,7 @@ library
|
||||
, resourcet
|
||||
, split
|
||||
, text
|
||||
, unix
|
||||
default-language: Haskell2010
|
||||
|
||||
executable geval
|
||||
|
@ -26,9 +26,11 @@ import Data.Text
|
||||
import Data.Text.Read as TR
|
||||
import Control.Applicative
|
||||
import Control.Exception
|
||||
import Control.Conditional (unlessM)
|
||||
import Control.Conditional (unlessM, whenM)
|
||||
import qualified System.Directory as D
|
||||
|
||||
import System.Posix
|
||||
|
||||
import System.FilePath
|
||||
import Data.Maybe
|
||||
|
||||
@ -79,6 +81,7 @@ data GEvalException = NoExpectedFile FilePath
|
||||
| FileAlreadyThere FilePath
|
||||
| TooFewLines
|
||||
| TooManyLines
|
||||
| EmptyOutput
|
||||
deriving (Eq)
|
||||
|
||||
instance Exception GEvalException
|
||||
@ -93,6 +96,7 @@ instance Show GEvalException where
|
||||
show (FileAlreadyThere filePath) = somethingWrongWithFilesMessage "File already there" filePath
|
||||
show TooFewLines = "Too few lines in the output file"
|
||||
show TooManyLines = "Too many lines in the output file"
|
||||
show EmptyOutput = "The output file is empty"
|
||||
|
||||
somethingWrongWithFilesMessage :: String -> FilePath -> String
|
||||
somethingWrongWithFilesMessage msg filePath = Prelude.concat
|
||||
@ -106,6 +110,10 @@ defaultGEvalSpecification = GEvalSpecification {
|
||||
gesExpectedFile = defaultExpectedFile,
|
||||
gesMetric = defaultMetric }
|
||||
|
||||
isEmptyFile :: FilePath -> IO (Bool)
|
||||
isEmptyFile path = do
|
||||
stat <- getFileStatus path
|
||||
return ((fileSize stat) == 0)
|
||||
|
||||
geval :: GEvalSpecification -> IO (MetricValue)
|
||||
geval gevalSpec = do
|
||||
@ -131,6 +139,7 @@ gevalCore RMSE expectedFilePath outFilePath = do
|
||||
gevalCore metric expectedFilePath outFilePath = do
|
||||
unlessM (D.doesFileExist expectedFilePath) $ throwM $ NoExpectedFile expectedFilePath
|
||||
unlessM (D.doesFileExist outFilePath) $ throwM $ NoOutFile outFilePath
|
||||
whenM (isEmptyFile outFilePath) $ throwM $ EmptyOutput
|
||||
gevalCore' metric expectedFilePath outFilePath
|
||||
|
||||
gevalCore' :: Metric -> String -> String -> IO (MetricValue)
|
||||
|
@ -38,7 +38,8 @@ main = hspec $ do
|
||||
runGEvalTest "error-too-few-lines" `shouldThrow` (== TooFewLines)
|
||||
it "too many lines are handled" $ do
|
||||
runGEvalTest "error-too-many-lines" `shouldThrow` (== TooManyLines)
|
||||
|
||||
it "empty output is handled" $ do
|
||||
runGEvalTest "empty-output" `shouldThrow` (== EmptyOutput)
|
||||
|
||||
extractVal :: (Either (ParserResult GEvalOptions) (Maybe MetricValue)) -> IO MetricValue
|
||||
extractVal (Right (Just val)) = return val
|
||||
|
1
test/empty-output/empty-output/config.txt
Normal file
1
test/empty-output/empty-output/config.txt
Normal file
@ -0,0 +1 @@
|
||||
--metric BLEU
|
2
test/empty-output/empty-output/test-A/expected.tsv
Normal file
2
test/empty-output/empty-output/test-A/expected.tsv
Normal file
@ -0,0 +1,2 @@
|
||||
Alice has a cat.
|
||||
Basia has a dog.
|
|
2
test/empty-output/empty-output/test-A/in.tsv
Normal file
2
test/empty-output/empty-output/test-A/in.tsv
Normal file
@ -0,0 +1,2 @@
|
||||
Ala ma kota.
|
||||
Basia ma psa.
|
|
Loading…
Reference in New Issue
Block a user