Skip to content

Commit

Permalink
feat(eo-phi-normalizer): add rewriting test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Dec 29, 2024
1 parent 6c87fb2 commit 92ba40d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,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
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

0 comments on commit 92ba40d

Please sign in to comment.