-
Notifications
You must be signed in to change notification settings - Fork 3
/
time_spec.cr
74 lines (60 loc) 路 2.63 KB
/
time_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require "../spec_helper"
require "../../src/http-params-serializable/ext/time/epoch_converter"
# Used to test the converter feature
struct TimeParams
include HTTP::Params::Serializable
@[HTTP::Param(converter: Time::EpochConverter)]
getter epoch_time : Time
@[HTTP::Param(converter: Time::EpochConverter)]
getter nilable_epoch_time : Time?
@[HTTP::Param(converter: Time::EpochConverter)]
getter array_epoch_time : Array(Time)
@[HTTP::Param(converter: Time::EpochConverter)]
getter nilable_array_epoch_time : Array(Time)?
@[HTTP::Param(converter: Time::EpochConverter)]
getter array_nilable_epoch_time : Array(Time?)
@[HTTP::Param(converter: Time::EpochConverter)]
getter nilable_array_nilable_epoch_time : Array(Time?)?
end
describe TimeParams do
it do
v = TimeParams.from_query("epoch_time=1544958806&nilableEpochTime=1544958806&ArrayEpochTime[]=1544958806&nilable-array-epoch-time[0]=1544958806&Nilable-Array-Nilable-Epoch-Time[]=1544958806&array_nilable_epoch_time[0]=")
v.epoch_time.should eq Time.unix(1544958806)
v.nilable_epoch_time.should eq Time.unix(1544958806)
v.array_epoch_time.should eq [Time.unix(1544958806)]
v.nilable_array_epoch_time.should eq [Time.unix(1544958806)]
v.array_nilable_epoch_time.should eq [nil]
v.nilable_array_nilable_epoch_time.should eq [Time.unix(1544958806)]
v.to_query.should eq escape("epochTime=1544958806&nilableEpochTime=1544958806&arrayEpochTime[]=1544958806&nilableArrayEpochTime[]=1544958806&nilableArrayNilableEpochTime[]=1544958806")
end
it "raises on type mismatch" do
assert_raise(
TimeParams,
"epoch_time=foo",
HTTP::Params::Serializable::ParamTypeCastError,
"Parameter \"epoch_time\" cannot be cast from \"foo\" to Time",
["epoch_time"],
)
assert_raise(
TimeParams,
"epoch_time=1544958806&nilable_epoch_time=foo",
HTTP::Params::Serializable::ParamTypeCastError,
"Parameter \"nilable_epoch_time\" cannot be cast from \"foo\" to (Time | Nil)",
["nilable_epoch_time"],
)
assert_raise(
TimeParams,
"epoch_time=1544958806&array_epoch_time[]=foo",
HTTP::Params::Serializable::ParamTypeCastError,
"Parameter \"array_epoch_time[]\" cannot be cast from \"foo\" to Time",
["array_epoch_time", ""],
)
assert_raise(
TimeParams,
"epoch_time=1544958806&array_epoch_time[]=1544958806&nilable_array_nilable_epoch_time[]=foo",
HTTP::Params::Serializable::ParamTypeCastError,
"Parameter \"nilable_array_nilable_epoch_time[]\" cannot be cast from \"foo\" to (Time | Nil)",
["nilable_array_nilable_epoch_time", ""],
)
end
end