Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

654 add tests from hone-maven-plugin #655

Merged
merged 11 commits into from
Dec 29, 2024
8 changes: 6 additions & 2 deletions eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ extra-source-files:
data/0.50.0/org/eolang/tuple.phi
data/0.50.0/org/eolang/txt/sprintf.phi
data/0.50.0/org/eolang/while.phi
test/eo/phi/confluence.yaml
test/eo/phi/dataization.yaml
test/eo/phi/from-eo/as-phi.yaml
test/eo/phi/metrics.yaml
test/eo/phi/rewriting.yaml
test/eo/phi/rules/new.yaml
test/eo/phi/rules/streams.yaml
test/eo/phi/rules/yegor.yaml
Expand Down Expand Up @@ -430,7 +435,6 @@ library
Language.EO.Phi.Report.Html
Language.EO.Phi.Rules.Common
Language.EO.Phi.Rules.Fast
Language.EO.Phi.Rules.PhiPaper
Language.EO.Phi.Rules.RunYegor
Language.EO.Phi.Rules.Yaml
Language.EO.Phi.Syntax
Expand Down Expand Up @@ -561,7 +565,6 @@ test-suite doctests
Language.EO.Phi.Report.Html
Language.EO.Phi.Rules.Common
Language.EO.Phi.Rules.Fast
Language.EO.Phi.Rules.PhiPaper
Language.EO.Phi.Rules.RunYegor
Language.EO.Phi.Rules.Yaml
Language.EO.Phi.Syntax
Expand Down Expand Up @@ -625,6 +628,7 @@ test-suite spec
main-is: Main.hs
other-modules:
Language.EO.Phi.DataizeSpec
Language.EO.Phi.RewriteSpec
Language.EO.PhiSpec
Language.EO.Rules.PhiPaperSpec
Language.EO.YamlSpec
Expand Down
2 changes: 1 addition & 1 deletion eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extra-source-files:
- grammar/EO/Phi/Syntax.cf
- report/**/*
- data/**/*
- test/eo/phi/rules/**
- test/eo/phi/**/*

verbatim:
cabal-version: 1.24
Expand Down
28 changes: 9 additions & 19 deletions eo-phi-normalizer/src/Language/EO/Phi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,18 @@
-- SOFTWARE.
{- FOURMOLU_ENABLE -}
module Language.EO.Phi (
defaultMain,
normalize,
parseProgram,
unsafeParseObject,
unsafeParseProgram,
unsafeParseProgramFromFile,
module Language.EO.Phi.Syntax,
) where

import System.Exit (exitFailure)

import Language.EO.Phi.Syntax.Abs qualified as Phi
import Language.EO.Phi.Syntax.Par qualified as Phi

import Language.EO.Phi.Normalize
import Language.EO.Phi.Syntax
import Language.EO.Phi.Syntax.Abs qualified as Phi
import Language.EO.Phi.Syntax.Par qualified as Phi

-- | Parse a 'Program' or return a parsing error.
parseProgram :: String -> Either String Phi.Program
Expand All @@ -59,16 +56,9 @@ unsafeParseProgram input =
unsafeParseObject :: String -> Phi.Object
unsafeParseObject = either error id . parseObject

-- | Default entry point.
-- Parses a 𝜑-program from standard input, normalizes,
-- then pretty-prints the result to standard output.
defaultMain :: IO ()
defaultMain = do
input <- getContents -- read entire standard input
let tokens = Phi.myLexer input
case Phi.pProgram tokens of
Left parseError -> do
putStrLn parseError
exitFailure
Right program -> do
putStrLn (printTree (normalize program))
unsafeParseProgramFromFile :: FilePath -> IO Phi.Program
unsafeParseProgramFromFile inputFile = do
src <- readFile inputFile
case parseProgram src of
Left err -> error ("Error parsing program from '" ++ inputFile ++ "':\n" ++ err)
Right program -> pure program
42 changes: 0 additions & 42 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/PhiPaper.hs

This file was deleted.

5 changes: 4 additions & 1 deletion eo-phi-normalizer/src/Language/EO/Phi/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
module Language.EO.Phi.TH where

import Data.Aeson (Options (..), SumEncoding (..), camelTo2)
import Data.Aeson.TH as TH (deriveJSON)
import Data.Aeson.TH as TH (deriveFromJSON, deriveJSON)
import Data.Aeson.Types (defaultOptions)
import Language.Haskell.TH (Dec, Name, Q)

Expand All @@ -38,3 +38,6 @@ defaultOptions' =

deriveJSON :: Name -> Q [Dec]
deriveJSON = TH.deriveJSON defaultOptions'

deriveFromJSON :: Name -> Q [Dec]
deriveFromJSON = TH.deriveFromJSON defaultOptions'
14 changes: 2 additions & 12 deletions eo-phi-normalizer/test/Language/EO/Phi/DataizeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Language.EO.Phi.Dataize.Context (defaultContext)
import Language.EO.Phi.Dependencies (deepMergePrograms)
import Language.EO.Phi.Rules.Common (equalObject)
import Language.EO.Phi.Rules.Yaml (convertRuleNamed, parseRuleSetFromFile, rules)
import Test.EO.Phi (DataizationResult (Bytes, Object), DataizeTest (..), DataizeTestGroup (..), dataizationTests)
import Test.EO.Phi (DataizationResult (Bytes, Object), DataizeTest (..), DataizeTestGroup (..), dataizationTests, progToObj)

newtype ObjectOrBytes = ObjectOrBytes (Either Phi.Object Phi.Bytes)

Expand All @@ -53,13 +53,6 @@ instance Eq ObjectOrBytes where
x == y
_ == _ = False

getProgram :: FilePath -> IO Phi.Program
getProgram inputFile = do
src <- readFile inputFile
case Phi.parseProgram src of
Left err -> error ("Error parsing program from '" ++ inputFile ++ "': " ++ err)
Right program -> pure program

spec :: Spec
spec = do
DataizeTestGroup{..} <- runIO (dataizationTests "test/eo/phi/dataization.yaml")
Expand All @@ -75,7 +68,7 @@ spec = do
describe rulesTitle do
forM_ tests $
\test -> do
deps <- runIO $ mapM getProgram test.dependencies
deps <- runIO $ mapM Phi.unsafeParseProgramFromFile test.dependencies
let mergedProgs = case deepMergePrograms (test.input : deps) of
Left err -> error ("Error merging programs: " ++ err)
Right prog -> prog
Expand All @@ -87,6 +80,3 @@ spec = do
it test.name $ do
let dataizedResult = dataizeRecursively ctx inputObj
ObjectOrBytes dataizedResult `shouldBe` ObjectOrBytes expectedResult

progToObj :: Phi.Program -> Phi.Object
progToObj (Phi.Program bindings) = Phi.Formation bindings
81 changes: 81 additions & 0 deletions eo-phi-normalizer/test/Language/EO/Phi/RewriteSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{- FOURMOLU_DISABLE -}
-- The MIT License (MIT)

-- Copyright (c) 2016-2024 Objectionary.com

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:

-- The above copyright notice and this permission notice shall be included
-- in all copies or substantial portions of the Software.

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
{- FOURMOLU_ENABLE -}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

module Language.EO.Phi.RewriteSpec where

import Control.Monad (forM_)
import Data.Yaml qualified as Yaml
import GHC.Generics (Generic)
import Language.EO.Phi (Program (..))
import Language.EO.Phi.Dataize.Context (defaultContext)
import Language.EO.Phi.Rules.Common (applyRules)
import Language.EO.Phi.Rules.Yaml (convertRuleNamed, parseRuleSetFromFile, rules)
import Language.EO.Phi.Syntax (printTree)
import Language.EO.Phi.TH
import Test.EO.Phi (progToObj)
import Test.Hspec

data Test = Test
{ name :: String
, input :: Program
, output :: Program
}
deriving stock (Generic, Show)

$(deriveFromJSON ''Test)

data RewriteTests = RewriteTests
{ title :: String
, tests :: [Test]
}
deriving stock (Generic, Show)

$(deriveFromJSON ''RewriteTests)

spec :: Spec
spec = do
rewriteTests <- runIO (Yaml.decodeFileThrow @_ @RewriteTests "test/eo/phi/rewriting.yaml")
describe rewriteTests.title do
forM_
[ ("New Yegor's rules", "test/eo/phi/rules/new.yaml")
]
$ \(rulesTitle, rulesFile) -> do
ruleset <- runIO $ parseRuleSetFromFile rulesFile
let rules' = convertRuleNamed <$> ruleset.rules
describe rulesTitle do
forM_ rewriteTests.tests $
\test -> it test.name do
let
inputObj = progToObj test.input
expectedOutputObj = progToObj test.output
ctx = defaultContext rules' inputObj
outputObj = head $ applyRules ctx inputObj
printTree outputObj `shouldBe` printTree expectedOutputObj
25 changes: 4 additions & 21 deletions eo-phi-normalizer/test/Language/EO/PhiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}

module Language.EO.PhiSpec where

Expand All @@ -37,15 +35,12 @@ import Data.Char (isSpace)
import Data.List (dropWhileEnd)
import Data.String (IsString (..))
import Data.Yaml (decodeFileThrow)
import Language.EO.Phi
import Language.EO.Phi.Dataize.Context (defaultContext)
import Language.EO.Phi (Binding (..), Object, Program (..), normalize, printTree)
import Language.EO.Phi.Metrics.Collect (getProgramMetrics)
import Language.EO.Phi.Metrics.Data (BindingsByPathMetrics (..), ProgramMetrics (..))
import Language.EO.Phi.Rules.Common (Rule, equalProgram)
import Language.EO.Phi.Rules.PhiPaper (rule6)
import PyF (fmt)
import Test.EO.Phi
import Test.Hspec
import Language.EO.Phi.Rules.Common (equalProgram)
import Test.EO.Phi (PhiTest (..), PhiTestGroup (..), allPhiTests)
import Test.Hspec (Spec, describe, it, runIO, shouldBe, shouldSatisfy)
import Test.Metrics.Phi (MetricsTest (..), MetricsTestSet (..))

applyRule :: (Object -> [Object]) -> Program -> [Program]
Expand All @@ -57,15 +52,6 @@ applyRule rule = \case

spec :: Spec
spec = do
describe "Pre-defined rules" $
forM_ ([(6, rule6)] :: [(Int, Rule)]) $
\(idx, rule) -> do
PhiTestGroup{..} <- runIO (fileTests [fmt|test/eo/phi/rule-{idx}.yaml|])
describe title $
forM_ tests $
\PhiTest{..} ->
it name $
applyRule (rule (defaultContext [] (progToObj input))) input `shouldBe` [normalized]
describe "Programs translated from EO" $ do
phiTests <- runIO (allPhiTests "test/eo/phi/from-eo/")
forM_ phiTests $ \PhiTestGroup{..} ->
Expand All @@ -86,6 +72,3 @@ spec = do

trim :: String -> String
trim = dropWhileEnd isSpace

progToObj :: Program -> Object
progToObj (Program bindings) = Formation bindings
5 changes: 5 additions & 0 deletions eo-phi-normalizer/test/Test/EO/Phi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ data DataizeTest = DataizeTest
, dependencies :: [FilePath]
}
deriving (Generic, FromJSON)

data DataizationResult
= Bytes {bytes :: Phi.Bytes}
| Object {object :: Phi.Object}
deriving (Generic, Show)

instance FromJSON DataizationResult where
parseJSON = genericParseJSON defaultOptions{sumEncoding = UntaggedValue}

Expand All @@ -101,3 +103,6 @@ instance FromJSON Phi.Object where
parseJSON = fmap unsafeParseObject . parseJSON

deriving newtype instance FromJSON Phi.Bytes

progToObj :: Phi.Program -> Phi.Object
progToObj (Phi.Program bindings) = Phi.Formation bindings
Loading
Loading