aboutsummaryrefslogtreecommitdiff
path: root/js/test/Main.purs
blob: e23f9e28565d2e6d7c6a2a2a9e65250eada1ea1c (plain)
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
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