summaryrefslogtreecommitdiff
path: root/spec/dates_spec.lua
blob: d820080aef55d505b974127a200d7618c7141296 (plain)
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
local dates = require "activity.dates"


describe("Days in a year", function()
	it("should support regular years", function()
		assert.are.equal(365, dates.days_in(1970))
		assert.are.equal(365, dates.days_in(2001))
	end)

	it("should support lean years", function()
		assert.are.equal(366, dates.days_in(2004))
		assert.are.equal(366, dates.days_in(1980))
	end)

	it("should support *00 years", function()
		assert.are.equal(366, dates.days_in(2000))
		assert.are.equal(365, dates.days_in(2100))
		assert.are.equal(365, dates.days_in(2200))
		assert.are.equal(365, dates.days_in(2300))
		assert.are.equal(366, dates.days_in(2400))
	end)
end)


describe("This year is", function()
	local this_year = tonumber(os.date"%Y")  -- Quite useless test

	it(tostring(this_year), function()
		assert.are.equal(this_year, dates.this_year())
	end)
end)


describe("First week day of", function()
	it("2023 is Sunday", function()
		assert.are.equal(1, dates.first_week_day(2023))
	end)

	it("2022 is Saturday", function()
		assert.are.equal(7, dates.first_week_day(2022))
	end)

	it("2020 is Wednesday", function()
		assert.are.equal(4, dates.first_week_day(2020))
	end)
end)


describe("Day 5 days before", function()
	it("3 Mar 2023 is 26 Feb", function()
		local date = {year=2023, month=3, day=3}
		assert.are.same({year=2023, month=2, day=26}, dates.before(date, 5))
	end)

	it("3 Mar 2024 is 27 Feb", function()
		local date = {year=2024, month=3, day=3}
		assert.are.same({year=2024, month=2, day=27}, dates.before(date, 5))
	end)

	it("2 Jan 2024 is 28 Dec 2023", function()
		local date = {year=2024, month=1, day=2}
		assert.are.same({year=2023, month=12, day=28}, dates.before(date, 5))
	end)
end)