-- | Useful to test encoding of data stored in redis.
module Test.Encoding.Redis (test) where

import Data.Proxy (Proxy (Proxy))
import qualified Data.Typeable as Typeable
import qualified Examples
import NriPrelude
import Test (Test)
import qualified Test.Encoding
import qualified Text

-- | Turns a Redis.Api into a test.
-- The test does the following:
--
-- 1. get examples from the `HasExamples` constraint
-- 2. encoded the examples into JSON
-- 3. check the encoded JSON against the generated file
--   NOTE: it will generate the file if it doesn't exist yet
test :: forall m key a. (Typeable.Typeable a, Examples.HasExamples a) => m key a -> Test
test :: m key a -> Test
test m key a
_ =
  let proxy :: Proxy a
proxy = Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a
      tyCon :: TyCon
tyCon =
        Proxy a -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
Typeable.typeRep Proxy a
proxy
          TypeRep -> (TypeRep -> TyCon) -> TyCon
forall a b. a -> (a -> b) -> b
|> TypeRep -> TyCon
Typeable.typeRepTyCon
      typeName :: Text
typeName =
        TyCon -> String
Typeable.tyConModule TyCon
tyCon String -> String -> String
forall appendable.
Semigroup appendable =>
appendable -> appendable -> appendable
++ String
"." String -> String -> String
forall appendable.
Semigroup appendable =>
appendable -> appendable -> appendable
++ TyCon -> String
Typeable.tyConName TyCon
tyCon
          String -> (String -> Text) -> Text
forall a b. a -> (a -> b) -> b
|> String -> Text
Text.fromList
   in Proxy a -> Examples
forall k (t :: k). HasExamples t => Proxy t -> Examples
Examples.examples Proxy a
proxy
        Examples -> (Examples -> Test) -> Test
forall a b. a -> (a -> b) -> b
|> Text -> Text -> Examples -> Test
Test.Encoding.examplesToTest Text
typeName (Text
"redis-encoding-" Text -> Text -> Text
forall appendable.
Semigroup appendable =>
appendable -> appendable -> appendable
++ Text
typeName)