Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds some basic tests for this library #20

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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