-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGlobals.purs
More file actions
92 lines (89 loc) · 3.54 KB
/
Copy pathGlobals.purs
File metadata and controls
92 lines (89 loc) · 3.54 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module Test.Globals (testGlobals) where
import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console (log)
import Global
( readFloat
, readInt
, isFinite
, infinity
, nan
, isNaN
, toPrecision
, toExponential
, toFixed
, decodeURI
, encodeURI
, decodeURIComponent
, encodeURIComponent
)
import Global.Unsafe (unsafeToPrecision, unsafeToExponential, unsafeToFixed)
import Test.Assert (assert)
testGlobals :: Effect Unit
testGlobals = do
let
num = 12345.6789
log "nan /= nan"
assert $ nan /= nan
log "not (isNaN 6.0)"
assert $ not (isNaN 6.0)
log "isNaN nan"
assert $ isNaN nan
log "infinity > 0.0"
assert $ infinity > 0.0
log "-infinity < 0.0"
assert $ -infinity < 0.0
log "not (isFinite infinity)"
assert $ not (isFinite infinity)
log "isFinite 0.0"
assert $ isFinite 0.0
log "readInt 16 \"0xFF\" == 255.0"
assert $ readInt 16 "0xFF" == 255.0
log "readInt 10 \"3.5\" == 3.0"
assert $ readInt 10 "3.5" == 3.0
log "readFloat \"3.5\" == 3.5"
assert $ readFloat "3.5" == 3.5
-- note the rounding
log $ "unsafeToFixed 1" <> (show num) <> " == \"12345.7\""
assert $ unsafeToFixed 1 num == "12345.7"
-- padded with zeros
log $ "unsafeToFixed 6" <> (show num) <> " == \"12345.678900\""
assert $ unsafeToFixed 6 num == "12345.678900"
log $ "unsafeToExponential 4" <> (show num) <> " == \"1.2346e+4\""
assert $ unsafeToExponential 4 num == "1.2346e+4"
-- TODO: fix toPrecision problems here
-- log $ "unsafeToPrecision 3" <> (show num) <> " == \"1.23e+4\""
-- assert $ unsafeToPrecision 3 num == "1.23e+4"
-- log $ "unsafeToPrecision 6" <> (show num) <> " == \"12345.7\""
-- assert $ unsafeToPrecision 6 num == "12345.7"
-- note the rounding
log $ "toFixed 1" <> (show num) <> " == (Just \"12345.7\")"
assert $ toFixed 1 num == Just "12345.7"
-- padded with zeros
log $ "toFixed 6" <> (show num) <> " == (Just \"12345.678900\")"
assert $ toFixed 6 num == Just "12345.678900"
log $ "toExponential 4" <> (show num) <> " == (Just \"1.2346e+4\")"
assert $ toExponential 4 num == Just "1.2346e+4"
-- TODO: fix toPrecision problems here
-- log $ "toPrecision 3" <> (show num) <> " == (Just \"1.23e+4\")"
-- assert $ toPrecision 3 num == Just "1.23e+4"
-- log $ "toPrecision 6" <> (show num) <> " == (Just \"12345.7\")"
-- assert $ toPrecision 6 num == Just "12345.7"
log $ "decodeURI \"http://test/api?q=hello%20world\" == Just \"http://test/api?q=hello world\""
assert $ decodeURI "http://test/api?q=hello%20world" == Just "http://test/api?q=hello world"
-- log $ "decodeURI \"http://test/api?q=hello%8\" == Nothing\""
-- assert $ decodeURI "http://test/api?q=hello%8" == Nothing
log $ "encodeURI \"http://test/api?q=hello world\" == Just \"http://test/api?q=hello%20world\""
assert $ encodeURI "http://test/api?q=hello world" == Just "http://test/api?q=hello%20world"
-- log $ "encodeURI \"http://test/api?q=" <> unencodable <> "\" == Nothing"
-- assert $ encodeURI ("http://test/api?q=" <> unencodable) == Nothing
log $ "decodeURIComponent \"hello%20world\" == Just \"hello world\""
assert $ decodeURIComponent "hello%20world" == Just "hello world"
-- log $ "decodeURIComponent \"hello%8\" == Nothing"
-- assert $ decodeURIComponent "hello%8" == Nothing
log $ "encodeURIComponent \"hello world\" == Just \"hello%20world\""
assert $ encodeURIComponent "hello world" == Just "hello%20world"
-- log $ "encodeURIComponent \"" <> unencodable <> "\" == Nothing"
-- assert $ encodeURIComponent unencodable == Nothing
foreign import unencodable :: String