check emptiness

This commit is contained in:
Filip Gralinski 2015-11-06 22:42:08 +01:00 committed by Filip Gralinski
parent 570411b702
commit 7f3973890d
7 changed files with 18 additions and 2 deletions

View File

@ -32,6 +32,7 @@ library
, resourcet , resourcet
, split , split
, text , text
, unix
default-language: Haskell2010 default-language: Haskell2010
executable geval executable geval

View File

@ -26,9 +26,11 @@ import Data.Text
import Data.Text.Read as TR import Data.Text.Read as TR
import Control.Applicative import Control.Applicative
import Control.Exception import Control.Exception
import Control.Conditional (unlessM) import Control.Conditional (unlessM, whenM)
import qualified System.Directory as D import qualified System.Directory as D
import System.Posix
import System.FilePath import System.FilePath
import Data.Maybe import Data.Maybe
@ -79,6 +81,7 @@ data GEvalException = NoExpectedFile FilePath
| FileAlreadyThere FilePath | FileAlreadyThere FilePath
| TooFewLines | TooFewLines
| TooManyLines | TooManyLines
| EmptyOutput
deriving (Eq) deriving (Eq)
instance Exception GEvalException instance Exception GEvalException
@ -93,6 +96,7 @@ instance Show GEvalException where
show (FileAlreadyThere filePath) = somethingWrongWithFilesMessage "File already there" filePath show (FileAlreadyThere filePath) = somethingWrongWithFilesMessage "File already there" filePath
show TooFewLines = "Too few lines in the output file" show TooFewLines = "Too few lines in the output file"
show TooManyLines = "Too many 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 :: String -> FilePath -> String
somethingWrongWithFilesMessage msg filePath = Prelude.concat somethingWrongWithFilesMessage msg filePath = Prelude.concat
@ -106,6 +110,10 @@ defaultGEvalSpecification = GEvalSpecification {
gesExpectedFile = defaultExpectedFile, gesExpectedFile = defaultExpectedFile,
gesMetric = defaultMetric } gesMetric = defaultMetric }
isEmptyFile :: FilePath -> IO (Bool)
isEmptyFile path = do
stat <- getFileStatus path
return ((fileSize stat) == 0)
geval :: GEvalSpecification -> IO (MetricValue) geval :: GEvalSpecification -> IO (MetricValue)
geval gevalSpec = do geval gevalSpec = do
@ -131,6 +139,7 @@ gevalCore RMSE expectedFilePath outFilePath = do
gevalCore metric expectedFilePath outFilePath = do gevalCore metric expectedFilePath outFilePath = do
unlessM (D.doesFileExist expectedFilePath) $ throwM $ NoExpectedFile expectedFilePath unlessM (D.doesFileExist expectedFilePath) $ throwM $ NoExpectedFile expectedFilePath
unlessM (D.doesFileExist outFilePath) $ throwM $ NoOutFile outFilePath unlessM (D.doesFileExist outFilePath) $ throwM $ NoOutFile outFilePath
whenM (isEmptyFile outFilePath) $ throwM $ EmptyOutput
gevalCore' metric expectedFilePath outFilePath gevalCore' metric expectedFilePath outFilePath
gevalCore' :: Metric -> String -> String -> IO (MetricValue) gevalCore' :: Metric -> String -> String -> IO (MetricValue)

View File

@ -38,7 +38,8 @@ main = hspec $ do
runGEvalTest "error-too-few-lines" `shouldThrow` (== TooFewLines) runGEvalTest "error-too-few-lines" `shouldThrow` (== TooFewLines)
it "too many lines are handled" $ do it "too many lines are handled" $ do
runGEvalTest "error-too-many-lines" `shouldThrow` (== TooManyLines) 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 :: (Either (ParserResult GEvalOptions) (Maybe MetricValue)) -> IO MetricValue
extractVal (Right (Just val)) = return val extractVal (Right (Just val)) = return val

View File

@ -0,0 +1 @@
--metric BLEU

View File

@ -0,0 +1,2 @@
Alice has a cat.
Basia has a dog.
1 Alice has a cat.
2 Basia has a dog.

View File

@ -0,0 +1,2 @@
Ala ma kota.
Basia ma psa.
1 Ala ma kota.
2 Basia ma psa.