aboutsummaryrefslogtreecommitdiff
path: root/js/test
diff options
context:
space:
mode:
Diffstat (limited to 'js/test')
-rw-r--r--js/test/Main.purs61
1 files changed, 61 insertions, 0 deletions
diff --git a/js/test/Main.purs b/js/test/Main.purs
new file mode 100644
index 0000000..e23f9e2
--- /dev/null
+++ b/js/test/Main.purs
@@ -0,0 +1,61 @@
+module Test.Main
+ ( main
+ ) where
+
+import Control.Monad.Eff (Eff)
+import Data.Maybe (Maybe(Just, Nothing))
+import Prelude
+import Test.Spec (describe, it)
+import Test.Spec.Assertions (shouldEqual)
+import Test.Spec.Reporter.Console (consoleReporter)
+import Test.Spec.Runner (RunnerEffects, run)
+
+import Number (roundAt, format) as Number
+import Parser (TextWithNumber)
+import Parser (textWithNumber, number) as Parser
+
+main :: Eff (RunnerEffects ()) Unit
+main = run [consoleReporter] do
+
+ describe "Number" do
+ it "rounds numbers" $ do
+ (Number.roundAt 0 0.0) `shouldEqual` (0.0)
+ (Number.roundAt 5 0.0) `shouldEqual` (0.0)
+ (Number.roundAt 0 12.3456) `shouldEqual` (12.0)
+ (Number.roundAt 1 12.3456) `shouldEqual` (12.3)
+ (Number.roundAt 2 12.3456) `shouldEqual` (12.35)
+ (Number.roundAt 3 12.3456) `shouldEqual` (12.346)
+ (Number.roundAt 4 12.3456) `shouldEqual` (12.3456)
+ (Number.roundAt 5 12.3456) `shouldEqual` (12.3456)
+
+ it "formats numbers" $ do
+ (Number.format 0.0) `shouldEqual` "0"
+ (Number.format 0.1) `shouldEqual` "0,1"
+ (Number.format 12.3456) `shouldEqual` "12,3"
+ (Number.format 12.9) `shouldEqual` "12,9"
+ (Number.format 12.99) `shouldEqual` "13,0"
+ (Number.format 123456.0) `shouldEqual` "123456"
+ (Number.format 1234.5678) `shouldEqual` "1234,6"
+
+ describe "Parser" do
+ it "parses number" $ do
+ (Parser.number "") `shouldEqual` Nothing
+ (Parser.number "auie") `shouldEqual` Nothing
+ (Parser.number "13.8auie") `shouldEqual` Nothing
+ (Parser.number "13.") `shouldEqual` Nothing
+ (Parser.number ".8") `shouldEqual` Nothing
+ (Parser.number "13") `shouldEqual` (Just 13.0)
+ (Parser.number "13,8") `shouldEqual` (Just 13.8)
+ (Parser.number "13.8") `shouldEqual` (Just 13.8)
+ (Parser.number "123 456") `shouldEqual` Nothing
+
+ it "parses textWithNumber" $ do
+ map showTwn (Parser.textWithNumber "") `shouldEqual` Nothing
+ map showTwn (Parser.textWithNumber "0") `shouldEqual` Just (showTwn { begin: "", number: 0.0, end: "" })
+ map showTwn (Parser.textWithNumber "13,8") `shouldEqual` Just (showTwn { begin: "", number: 13.8, end: "" })
+ map showTwn (Parser.textWithNumber "begin 0 end") `shouldEqual` Just (showTwn { begin: "begin ", number: 0.0, end: " end" })
+ map showTwn (Parser.textWithNumber "14end") `shouldEqual` Just (showTwn { begin: "", number: 14.0, end: "end" })
+ map showTwn (Parser.textWithNumber "begin14") `shouldEqual` Just (showTwn { begin: "begin", number: 14.0, end: "" })
+
+showTwn :: TextWithNumber -> String
+showTwn { begin, number, end } = begin <> show number <> end