holidays 3.3.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -0
  3. data/CONTRIBUTING.md +37 -0
  4. data/README.md +16 -25
  5. data/REFERENCES +4 -1
  6. data/Rakefile +38 -8
  7. data/benchmark.rb +8 -0
  8. data/definitions/README.md +187 -8
  9. data/definitions/ar.yaml +2 -1
  10. data/definitions/at.yaml +17 -13
  11. data/definitions/au.yaml +65 -60
  12. data/definitions/be_fr.yaml +14 -10
  13. data/definitions/be_nl.yaml +8 -4
  14. data/definitions/bg.yaml +12 -6
  15. data/definitions/br.yaml +6 -3
  16. data/definitions/ca.yaml +8 -6
  17. data/definitions/ch.yaml +21 -15
  18. data/definitions/cl.yaml +4 -2
  19. data/definitions/cr.yaml +5 -3
  20. data/definitions/cz.yaml +4 -2
  21. data/definitions/de.yaml +25 -14
  22. data/definitions/dk.yaml +26 -17
  23. data/definitions/ecb_target.yaml +4 -2
  24. data/definitions/el.yaml +23 -18
  25. data/definitions/es.yaml +31 -28
  26. data/definitions/federal_reserve.yaml +12 -12
  27. data/definitions/fedex.yaml +6 -6
  28. data/definitions/fi.yaml +26 -25
  29. data/definitions/fr.yaml +8 -4
  30. data/definitions/gb.yaml +9 -7
  31. data/definitions/hr.yaml +8 -6
  32. data/definitions/hu.yaml +8 -6
  33. data/definitions/ie.yaml +17 -16
  34. data/definitions/index.yaml +1 -1
  35. data/definitions/is.yaml +29 -19
  36. data/definitions/it.yaml +10 -9
  37. data/definitions/jp.yaml +92 -44
  38. data/definitions/li.yaml +25 -20
  39. data/definitions/lt.yaml +2 -1
  40. data/definitions/ma.yaml +7 -7
  41. data/definitions/mx.yaml +11 -11
  42. data/definitions/nerc.yaml +6 -6
  43. data/definitions/nl.yaml +22 -18
  44. data/definitions/no.yaml +19 -11
  45. data/definitions/north_america_informal.yaml +6 -6
  46. data/definitions/nyse.yaml +9 -8
  47. data/definitions/nz.yaml +33 -29
  48. data/definitions/ph.yaml +15 -8
  49. data/definitions/pl.yaml +27 -17
  50. data/definitions/pt.yaml +4 -2
  51. data/definitions/ro.yaml +21 -18
  52. data/definitions/se.yaml +24 -18
  53. data/definitions/sg.yaml +10 -9
  54. data/definitions/si.yaml +4 -2
  55. data/definitions/sk.yaml +4 -2
  56. data/definitions/united_nations.yaml +12 -12
  57. data/definitions/ups.yaml +6 -6
  58. data/definitions/us.yaml +12 -11
  59. data/definitions/ve.yaml +8 -4
  60. data/definitions/vi.yaml +6 -6
  61. data/definitions/za.yaml +26 -24
  62. data/holidays.gemspec +3 -1
  63. data/lib/generated_definitions/MANIFEST +1 -1
  64. data/lib/generated_definitions/ar.rb +8 -6
  65. data/lib/generated_definitions/at.rb +11 -9
  66. data/lib/generated_definitions/au.rb +75 -99
  67. data/lib/generated_definitions/be.rb +12 -10
  68. data/lib/generated_definitions/be_fr.rb +12 -10
  69. data/lib/generated_definitions/be_nl.rb +12 -10
  70. data/lib/generated_definitions/bg.rb +14 -13
  71. data/lib/generated_definitions/br.rb +11 -9
  72. data/lib/generated_definitions/ca.rb +20 -20
  73. data/lib/generated_definitions/ch.rb +41 -44
  74. data/lib/generated_definitions/cl.rb +9 -7
  75. data/lib/generated_definitions/cr.rb +9 -7
  76. data/lib/generated_definitions/cz.rb +9 -6
  77. data/lib/generated_definitions/de.rb +25 -25
  78. data/lib/generated_definitions/dk.rb +17 -15
  79. data/lib/generated_definitions/ecb_target.rb +9 -7
  80. data/lib/generated_definitions/el.rb +13 -11
  81. data/lib/generated_definitions/es.rb +35 -33
  82. data/lib/generated_definitions/europe.rb +234 -247
  83. data/lib/generated_definitions/federal_reserve.rb +11 -9
  84. data/lib/generated_definitions/fedex.rb +42 -0
  85. data/lib/generated_definitions/fi.rb +32 -36
  86. data/lib/generated_definitions/fr.rb +12 -10
  87. data/lib/generated_definitions/gb.rb +15 -13
  88. data/lib/generated_definitions/hr.rb +10 -8
  89. data/lib/generated_definitions/hu.rb +9 -7
  90. data/lib/generated_definitions/ie.rb +17 -17
  91. data/lib/generated_definitions/is.rb +26 -26
  92. data/lib/generated_definitions/it.rb +9 -7
  93. data/lib/generated_definitions/jp.rb +112 -89
  94. data/lib/generated_definitions/li.rb +14 -12
  95. data/lib/generated_definitions/lt.rb +9 -7
  96. data/lib/generated_definitions/ma.rb +7 -5
  97. data/lib/generated_definitions/mx.rb +7 -5
  98. data/lib/generated_definitions/nerc.rb +10 -8
  99. data/lib/generated_definitions/nl.rb +15 -13
  100. data/lib/generated_definitions/no.rb +16 -14
  101. data/lib/generated_definitions/north_america.rb +34 -37
  102. data/lib/generated_definitions/nyse.rb +10 -8
  103. data/lib/generated_definitions/nz.rb +40 -40
  104. data/lib/generated_definitions/ph.rb +17 -13
  105. data/lib/generated_definitions/pl.rb +25 -27
  106. data/lib/generated_definitions/pt.rb +10 -8
  107. data/lib/generated_definitions/ro.rb +11 -9
  108. data/lib/generated_definitions/scandinavia.rb +92 -102
  109. data/lib/generated_definitions/se.rb +25 -27
  110. data/lib/generated_definitions/sg.rb +11 -9
  111. data/lib/generated_definitions/si.rb +10 -8
  112. data/lib/generated_definitions/sk.rb +9 -7
  113. data/lib/generated_definitions/united_nations.rb +7 -5
  114. data/lib/generated_definitions/ups.rb +13 -12
  115. data/lib/generated_definitions/us.rb +20 -21
  116. data/lib/generated_definitions/ve.rb +11 -9
  117. data/lib/generated_definitions/vi.rb +7 -5
  118. data/lib/generated_definitions/za.rb +19 -17
  119. data/lib/holidays.rb +20 -83
  120. data/lib/holidays/date_calculator/weekend_modifier.rb +22 -5
  121. data/lib/holidays/definition/context/generator.rb +67 -29
  122. data/lib/holidays/definition/context/merger.rb +8 -8
  123. data/lib/holidays/definition/decorator/custom_method_proc.rb +28 -0
  124. data/lib/holidays/definition/decorator/custom_method_source.rb +30 -0
  125. data/lib/holidays/definition/entity/custom_method.rb +11 -0
  126. data/lib/holidays/definition/parser/custom_method.rb +69 -0
  127. data/lib/holidays/definition/repository/custom_methods.rb +27 -0
  128. data/lib/holidays/definition/repository/holidays_by_month.rb +1 -1
  129. data/lib/holidays/definition/repository/{proc_cache.rb → proc_result_cache.rb} +19 -4
  130. data/lib/holidays/definition/validator/custom_method.rb +31 -0
  131. data/lib/holidays/definition_factory.rb +42 -6
  132. data/lib/holidays/errors.rb +6 -0
  133. data/lib/holidays/load_all_definitions.rb +57 -0
  134. data/lib/holidays/option/context/parse_options.rb +26 -16
  135. data/lib/holidays/option_factory.rb +1 -0
  136. data/lib/holidays/use_case/context/between.rb +41 -14
  137. data/lib/holidays/use_case_factory.rb +2 -1
  138. data/lib/holidays/version.rb +1 -1
  139. data/test/data/test_single_custom_holiday_with_custom_procs.yaml +24 -0
  140. data/test/defs/test_defs_at.rb +1 -1
  141. data/test/defs/test_defs_au.rb +3 -2
  142. data/test/defs/test_defs_cr.rb +1 -0
  143. data/test/defs/test_defs_cz.rb +1 -0
  144. data/test/defs/test_defs_dk.rb +2 -2
  145. data/test/defs/test_defs_el.rb +7 -6
  146. data/test/defs/test_defs_europe.rb +40 -33
  147. data/test/defs/test_defs_fedex.rb +24 -0
  148. data/test/defs/test_defs_fi.rb +4 -3
  149. data/test/defs/test_defs_hr.rb +2 -2
  150. data/test/defs/test_defs_hu.rb +2 -2
  151. data/test/defs/test_defs_is.rb +2 -1
  152. data/test/defs/test_defs_it.rb +2 -1
  153. data/test/defs/test_defs_jp.rb +1 -1
  154. data/test/defs/test_defs_li.rb +1 -1
  155. data/test/defs/test_defs_ma.rb +2 -1
  156. data/test/defs/test_defs_mx.rb +4 -3
  157. data/test/defs/test_defs_nl.rb +7 -6
  158. data/test/defs/test_defs_no.rb +1 -0
  159. data/test/defs/test_defs_north_america.rb +4 -3
  160. data/test/defs/test_defs_nyse.rb +2 -1
  161. data/test/defs/test_defs_pl.rb +1 -0
  162. data/test/defs/test_defs_ro.rb +11 -11
  163. data/test/defs/test_defs_scandinavia.rb +12 -8
  164. data/test/defs/test_defs_se.rb +3 -2
  165. data/test/defs/test_defs_sg.rb +2 -1
  166. data/test/defs/test_defs_vi.rb +1 -1
  167. data/test/defs/test_defs_za.rb +3 -2
  168. data/test/holidays/date_calculator/test_weekend_modifier.rb +11 -0
  169. data/test/holidays/definition/context/test_generator.rb +64 -5
  170. data/test/holidays/definition/context/test_merger.rb +5 -2
  171. data/test/holidays/definition/decorator/test_custom_method_proc.rb +113 -0
  172. data/test/holidays/definition/decorator/test_custom_method_source.rb +96 -0
  173. data/test/holidays/definition/parser/test_custom_method.rb +79 -0
  174. data/test/holidays/definition/repository/test_custom_methods.rb +43 -0
  175. data/test/holidays/definition/repository/test_holidays_by_month.rb +0 -32
  176. data/test/holidays/definition/repository/test_proc_result_cache.rb +84 -0
  177. data/test/holidays/definition/validator/test_custom_method.rb +89 -0
  178. data/test/holidays/option/context/test_parse_options.rb +5 -0
  179. data/test/holidays/test_definition_factory.rb +17 -2
  180. data/test/holidays/use_case/context/test_between.rb +2 -0
  181. data/test/test_all_regions.rb +7 -49
  182. data/test/test_custom_holidays.rb +8 -2
  183. data/test/test_helper.rb +9 -2
  184. data/test/test_holidays.rb +9 -29
  185. metadata +46 -11
  186. data/lib/generated_definitions/fed_ex.rb +0 -41
  187. data/test/holidays/definition/repository/test_proc_cache.rb +0 -29
  188. data/test/test_parse_definitions.rb +0 -30
@@ -58,8 +58,8 @@ end
58
58
  end
59
59
 
60
60
 
61
- {Date.civil(2007,1,1) => 'Año nuevo',
62
- Date.civil(2007,2,5) => 'Día de la Constitución',
61
+ {Date.civil(2007,1,1) => 'Año nuevo',
62
+ Date.civil(2007,2,5) => 'Día de la Constitución',
63
63
  Date.civil(2007,5,1) => 'Día del Trabajo',
64
64
  Date.civil(2007,5,5) => 'Cinco de Mayo',
65
65
  Date.civil(2007,9,16) => 'Día de la Independencia',
@@ -68,7 +68,8 @@ end
68
68
  Date.civil(2007,11,19) => 'Día de la Revolución',
69
69
  Date.civil(2007,12,25) => 'Navidad'}.each do |date, name|
70
70
  assert_equal name, (Holidays.on(date, :mx, :informal)[0] || {})[:name]
71
- end
71
+ end
72
+
72
73
 
73
74
  {Date.civil(2008,1,1) => 'New Year\'s Day',
74
75
  Date.civil(2008,1,21) => 'Martin Luther King, Jr. Day',
@@ -7,7 +7,7 @@ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
7
7
  class NyseDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_nyse
10
- {Date.civil(2008,1,1) => 'New Year\'s Day',
10
+ {Date.civil(2008,1,1) => 'New Year\'s Day',
11
11
  Date.civil(2008,1,21) => 'Martin Luther King, Jr. Day',
12
12
  Date.civil(2008,2,18) => 'Presidents\' Day',
13
13
  Date.civil(2008,3,21) => 'Good Friday',
@@ -18,5 +18,6 @@ class NyseDefinitionTests < Test::Unit::TestCase # :nodoc:
18
18
  Date.civil(2008,12,25) => 'Christmas Day'}.each do |date, name|
19
19
  assert_equal name, (Holidays.on(date, :nyse)[0] || {})[:name]
20
20
  end
21
+
21
22
  end
22
23
  end
@@ -141,5 +141,6 @@ end
141
141
  # Wniebowzięcie Najświętszej Maryi Panny & Święto Wojska Polskiego
142
142
  assert_equal 'Wniebowzięcie Najświętszej Maryi Panny', (Holidays.on(Date.civil(2011,8,15), :pl)[0] || {})[:name]
143
143
  assert_equal 'Święto Wojska Polskiego', (Holidays.on(Date.civil(2011,8,15), :pl)[1] || {})[:name]
144
+
144
145
  end
145
146
  end
@@ -8,17 +8,17 @@ class RoDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_ro
10
10
  {
11
- Date.civil(1961,4,9) => 'Paștele',
12
- Date.civil(1961,4,10) => 'Paștele',
13
- Date.civil(2013,5,5) => 'Paștele',
14
- Date.civil(2013,5,6) => 'Paștele',
15
- Date.civil(2027,5,2) => 'Paștele',
16
- Date.civil(2027,5,3) => 'Paștele',
17
- Date.civil(2012,6,4) => 'Rusaliile',
18
- Date.civil(2013,6,23) => 'Rusaliile',
19
- Date.civil(2013,6,24) => 'Rusaliile',
20
- Date.civil(1986,6,22) => 'Rusaliile',
21
- Date.civil(1986,6,23) => 'Rusaliile',
11
+ Date.civil(1961,4,9) => 'Paștele - duminică',
12
+ Date.civil(1961,4,10) => 'Paștele - luni',
13
+ Date.civil(2013,5,5) => 'Paștele - duminică',
14
+ Date.civil(2013,5,6) => 'Paștele - luni',
15
+ Date.civil(2027,5,2) => 'Paștele - duminică',
16
+ Date.civil(2027,5,3) => 'Paștele - luni',
17
+ Date.civil(2012,6,4) => 'Rusaliile - 51',
18
+ Date.civil(2013,6,23) => 'Rusaliile - 50',
19
+ Date.civil(2013,6,24) => 'Rusaliile - 51',
20
+ Date.civil(1986,6,22) => 'Rusaliile - 50',
21
+ Date.civil(1986,6,23) => 'Rusaliile - 51',
22
22
  Date.civil(2009,1,1) => 'Anul nou',
23
23
  Date.civil(2014,1,2) => 'Anul nou',
24
24
  Date.civil(2009,5,1) => 'Ziua muncii',
@@ -7,8 +7,8 @@ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
7
7
  class ScandinaviaDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_scandinavia
10
- {Date.civil(2007,1,1) => 'Nytårsdag',
11
- Date.civil(2007,2,18) => 'Fastelavn',
10
+ {Date.civil(2007,1,1) => 'Nytårsdag',
11
+ Date.civil(2007,2,18) => 'Fastelavn',
12
12
  Date.civil(2007,4,9) => 'Danmarks besættelse',
13
13
  Date.civil(2007,4,16) => 'Dronningens fødselsdag',
14
14
  Date.civil(2007,4,5) => 'Skærtorsdag',
@@ -28,7 +28,7 @@ class ScandinaviaDefinitionTests < Test::Unit::TestCase # :nodoc:
28
28
  end
29
29
 
30
30
 
31
- {Date.civil(2007,1,1) => 'Nýársdagur',
31
+ {Date.civil(2007,1,1) => 'Nýársdagur',
32
32
  Date.civil(2007,1,6) => 'Þrettándinn',
33
33
  Date.civil(2007,1,19) => 'Bóndadagur',
34
34
  Date.civil(2007,2,18) => 'Konudagur',
@@ -51,6 +51,7 @@ end
51
51
  assert_equal name, (Holidays.on(date, :is, :informal)[0] || {})[:name]
52
52
  end
53
53
 
54
+
54
55
  {Date.civil(2010,1,1) => 'Nyttårsdag',
55
56
  Date.civil(2010,5,1) => '1. mai',
56
57
  Date.civil(2010,5,17) => '17. mai',
@@ -70,11 +71,12 @@ end
70
71
  assert_equal name, (Holidays.on(date, :no, :informal)[0] || {})[:name]
71
72
  end
72
73
 
73
- {Date.civil(2008,1,1) => 'Nyårsdagen',
74
+
75
+ {Date.civil(2008,1,1) => 'Nyårsdagen',
74
76
  Date.civil(2008,1,6) => 'Trettondedag jul',
75
77
  Date.civil(2008,3,21) => 'Långfredagen',
76
78
  Date.civil(2008,3,22) => 'Påskafton',
77
- Date.civil(2008,3,23) => 'Påskdagen',
79
+ Date.civil(2008,3,23) => 'Påskdagen',
78
80
  Date.civil(2008,3,24) => 'Annandag påsk',
79
81
  Date.civil(2008,5,1) => 'Första maj',
80
82
  Date.civil(2008,5,1) => 'Kristi himmelsfärdsdag',
@@ -99,10 +101,11 @@ end
99
101
  assert_equal name, (Holidays.on(date, :se, :informal)[0] || {})[:name]
100
102
  end
101
103
 
102
- {Date.civil(2008,1,1) => 'Uudenvuodenpäivä',
104
+
105
+ {Date.civil(2008,1,1) => 'Uudenvuodenpäivä',
103
106
  Date.civil(2008,1,6) => 'Loppiainen',
104
- Date.civil(2008,3,21) => 'Pitkäperjantai',
105
- Date.civil(2008,3,23) => 'Pääsiäispäivä',
107
+ Date.civil(2008,3,21) => 'Pitkäperjantai',
108
+ Date.civil(2008,3,23) => 'Pääsiäispäivä',
106
109
  Date.civil(2008,3,24) => '2. Pääsiäispäivä',
107
110
  Date.civil(2008,5,1) => 'Vappu',
108
111
  Date.civil(2008,5,1) => 'Helatorstai',
@@ -126,5 +129,6 @@ end
126
129
  Date.civil(2008,12,26) => 'Tapaninpäivä'}.each do |date, name|
127
130
  assert_equal name, (Holidays.on(date, :fi, :informal)[0] || {})[:name]
128
131
  end
132
+
129
133
  end
130
134
  end
@@ -7,11 +7,11 @@ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
7
7
  class SeDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_se
10
- {Date.civil(2008,1,1) => 'Nyårsdagen',
10
+ {Date.civil(2008,1,1) => 'Nyårsdagen',
11
11
  Date.civil(2008,1,6) => 'Trettondedag jul',
12
12
  Date.civil(2008,3,21) => 'Långfredagen',
13
13
  Date.civil(2008,3,22) => 'Påskafton',
14
- Date.civil(2008,3,23) => 'Påskdagen',
14
+ Date.civil(2008,3,23) => 'Påskdagen',
15
15
  Date.civil(2008,3,24) => 'Annandag påsk',
16
16
  Date.civil(2008,5,1) => 'Första maj',
17
17
  Date.civil(2008,5,1) => 'Kristi himmelsfärdsdag',
@@ -35,5 +35,6 @@ class SeDefinitionTests < Test::Unit::TestCase # :nodoc:
35
35
  }.each do |date, name|
36
36
  assert_equal name, (Holidays.on(date, :se, :informal)[0] || {})[:name]
37
37
  end
38
+
38
39
  end
39
40
  end
@@ -7,7 +7,7 @@ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
7
7
  class SgDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_sg
10
- {Date.civil(2014,1,1) => 'New Year\'s Day',
10
+ {Date.civil(2014,1,1) => 'New Year\'s Day',
11
11
  Date.civil(2014,2,14) => 'Valentine\'s Day',
12
12
  Date.civil(2014,2,15) => 'Total Defence Day',
13
13
  Date.civil(2014,4,18) => 'Good Friday',
@@ -16,5 +16,6 @@ class SgDefinitionTests < Test::Unit::TestCase # :nodoc:
16
16
  Date.civil(2014,12,25) => 'Christmas Day'}.each do |date, name|
17
17
  assert_equal name, (Holidays.on(date, :sg, :informal)[0] || {})[:name]
18
18
  end
19
+
19
20
  end
20
21
  end
@@ -7,7 +7,7 @@ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
7
7
  class ViDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_vi
10
- {Date.civil(2014,1,1) => 'New Year',
10
+ {Date.civil(2014,1,1) => 'New Year',
11
11
  Date.civil(2014,4,30) => 'Liberation Day',
12
12
  Date.civil(2014,5,1) => "International Workers' Day",
13
13
  Date.civil(2014,9,2) => 'National Day'}.each do |date, name|
@@ -7,8 +7,8 @@ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
7
7
  class ZaDefinitionTests < Test::Unit::TestCase # :nodoc:
8
8
 
9
9
  def test_za
10
- {Date.civil(2007,1,1) => 'New Year\'s Day',
11
- Date.civil(2007,3,21) => 'Human Rights Day',
10
+ {Date.civil(2007,1,1) => 'New Year\'s Day',
11
+ Date.civil(2007,3,21) => 'Human Rights Day',
12
12
  Date.civil(2007,4,6) => 'Good Friday',
13
13
  Date.civil(2007,4,9) => 'Family Day',
14
14
  Date.civil(2007,4,27) => 'Freedom Day',
@@ -21,5 +21,6 @@ class ZaDefinitionTests < Test::Unit::TestCase # :nodoc:
21
21
  Date.civil(2007,12,26) => 'Day of Goodwill'}.each do |date, name|
22
22
  assert_equal name, (Holidays.on(date, :za, :informal)[0] || {})[:name]
23
23
  end
24
+
24
25
  end
25
26
  end
@@ -10,10 +10,15 @@ class WeekendModifierDateCalculatorTests < Test::Unit::TestCase
10
10
  def test_to_monday_if_weekend
11
11
  assert_equal Date.civil(2015, 5, 4), @subject.to_monday_if_weekend(Date.civil(2015, 5, 3))
12
12
  assert_equal Date.civil(2015, 5, 4), @subject.to_monday_if_weekend(Date.civil(2015, 5, 2))
13
+ assert_equal Date.civil(2015, 5, 4), @subject.to_monday_if_weekend(Date.civil(2015, 5, 4))
14
+ assert_equal Date.civil(2015, 5, 1), @subject.to_monday_if_weekend(Date.civil(2015, 5, 1))
13
15
  end
14
16
 
15
17
  def test_to_monday_if_sunday
16
18
  assert_equal Date.civil(2015, 5, 4), @subject.to_monday_if_sunday(Date.civil(2015, 5, 3))
19
+ assert_equal Date.civil(2015, 5, 4), @subject.to_monday_if_sunday(Date.civil(2015, 5, 4))
20
+ assert_equal Date.civil(2015, 5, 1), @subject.to_monday_if_sunday(Date.civil(2015, 5, 1))
21
+ assert_equal Date.civil(2015, 5, 2), @subject.to_monday_if_sunday(Date.civil(2015, 5, 2))
17
22
  end
18
23
 
19
24
  def test_to_weekday_if_boxing_weekend
@@ -40,4 +45,10 @@ class WeekendModifierDateCalculatorTests < Test::Unit::TestCase
40
45
  assert_equal Date.civil(2016, 12, 26), @subject.to_tuesday_if_sunday_or_monday_if_saturday(Date.civil(2016, 12, 24)) # sat to mon
41
46
  assert_equal Date.civil(2016, 12, 27), @subject.to_tuesday_if_sunday_or_monday_if_saturday(Date.civil(2016, 12, 25)) # sun to tue
42
47
  end
48
+
49
+ def test_to_next_weekday
50
+ assert_equal Date.civil(2016, 3, 21), @subject.to_next_weekday(Date.civil(2016, 3, 20))
51
+ assert_equal Date.civil(2016, 3, 22), @subject.to_next_weekday(Date.civil(2016, 3, 21))
52
+ assert_equal Date.civil(2016, 3, 28), @subject.to_next_weekday(Date.civil(2016, 3, 25))
53
+ end
43
54
  end
@@ -4,7 +4,21 @@ require 'holidays/definition/context/generator'
4
4
 
5
5
  class GeneratorTests < Test::Unit::TestCase
6
6
  def setup
7
- @generator = Holidays::Definition::Context::Generator.new
7
+ @custom_method_parser = mock()
8
+ @custom_method_source_decorator = mock()
9
+ @custom_methods_repository = mock()
10
+
11
+ @parsed_custom_method = Holidays::Definition::Entity::CustomMethod.new(
12
+ name: 'custom_method',
13
+ arguments: [:year, :month],
14
+ source: "some source",
15
+ )
16
+
17
+ @generator = Holidays::Definition::Context::Generator.new(
18
+ @custom_method_parser,
19
+ @custom_method_source_decorator,
20
+ @custom_methods_repository,
21
+ )
8
22
  end
9
23
 
10
24
  def test_parse_definition_files_raises_error_if_argument_is_nil
@@ -21,6 +35,8 @@ class GeneratorTests < Test::Unit::TestCase
21
35
 
22
36
  def test_parse_definition_files_correctly_parse_regions
23
37
  files = ['test/data/test_single_custom_holiday_defs.yaml']
38
+ @custom_method_parser.expects(:call).with(nil).returns({})
39
+
24
40
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
25
41
 
26
42
  assert_equal [:custom_single_file], regions
@@ -28,6 +44,8 @@ class GeneratorTests < Test::Unit::TestCase
28
44
 
29
45
  def test_parse_definitions_files_correctly_parse_rules_by_month
30
46
  files = ['test/data/test_single_custom_holiday_defs.yaml']
47
+ @custom_method_parser.expects(:call).with(nil).returns({})
48
+
31
49
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
32
50
 
33
51
  expected_rules_by_month = {
@@ -45,6 +63,8 @@ class GeneratorTests < Test::Unit::TestCase
45
63
 
46
64
  def test_parse_definition_files_correctly_parse_custom_methods
47
65
  files = ['test/data/test_single_custom_holiday_defs.yaml']
66
+ @custom_method_parser.expects(:call).with(nil).returns({})
67
+
48
68
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
49
69
 
50
70
  expected_custom_methods = {}
@@ -53,6 +73,8 @@ class GeneratorTests < Test::Unit::TestCase
53
73
 
54
74
  def test_parse_definition_files_correctly_parse_tests
55
75
  files = ['test/data/test_single_custom_holiday_defs.yaml']
76
+ @custom_method_parser.expects(:call).with(nil).returns({})
77
+
56
78
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
57
79
 
58
80
  expected_tests = [[
@@ -64,16 +86,20 @@ class GeneratorTests < Test::Unit::TestCase
64
86
 
65
87
  def test_generate_definition_source_correctly_generate_module_src
66
88
  files = ['test/data/test_single_custom_holiday_defs.yaml']
89
+ @custom_method_parser.expects(:call).with(nil).returns({})
90
+
67
91
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
68
92
  module_src, test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)
69
93
 
70
- expected_module_src = "# encoding: utf-8\nmodule Holidays\n # This file is generated by the Ruby Holidays gem.\n #\n # Definitions loaded: test/data/test_single_custom_holiday_defs.yaml\n #\n # To use the definitions in this file, load it right after you load the\n # Holiday gem:\n #\n # require 'holidays'\n # require 'generated_definitions/test'\n #\n # All the definitions are available at https://github.com/alexdunae/holidays\n module TEST # :nodoc:\n def self.defined_regions\n [:custom_single_file]\n end\n\n def self.holidays_by_month\n {\n 6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n }\n end\n end\n\n\nend\n\nHolidays.merge_defs(Holidays::TEST.defined_regions, Holidays::TEST.holidays_by_month)\n"
94
+ expected_module_src = "# encoding: utf-8\nmodule Holidays\n # This file is generated by the Ruby Holidays gem.\n #\n # Definitions loaded: test/data/test_single_custom_holiday_defs.yaml\n #\n # To use the definitions in this file, load it right after you load the\n # Holiday gem:\n #\n # require 'holidays'\n # require 'generated_definitions/test'\n #\n # All the definitions are available at https://github.com/holidays/holidays\n module TEST # :nodoc:\n def self.defined_regions\n [:custom_single_file]\n end\n\n def self.holidays_by_month\n {\n 6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n }\n end\n\n def self.custom_methods\n {\n \n }\n end\n end\nend\n"
71
95
 
72
96
  assert_equal expected_module_src, module_src
73
97
  end
74
98
 
75
- def test_generate_definition_source_correctly_genrate_test_src
99
+ def test_generate_definition_source_correctly_generate_test_src
76
100
  files = ['test/data/test_single_custom_holiday_defs.yaml']
101
+ @custom_method_parser.expects(:call).with(nil).returns({})
102
+
77
103
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
78
104
  module_src, test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)
79
105
 
@@ -84,6 +110,8 @@ class GeneratorTests < Test::Unit::TestCase
84
110
 
85
111
  def test_parse_definitions_files_correctly_parse_year_range_by_month
86
112
  files = ['test/data/test_custom_year_range_holiday_defs.yaml']
113
+ @custom_method_parser.expects(:call).with(nil).returns({})
114
+
87
115
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
88
116
 
89
117
  expected_rules_by_month = {
@@ -124,13 +152,44 @@ class GeneratorTests < Test::Unit::TestCase
124
152
  assert_equal expected_rules_by_month, rules_by_month
125
153
  end
126
154
 
127
- def test_generate_definition_source_correctly_genrate_yearrange_test_src
155
+ def test_generate_definition_source_correctly_generate_yearrange_test_src
128
156
  files = ['test/data/test_custom_year_range_holiday_defs.yaml']
157
+ @custom_method_parser.expects(:call).with(nil).returns({})
158
+
129
159
  regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
130
160
  module_src, test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)
131
- expected_module_src = "# encoding: utf-8\nmodule Holidays\n # This file is generated by the Ruby Holidays gem.\n #\n # Definitions loaded: test/data/test_custom_year_range_holiday_defs.yaml\n #\n # To use the definitions in this file, load it right after you load the\n # Holiday gem:\n #\n # require 'holidays'\n # require 'generated_definitions/test'\n #\n # All the definitions are available at https://github.com/alexdunae/holidays\n module TEST # :nodoc:\n def self.defined_regions\n [:custom_year_range_file]\n end\n\n def self.holidays_by_month\n {\n 6 => [{:mday => 1, :year_ranges => [{:after => 2016}],:name => \"after_year\", :regions => [:custom_year_range_file]},\n {:mday => 2, :year_ranges => [{:before => 2017}],:name => \"before_year\", :regions => [:custom_year_range_file]},\n {:mday => 3, :year_ranges => [{:between => 2016..2018}],:name => \"between_year\", :regions => [:custom_year_range_file]},\n {:mday => 4, :year_ranges => [{:limited => [2016, 2018, 2019]}],:name => \"limited_year\", :regions => [:custom_year_range_file]},\n {:mday => 5, :year_ranges => [{:before => 2015},{:after => 2017}],:name => \"multiple_conditions\", :regions => [:custom_year_range_file]}]\n }\n end\n end\n\n\nend\n\nHolidays.merge_defs(Holidays::TEST.defined_regions, Holidays::TEST.holidays_by_month)\n"
161
+ expected_module_src = "# encoding: utf-8\nmodule Holidays\n # This file is generated by the Ruby Holidays gem.\n #\n # Definitions loaded: test/data/test_custom_year_range_holiday_defs.yaml\n #\n # To use the definitions in this file, load it right after you load the\n # Holiday gem:\n #\n # require 'holidays'\n # require 'generated_definitions/test'\n #\n # All the definitions are available at https://github.com/holidays/holidays\n module TEST # :nodoc:\n def self.defined_regions\n [:custom_year_range_file]\n end\n\n def self.holidays_by_month\n {\n 6 => [{:mday => 1, :year_ranges => [{:after => 2016}],:name => \"after_year\", :regions => [:custom_year_range_file]},\n {:mday => 2, :year_ranges => [{:before => 2017}],:name => \"before_year\", :regions => [:custom_year_range_file]},\n {:mday => 3, :year_ranges => [{:between => 2016..2018}],:name => \"between_year\", :regions => [:custom_year_range_file]},\n {:mday => 4, :year_ranges => [{:limited => [2016, 2018, 2019]}],:name => \"limited_year\", :regions => [:custom_year_range_file]},\n {:mday => 5, :year_ranges => [{:before => 2015},{:after => 2017}],:name => \"multiple_conditions\", :regions => [:custom_year_range_file]}]\n }\n end\n\n def self.custom_methods\n {\n \n }\n end\n end\nend\n"
132
162
 
133
163
  assert_equal expected_module_src, module_src
134
164
  end
135
165
 
166
+ def test_generate_definition_source_correctly_generate_module_src_with_custom_methods
167
+ files = ['test/data/test_single_custom_holiday_with_custom_procs.yaml']
168
+
169
+ @custom_method_parser.expects(:call).with('custom_method' => {'arguments' => 'year, month', 'source' => "d = Date.civil(year, month, 1)\nd + 2\n"}).returns({"custom_method(year, month)" => @parsed_custom_method})
170
+ @custom_methods_repository.expects(:find).twice.with('custom_method(year, month)').returns(nil)
171
+ @custom_method_source_decorator.expects(:call).once.with(@parsed_custom_method).returns("\"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n}")
172
+
173
+ regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
174
+ module_src, test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)
175
+
176
+ expected_module_src = "# encoding: utf-8\nmodule Holidays\n # This file is generated by the Ruby Holidays gem.\n #\n # Definitions loaded: test/data/test_single_custom_holiday_with_custom_procs.yaml\n #\n # To use the definitions in this file, load it right after you load the\n # Holiday gem:\n #\n # require 'holidays'\n # require 'generated_definitions/test'\n #\n # All the definitions are available at https://github.com/holidays/holidays\n module TEST # :nodoc:\n def self.defined_regions\n [:custom_single_file]\n end\n\n def self.holidays_by_month\n {\n 0 => [{:function => \"custom_method(year, month)\", :function_arguments => [:year, :month], :function_modifier => 5, :name => \"Custom Holiday\", :regions => [:custom_single_file]}],\n 6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n }\n end\n\n def self.custom_methods\n {\n \"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n},\n\n\n }\n end\n end\nend\n"
177
+
178
+ assert_equal expected_module_src, module_src
179
+ end
180
+
181
+ def test_generate_definition_source_correctly_generate_test_src_with_custom_methods
182
+ files = ['test/data/test_single_custom_holiday_with_custom_procs.yaml']
183
+
184
+ @custom_method_parser.expects(:call).with('custom_method' => {'arguments' => 'year, month', 'source' => "d = Date.civil(year, month, 1)\nd + 2\n"}).returns({:parsed => "method"})
185
+ @custom_methods_repository.expects(:find).twice.with('custom_method(year, month)').returns(nil)
186
+ @custom_method_source_decorator.expects(:call).once.with("method").returns("\"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n}")
187
+
188
+ regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
189
+ module_src, test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)
190
+
191
+ expected_test_src = "# encoding: utf-8\nrequire File.expand_path(File.dirname(__FILE__)) + '/../test_helper'\n\n# This file is generated by the Ruby Holiday gem.\n#\n# Definitions loaded: test/data/test_single_custom_holiday_with_custom_procs.yaml\nclass TestDefinitionTests < Test::Unit::TestCase # :nodoc:\n\n def test_test\n{Date.civil(2013,6,20) => 'Company Founding'}.each do |date, name|\n assert_equal name, (Holidays.on(date, :custom_single_file)[0] || {})[:name]\nend\n\n{Date.civil(2015, 1, 1) => 'Custom Holiday'}.each do |date, name|\n assert_equal name, (Holidays.on(date, :custom_single_file)[0] || {}[:name]\nend\n\n end\nend\n"
192
+
193
+ assert_equal expected_test_src, test_src
194
+ end
136
195
  end
@@ -6,17 +6,20 @@ class MergerTests < Test::Unit::TestCase
6
6
  def setup
7
7
  @target_regions = [:new_region]
8
8
  @target_holidays = {0 => [:mday => 1, :name => "Test", :regions => [:test2, :test]]}
9
+ @target_custom_methods = {"test_method" => Proc.new { |year| Date.civil(year, 1, 1) } }
9
10
 
10
11
  @holidays_repo = mock()
11
12
  @regions_repo = mock()
13
+ @custom_methods_repo = mock()
12
14
 
13
- @subject = Holidays::Definition::Context::Merger.new(@holidays_repo, @regions_repo)
15
+ @subject = Holidays::Definition::Context::Merger.new(@holidays_repo, @regions_repo, @custom_methods_repo)
14
16
  end
15
17
 
16
18
  def test_repos_are_called_to_add_regions_and_holidays
17
19
  @holidays_repo.expects(:add).with(@target_holidays)
18
20
  @regions_repo.expects(:add).with(@target_regions)
21
+ @custom_methods_repo.expects(:add).with(@target_custom_methods)
19
22
 
20
- @subject.call(@target_regions, @target_holidays)
23
+ @subject.call(@target_regions, @target_holidays, @target_custom_methods)
21
24
  end
22
25
  end
@@ -0,0 +1,113 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../../../test_helper'
2
+
3
+ require 'holidays/definition/decorator/custom_method_proc'
4
+ require 'holidays/definition/entity/custom_method'
5
+
6
+ class DecoratorCustomMethodProcTests < Test::Unit::TestCase
7
+ def setup
8
+ @decorator = Holidays::Definition::Decorator::CustomMethodProc.new
9
+ end
10
+
11
+ def test_generates_lambda_from_entity
12
+ entity = Holidays::Definition::Entity::CustomMethod.new(
13
+ name: "test",
14
+ arguments: ["year"],
15
+ source: "Date.civil(year, 1, 1)"
16
+ )
17
+
18
+ proc = @decorator.call(entity)
19
+ expected_proc = Proc.new { |year| eval("Date.civil(year, 1, 1)") }
20
+
21
+ assert_equal expected_proc.call(2015), proc.call(2015)
22
+ end
23
+
24
+ def test_generates_lamba_from_entity_with_multiple_lines
25
+ entity = Holidays::Definition::Entity::CustomMethod.new(
26
+ name: "test",
27
+ arguments: ["year"],
28
+ source: "d = Date.civil(year, 1, 1)\nd + 2"
29
+ )
30
+
31
+ proc = @decorator.call(entity)
32
+ expected_proc = Proc.new { |year| eval("d = Date.civil(year, 1, 1)\nd + 2") }
33
+
34
+ assert_equal expected_proc.call(2015), proc.call(2015)
35
+ end
36
+
37
+ def test_generates_lamba_from_entity_with_multiple_arguments
38
+ entity = Holidays::Definition::Entity::CustomMethod.new(
39
+ name: "test",
40
+ arguments: ["year", "month"],
41
+ source: "Date.civil(year, month, 1)"
42
+ )
43
+
44
+ proc = @decorator.call(entity)
45
+ expected_proc = Proc.new { |year, month| eval("Date.civil(year, month, 1)") }
46
+
47
+ assert_equal expected_proc.call(2015, 6), proc.call(2015, 6)
48
+ end
49
+
50
+ def test_generate_returns_error_if_missing_name
51
+ entity = Holidays::Definition::Entity::CustomMethod.new(
52
+ arguments: ["year", "month"],
53
+ source: "Date.civil(year, month, 1)"
54
+ )
55
+
56
+ assert_raises ArgumentError do
57
+ @decorator.call(entity)
58
+ end
59
+
60
+ entity = Holidays::Definition::Entity::CustomMethod.new(
61
+ name: "",
62
+ arguments: ["year", "month"],
63
+ source: "Date.civil(year, month, 1)"
64
+ )
65
+
66
+ assert_raises ArgumentError do
67
+ @decorator.call(entity)
68
+ end
69
+ end
70
+
71
+ def test_generate_returns_error_if_arguments_is_missing
72
+ entity = Holidays::Definition::Entity::CustomMethod.new(
73
+ name: "test",
74
+ source: "Date.civil(year, month, 1)"
75
+ )
76
+
77
+ assert_raises ArgumentError do
78
+ @decorator.call(entity)
79
+ end
80
+
81
+ entity = Holidays::Definition::Entity::CustomMethod.new(
82
+ name: "test",
83
+ arguments: [],
84
+ source: "Date.civil(year, month, 1)"
85
+ )
86
+
87
+ assert_raises ArgumentError do
88
+ @decorator.call(entity)
89
+ end
90
+ end
91
+
92
+
93
+ def test_generate_returns_error_if_source_is_missing
94
+ entity = Holidays::Definition::Entity::CustomMethod.new(
95
+ name: "test",
96
+ arguments: ["year"],
97
+ )
98
+
99
+ assert_raises ArgumentError do
100
+ @decorator.call(entity)
101
+ end
102
+
103
+ entity = Holidays::Definition::Entity::CustomMethod.new(
104
+ name: "test",
105
+ arguments: ["year"],
106
+ source: "",
107
+ )
108
+
109
+ assert_raises ArgumentError do
110
+ @decorator.call(entity)
111
+ end
112
+ end
113
+ end