{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE LambdaCase #-}{-# OPTIONS_GHC -Wno-orphans #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule RewriterSpec whereimport AST (Expression (ExGlobal))import Control.Monad (forM_, unless)import Data.Aesonimport Data.Char (isSpace)import Data.List.NonEmpty qualified as NEimport Data.Yaml qualified as Yamlimport Deps (dontSaveStep)import Functions (buildTerm)import GHC.Genericsimport Misc (allPathsIn, ensuredFile)import Must (Must (..))import Parser (parseProgramThrows)import Printer (printProgram)import Rewriter (RewriteContext (RewriteContext), rewrite)import System.FilePath (makeRelative, replaceExtension, (</>))import Test.Hspec (Spec, describe, expectationFailure, it, pending, runIO)import Yaml (normalizationRules)import Yaml qualified as Ydata Rules = Rules { basic :: Maybe [String] , custom :: Maybe [Y.Rule] } deriving (Generic, FromJSON, Show)data YamlPack = YamlPack { input :: String , output :: String , rules :: Maybe Rules , skip :: Maybe Bool , repeat_ :: Maybe Int , must :: Maybe Int , normalize :: Maybe Bool } deriving (Generic, Show)instance FromJSON YamlPack where parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = \case "repeat_" -> "repeat" other -> other }yamlPack :: FilePath -> IO YamlPackyamlPack = Yaml.decodeFileThrownoSpaces :: String -> StringnoSpaces = filter (not . isSpace)spec :: Specspec = describe "rewrite packs" $ do let resources = "test-resources/rewriter-packs" packs <- runIO (allPathsIn resources) forM_ packs ( \pth -> it (makeRelative resources pth) $ do pack <- yamlPack pth let normalize' = case normalize pack of Just _ -> True _ -> False repeat' = if normalize' then 50 else case repeat_ pack of Just num -> num _ -> 1 must' = case must pack of Just num -> MtExact num _ -> MtDisabled case skip pack of Just True -> pending _ -> do prog <- parseProgramThrows (input pack) rules' <- case rules pack of Just _rules -> case custom _rules of Just custom' -> pure custom' _ -> case basic _rules of Just basic' -> mapM ( \name -> do yaml <- ensuredFile ("resources" </> replaceExtension name ".yaml") Y.yamlRule yaml ) basic' _ -> pure [] Nothing -> if normalize' then pure normalizationRules else pure [] (rewrittens, _) <- rewrite prog rules' ( RewriteContext ExGlobal repeat' repeat' False buildTerm must' dontSaveStep ) let (program, _) = NE.last rewrittens result' <- parseProgramThrows (output pack) unless (program == result') $ expectationFailure ( "Wrong rewritten program. Expected:\n" ++ printProgram result' ++ "\nGot:\n" ++ printProgram program ) )