-
Notifications
You must be signed in to change notification settings - Fork 10
/
date_spec.rb
139 lines (117 loc) · 4.63 KB
/
date_spec.rb
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
require "spec_helper"
RSpec.describe Keisan::AST::Date do
describe "is_constant?" do
it "is true" do
date = Keisan::AST.parse("date(2018, 11, 20)").evaluate
expect(date.is_constant?).to eq true
end
end
describe "evaluate" do
it "reduces to a date when adding numbers" do
ast = Keisan::AST.parse("date(2018, 11, 20) + 1")
expect(ast.evaluate).to be_a(described_class)
expect(ast.evaluate.value).to eq Date.new(2018, 11, 21)
ast = Keisan::AST.parse("2 + date(2018, 11)")
expect(ast.evaluate).to be_a(described_class)
expect(ast.evaluate.value).to eq Date.new(2018, 11, 3)
ast = Keisan::AST.parse("3 + date('3rd Feb 2001')")
expect(ast.evaluate).to be_a(described_class)
expect(ast.evaluate.value).to eq Date.new(2001, 2, 6)
ast = Keisan::AST.parse("date(2017) > date(2018)")
expect(ast.evaluate).to be_a(Keisan::AST::Boolean)
expect(ast.evaluate.value).to eq false
ast = Keisan::AST.parse("date(2017, 10, 10) >= date(2017, 10, 10)")
expect(ast.evaluate).to be_a(Keisan::AST::Boolean)
expect(ast.evaluate.value).to eq true
ast = Keisan::AST.parse("date(2017) < date(2018)")
expect(ast.evaluate).to be_a(Keisan::AST::Boolean)
expect(ast.evaluate.value).to eq true
ast = Keisan::AST.parse("date(2017, 10, 10) <= date(2017, 10, 10)")
expect(ast.evaluate).to be_a(Keisan::AST::Boolean)
expect(ast.evaluate.value).to eq true
ast = Keisan::AST.parse("date(1999, 12, 31) == date('1999-12-31')")
expect(ast.evaluate).to be_a(Keisan::AST::Boolean)
expect(ast.evaluate.value).to eq true
ast = Keisan::AST.parse("date(1999, 12, 31) != date('1999-12-31')")
expect(ast.evaluate).to be_a(Keisan::AST::Boolean)
expect(ast.evaluate.value).to eq false
ast = Keisan::AST.parse("date(2000) + date(2000)")
expect{ast.evaluate}.to raise_error(Keisan::Exceptions::InvalidExpression)
end
it "works in arrays" do
calculator = Keisan::Calculator.new
calculator.evaluate("a = [10, date(2018, 11, 10), today()]")
calculator.evaluate("a[1] += 1")
expect(calculator.evaluate("a[0] + a[1]").value).to eq Date.new(2018, 11, 21)
end
end
describe "date methods" do
describe "today" do
it "returns todays date" do
allow(Date).to receive(:today).and_return(Date.new(2000, 4, 15))
ast = Keisan::AST.parse("today()")
expect(ast.evaluate).to be_a(described_class)
expect(ast.evaluate.value).to eq Date.new(2000, 4, 15)
end
end
describe "weekday" do
it "returns the weekday number" do
ast = Keisan::AST.parse("date(2018, 11, 20).weekday()")
expect(ast.evaluate).to be_a(Keisan::AST::Number)
expect(ast.evaluate.value).to eq 2
end
end
describe "day" do
it "returns the day number in month" do
ast = Keisan::AST.parse("date(2018, 11, 20).day()")
expect(ast.evaluate).to be_a(Keisan::AST::Number)
expect(ast.evaluate.value).to eq 20
end
end
describe "month" do
it "returns the month number" do
ast = Keisan::AST.parse("date(2018, 11, 20).month()")
expect(ast.evaluate).to be_a(Keisan::AST::Number)
expect(ast.evaluate.value).to eq 11
end
end
describe "year" do
it "returns the year number" do
ast = Keisan::AST.parse("date(2018, 11, 20).year()")
expect(ast.evaluate).to be_a(Keisan::AST::Number)
expect(ast.evaluate.value).to eq 2018
end
end
describe "#to_time" do
it "converts date to time" do
calculator = Keisan::Calculator.new
time = calculator.ast("date(2018, 11, 20).to_time").evaluate
expect(time).to be_a(Keisan::AST::Time)
expect(time.to_s).to eq "2018-11-20 00:00:00"
end
end
describe "#epoch_days" do
it "returns the number of days since Unix epoch" do
calculator = Keisan::Calculator.new
time = calculator.ast("date(1970, 2, 2).epoch_days").evaluate
expect(time).to be_a(Keisan::AST::Number)
expect(time.value).to eq 32
end
end
describe "#epoch_seconds" do
it "returns the number of days since Unix epoch" do
calculator = Keisan::Calculator.new
time = calculator.ast("date(1970, 1, 2).epoch_seconds").evaluate
expect(time).to be_a(Keisan::AST::Number)
expect(time.value).to eq 86400
end
end
end
describe "#to_s" do
it "outputs correct date format" do
calculator = Keisan::Calculator.new
date = calculator.ast("date(2018, 11, 20)").evaluate
expect(date.to_s).to eq "2018-11-20"
end
end
end