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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
before:
this_module = 'posix.time'
global_table = '_G'
M = require(this_module)
specify posix.time:
- context when required:
- it does not touch the global table:
expect(show_apis {added_to=global_table, by=this_module}).
to_equal {}
- before:
EPOCH = M.time()
- describe clock_getres:
- before:
clock_getres = M.clock_getres
- context with bad arguments:
if clock_getres then
badargs.diagnose(clock_getres, "(int)")
end
- it returns a PosixTimespec:
if clock_getres then
expect(prototype(clock_getres(M.CLOCK_REALTIME))).
to_be "PosixTimespec"
end
- describe clock_gettime:
- before:
clock_gettime = M.clock_gettime
- context with bad arguments:
if clock_gettime then
badargs.diagnose(clock_gettime, "(int)")
end
- it returns a PosixTimespec:
if clock_getres then
expect(prototype(clock_gettime(M.CLOCK_REALTIME))).
to_be "PosixTimespec"
end
- describe gmtime:
- before:
gmtime = M.gmtime
- context with bad arguments:
badargs.diagnose(gmtime, "(int)")
- it returns a PosixTm:
expect(prototype(gmtime(EPOCH))).to_be "PosixTm"
- it fetches broken-down time values:
t = gmtime(EPOCH)
fields = {"tm_sec", "tm_min", "tm_hour", "tm_mday",
"tm_mon", "tm_year", "tm_wday", "tm_yday", "tm_isdst"}
if t.tm_gmtoff ~= nil then fields[1+#fields] = "tm_gmtoff" end
if t.tm_zone ~= nil then fields[1+#fields] = "tm_zone" end
expect(t).to_contain.a_permutation_of(fields)
for _, field in pairs(fields) do
if field == "tm_zone" then
expect(type(t[field])).to_be "string"
else
expect(type(t[field])).to_be "number"
if field ~= "tm_gmtoff" then
expect(t[field] >= 0).to_be(true)
else
expect(t[field] == 0).to_be(true)
end
end
end
- it returns a month in the range 0-11:
# A recent December afternoon in epoch seconds...
expect(gmtime(1418734089).tm_mon).to_be(11)
t = gmtime(EPOCH)
expect(t.tm_mon >= 0 and t.tm_mon < 12).to_be(true)
- it returns years since 1900:
expect(gmtime(EPOCH).tm_year < 1900).to_be(true)
- it does not wrap around after 2^31 seconds:
expect(gmtime(2147483647).tm_year).to_be(gmtime(2147483648).tm_year)
- describe localtime:
- before:
localtime = M.localtime
- context with bad arguments:
badargs.diagnose(localtime, "(int)")
- it returns a PosixTm:
expect(prototype(localtime(EPOCH))).to_be "PosixTm"
- it fetches broken-down time values:
t = localtime(EPOCH)
fields = {"tm_sec", "tm_min", "tm_hour", "tm_mday",
"tm_mon", "tm_year", "tm_wday", "tm_yday", "tm_isdst"}
if t.tm_gmtoff ~= nil then fields[1+#fields] = "tm_gmtoff" end
if t.tm_zone ~= nil then fields[1+#fields] = "tm_zone" end
expect(t).to_contain.a_permutation_of(fields)
for _, field in pairs(fields) do
if field == "tm_zone" then
expect(type(t[field])).to_be "string"
else
expect(type(t[field])).to_be "number"
if field ~= "tm_gmtoff" then
expect(t[field] >= 0).to_be(true)
end
end
end
- it returns a month in the range 0-11:
# A recent December afternoon in epoch seconds...
expect(localtime(1418734089).tm_mon).to_be(11)
t = localtime(EPOCH)
expect(t.tm_mon >= 0 and t.tm_mon < 12).to_be(true)
- it returns years since 1900:
expect(localtime(EPOCH).tm_year < 1900).to_be(true)
- it does not wrap around after 2^31 seconds:
expect(localtime(2147483647).tm_year).to_be(localtime(2147483648).tm_year)
- describe mktime:
- before:
mktime = M.mktime
t = M.localtime(EPOCH)
- context with bad arguments:
badargs.diagnose(mktime, "(table)")
- it returns an epoch time:
expect(prototype(mktime(t))).to_be "number"
- it is the inverse of localtime:
expect(mktime(t)).to_be(EPOCH)
- describe nanosleep:
- before:
nanosleep, typeerrors = init(M, "nanosleep")
- context with bad arguments: |
badargs.diagnose(nanosleep, "(table)")
examples {
["context diagnosing timespec table fields"] = {
{
["it diagnoses argument #1 tv_sec field type not integer"] = function()
expect(nanosleep {tv_sec = false}).
to_raise.any_of(typeerrors(1, "integer", "tv_sec", "boolean"))
end
},
{
["it diagnoses argument #1 tv_nsec field type not integer"] = function()
expect(nanosleep {tv_sec = -1, tv_nsec = false}).
to_raise.any_of(typeerrors(1, "integer", "tv_nsec", "boolean"))
end
},
{
["it diagnoses argument #1 spurious fields"] = function()
expect(nanosleep {tv_sec = -1, tv_nsec = -1, bogus = false}).
to_raise.any_of(typeerrors(1, nil, "bogus"))
end
},
}
}
- it returns an integer:
expect(nanosleep {tv_sec = 0, tv_nsec = 10}).to_be(0)
- describe strftime:
- before:
strftime = M.strftime
t = {
tm_isdst=0, tm_wday=1, tm_sec=2, tm_min=3, tm_hour=4,
tm_mday=5, tm_mon=6, tm_year=7, tm_yday=8, tm_gmtoff=-32400,
tm_zone="AKST"
}
- context with bad arguments:
badargs.diagnose(strftime, "(string, table)")
- context with place-holders:
- it plugs tm_wday:
expect(strftime("%w", t)).to_be "1"
- it plugs tm_sec:
expect(strftime("%S", t)).to_be "02"
- it plugs tm_min:
expect(strftime("%M", t)).to_be "03"
- it plugs tm_hour:
expect(strftime("%H", t)).to_be "04"
- it plugs tm_mday:
expect(strftime("%d", t)).to_be "05"
- it plugs tm_mon:
expect(strftime("%m", t)).to_be "07"
- it plugs tm_year:
expect(strftime("%y", t)).to_be "07"
- it plugs tm_yday:
expect(strftime("%j", t)).to_be "009"
- it plugs timezone:
expect(strftime("%Z", t)).to_be "AKST"
- describe strptime:
- before:
strptime = M.strptime
- context with bad arguments:
badargs.diagnose(strptime, "(string, string)")
- context with place-holders:
- before:
t, i = strptime("Mon Jun 4 03:02:01 BST 1906 garbage",
"%a %b %d %H:%M:%S BST %Y")
- it returns the first unconsumed character:
expect(i).to_be(29)
# tm_yday and tm_isdst are not set by strptime
- it scans into tm_wday:
expect(t.tm_wday).to_be(1)
- it scans into tm_sec:
expect(t.tm_sec).to_be(1)
- it scans into tm_min:
expect(t.tm_min).to_be(2)
- it scans into tm_hour:
expect(t.tm_hour).to_be(3)
- it scans into tm_mday:
expect(t.tm_mday).to_be(4)
- it scans into tm_mon:
expect(t.tm_mon).to_be(5)
- it scans into tm_year:
expect(t.tm_year).to_be(6)
- describe time:
- before:
time = M.time
- context with bad arguments:
badargs.diagnose(time, "()")
- it returns epoch seconds:
expect(type(time())).to_be "number"
expect(time() > 0).to_be(true)
|