packages feed

hydra-kernel-0.18.1: src/main/haskell/Hydra/Core/Test/Transform.hs

-- Note: this is an automatically generated file. Do not edit.

-- | Transform test cases for code generation, filtering to tests that can be compiled to target languages

module Hydra.Core.Test.Transform where

import qualified Hydra.Core.Overlay.Haskell.Lib.Eithers as Eithers
import qualified Hydra.Core.Overlay.Haskell.Lib.Lists as Lists
import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Pairs as Pairs
import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Core.Model as Model
import qualified Hydra.Core.Packaging as Packaging
import qualified Hydra.Core.Testing as Testing
import qualified Hydra.Core.Util as Util
import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void

-- | Add generation namespace prefix
addGenerationPrefix :: Packaging.ModuleName -> Packaging.ModuleName
addGenerationPrefix ns_ = Packaging.ModuleName (Strings.concat2 "generation." (Packaging.unModuleName ns_))

-- | Build a Term representing a convertCase function call
buildConvertCaseCall :: Util.CaseConvention -> Util.CaseConvention -> String -> Model.Term
buildConvertCaseCall fromConv toConv input_ =
    Model.TermApplication (Model.Application {
      Model.applicationFunction = (Model.TermApplication (Model.Application {
        Model.applicationFunction = (Model.TermApplication (Model.Application {
          Model.applicationFunction = (Model.TermVariable (Model.Name "hydra.core.formatting.convertCase")),
          Model.applicationArgument = (encodeCaseConvention fromConv)})),
        Model.applicationArgument = (encodeCaseConvention toConv)})),
      Model.applicationArgument = (Model.TermLiteral (Model.LiteralString input_))})

-- | Build a Term representing a topologicalSort function call
buildTopologicalSortCall :: [(Int, [Int])] -> Model.Term
buildTopologicalSortCall adjList =
    Model.TermApplication (Model.Application {
      Model.applicationFunction = (Model.TermVariable (Model.Name "hydra.core.sorting.topologicalSort")),
      Model.applicationArgument = (encodeAdjacencyList adjList)})

-- | Build a Term representing a topologicalSortComponents function call
buildTopologicalSortSCCCall :: [(Int, [Int])] -> Model.Term
buildTopologicalSortSCCCall adjList =
    Model.TermApplication (Model.Application {
      Model.applicationFunction = (Model.TermVariable (Model.Name "hydra.core.sorting.topologicalSortComponents")),
      Model.applicationArgument = (encodeAdjacencyList adjList)})

-- | Collect all test cases from a test group (flattening hierarchy)
collectTestCases :: Testing.TestGroup -> [Testing.TestCaseWithMetadata]
collectTestCases tg =
    Lists.concat2 (Testing.testGroupCases tg) (Lists.concat (Lists.map (\sg -> collectTestCases sg) (Testing.testGroupSubgroups tg)))

-- | Encode an adjacency list as a Term
encodeAdjacencyList :: [(Int, [Int])] -> Model.Term
encodeAdjacencyList pairs =
    Model.TermList (Lists.map (\p -> Model.TermPair (encodeInt (Pairs.first p), (Model.TermList (Lists.map (\d -> encodeInt d) (Pairs.second p))))) pairs)

-- | Encode CaseConvention as a Term (unit variant)
encodeCaseConvention :: Util.CaseConvention -> Model.Term
encodeCaseConvention conv =
    Model.TermInject (Model.Injection {
      Model.injectionTypeName = (Model.Name "hydra.core.util.CaseConvention"),
      Model.injectionField = Model.Field {
        Model.fieldName = case conv of
          Util.CaseConventionLowerSnake -> Model.Name "lowerSnake"
          Util.CaseConventionUpperSnake -> Model.Name "upperSnake"
          Util.CaseConventionCamel -> Model.Name "camel"
          Util.CaseConventionPascal -> Model.Name "pascal"
          Util.CaseConventionUpperDashed -> Model.Name "upperDashed",
        Model.fieldTerm = Model.TermUnit}})

-- | Encode Either [[Int]] [Int] as a Term
encodeEitherListList :: Either [[Int]] [Int] -> Model.Term
encodeEitherListList e =
    Model.TermEither (Eithers.bimap (\cycles -> encodeListList cycles) (\sorted -> encodeIntList sorted) e)

-- | Encode an Int as a Term
encodeInt :: Int -> Model.Term
encodeInt n = Model.TermLiteral (Model.LiteralInteger (Model.IntegerValueInt32 n))

-- | Encode [Int] as a Term
encodeIntList :: [Int] -> Model.Term
encodeIntList ints = Model.TermList (Lists.map (\n -> encodeInt n) ints)

-- | Encode [[Int]] as a Term
encodeListList :: [[Int]] -> Model.Term
encodeListList lists = Model.TermList (Lists.map (\l -> encodeIntList l) lists)

-- | Transform module with generation namespace
transformModule :: Packaging.Module -> Packaging.Module
transformModule m =
    Packaging.Module {
      Packaging.moduleName = (addGenerationPrefix (Packaging.moduleName m)),
      Packaging.moduleMetadata = (Packaging.moduleMetadata m),
      Packaging.moduleDependencies = (Packaging.moduleDependencies m),
      Packaging.moduleDefinitions = (Packaging.moduleDefinitions m)}

-- | Pass through test cases unchanged
transformTestCase :: t0 -> Maybe t0
transformTestCase tcm = Just tcm

-- | Transform test group hierarchy to only include delegated evaluation tests
transformToCompiledTests :: Testing.TestGroup -> Maybe Testing.TestGroup
transformToCompiledTests tg =

      let name_ = Testing.testGroupName tg
          desc = Testing.testGroupDescription tg
          subgroups = Testing.testGroupSubgroups tg
          cases_ = Testing.testGroupCases tg
          transformedCases = Optionals.givens (Lists.map (\tc -> transformTestCase tc) cases_)
          transformedSubgroups = Optionals.givens (Lists.map (\sg -> transformToCompiledTests sg) subgroups)
      in (Logic.ifElse (Logic.and (Lists.isEmpty transformedCases) (Lists.isEmpty transformedSubgroups)) Nothing (Just (Testing.TestGroup {
        Testing.testGroupName = name_,
        Testing.testGroupDescription = desc,
        Testing.testGroupSubgroups = transformedSubgroups,
        Testing.testGroupCases = transformedCases})))