Skip to content

Commit

Permalink
Add some basic tests for this library (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntwilson authored Jul 28, 2021
1 parent 3816f11 commit 42819f2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
Bugfixes:

Other improvements:
- Added tests (#20 by @ntwilson)

## [v5.0.0](https://github.com/purescript-contrib/purescript-now/releases/tag/v5.0.0) - 2021-02-26

Expand Down
8 changes: 7 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"purescript-effect": "^3.0.0"
},
"devDependencies": {
"purescript-console": "^5.0.0"
"purescript-assert": "^5.0.0",
"purescript-console": "^5.0.0",
"purescript-either": "^5.0.0",
"purescript-exceptions": "^5.0.0",
"purescript-node-process": "^8.0.0",
"purescript-prelude": "^5.0.0",
"purescript-transformers": "^5.0.0"
}
}
13 changes: 12 additions & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{ name = "now"
, dependencies = [ "console", "datetime", "effect", "psci-support" ]
, dependencies =
[ "assert"
, "console"
, "datetime"
, "effect"
, "either"
, "exceptions"
, "node-process"
, "prelude"
, "psci-support"
, "transformers"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
59 changes: 56 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,63 @@ module Test.Main where

import Prelude

import Control.Monad.Error.Class (try)
import Control.Monad.State (StateT, execStateT, lift, modify)
import Data.Either (Either(..))
import Data.Time.Duration (Minutes(..))
import Effect (Effect)
import Effect.Class.Console (log)
import Effect.Class.Console (log, logShow)
import Effect.Exception (throw)
import Effect.Now (getTimezoneOffset, now, nowDate, nowTime)
import Node.Process (exit)
import Test.Assert (assert, assert')

type FailedTestCount = Int
type Test = StateT FailedTestCount Effect Unit

test :: String -> Effect Unit -> Test
test name body =
try (lift body) >>= case _ of
Right _ -> log ("✔️ " <> name)
Left err -> do
log ("" <> name)
logShow err
void $ modify (_ + 1)

assertDoesNotThrow :: a. Effect a -> Effect Unit
assertDoesNotThrow action = try action >>= case _ of
Right _ -> pure unit
Left err -> do throw ("Expected to not throw an exception, but one was thrown.\n" <> show err)


timeAdvances :: time. Show time => Ord time => Effect time -> String -> Test
timeAdvances getTime name = test ("time advances with " <> name) do
startTime <- getTime
endTime <- getTime
assert' (show endTime <> " should be greater than than " <> show startTime) $ endTime >= startTime

canGetTheCurrentTime :: Test
canGetTheCurrentTime = test "can get the current time" do
assertDoesNotThrow nowTime

offsetSeemsSensible :: Test
offsetSeemsSensible = test "UTC offset between -13:00 and +15:00" do
minutes <- getTimezoneOffset
assert $ between lbound ubound minutes

where
lbound = Minutes $ (-13.0) * minPerHour
ubound = Minutes $ 15.0 * minPerHour
minPerHour = 60.0


main :: Effect Unit
main = do
log "🍝"
log "You should add some tests."
failedTests <- flip execStateT 0 do
timeAdvances now "now"
timeAdvances nowDate "nowDate"
canGetTheCurrentTime
offsetSeemsSensible

exit failedTests

0 comments on commit 42819f2

Please sign in to comment.