Skip to content

Commit

Permalink
Used NamedTuple, add print and repr tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fchorney committed Jan 21, 2019
1 parent 48b495b commit 77cd499
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ using Intervals: canonicalize
# show interpretation,
# show interpretation for VERSION < v"1.2.0-Dev.30"
# )
# Types
T = Tuple{AnchoredInterval, String, String, String}
# Fields
F = (:interval, :string, :show, :show_old)
display_tests = [
(
NamedTuple{F, T}((
HourEnding(dt),
"(2016-08-11 HE02]",
string(
Expand All @@ -167,8 +171,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 hour,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
HourEnding(DateTime(2013, 2, 13), Inclusivity(true, false)),
"[2013-02-12 HE24)",
string(
Expand All @@ -179,8 +183,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 hour,DateTime}",
"(2013-02-13T00:00:00, Inclusivity(true, false))",
),
),
(
)),
NamedTuple{F, T}((
HourEnding(dt + Minute(15) + Second(30)),
"(2016-08-11 HE02:15:30]",
string(
Expand All @@ -191,8 +195,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 hour,DateTime}",
"(2016-08-11T02:15:30, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
HourEnding(dt + Millisecond(2)),
"(2016-08-11 HE02:00:00.002]",
string(
Expand All @@ -203,8 +207,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 hour,DateTime}",
"(2016-08-11T02:00:00.002, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
HourEnding(DateTime(2013, 2, 13, 0, 1), Inclusivity(true, false)),
"[2013-02-13 HE00:01:00)",
string(
Expand All @@ -215,8 +219,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 hour,DateTime}",
"(2013-02-13T00:01:00, Inclusivity(true, false))",
),
),
(
)),
NamedTuple{F, T}((
HourBeginning(dt),
"[2016-08-11 HB02)",
string(
Expand All @@ -227,8 +231,8 @@ using Intervals: canonicalize
"AnchoredInterval{1 hour,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(true, false))",
),
),
(
)),
NamedTuple{F, T}((
HourBeginning(DateTime(2013, 2, 13), Inclusivity(false, true)),
"(2013-02-13 HB00]",
string(
Expand All @@ -239,8 +243,8 @@ using Intervals: canonicalize
"AnchoredInterval{1 hour,DateTime}",
"(2013-02-13T00:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
HourEnding(ZonedDateTime(dt, tz"America/Winnipeg")),
"(2016-08-11 HE02-05:00]",
string(
Expand All @@ -252,26 +256,26 @@ using Intervals: canonicalize
"AnchoredInterval{-1 hour,$ZonedDateTime}",
"(2016-08-11T02:00:00-05:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Year(-1)}(Date(dt)),
"(YE 2016-08-11]",
string(
"AnchoredInterval{-1 year,Date}",
"(Date(2016, 8, 11), Inclusivity(false, true))",
),
"AnchoredInterval{-1 year,Date}(2016-08-11, Inclusivity(false, true))",
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Year(-1)}(ceil(Date(dt), Year)),
"(YE 2017-01-01]",
string(
"AnchoredInterval{-1 year,Date}",
"(Date(2017, 1, 1), Inclusivity(false, true))",
),
"AnchoredInterval{-1 year,Date}(2017-01-01, Inclusivity(false, true))",
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Month(-1)}(dt),
"(MoE 2016-08-11 02:00:00]",
string(
Expand All @@ -282,8 +286,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 month,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Month(-1)}(ceil(dt, Month)),
"(MoE 2016-09-01]",
string(
Expand All @@ -294,8 +298,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 month,DateTime}",
"(2016-09-01T00:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Day(-1)}(DateTime(dt)),
"(DE 2016-08-11 02:00:00]",
string(
Expand All @@ -306,8 +310,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 day,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Day(-1)}(ceil(DateTime(dt), Day)),
"(DE 2016-08-12]",
string(
Expand All @@ -318,8 +322,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 day,DateTime}",
"(2016-08-12T00:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
# Date(dt) will truncate the DateTime to the nearest day
AnchoredInterval{Day(-1)}(Date(dt)),
"(DE 2016-08-11]",
Expand All @@ -328,8 +332,8 @@ using Intervals: canonicalize
"(Date(2016, 8, 11), Inclusivity(false, true))",
),
"AnchoredInterval{-1 day,Date}(2016-08-11, Inclusivity(false, true))",
),
(
)),
NamedTuple{F, T}((
# Prevent confusion when dealing with time zones by ensuring
# that the full date and time are displayed
AnchoredInterval{Day(-1)}(
Expand All @@ -345,8 +349,8 @@ using Intervals: canonicalize
"AnchoredInterval{-1 day,$(tz_prefix)ZonedDateTime}",
"(2016-08-12T00:00:00-05:00, Inclusivity(false, true))"
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Minute(-5)}(dt),
"(2016-08-11 5ME02:00]",
string(
Expand All @@ -357,8 +361,8 @@ using Intervals: canonicalize
"AnchoredInterval{-5 minutes,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Second(-30)}(dt),
"(2016-08-11 30SE02:00:00]",
string(
Expand All @@ -369,8 +373,8 @@ using Intervals: canonicalize
"AnchoredInterval{-30 seconds,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(false, true))",
),
),
(
)),
NamedTuple{F, T}((
AnchoredInterval{Millisecond(-10)}(dt),
"(2016-08-11 10msE02:00:00.000]",
string(
Expand All @@ -381,19 +385,18 @@ using Intervals: canonicalize
"AnchoredInterval{-10 milliseconds,DateTime}",
"(2016-08-11T02:00:00, Inclusivity(false, true))",
),
),
)),
]


for test in display_tests
interval = test[1]
@test string(interval) == test[2]
interval = test.interval
@test string(interval) == test.string
@test sprint(print, interval) == test.string
@test sprint(show, interval, context=:compact=>true) == string(interval)
if VERSION < v"1.2.0-DEV.29"
@test sprint(show, interval) == mod_prefix * test[4]
else
@test sprint(show, interval) == mod_prefix * test[3]
end
show_str = VERSION < v"1.2.0-DEV.29" ? test.show_old : test.show
@test sprint(show, interval) == mod_prefix * show_str
@test repr(interval) == mod_prefix * show_str
end

interval = AnchoredInterval{Second(-10)}(Time(1, 0, 0))
Expand Down

0 comments on commit 77cd499

Please sign in to comment.