| 1 |
--[[ Copyright (c) 2018 Pavel "sofo" Schoffer |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
require("class_test_base") |
| 22 |
|
| 23 |
require("date") |
| 24 |
|
| 25 |
describe("Date", function() |
| 26 |
it("can return correct month last day", function() |
| 27 |
assert.are.equal(Date(1, 1):lastDayOfMonth(), 31) |
| 28 |
assert.are.equal(Date(1, 2):lastDayOfMonth(), 28) |
| 29 |
assert.are.equal(Date(1, 6):lastDayOfMonth(), 30) |
| 30 |
assert.are.equal(Date(1, 12):lastDayOfMonth(), 31) |
| 31 |
end) |
| 32 |
|
| 33 |
it("default works", function() |
| 34 |
local date = Date(5, 12, 22, 5) |
| 35 |
assert.are.equal(date:hourOfDay(), 5) |
| 36 |
assert.are.equal(date:dayOfMonth(), 22) |
| 37 |
assert.are.equal(date:monthOfYear(), 12) |
| 38 |
assert.are.equal(date:year(), 5) |
| 39 |
|
| 40 |
date = Date(5, 12, 22) |
| 41 |
assert.are.equal(date:hourOfDay(), 0) |
| 42 |
assert.are.equal(date:dayOfMonth(), 22) |
| 43 |
assert.are.equal(date:monthOfYear(), 12) |
| 44 |
assert.are.equal(date:year(), 5) |
| 45 |
|
| 46 |
date = Date(4, 12) |
| 47 |
assert.are.equal(date:dayOfMonth(), 1) |
| 48 |
assert.are.equal(date:monthOfYear(), 12) |
| 49 |
assert.are.equal(date:year(), 4) |
| 50 |
|
| 51 |
date = Date(24) |
| 52 |
assert.are.equal(date:dayOfMonth(), 1) |
| 53 |
assert.are.equal(date:monthOfYear(), 1) |
| 54 |
assert.are.equal(date:year(), 24) |
| 55 |
|
| 56 |
date = Date() |
| 57 |
assert.are.equal(date:dayOfMonth(), 1) |
| 58 |
assert.are.equal(date:monthOfYear(), 1) |
| 59 |
assert.are.equal(date:year(), 1) |
| 60 |
end) |
| 61 |
|
| 62 |
it("cannot be wrong date", function() |
| 63 |
local date = Date(1, 14) |
| 64 |
assert.are.equal(date:monthOfYear(), 2) |
| 65 |
|
| 66 |
date = Date(1, 24) |
| 67 |
assert.are.equal(date:monthOfYear(), 12) |
| 68 |
end) |
| 69 |
|
| 70 |
it("can handle hour adjustmentse", function() |
| 71 |
local date = Date(1, 1, 5, 50) |
| 72 |
assert.are.equal(6, date:dayOfMonth()) |
| 73 |
assert.are.equal(0, date:hourOfDay()) |
| 74 |
|
| 75 |
date = Date(1, 1, 1, 55) |
| 76 |
assert.are.equal(2, date:dayOfMonth()) |
| 77 |
assert.are.equal(5, date:hourOfDay()) |
| 78 |
end) |
| 79 |
|
| 80 |
it("can add and read months", function() |
| 81 |
local date = Date():plusMonths(1) |
| 82 |
assert.are.equal(date:monthOfYear(), 2) |
| 83 |
|
| 84 |
date = Date(20, 12):plusMonths(1) |
| 85 |
assert.are.equal(date:monthOfYear(), 1) |
| 86 |
end) |
| 87 |
|
| 88 |
it("can handle complex adjustments", function() |
| 89 |
local date = Date(2,2,31,55) |
| 90 |
assert.are.equal("2-03-04T05", date:tostring()) |
| 91 |
|
| 92 |
date = Date(1,12,31,55) |
| 93 |
assert.are.equal("2-01-01T05", date:tostring()) |
| 94 |
|
| 95 |
-- 31 - January, 28 - February, 7 - March |
| 96 |
date = Date(1,1,66) |
| 97 |
assert.are.equal("1-03-07T00", date:tostring()) |
| 98 |
end) |
| 99 |
|
| 100 |
it("can print date", function() |
| 101 |
local date = Date(2,12,1,6) |
| 102 |
assert.are.equal("2-12-01T06", date:tostring()) |
| 103 |
end) |
| 104 |
it("can add days", function() |
| 105 |
local date = Date(2,12,30) |
| 106 |
-- 1 - December, 29 - January |
| 107 |
date = date:plusDays(30) |
| 108 |
assert.are.equal("3-01-29T00", date:tostring()) |
| 109 |
|
| 110 |
date = Date(1,3,15) |
| 111 |
-- 16 - March, 30 - April, 31 - June, 13 - July |
| 112 |
date = date:plusDays(90) |
| 113 |
assert.are.equal("1-06-13T00", date:tostring()) |
| 114 |
end) |
| 115 |
it("can add years", function() |
| 116 |
local date = Date(3, 2, 1, 6) |
| 117 |
local expected_date = Date(8, 2, 1, 6) |
| 118 |
|
| 119 |
local adjusted_date = date:plusYears(5) |
| 120 |
assert.True(expected_date == adjusted_date) |
| 121 |
end) |
| 122 |
it("can add hours", function() |
| 123 |
local date = Date(1,1,1,5) |
| 124 |
|
| 125 |
local adjusted_date = date:plusHours(50) |
| 126 |
assert.are.equals(2, adjusted_date:dayOfMonth()) |
| 127 |
assert.are.equals(5, adjusted_date:hourOfDay()) |
| 128 |
end) |
| 129 |
it("can add negative hours", function() |
| 130 |
local date = Date(3,1,1,0) |
| 131 |
local expected_date = Date(2,12,31,49) |
| 132 |
|
| 133 |
local adjusted_date = date:plusHours(-1) |
| 134 |
assert.True(expected_date == adjusted_date) |
| 135 |
end) |
| 136 |
it("can add negative days", function() |
| 137 |
local date = Date(3,1,1) |
| 138 |
local adjusted_date = date:plusDays(-40) |
| 139 |
local expected_date = Date(2,11,22) |
| 140 |
|
| 141 |
assert.True(expected_date == adjusted_date) |
| 142 |
end) |
| 143 |
it("can tell the last days", function() |
| 144 |
local date = Date(1,12,3) |
| 145 |
assert.False(date:isLastDayOfMonth()) |
| 146 |
assert.False(date:isLastDayOfYear()) |
| 147 |
|
| 148 |
date = Date(1,1,31) |
| 149 |
assert.True(date:isLastDayOfMonth()) |
| 150 |
assert.False(date:isLastDayOfYear()) |
| 151 |
|
| 152 |
date = Date(1,12,31) |
| 153 |
assert.True(date:isLastDayOfMonth()) |
| 154 |
assert.True(date:isLastDayOfYear()) |
| 155 |
end) |
| 156 |
it("get total elapsed month", function() |
| 157 |
local date = Date(8,11,1) |
| 158 |
assert.are.equals(95, date:monthOfGame()) |
| 159 |
end) |
| 160 |
it("can get compared", function() |
| 161 |
local date1 = Date(3,2,1) |
| 162 |
local other_date1 = Date(3,2,1) |
| 163 |
local date2 = Date(1,2,3) |
| 164 |
local date_with_different_hour = Date(3,2,1,40) |
| 165 |
assert.True(date1 == other_date1) |
| 166 |
assert.False(date1 == date2) |
| 167 |
assert.True(date1 > date2) |
| 168 |
assert.False(date1 < date2) |
| 169 |
assert.False(date1 < other_date1) |
| 170 |
assert.True(date1 <= other_date1) |
| 171 |
assert.False(date1 <= date2) |
| 172 |
assert.True(date1 >= other_date1) |
| 173 |
assert.False(date_with_different_hour == date1) |
| 174 |
assert.True(date_with_different_hour > date1) |
| 175 |
end) |
| 176 |
it("can clone itself", function() |
| 177 |
local origin_date = Date(12,3,2,6) |
| 178 |
local clone_date = origin_date:clone() |
| 179 |
assert.True(origin_date == clone_date) |
| 180 |
end) |
| 181 |
it("can provide hours per day", function() |
| 182 |
assert.equals(50, Date.hoursPerDay()) |
| 183 |
end) |
| 184 |
it("can check if two days are the same", function() |
| 185 |
assert.True(Date(1,2,3,5):isSameDay(Date(1,2,3,9))) |
| 186 |
assert.False(Date(1,2,3):isSameDay(Date(1,4,3))) |
| 187 |
end) |
| 188 |
end) |