-- | Specifies the base tasty-sweet types and common class instance
-- definitions for those types.

{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Test.Tasty.Sugar.Types where

import           Data.Function ( on )
import qualified Data.List as L
import           Data.Maybe ( catMaybes )
import           Numeric.Natural
import           System.FilePath -- ( (</>) )
import qualified System.FilePath.GlobPattern as FPGP
#if MIN_VERSION_prettyprinter(1,7,0)
import Prettyprinter
#else
import Data.Text.Prettyprint.Doc
#endif

import Prelude hiding ( exp )


-- | This is the type used to specify file suffixes.  The synonym name
-- is primarily used to indicate where this suffix specification is
-- used.
type FileSuffix = String


-- | Specifies the parameters and patterns to use when searching for
-- samples to build tests from.  The 'mkCUBE' function should be used
-- to obtain a 'CUBE' structure initialized with overrideable
-- defaults.
--
-- The primary elements to specify are the 'rootName' and the
-- 'expectedSuffix'.  With these two specifications (and possibly the
-- 'inputDirs') the 'Test.Tasty.Sugar' functionality will be similar to
-- a "golden" testing package.
--
-- The 'validParams' is an optional feature that is useful when
-- multiple expected results files are generated from a single
-- 'rootName', differing by the specified parameters.
--
-- The 'associatedNames' is an optional feature that is useful for
-- when there are other files to be associated with a test in addition
-- to the 'rootFile' and the 'expectedFile'.
--
data CUBE = CUBE
   {
     -- | The original directory in which the sample files can be found.  This is
     -- provided for backward-compatibility, but the use of the 'inputDirs'
     -- alternative is recommended.
     CUBE -> String
inputDir :: FilePath

     -- | The directories in which the sample files that drive the
     -- testing exist.  When specified as a relative filepath
     -- (suggested) then a directory is relative to the cabal file.
   , CUBE -> [String]
inputDirs :: [FilePath]

     -- | The name of the "root" file for each test scenario.  The
     -- contents of this file are opaque to 'tasty-sweet' and are
     -- interpreted by the tests themselves.  Each "root" file is
     -- the kernel for a set of test cases.
     --
     -- The root file should not be specified with any path element,
     -- it should exist in one of the 'inputDirs' location and it can be
     -- specified as a glob pattern.
     --
     -- The corresponding expected results files will be identified by
     -- finding files which match a portion of this name with a
     -- "{separator}{expectedSuffix}" appended to it.
   , CUBE -> String
rootName :: FPGP.GlobPattern

     -- | The expected suffix for a target pattern for running a test.
     -- There may be multiple files specifying expected results for a
     -- test (see the 'validParams' below), but a particular test case
     -- is comprised of a source file along with a corresponding
     -- "expected result" file that is the name of the source file
     -- with the 'expectedSuffix' suffix.  The suffix should not contain
     -- any glob match characters. Note that the suffix is the text
     -- that comes after one of the 'separators' below.
     --
     -- The 'expectedSuffix' *may* start with one of the characters in
     -- 'separators'.  If this occurs, then the suffix will only be
     -- considered if preceeded by that specific separator; otherwise
     -- any of the 'separators' may be used prior to the
     -- 'expectedSuffix'.
   , CUBE -> String
expectedSuffix :: FileSuffix

     -- | The 'separators' specify the characters which separate the
     -- expected suffix from the rootName, and which also separate
     -- the various parameters (if any, see 'validParams' below).  Any
     -- one of the separators in this list can be used, and a file can
     -- used a mixture of the separators in the filename.
     --
     -- It is also valid to specify no separators, in which case the
     -- 'rootName' and 'expectedSuffix' are directly concatenated.  This
     -- is not a typical usage, however.
     --
     -- The default separators (returned by 'mkCUBE') are ".-" meaning
     -- that extensions (and parameters) can be separated from the
     -- base name by either a period or a dash.
   , CUBE -> String
separators :: Separators

     -- | The 'associatedNames' specifies other files that are
     -- associated with a particular test configuration.  These files
     -- are optional and not all of them appear, but different
     -- suffixes may be associated here with a general name.  When a
     -- test is being generated, any associatedNames that were found
     -- will be passed to the test generator for use as supplemental
     -- data.
     --
     -- Specified as a list of tuples, where each tuple is the
     -- (arbitrary) name of the associated file type, and the file
     -- type suffix (with no period or other separator).
     --
     -- If there is a blank FileSuffix for an associated name, that indicates
     -- that the associated name matches the rootname *without* any suffix (and
     -- implies that the root name has a suffix that can be removed).
   , CUBE -> [(String, String)]
associatedNames :: [ (String, FileSuffix) ]

     -- | The 'validParams' can be used to specify various parameters
     -- that may be present in the filename.
     --
     -- For example, tests might be parameterized by which C compiler
     -- (@"gcc"@ or @"clang"@) was used, which target architecture
     -- (@"x86_64"@ or @"ppc"@ or @"arm"@), and which optimization
     -- level.  The values for these parameters appear in any order in
     -- the filenames of any file (other than the 'rootName')
     -- delineated by any of the separators.  Not all parameter values
     -- are required to appear.
     --
     -- The following are valid examples:
     --
     -- > foo-gcc-ppc-O3.o
     -- > foo-clang.x86_64.o
     -- > foo.O0-clang.o
     --
     -- The sugar matching code will attempt to identify the various parameter
     -- values appearing in the _EXPECTED_ filename which correspond to the same
     -- values in the _ROOT_ filename and provide that information to the test
     -- generation process to allow the generated test to be customized to the
     -- available set of parameters.
     --
     -- The 'associatedNames' provided to the test generator will be
     -- constrained to those associated names that match the parameter
     -- values explicit in the expected name, and called for each
     -- combination of unspecified parameter values present in
     -- associated names.
     --
     -- There may actually be multiple sets of parameterized files for
     -- each 'rootName' file: the test generator will be called for
     -- each set of parameters.
     --
     -- Each entry in the 'validParams' specifies the name of the
     -- parameter and the set of values; one (and only one) parameter
     -- may have existential values rather than pre-determined values,
     -- as indicated by a Nothing for the parameter value set.  Valid
     -- parameter values are *not* matched with file globbing (they
     -- must be explicit and precise matches) and they cannot be blank
     -- (the lack of a parameter is handled automatically rather than
     -- an explicit blank value).
   , CUBE -> [ParameterPattern]
validParams :: [ParameterPattern]
   }
   deriving Int -> CUBE -> ShowS
[CUBE] -> ShowS
CUBE -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CUBE] -> ShowS
$cshowList :: [CUBE] -> ShowS
show :: CUBE -> String
$cshow :: CUBE -> String
showsPrec :: Int -> CUBE -> ShowS
$cshowsPrec :: Int -> CUBE -> ShowS
Show

{-# DEPRECATED inputDir "Use inputDirs instead" #-}

-- | Parameters are specified by their name and a possible list of
-- valid values.  If there is no list of valid values, any value is
-- accepted for that parameter position.  Parameters are listed in the
-- order that they should appear in the filenames to be matched.

type ParameterPattern = (String, Maybe [String])

-- | Separators for the path and suffix specifications.  Any separator
-- is accepted in any position between parameters and prior to the
-- expected suffix. The synonym name is primarily used to indicate
-- where this separators specification is intended to be used.

type Separators = String

-- | Generates the default 'CUBE' configuration; callers should override
-- individual fields as appropriate.  This is the preferred way to initialize a
-- CUBE if defaults are to be used for various fields:
--
--   * inputDirs:      [ "test/samples" ]
--   * inputDir:       "test/samples"
--   * separators:     .-
--   * rootName:       *
--   * expectedSuffix: exp

mkCUBE :: CUBE
mkCUBE :: CUBE
mkCUBE = CUBE { inputDirs :: [String]
inputDirs = [String
"test/samples"]
              , inputDir :: String
inputDir = String
""
              , separators :: String
separators = String
".-"
              , rootName :: String
rootName = String
"*"
              , associatedNames :: [(String, String)]
associatedNames = []
              , expectedSuffix :: String
expectedSuffix = String
"exp"
              , validParams :: [ParameterPattern]
validParams = []
              }


instance Pretty CUBE where
  pretty :: forall ann. CUBE -> Doc ann
pretty CUBE
cube =
    let assoc :: Maybe (Doc ann)
assoc = forall ann. [(String, String)] -> Maybe (Doc ann)
prettyAssocNames forall a b. (a -> b) -> a -> b
$ CUBE -> [(String, String)]
associatedNames CUBE
cube
        parms :: Maybe (Doc ann)
parms = forall ann. [ParameterPattern] -> Maybe (Doc ann)
prettyParamPatterns forall a b. (a -> b) -> a -> b
$ CUBE -> [ParameterPattern]
validParams CUBE
cube
        hdrs :: [Doc ann]
hdrs = [ Doc ann
"input dirs: "
                 forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty (forall a. Eq a => [a] -> [a]
L.nub forall a b. (a -> b) -> a -> b
$ CUBE -> String
inputDir CUBE
cube forall a. a -> [a] -> [a]
: CUBE -> [String]
inputDirs CUBE
cube)
               , Doc ann
"rootName: " forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty (CUBE -> String
rootName CUBE
cube)
               , Doc ann
"expected: " forall ann. Doc ann -> Doc ann -> Doc ann
<+>
                 forall ann. Doc ann -> Doc ann
brackets (forall a ann. Pretty a => a -> Doc ann
pretty forall a b. (a -> b) -> a -> b
$ CUBE -> String
separators CUBE
cube) forall a. Semigroup a => a -> a -> a
<>
                 forall a ann. Pretty a => a -> Doc ann
pretty (CUBE -> String
expectedSuffix CUBE
cube)
               ]
    in Doc ann
"Sugar.CUBE" forall a. Semigroup a => a -> a -> a
<> (forall ann. Int -> Doc ann -> Doc ann
indent Int
1 forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall {ann}. [Doc ann]
hdrs forall a. Semigroup a => a -> a -> a
<> forall a. [Maybe a] -> [a]
catMaybes [forall {ann}. Maybe (Doc ann)
assoc, forall {ann}. Maybe (Doc ann)
parms])


-- | Pretty printing for a set of associated names
prettyAssocNames :: [(String, String)] -> Maybe (Doc ann)
prettyAssocNames :: forall ann. [(String, String)] -> Maybe (Doc ann)
prettyAssocNames = \case
  [] -> forall a. Maybe a
Nothing
  [(String, String)]
nms -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Doc ann
"associated:" forall a. Semigroup a => a -> a -> a
<> (forall ann. Int -> Doc ann -> Doc ann
indent Int
1 forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a ann. Pretty a => a -> Doc ann
pretty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Show a => a -> String
show) [(String, String)]
nms)

-- | Pretty printing for a list of parameter patterns
prettyParamPatterns :: [ParameterPattern] -> Maybe (Doc ann)
prettyParamPatterns :: forall ann. [ParameterPattern] -> Maybe (Doc ann)
prettyParamPatterns = \case
  [] -> forall a. Maybe a
Nothing
  [ParameterPattern]
prms -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Doc ann
"params:" forall a. Semigroup a => a -> a -> a
<>
          (let pp :: (a, Maybe [a]) -> Doc ann
pp (a
pn,Maybe [a]
mpv) =
                 forall a ann. Pretty a => a -> Doc ann
pretty a
pn forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall ann. Doc ann
equals forall ann. Doc ann -> Doc ann -> Doc ann
<+>
                 case Maybe [a]
mpv of
                   Maybe [a]
Nothing -> Doc ann
"*"
                   Just [a]
vl -> forall ann. [Doc ann] -> Doc ann
hsep forall a b. (a -> b) -> a -> b
$
                              forall a. a -> [a] -> [a]
L.intersperse forall ann. Doc ann
pipe forall a b. (a -> b) -> a -> b
$
                              forall a b. (a -> b) -> [a] -> [b]
map forall a ann. Pretty a => a -> Doc ann
pretty [a]
vl
            in forall ann. Int -> Doc ann -> Doc ann
indent Int
1 forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {a} {a} {ann}.
(Pretty a, Pretty a) =>
(a, Maybe [a]) -> Doc ann
pp [ParameterPattern]
prms)

-- | Internally, this keeps the association between a possible file and the input
-- directory it came from.  The "file" portion is relative to the input
-- directory.

data CandidateFile = CandidateFile
                     {
                       -- | The 'candidateDir' is the top-level directory path
                       -- for this candidate and is usually one of the CUBE
                       -- inputDirs
                       CandidateFile -> String
candidateDir :: FilePath
                       -- | The 'candidateSubdirs' is the sequence of
                       -- subdirectories beneath the 'candidateDir' where the
                       -- 'candidateFile' is located.  These subdirectories may
                       -- provide parameter matches.
                     , CandidateFile -> [String]
candidateSubdirs :: [ FilePath ]
                       -- | The 'candidateFile' is the filename portion (only) of
                       -- the candidate file.  (Use 'candidateToPath' to get the
                       -- full filepath from a CandidateFile).
                     , CandidateFile -> String
candidateFile :: FilePath
                       -- | Portions of the candidateFile (or candidateSubdirs)
                       -- that match parameters
                     , CandidateFile -> [NamedParamMatch]
candidatePMatch :: [NamedParamMatch]
                       -- | If there are candidatePMatch, this is the index of
                       -- the first match.  This therefore is also the end of the
                       -- "root" file match portion.  If no candidatePMatch, this
                       -- is the length of the candidateFile.
                     , CandidateFile -> Natural
candidateMatchIdx :: Natural
                     }
                   deriving (CandidateFile -> CandidateFile -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CandidateFile -> CandidateFile -> Bool
$c/= :: CandidateFile -> CandidateFile -> Bool
== :: CandidateFile -> CandidateFile -> Bool
$c== :: CandidateFile -> CandidateFile -> Bool
Eq, Int -> CandidateFile -> ShowS
[CandidateFile] -> ShowS
CandidateFile -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CandidateFile] -> ShowS
$cshowList :: [CandidateFile] -> ShowS
show :: CandidateFile -> String
$cshow :: CandidateFile -> String
showsPrec :: Int -> CandidateFile -> ShowS
$cshowsPrec :: Int -> CandidateFile -> ShowS
Show)  -- Show is for for debugging/tracing


-- | Each identified test input set is represented as a 'Sweets'
-- object.. a Specifications With Existing Expected Testing Samples.

data Sweets = Sweets
  { Sweets -> String
rootBaseName :: String
    -- ^ The base of the root path for matching to expected.  This has no path
    -- elements, no extensions and no parameters.  It can be useful to use to
    -- compare to other fields in the 'expected' Expectation list of this
    -- structure.  Note that if the root file matched had parameters as part of
    -- the filename, those are not present in this rootBaseName.
  , Sweets -> String
rootMatchName :: String
    -- ^ Matched root.  This is the name of the matched file, (no path elements)
    -- that matched the rootName in the input CUBE.  This includes any extension
    -- or parameter substitutions.  This is often the best name to use for
    -- displaying this matched item.
  , Sweets -> String
rootFile :: FilePath
    -- ^ The full actual filepath of the matched root, with all path elements,
    -- extensions, parameters, and suffixes present.  This is most useful to open
    -- or otherwise access the file.
  , Sweets -> [ParameterPattern]
cubeParams :: [ParameterPattern] -- ^ parameters for match
  , Sweets -> [Expectation]
expected :: [Expectation] -- ^ all expected files and associated
  }
  deriving (Int -> Sweets -> ShowS
[Sweets] -> ShowS
Sweets -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Sweets] -> ShowS
$cshowList :: [Sweets] -> ShowS
show :: Sweets -> String
$cshow :: Sweets -> String
showsPrec :: Int -> Sweets -> ShowS
$cshowsPrec :: Int -> Sweets -> ShowS
Show, Sweets -> Sweets -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Sweets -> Sweets -> Bool
$c/= :: Sweets -> Sweets -> Bool
== :: Sweets -> Sweets -> Bool
$c== :: Sweets -> Sweets -> Bool
Eq)

instance Pretty Sweets where
  pretty :: forall ann. Sweets -> Doc ann
pretty Sweets
inp = Doc ann
"Sweet" forall ann. Doc ann -> Doc ann -> Doc ann
<+>
               (forall ann. Doc ann -> Doc ann
align forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a. [Maybe a] -> [a]
catMaybes
                 [ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a ann. Pretty a => a -> Doc ann
pretty (Sweets -> String
rootMatchName Sweets
inp)
                 , forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Doc ann
"root:" forall ann. Doc ann -> Doc ann -> Doc ann
<+>
                   forall ann. Doc ann -> Doc ann
align (forall ann. [Doc ann] -> Doc ann
vsep [ forall a ann. Pretty a => a -> Doc ann
pretty (Sweets -> String
rootBaseName Sweets
inp)
                               , forall a ann. Pretty a => a -> Doc ann
pretty (Sweets -> String
rootFile Sweets
inp)
                               ])
                 , forall ann. [ParameterPattern] -> Maybe (Doc ann)
prettyParamPatterns forall a b. (a -> b) -> a -> b
$ Sweets -> [ParameterPattern]
cubeParams Sweets
inp
                 , forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a ann. Pretty a => a -> Doc ann
pretty forall a b. (a -> b) -> a -> b
$ Sweets -> [Expectation]
expected Sweets
inp
                 ])

-- | The 'Association' specifies the name of the associated file entry
-- and the actual filepath of that associated file.

type Association = (String, FilePath)

-- | The 'NamedParamMatch' specifies the parameter name and the
-- corresponding value for the expected file found.  These can be
-- extracted from the name of the expected file and the set of
-- 'ParameterPattern' entries, but they are presented in an associated
-- list format for easy utilization by the invoked test target.

type NamedParamMatch = (String, ParamMatch)

-- | The 'Expectation' represents a valid test configuration based on
-- the set of provided files.  The 'Expectation' consists of an
-- expected file which matches the 'rootFile' in the containing
-- 'Sweets' data object.  The 'expectedFile' field is the name of the
-- file containing expected output, the 'expParamsMatch' field
-- specifies the 'ParameterPattern' matching values for this expected
-- file, and the 'associated' field provides a list of files
-- associated with this expected file.

data Expectation = Expectation
  { Expectation -> String
expectedFile :: FilePath  -- ^ file containing Expected results
  , Expectation -> [NamedParamMatch]
expParamsMatch :: [ NamedParamMatch ] -- ^ set of CUBE parameters
                                          -- matched and the matched
                                          -- values.
  , Expectation -> [(String, String)]
associated :: [ Association ] -- ^ Associated files found
  }
  deriving Int -> Expectation -> ShowS
[Expectation] -> ShowS
Expectation -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Expectation] -> ShowS
$cshowList :: [Expectation] -> ShowS
show :: Expectation -> String
$cshow :: Expectation -> String
showsPrec :: Int -> Expectation -> ShowS
$cshowsPrec :: Int -> Expectation -> ShowS
Show

-- | Equality comparisons of two 'Expectation' objects ignores the
-- order of the 'expParamsMatch' and 'associated' fields.
instance Eq Expectation where
  Expectation
e1 == :: Expectation -> Expectation -> Bool
== Expectation
e2 = let bagCmp :: [a] -> [a] -> Bool
bagCmp [a]
a [a]
b = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ([a]
a forall a. Eq a => a -> a -> Bool
==) forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [[a]]
L.permutations [a]
b
             in forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ Expectation -> String
expectedFile Expectation
e1 forall a. Eq a => a -> a -> Bool
== Expectation -> String
expectedFile Expectation
e2
                    , (forall {a}. Eq a => [a] -> [a] -> Bool
bagCmp forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Expectation -> [NamedParamMatch]
expParamsMatch) Expectation
e1 Expectation
e2
                    , (forall {a}. Eq a => [a] -> [a] -> Bool
bagCmp forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Expectation -> [(String, String)]
associated) Expectation
e1 Expectation
e2
                    ]

instance Pretty Expectation where
  pretty :: forall ann. Expectation -> Doc ann
pretty Expectation
exp =
    let p :: [NamedParamMatch]
p = Expectation -> [NamedParamMatch]
expParamsMatch Expectation
exp
        pp :: Maybe (Doc ann)
pp = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [NamedParamMatch]
p
             then forall a. Maybe a
Nothing
             else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Doc ann
"Matched Params:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> (forall ann. Doc ann -> Doc ann
align forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {a} {a} {ann}. (Pretty a, Pretty a) => (a, a) -> Doc ann
ppp [NamedParamMatch]
p)
        ppp :: (a, a) -> Doc ann
ppp (a
n,a
v) = forall a ann. Pretty a => a -> Doc ann
pretty a
n forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall ann. Doc ann
equals forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty a
v
        a :: [(String, String)]
a = Expectation -> [(String, String)]
associated Expectation
exp
        pa :: Maybe (Doc ann)
pa = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(String, String)]
a
             then forall a. Maybe a
Nothing
             else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Doc ann
"Associated:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> (forall ann. Doc ann -> Doc ann
align forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a ann. Pretty a => a -> Doc ann
pretty [(String, String)]
a)
    in forall ann. Int -> Doc ann -> Doc ann
hang Int
4 forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a. [Maybe a] -> [a]
catMaybes
       [ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Doc ann
"Expected: " forall ann. Doc ann -> Doc ann -> Doc ann
<+> (forall ann. Doc ann -> Doc ann
align forall a b. (a -> b) -> a -> b
$ forall a ann. Pretty a => a -> Doc ann
pretty (Expectation -> String
expectedFile Expectation
exp))
       , forall {ann}. Maybe (Doc ann)
pp
       , forall {ann}. Maybe (Doc ann)
pa
       ]

-- | Indicates the matching parameter value for this identified
-- expected test.  If the parameter value is explicitly specified in
-- the expected filename, it is an 'Explicit' entry, otherwise it is
-- 'Assumed' (for each of the valid 'ParameterPattern' values) or
-- NotSpecified if there are no known 'ParameterPattern' values.

data ParamMatch =
  -- | This parameter value was not specified in the filename for the
  -- expected file.  In addition, the associated 'ParameterPattern'
  -- specified no defined values (i.e. 'Nothing'), so it is not
  -- possible to identify any actual values.  Instead, the
  -- 'Expectation' generated for this expected file will supply this
  -- 'NotSpecified' for this type of parameter.
  NotSpecified

  -- | This parameter value was not specified in the filename of the
  -- expected file, so the value is being synthetically supplied.
  -- This is used for parameters that have known values but none is
  -- present: an 'Expectation' is created for each possible parameter
  -- value, identifying each as 'Assumed'.
  | Assumed String

  -- | This parameter value was explicitly specified in the filename
  -- of the expected file.
  | Explicit String

  deriving (Int -> ParamMatch -> ShowS
[ParamMatch] -> ShowS
ParamMatch -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParamMatch] -> ShowS
$cshowList :: [ParamMatch] -> ShowS
show :: ParamMatch -> String
$cshow :: ParamMatch -> String
showsPrec :: Int -> ParamMatch -> ShowS
$cshowsPrec :: Int -> ParamMatch -> ShowS
Show, ParamMatch -> ParamMatch -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParamMatch -> ParamMatch -> Bool
$c/= :: ParamMatch -> ParamMatch -> Bool
== :: ParamMatch -> ParamMatch -> Bool
$c== :: ParamMatch -> ParamMatch -> Bool
Eq, Eq ParamMatch
ParamMatch -> ParamMatch -> Bool
ParamMatch -> ParamMatch -> Ordering
ParamMatch -> ParamMatch -> ParamMatch
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ParamMatch -> ParamMatch -> ParamMatch
$cmin :: ParamMatch -> ParamMatch -> ParamMatch
max :: ParamMatch -> ParamMatch -> ParamMatch
$cmax :: ParamMatch -> ParamMatch -> ParamMatch
>= :: ParamMatch -> ParamMatch -> Bool
$c>= :: ParamMatch -> ParamMatch -> Bool
> :: ParamMatch -> ParamMatch -> Bool
$c> :: ParamMatch -> ParamMatch -> Bool
<= :: ParamMatch -> ParamMatch -> Bool
$c<= :: ParamMatch -> ParamMatch -> Bool
< :: ParamMatch -> ParamMatch -> Bool
$c< :: ParamMatch -> ParamMatch -> Bool
compare :: ParamMatch -> ParamMatch -> Ordering
$ccompare :: ParamMatch -> ParamMatch -> Ordering
Ord)

instance Pretty ParamMatch where
  pretty :: forall ann. ParamMatch -> Doc ann
pretty (Explicit String
s) = forall a ann. Pretty a => a -> Doc ann
pretty String
s
  pretty (Assumed String
s)  = forall ann. Doc ann -> Doc ann
brackets forall a b. (a -> b) -> a -> b
$ forall a ann. Pretty a => a -> Doc ann
pretty String
s
  pretty ParamMatch
NotSpecified = Doc ann
"*"


-- | The 'paramMatchVal' function is used to determine if a specific
-- value matches the corresponding 'ParamMatch'
paramMatchVal :: String -> ParamMatch -> Bool
paramMatchVal :: String -> ParamMatch -> Bool
paramMatchVal String
v (Explicit String
s) = String
s forall a. Eq a => a -> a -> Bool
== String
v
paramMatchVal String
v (Assumed String
s) = String
s forall a. Eq a => a -> a -> Bool
== String
v
paramMatchVal String
_ ParamMatch
NotSpecified = Bool
True


-- | Predicate test returning true for Explicit param values.
isExplicit :: ParamMatch -> Bool
isExplicit :: ParamMatch -> Bool
isExplicit = \case
  Explicit String
_ -> Bool
True
  ParamMatch
_ -> Bool
False


-- | Extracts explicit value or Nothing
getExplicit :: ParamMatch -> Maybe String
getExplicit :: ParamMatch -> Maybe String
getExplicit (Explicit String
v) = forall a. a -> Maybe a
Just String
v
getExplicit ParamMatch
_            = forall a. Maybe a
Nothing

-- | If there is a value associated with this parameter, return the value,
-- regardless of whether it is Explicit or Assumed.  A wildcard is a Nothing
-- return.
getParamVal :: ParamMatch -> Maybe String
getParamVal :: ParamMatch -> Maybe String
getParamVal (Explicit String
v) = forall a. a -> Maybe a
Just String
v
getParamVal (Assumed String
v) = forall a. a -> Maybe a
Just String
v
getParamVal ParamMatch
_            = forall a. Maybe a
Nothing

-- | Returns a value indicating how "strong" a set of ParamMatch values is.  This
-- is used to compare between sets of ParamMatches to prefer stronger matches
-- over weaker matches.
matchStrength :: [ParamMatch] -> Natural
matchStrength :: [ParamMatch] -> Natural
matchStrength = \case
  [] -> Natural
0
  (ParamMatch
NotSpecified : [ParamMatch]
ps) -> [ParamMatch] -> Natural
matchStrength [ParamMatch]
ps
  ((Explicit String
_) : [ParamMatch]
ps) -> Natural
1 forall a. Num a => a -> a -> a
+ [ParamMatch] -> Natural
matchStrength [ParamMatch]
ps
  ((Assumed String
_) : [ParamMatch]
ps) -> Natural
1 forall a. Num a => a -> a -> a
+ [ParamMatch] -> Natural
matchStrength [ParamMatch]
ps


----------------------------------------------------------------------

-- | The 'SweetExplanation' is the data type that contains the
-- description of the 'Test.Tasty.Sugar.findSugar' process and
-- results.
data SweetExplanation =
  SweetExpl { SweetExplanation -> String
rootPath :: FilePath
            , SweetExplanation -> String
base :: String
            , SweetExplanation -> [String]
expectedNames :: [String]  -- ^ candidates
            , SweetExplanation -> Sweets
results :: Sweets -- ^ actual results
            }

instance Pretty SweetExplanation where
  pretty :: forall ann. SweetExplanation -> Doc ann
pretty SweetExplanation
expl =
    let nms :: [String]
nms = SweetExplanation -> [String]
expectedNames SweetExplanation
expl
    in forall ann. Doc ann -> Doc ann
align forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a. [Maybe a] -> [a]
catMaybes [
      forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
fillSep forall a b. (a -> b) -> a -> b
$ forall ann. Doc ann -> [Doc ann] -> [Doc ann]
punctuate Doc ann
","
        [ Doc ann
"rootPath" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall ann. Doc ann -> Doc ann
dquotes (forall a ann. Pretty a => a -> Doc ann
pretty forall a b. (a -> b) -> a -> b
$ SweetExplanation -> String
rootPath SweetExplanation
expl)
        , Doc ann
"base" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall ann. Doc ann -> Doc ann
dquotes (forall a ann. Pretty a => a -> Doc ann
pretty forall a b. (a -> b) -> a -> b
$ SweetExplanation -> String
base SweetExplanation
expl)
        , if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
nms
          then Doc ann
"no matches"
          else (forall a ann. Pretty a => a -> Doc ann
pretty forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
nms) forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"possible matches"
        ]
      , if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
nms
        then forall a. Maybe a
Nothing
        else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall ann. Int -> Doc ann -> Doc ann
indent Int
8 forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a ann. Pretty a => a -> Doc ann
pretty [String]
nms
      , forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a ann. Pretty a => a -> Doc ann
pretty forall a b. (a -> b) -> a -> b
$ SweetExplanation -> Sweets
results SweetExplanation
expl
    ]

------------------------------------------------------------------------