Skip to content

Commit aa8672c

Browse files
authored
Improve test coverage of the Time module (#14525)
* Add tests for Time.to_iso8601/2 * Add tests for Time.compare/2 with different calendars * Add tests for Time.diff/3 * Test Time.shift/2 with invalid values
1 parent 5c27003 commit aa8672c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lib/elixir/test/elixir/calendar/time_test.exs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ defmodule TimeTest do
3838
end
3939
end
4040

41+
test "to_iso8601/2" do
42+
time1 = ~T[23:00:07.005]
43+
assert Time.to_iso8601(time1) == "23:00:07.005"
44+
assert Time.to_iso8601(Map.from_struct(time1)) == "23:00:07.005"
45+
assert Time.to_iso8601(time1, :basic) == "230007.005"
46+
47+
time2 = ~T[23:00:07.005 Calendar.Holocene]
48+
assert Time.to_iso8601(time2) == "23:00:07.005"
49+
assert Time.to_iso8601(Map.from_struct(time2)) == "23:00:07.005"
50+
assert Time.to_iso8601(time2, :basic) == "230007.005"
51+
end
52+
4153
test "to_string/1" do
4254
time = ~T[23:00:07.005]
4355
assert to_string(time) == "23:00:07.005"
@@ -65,6 +77,11 @@ defmodule TimeTest do
6577
assert Time.compare(time1, time2) == :eq
6678
assert Time.compare(time1, time3) == :lt
6779
assert Time.compare(time3, time2) == :gt
80+
81+
time1_holocene = ~T[01:01:01.005 Calendar.Holocene]
82+
assert Time.compare(time1_holocene, time1) == :eq
83+
assert Time.compare(time1_holocene, time2) == :eq
84+
assert Time.compare(time1_holocene, time3) == :lt
6885
end
6986

7087
test "before?/2 and after?/2" do
@@ -78,6 +95,26 @@ defmodule TimeTest do
7895
assert not Time.after?(time1, time2)
7996
end
8097

98+
test "diff/3" do
99+
time1 = ~T[05:02:01.234]
100+
time2 = ~T[10:00:04.123]
101+
time1_holocene = ~T[05:02:01.234 Calendar.Holocene]
102+
103+
assert Time.diff(time1, time2) == -17883
104+
assert Time.diff(time1, time2, :hour) == -4
105+
assert Time.diff(time1, time2, :minute) == -298
106+
assert Time.diff(time1, time2, :second) == -17883
107+
assert Time.diff(time1, time2, :millisecond) == -17_882_889
108+
assert Time.diff(time1, time2, :microsecond) == -17_882_889_000
109+
110+
assert Time.diff(time1_holocene, time2) == -17883
111+
assert Time.diff(time1_holocene, time2, :hour) == -4
112+
assert Time.diff(time1_holocene, time2, :minute) == -298
113+
assert Time.diff(time1_holocene, time2, :second) == -17883
114+
assert Time.diff(time1_holocene, time2, :millisecond) == -17_882_889
115+
assert Time.diff(time1_holocene, time2, :microsecond) == -17_882_889_000
116+
end
117+
81118
test "truncate/2" do
82119
assert Time.truncate(~T[01:01:01.123456], :microsecond) == ~T[01:01:01.123456]
83120

@@ -128,5 +165,13 @@ defmodule TimeTest do
128165
assert_raise ArgumentError,
129166
"cannot shift time by date scale unit. Expected :hour, :minute, :second, :microsecond",
130167
fn -> Time.shift(time, %Duration{day: 1}) end
168+
169+
assert_raise ArgumentError,
170+
"unsupported value nil for :minute. Expected an integer",
171+
fn -> Time.shift(time, minute: nil) end
172+
173+
assert_raise ArgumentError,
174+
~r/unsupported value 1 for :microsecond. Expected a tuple \{ms, precision\}/,
175+
fn -> Time.shift(time, microsecond: 1) end
131176
end
132177
end

0 commit comments

Comments
 (0)