Skip to content

Adding tests that cover the ecma262 specs #80

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
72 changes: 72 additions & 0 deletions test/Test/Date.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module Test.Date where

import Prelude

import Data.Date as Date
import Data.Enum (toEnum)
import Data.Maybe (Maybe(..), fromJust)
import Data.Time.Duration as Duration
import Effect (Effect)
import Effect.Console (log)
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert)
import Test.Helpers (checkBounded, checkBoundedEnum, epochDate)
import Type.Proxy (Proxy(..))

dateTests :: Effect Unit
dateTests = do
log "--------- Date Tests ---------"
log "Check that Year is a good BoundedEnum"
checkBoundedEnum (Proxy :: Proxy Date.Year)

log "Check that Month is a good BoundedEnum"
checkBoundedEnum (Proxy :: Proxy Date.Month)

log "Check that Day is a good BoundedEnum"
checkBoundedEnum (Proxy :: Proxy Date.Day)

log "Check that Date is a good Bounded"
checkBounded (Proxy :: Proxy Date.Date)

log "Check that the earliest date is a valid date"
assert $ Just bottom == Date.exactDate bottom bottom bottom

log "Check that the latest date is a valid date"
assert $ Just top == Date.exactDate top top top

log "Check that weekday behaves as expected"
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 6) == Date.Monday
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 7) == Date.Tuesday
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 8) == Date.Wednesday
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 9) == Date.Thursday
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 10) == Date.Friday
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 11) == Date.Saturday
assert $ Date.weekday (unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.June <*> toEnum 12) == Date.Sunday

let d1 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.January <*> toEnum 1
let d2 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.February <*> toEnum 1
let d3 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.March <*> toEnum 1
let d4 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2018 <*> pure Date.September <*> toEnum 26
let d5 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 1988 <*> pure Date.August <*> toEnum 15

log "Check that diff behaves as expected"
assert $ Date.diff d2 d1 == Duration.Days 31.0
assert $ Date.diff d3 d2 == Duration.Days 29.0

let unsafeYear = unsafePartial fromJust <<< toEnum
log "Check that isLeapYear behaves as expected"
assert $ not $ Date.isLeapYear (unsafeYear 2017)
assert $ Date.isLeapYear (unsafeYear 2016)

log "Check that epoch is correctly constructed"
assert $ Just (Date.year epochDate) == toEnum 1
assert $ Date.month epochDate == bottom
assert $ Date.day epochDate == bottom

log "Check that adjust behaves as expected"
assert $ Date.adjust (Duration.Days 31.0) d1 == Just d2
assert $ Date.adjust (Duration.Days 999.0) d1 == Just d4
assert $ Date.adjust (Duration.Days 10000.0) d5 == Just d1
assert $ Date.adjust (Duration.Days (-31.0)) d2 == Just d1
assert $ Date.adjust (Duration.Days (- 999.0)) d4 == Just d1
assert $ Date.adjust (Duration.Days (-10000.0)) d1 == Just d5
36 changes: 36 additions & 0 deletions test/Test/DateTime.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Test.DateTime
( dateTimeTests
) where

import Prelude

import Data.Date as Date
import Data.DateTime as DateTime
import Data.Enum (toEnum)
import Data.Maybe (Maybe(..))
import Data.Newtype (over)
import Data.Time.Duration as Duration
import Effect (Effect)
import Effect.Console (log)
import Math (floor)
import Test.Assert (assert)
import Test.Helpers (dateTime1, dateTime2, dateTime3, dateTime4, dateTime5, epochDateTime)

dateTimeTests :: Effect Unit
dateTimeTests = do
log "--------- DateTime Tests ---------"
log "Check that adjust behaves as expected"
assert $ DateTime.adjust (Duration.fromDuration (Duration.Days 31.0) <> Duration.fromDuration (Duration.Minutes 40.0)) dateTime1 == Just dateTime4
assert $ (Date.year <<< DateTime.date <$>
(DateTime.adjust (Duration.Days 735963.0) epochDateTime))
== toEnum 2016

log "Check that diff behaves as expected"
assert $ DateTime.diff dateTime2 dateTime1 == Duration.Minutes 40.0
assert $ DateTime.diff dateTime1 dateTime2 == Duration.Minutes (-40.0)
assert $ DateTime.diff dateTime3 dateTime1 == Duration.Days 31.0
assert $ DateTime.diff dateTime5 dateTime3 == Duration.Days 29.0
assert $ DateTime.diff dateTime1 dateTime3 == Duration.Days (-31.0)
assert $ DateTime.diff dateTime4 dateTime1 == Duration.fromDuration (Duration.Days 31.0) <> Duration.fromDuration (Duration.Minutes 40.0)
assert $ over Duration.Days floor (DateTime.diff dateTime1 epochDateTime)
== Duration.Days 735963.0
32 changes: 32 additions & 0 deletions test/Test/Duration.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Test.Duration
( durationTests
) where

import Prelude

import Effect (Effect)
import Effect.Console (log)
import Data.Either (Either(..), isRight)
import Data.Interval as Interval
import Data.Interval.Duration.Iso as IsoDuration
import Test.Assert (assert)

durationTests :: Effect Unit
durationTests = do
log "--------- Durations Tests ---------"
log "check Duration monoid"
assert $ Interval.year 1.0 == mempty <> Interval.year 2.0 <> Interval.year 1.0 <> Interval.year (-2.0)
assert $ Interval.second 0.5 == Interval.millisecond 500.0
assert $ IsoDuration.mkIsoDuration (Interval.week 1.2 <> Interval.week 1.2)
== IsoDuration.mkIsoDuration (Interval.week 2.4)
assert $ isRight $ IsoDuration.mkIsoDuration (Interval.day 1.2 <> mempty)
assert $ isRight $ IsoDuration.mkIsoDuration (Interval.day 1.2 <> Interval.second 0.0)
assert $ isRight $ IsoDuration.mkIsoDuration (Interval.year 2.0 <> Interval.day 1.0)
assert $ IsoDuration.mkIsoDuration (Interval.year 2.5 <> Interval.day 1.0)
== Left (pure (IsoDuration.InvalidFractionalUse Interval.Year))
log $ show $ IsoDuration.mkIsoDuration (Interval.year 2.5 <> Interval.week 1.0)
== Left (pure IsoDuration.InvalidWeekComponentUsage <> pure (IsoDuration.InvalidFractionalUse Interval.Year))
assert $ IsoDuration.mkIsoDuration (Interval.year 2.0 <> Interval.day (-1.0))
== Left (pure (IsoDuration.ContainsNegativeValue Interval.Day))
assert $ IsoDuration.mkIsoDuration (mempty)
== Left (pure IsoDuration.IsEmpty)
108 changes: 108 additions & 0 deletions test/Test/Helpers.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module Test.Helpers
( checkBounded
, checkBoundedEnum
, date1
, date2
, date3
, date4
, date5
, dateTime1
, dateTime2
, dateTime3
, dateTime4
, dateTime5
, epochDate
, epochDateTime
, epochMillis
, time1
, time2
, time3
, time4
, time5
) where

import Prelude

import Data.Array as Array
import Data.Date as Date
import Data.DateTime as DateTime
import Data.Enum (class BoundedEnum, Cardinality, toEnum, enumFromTo, cardinality, succ, fromEnum, pred)
import Data.Maybe (Maybe(..), fromJust)
import Data.Newtype (unwrap)
import Data.Time as Time
import Effect (Effect)
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert)
import Type.Proxy (Proxy)

checkBounded :: forall e. Bounded e => Proxy e -> Effect Unit
checkBounded _ = do
assert $ Just (bottom :: Time.Hour) == toEnum (fromEnum (bottom :: Time.Hour))
assert $ pred (bottom :: Time.Hour) == Nothing
assert $ Just (top :: Time.Hour) == toEnum (fromEnum (top :: Time.Hour))
assert $ succ (top :: Time.Hour) == Nothing

checkBoundedEnum :: forall e. BoundedEnum e => Proxy e -> Effect Unit
checkBoundedEnum p = do
checkBounded p
let card = unwrap (cardinality :: Cardinality e)
assert $ Array.length (enumFromTo bottom (top :: e)) == card


date1 :: Date.Date
date1 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.January <*> toEnum 1

date2 :: Date.Date
date2 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.February <*> toEnum 1

date3 :: Date.Date
date3 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2016 <*> pure Date.March <*> toEnum 1

date4 :: Date.Date
date4 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 2018 <*> pure Date.September <*> toEnum 26

date5 :: Date.Date
date5 = unsafePartial fromJust $ Date.canonicalDate <$> toEnum 1988 <*> pure Date.August <*> toEnum 15


dateTime1 :: DateTime.DateTime
dateTime1 = DateTime.DateTime date1 time1

dateTime2 :: DateTime.DateTime
dateTime2 = DateTime.DateTime date1 time2

dateTime3 :: DateTime.DateTime
dateTime3 = DateTime.DateTime date2 time1

dateTime4 :: DateTime.DateTime
dateTime4 = DateTime.DateTime date2 time2

dateTime5 :: DateTime.DateTime
dateTime5 = DateTime.DateTime date3 time1

epochDate :: Date.Date
epochDate = unsafePartial fromJust $ Date.canonicalDate
<$> toEnum 1
<*> pure bottom
<*> pure bottom

epochDateTime :: DateTime.DateTime
epochDateTime = DateTime.DateTime epochDate bottom

epochMillis :: Number
epochMillis = -62135596800000.0

time1 :: Time.Time
time1 = unsafePartial $ fromJust $ Time.Time <$> toEnum 17 <*> toEnum 42 <*> toEnum 16 <*> toEnum 362

time2 :: Time.Time
time2 = unsafePartial $ fromJust $ Time.Time <$> toEnum 18 <*> toEnum 22 <*> toEnum 16 <*> toEnum 362

time3 :: Time.Time
time3 = unsafePartial $ fromJust $ Time.Time <$> toEnum 17 <*> toEnum 2 <*> toEnum 16 <*> toEnum 362

time4 :: Time.Time
time4 = unsafePartial $ fromJust $ Time.Time <$> toEnum 23 <*> toEnum 0 <*> toEnum 0 <*> toEnum 0

time5 :: Time.Time
time5 = unsafePartial $ fromJust $ Time.Time <$> toEnum 1 <*> toEnum 0 <*> toEnum 0 <*> toEnum 0
35 changes: 35 additions & 0 deletions test/Test/Instant.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Test.Instant
( instantTests
) where

import Prelude

import Data.DateTime.Instant as Instant
import Data.Maybe (Maybe(..))
import Data.Time.Duration as Duration
import Effect (Effect)
import Effect.Console (log)
import Test.Assert (assert)
import Test.Helpers (dateTime1, dateTime2, dateTime3, dateTime4, epochDateTime, epochMillis)

instantTests :: Effect Unit
instantTests = do
log "--------- Instant Tests ---------"
log "Check that the earliest date is a valid Instant"
let bottomInstant = Instant.fromDateTime bottom
assert $ Just bottomInstant == Instant.instant (Instant.unInstant bottomInstant)

log "Check that the latest date is a valid Instant"
let topInstant = Instant.fromDateTime top
assert $ Just topInstant == Instant.instant (Instant.unInstant topInstant)

log "Check that an Instant can be constructed from epoch"
assert $ (Instant.unInstant $ Instant.fromDateTime epochDateTime) == Duration.Milliseconds epochMillis

log "Check that instant/datetime conversion is bijective"
assert $ Instant.toDateTime (Instant.fromDateTime bottom) == bottom
assert $ Instant.toDateTime (Instant.fromDateTime top) == top
assert $ Instant.toDateTime (Instant.fromDateTime dateTime1) == dateTime1
assert $ Instant.toDateTime (Instant.fromDateTime dateTime2) == dateTime2
assert $ Instant.toDateTime (Instant.fromDateTime dateTime3) == dateTime3
assert $ Instant.toDateTime (Instant.fromDateTime dateTime4) == dateTime4
Loading