File: T47Spec.hs

package info (click to toggle)
haskell-reflection 2.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 148 kB
  • sloc: haskell: 791; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,691 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
-- | A regression test for issue #47.
module T47Spec where

import qualified Data.Map as M
import Data.Map (Map)
import Data.Reflection
import Test.Hspec

main :: IO ()
main = hspec spec

spec :: Spec
spec =
  describe "Given" $ do
    it "should give Normal properly" $
      give Normal (toJSON (Foo Bar)) `shouldBe`
      Object (M.fromList [("Foo",String "Bar")])
    it "should give ViaShow properly" $
      give ViaShow (toJSON (Foo Bar)) `shouldBe`
      Object (M.fromList [("Foo",String "SHOWBAR")])

----------------------------------------------------------------------------
-- Types
----------------------------------------------------------------------------

data Foo = Foo Bar

instance Show Foo where
  show _ = "SHOWFOO"

data Bar = Bar | BarBar

instance Show Bar where
  show _ = "SHOWBAR"

----------------------------------------------------------------------------
-- ToJSON instances
----------------------------------------------------------------------------

instance Given Style => ToJSON Foo where
  toJSON (Foo x) = Object $ M.singleton "Foo" (toJSON x)

instance Given Style => ToJSON Bar where
  toJSON x = case given of
    Normal -> String $ case x of
                Bar    -> "Bar"
                BarBar -> "BarBar"
    ViaShow -> String $ show x

data Style = Normal | ViaShow

----------------------------------------------------------------------------
-- Minimized aeson
----------------------------------------------------------------------------

class ToJSON a where
  toJSON :: a -> Value

data Value
  = Object !(Map String Value)
  | String !String
  deriving (Eq, Show)