@@ -2,45 +2,47 @@ module Test.Main where
22
33import Prelude
44
5+ import Control.Monad.Reader.Class (class MonadReader , ask , local )
6+ import Control.Monad.Reader.Trans (runReaderT )
57import Data.Array ((!!), length )
6- import Data.Either (fromRight )
8+ import Data.Either (Either , fromRight' )
79import Data.Int (toNumber )
810import Data.Maybe (Maybe (..), fromJust , fromMaybe )
911import Data.Natural (intToNat )
10- -- import Debug.Trace (traceM)
1112import Effect (Effect )
12- import Effect.Aff (Aff )
13- import Effect.Class (liftEffect )
14- import Effect.Console (logShow )
13+ import Effect.Aff (launchAff_ )
14+ import Effect.Class (class MonadEffect , liftEffect )
15+ import Effect.Console (logShow , log )
1516import Foreign (isUndefined , isNull , unsafeToForeign )
16- import Partial.Unsafe (unsafePartial )
17+ import Partial.Unsafe (unsafePartial , unsafeCrashWith )
18+ import Test.Assert as Assert
1719import Test.Data as TD
18- import Test.Unit (suite , test )
19- import Test.Unit.Main (runTest )
20- import Test.Unit.Assert as Assert
2120
2221import Web.DOM.Document (Document , toNode )
23- import Web.DOM.DOMParser (DOMParser , makeDOMParser , parseXMLFromString )
2422import Web.DOM.Document.XPath (NSResolver )
2523import Web.DOM.Document.XPath as XP
2624import Web.DOM.Document.XPath.ResultType as RT
25+ import Web.DOM.DOMParser (DOMParser , makeDOMParser , parseXMLFromString )
2726import Web.DOM.Element (Element , fromNode , getAttribute )
2827import Web.DOM.Node (Node , nodeName )
2928
29+ unsafeFromRight :: forall l r . Either l r -> r
30+ unsafeFromRight = fromRight' (\_ -> unsafeCrashWith " Value was not Right" )
31+
3032parseAtomFeedDoc :: DOMParser -> Effect Document
31- parseAtomFeedDoc dp = unsafePartial $ map fromRight $
33+ parseAtomFeedDoc dp = map unsafeFromRight $
3234 parseXMLFromString TD .atomFeedXml dp
3335
3436parseCatalogDoc :: DOMParser -> Effect Document
35- parseCatalogDoc dp = unsafePartial $ map fromRight $
37+ parseCatalogDoc dp = map unsafeFromRight $
3638 parseXMLFromString TD .cdCatalogXml dp
3739
3840parseNoteDoc :: DOMParser -> Effect Document
39- parseNoteDoc dp = unsafePartial $ map fromRight $
41+ parseNoteDoc dp = map unsafeFromRight $
4042 parseXMLFromString TD .noteXml dp
4143
4244parseMetajeloDoc :: DOMParser -> Effect Document
43- parseMetajeloDoc dp = unsafePartial $ map fromRight $
45+ parseMetajeloDoc dp = map unsafeFromRight $
4446 parseXMLFromString TD .metajeloXml dp
4547
4648atomResolver :: NSResolver
@@ -72,16 +74,43 @@ getMetajeloResolver node doc = do
7274 Nothing -> defNS
7375 Just ns -> ns
7476
75- mkCdYear :: Document -> Node -> Aff String
77+ mkCdYear :: forall m . MonadEffect m => Document -> Node -> m String
7678mkCdYear doc node = liftEffect $ XP .evaluateString
7779 " YEAR"
7880 node
7981 Nothing
8082 Nothing
8183 doc
8284
85+ -- ---------------------------------------------------------------
86+
87+ -- Provide similar API to purescript-spec to reduce code changes
88+
89+ suite :: forall m . MonadReader String m => MonadEffect m => String -> m Unit -> m Unit
90+ suite msg runTest = do
91+ previous <- ask
92+ let testName = previous <> msg
93+ liftEffect $ log testName
94+ local (_ <> " " ) runTest
95+
96+ test :: forall m . MonadReader String m => MonadEffect m => String -> m Unit -> m Unit
97+ test = suite
98+
99+ -- Replaces `test-unit`'s `Test.Unit.Assert.equal`, which has its first
100+ -- arg be the expected value and the second arg be the actual value.
101+ -- See `Test.Unit.Assert.shouldEqual` for proof.
102+ shouldEqual :: forall m a . MonadEffect m => Eq a => Show a => a -> a -> m Unit
103+ shouldEqual expected actual =
104+ liftEffect $ Assert .assertEqual { actual, expected }
105+
106+ assertFalse :: forall m . MonadEffect m => String -> Boolean -> m Unit
107+ assertFalse msg val =
108+ liftEffect $ Assert .assertFalse' msg val
109+
110+ -- ---------------------------------------------------------------
111+
83112main :: { browser :: Boolean } -> Effect Unit
84- main { browser } = runTest do
113+ main { browser } = launchAff_ $ flip runReaderT " " do
85114 suite " non-namespaced tests" do
86115 test " note.xml and catalog.xml" do
87116 domParser <- liftEffect $ makeDOMParser
@@ -100,22 +129,22 @@ main { browser } = runTest do
100129 " /note/to" note Nothing RT .string_type Nothing noteDoc
101130 noteTo <- liftEffect $ XP .stringValue noteToRes
102131 tlog $ " got a note to: " <> noteTo
103- Assert .equal RT .string_type (XP .resultType noteToRes)
104- Assert .equal " Tove" noteTo
132+ shouldEqual RT .string_type (XP .resultType noteToRes)
133+ shouldEqual " Tove" noteTo
105134
106135 cdPriceRes <- liftEffect $ XP .evaluate
107136 " /CATALOG/CD[2]/PRICE" catalog Nothing RT .number_type Nothing catalogDoc
108137 cdPrice <- liftEffect $ XP .numberValue cdPriceRes
109138 tlog $ " got a cd price: " <> (show cdPrice)
110- Assert .equal RT .number_type (XP .resultType cdPriceRes)
111- Assert .equal 9.90 cdPrice
139+ shouldEqual RT .number_type (XP .resultType cdPriceRes)
140+ shouldEqual 9.90 cdPrice
112141
113142 cdYearRes <- liftEffect $ XP .evaluate
114143 " /CATALOG/CD[2]/YEAR" catalog Nothing RT .number_type Nothing catalogDoc
115144 cdYear <- liftEffect $ XP .numberValue cdYearRes
116145 tlog $ " got a cd year: " <> (show cdYear)
117- Assert .equal RT .number_type (XP .resultType cdYearRes)
118- Assert .equal (toNumber 1988 ) cdYear
146+ shouldEqual RT .number_type (XP .resultType cdYearRes)
147+ shouldEqual (toNumber 1988 ) cdYear
119148
120149 cdsSnapRes <- liftEffect $ XP .evaluate
121150 " /CATALOG/CD"
@@ -126,42 +155,42 @@ main { browser } = runTest do
126155 catalogDoc
127156 cdsSnapLen <- liftEffect $ XP .snapshotLength cdsSnapRes
128157 tlog $ " got " <> (show cdsSnapLen) <> " CDs"
129- Assert .equal (intToNat 26 ) cdsSnapLen
158+ shouldEqual (intToNat 26 ) cdsSnapLen
130159 cdsSnap <- liftEffect $ XP .snapshot cdsSnapRes
131160 cdYearEval <- pure $ mkCdYear catalogDoc
132- Assert .equal 26 (length cdsSnap)
161+ shouldEqual 26 (length cdsSnap)
133162 year0 <- cdYearEval $ unsafePartial $ fromJust $ cdsSnap !! 0
134- Assert .equal " 1985" year0
163+ shouldEqual " 1985" year0
135164 year1 <- cdYearEval $ unsafePartial $ fromJust $ cdsSnap !! 1
136- Assert .equal " 1988" year1
165+ shouldEqual " 1988" year1
137166 year25 <- cdYearEval $ unsafePartial $ fromJust $ cdsSnap !! 25
138- Assert .equal " 1987" year25
167+ shouldEqual " 1987" year25
139168
140169 suite " namespaced tests" do
141170 test " NS resolver construction" do
142171 domParser <- liftEffect $ makeDOMParser
143172
144173 customRes <- pure $ XP .customNSResolver (\x -> " http://foo.com" )
145174
146- Assert . assertFalse " custom NS resolver shouldn't be undefined"
175+ assertFalse " custom NS resolver shouldn't be undefined"
147176 (isUndefined $ unsafeToForeign customRes)
148- Assert . assertFalse " custom NS resolver shouldn't be null"
177+ assertFalse " custom NS resolver shouldn't be null"
149178 (isNull $ unsafeToForeign customRes)
150179
151180 when browser $ do
152181 atomFeedDoc <- liftEffect $ parseAtomFeedDoc domParser
153182 atomFeed <- pure $ toNode atomFeedDoc
154183
155184 createdNSResolver <- pure $ XP .createNSResolver atomFeed atomFeedDoc
156- Assert . assertFalse " created NS resolver shouldn't be undefined"
185+ assertFalse " created NS resolver shouldn't be undefined"
157186 (isUndefined $ unsafeToForeign createdNSResolver)
158- Assert . assertFalse " created NS resolver shouldn't be null"
187+ assertFalse " created NS resolver shouldn't be null"
159188 (isNull $ unsafeToForeign createdNSResolver)
160189
161190 defNSResolver <- liftEffect $ XP .defaultNSResolver atomFeed atomFeedDoc
162- Assert . assertFalse " default NS resolver shouldn't be undefined"
191+ assertFalse " default NS resolver shouldn't be undefined"
163192 (isUndefined $ unsafeToForeign defNSResolver)
164- Assert . assertFalse " default NS resolver shouldn't be null"
193+ assertFalse " default NS resolver shouldn't be null"
165194 (isNull $ unsafeToForeign defNSResolver)
166195
167196 test " atom.xml" $ when browser do
@@ -179,7 +208,7 @@ main { browser } = runTest do
179208 atomFeedDoc
180209 atomEntriesLen <- liftEffect $ XP .snapshotLength atomEntriesRes
181210 tlog $ " got " <> (show atomEntriesLen) <> " atom entries"
182- Assert .equal (intToNat 3 ) atomEntriesLen
211+ shouldEqual (intToNat 3 ) atomEntriesLen
183212
184213 test " metajelo.xml" do
185214 domParser <- liftEffect $ makeDOMParser
@@ -198,8 +227,8 @@ main { browser } = runTest do
198227 metajeloDoc
199228 metajeloId <- liftEffect $ XP .stringValue metajeloIdRes
200229 tlog $ " got metajelo id" <> metajeloId
201- Assert .equal RT .string_type (XP .resultType metajeloIdRes)
202- when browser $ Assert .equal " OjlTjf" metajeloId
230+ shouldEqual RT .string_type (XP .resultType metajeloIdRes)
231+ when browser $ shouldEqual " OjlTjf" metajeloId
203232
204233 prod0pol0xpath <- pure $
205234 " /x:record/x:supplementaryProducts/x:supplementaryProduct[1]" <>
@@ -214,8 +243,8 @@ main { browser } = runTest do
214243 metajeloDoc
215244 mjProd0Pol0 <- liftEffect $ XP .stringValue mjProd0Pol0Res
216245 tlog $ " got metajelo ref policy " <> mjProd0Pol0
217- Assert .equal RT .string_type (XP .resultType mjProd0Pol0Res)
218- when browser $ Assert .equal " http://skGHargw/" mjProd0Pol0
246+ shouldEqual RT .string_type (XP .resultType mjProd0Pol0Res)
247+ when browser $ shouldEqual " http://skGHargw/" mjProd0Pol0
219248 --
220249 mjProd0Pol0AppliesRes <- liftEffect $ XP .evaluate
221250 (prod0pol0xpath <> " /@appliesToProduct" )
@@ -226,8 +255,8 @@ main { browser } = runTest do
226255 metajeloDoc
227256 mjProd0Pol0Applies <- liftEffect $ XP .stringValue mjProd0Pol0AppliesRes
228257 tlog $ " got metajelo policy appliesToProduct: " <> (show mjProd0Pol0Applies)
229- Assert .equal RT .string_type (XP .resultType mjProd0Pol0AppliesRes)
230- when browser $ Assert .equal " 0" mjProd0Pol0Applies
258+ shouldEqual RT .string_type (XP .resultType mjProd0Pol0AppliesRes)
259+ when browser $ shouldEqual " 0" mjProd0Pol0Applies
231260
232- tlog :: forall a . Show a => a -> Aff Unit
261+ tlog :: forall a m . MonadEffect m => Show a => a -> m Unit
233262tlog = liftEffect <<< logShow
0 commit comments