Spec.purs 2.77 KB
Newer Older
1 2 3
module Gargantext.Utils.Spec where

import Prelude
4 5 6 7 8

import Data.Argonaut as Argonaut
import Data.Either (Either(..), isLeft)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
9
import Gargantext.Utils as GU
10
import Gargantext.Utils.Argonaut (genericSumDecodeJson, genericSumEncodeJson)
11 12
import Gargantext.Utils.Crypto as GUC
import Gargantext.Utils.Math as GUM
13 14
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
15 16 17 18 19 20 21 22 23 24 25 26 27 28

data Fruit
  = Boat { hi :: Int }
  | Gravy String
  | Pork Int

derive instance eqFruit :: Eq Fruit
derive instance genericFruit :: Generic Fruit _
instance showFruit :: Show Fruit where
  show = genericShow
instance decodeJsonFruit :: Argonaut.DecodeJson Fruit where
  decodeJson = genericSumDecodeJson
instance encodeJsonFruit :: Argonaut.EncodeJson Fruit where
  encodeJson = genericSumEncodeJson
29 30 31 32 33

spec :: Spec Unit
spec =
  describe "G.Utils" do
    it "zeroPad 1 works" do
34 35 36
      GU.zeroPad 1 0 `shouldEqual` "0"
      GU.zeroPad 1 1 `shouldEqual` "1"
      GU.zeroPad 1 10 `shouldEqual` "10"
37
    it "zeroPad 2 works" do
38 39 40 41
      GU.zeroPad 2 0 `shouldEqual` "00"
      GU.zeroPad 2 1 `shouldEqual` "01"
      GU.zeroPad 2 10 `shouldEqual` "10"
      GU.zeroPad 2 100 `shouldEqual` "100"
42
    it "zeroPad 3 works" do
43 44 45 46 47 48 49
      GU.zeroPad 3 0 `shouldEqual` "000"
      GU.zeroPad 3 1 `shouldEqual` "001"
      GU.zeroPad 3 10 `shouldEqual` "010"
      GU.zeroPad 3 99 `shouldEqual` "099"
      GU.zeroPad 3 100 `shouldEqual` "100"
      GU.zeroPad 3 101 `shouldEqual` "101"
      GU.zeroPad 3 1000 `shouldEqual` "1000"
50
    it "log10 10" do
51 52 53 54 55
      GUM.log10 10.0 `shouldEqual` 1.0
    it "md5 works" do
      let text = "The quick brown fox jumps over the lazy dog"
      let textMd5 = "9e107d9d372bb6826bd81d3542a419d6"
      GUC.md5 text `shouldEqual` textMd5
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    it "genericSumDecodeJson works" do
      let result1 = Argonaut.decodeJson =<< Argonaut.jsonParser """{"Boat":{"hi":1}}"""
      result1 `shouldEqual` Right (Boat { hi: 1 })

      let result2 = Argonaut.decodeJson =<< Argonaut.jsonParser """{"Gravy":"hi"}"""
      result2 `shouldEqual` Right (Gravy "hi")

      let result3 = Argonaut.decodeJson =<< Argonaut.jsonParser """{"Boat":123}"""
      isLeft (result3 :: Either String Fruit) `shouldEqual` true

    it "genericSumEncodeJson works and loops back with decode" do
      let input1 = Boat { hi: 1 }
      let result1 = Argonaut.encodeJson input1
      let result1' = Argonaut.decodeJson result1
      Argonaut.stringify result1 `shouldEqual` """{"Boat":{"hi":1}}"""
      result1' `shouldEqual` Right input1

      let input2 = Gravy "hi"
      let result2 = Argonaut.encodeJson input2
      let result2' = Argonaut.decodeJson result2
      Argonaut.stringify result2 `shouldEqual` """{"Gravy":"hi"}"""
      result2' `shouldEqual` Right input2