Skip to content

Commit ee3c624

Browse files
authored
Show and Eq instances for Html (#16)
* Show and Eq instances for Html * suggested changes
1 parent 0cd8f51 commit ee3c624

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Flame/Types.purs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Flame.Types where
33

44
import Prelude
55

6+
import Data.Array as DA
7+
import Data.Foldable as DF
68
import Data.Tuple (Tuple(..))
79
import Effect (Effect)
810
import Foreign.Object (Object)
@@ -59,6 +61,24 @@ data Html message =
5961

6062
derive instance elementFunctor :: Functor Html
6163

64+
65+
instance showHtml :: Show (Html message) where
66+
show (Node tag nodeData children) = "(Node " <> tag <> " " <> show (isNonEventData nodeData) <> " " <> show children <> ")"
67+
show (Text t) = "(Text " <> t <> ")"
68+
69+
instance eqHtml :: Eq (Html message) where
70+
eq (Node tag nodeData children) (Node tag2 nodeData2 children2) = tag == tag2 && eqArrayNodeData nodeData nodeData2 && children == children2
71+
where eqArrayNodeData arr1 arr2 = DF.all (flip DF.elem (isNonEventData arr2)) (isNonEventData arr1)
72+
eq (Text t) (Text t2) = t == t2
73+
eq _ _ = false
74+
75+
76+
isNonEventData :: forall t. Array (NodeData t) -> Array (NodeData t)
77+
isNonEventData = DA.filter case _ of
78+
Attribute _ _ -> true
79+
Property _ _ -> true
80+
_ -> false
81+
6282
-- | Convenience wrapper around `VNodeData`
6383
--snabbom has support for style and class node data but I dont think it is worth it
6484
data NodeData message =
@@ -69,5 +89,16 @@ data NodeData message =
6989

7090
derive instance nodeDataFunctor :: Functor NodeData
7191

92+
instance showNodeData :: Show (NodeData message) where
93+
show (Attribute name val) = "(Attribute " <> name <> " " <> val <> ")"
94+
show (Property name val) = "(Property " <> name <> " " <> val <> ")"
95+
show _ = ""
96+
97+
instance eqNodeData :: Eq (NodeData message) where
98+
eq (Attribute name val) (Attribute name2 val2) = name == name2 && val == val2
99+
eq (Property name val) (Property name2 val2) = name == name2 && val == val2
100+
eq _ _ = false
101+
102+
72103
-- | Infix tuple constructor
73104
infixr 6 Tuple as :>

test/Main.purs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import Effect.Class (liftEffect)
1515
import Flame.Application.DOM as FAD
1616
import Flame.Application.Effectful as FAE
1717
import Flame.HTML.Attribute as HA
18-
import Test.Basic.ContentEditable as TBC
1918
import Flame.HTML.Element as HE
2019
import Flame.Renderer.String as FRS
2120
import Partial.Unsafe (unsafePartial)
2221
import Partial.Unsafe as PU
2322
import Signal.Channel as SC
23+
import Test.Basic.ContentEditable as TBC
2424
import Test.Basic.EffectList as TBEL
2525
import Test.Basic.Effectful as TBE
2626
import Test.Basic.NoEffects as TBN
@@ -57,6 +57,8 @@ foreign import keydownEvent :: Effect Event
5757
foreign import errorEvent :: Effect Event
5858
foreign import offlineEvent :: Effect Event
5959

60+
61+
6062
main :: Effect Unit
6163
main =
6264
runTest do
@@ -194,6 +196,29 @@ main =
194196
html' <- liftEffect $ FRS.render html
195197
--events are part of virtual dom data and do not show up on the rendered markup
196198
TUA.equal """<a>TEST</a>""" html'
199+
suite "show" do
200+
test "simple element" do
201+
let html = HE.div [HA.id "1"] [HE.text "T"]
202+
TUA.equal """(Node div [(Property id 1)] [(Text T)])""" $ show $ html
203+
test "events do not matter" do
204+
let html = HE.div [HA.id "1", HA.onClick "Test"] [HE.text "T"]
205+
TUA.equal """(Node div [(Property id 1)] [(Text T)])""" $ show $ html
206+
test "element with childs" do
207+
let html = HE.div_ [HE.div_ [HE.br]]
208+
TUA.equal """(Node div [] [(Node div [] [(Node br [] [])])])""" $ show $ html
209+
suite "eq" do
210+
test "simple element" do
211+
TUA.equal' "equal html" (HE.div [HA.id "1"] [HE.text "T"]) (HE.div [HA.id "1"] [HE.text "T"])
212+
TUA.assert "diffent property" $ (HE.div [HA.id "1"] [HE.text "T"]) /= (HE.div [HA.id "2"] [HE.text "T"])
213+
test "events do not matter" do
214+
TUA.equal' "equal html" (HE.div [HA.id "1", HA.onClick unit] [HE.text "T"]) (HE.div [HA.id "1"] [HE.text "T"])
215+
test "property order does not matter" do
216+
TUA.assert "should equal" $
217+
(HE.div [HA.class' "test", HA.id "1"] [HE.text "T"]) == (HE.div [HA.id "1", HA.class' "test"] [HE.text "T"])
218+
test "child order does matter" do
219+
TUA.assert "should not equal" $
220+
(HE.div_ [HE.text "T", HE.br]) /= (HE.div_ [HE.br, HE.text "T"])
221+
197222
suite "diff" do
198223
test "updates record fields" do
199224
TUA.equal { a: 23, b: "hello", c: true } $ FAE.diff' {c: true} { a : 23, b: "hello", c: false }

0 commit comments

Comments
 (0)