summaryrefslogtreecommitdiff
path: root/test/models/concerns/rrule_humanizer_test.rb
blob: d74717113d85d54ce32564fac7141d4fed0b6d63 (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
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
require 'test_helper'

class RruleHumanizerTest < ActiveSupport::TestCase
  def humanize(rrule, locale = :de)
    Event.new(rrule: rrule).humanize_rrule(locale)
  end

  test "weekly single day" do
    assert_equal "Jeden Dienstag", humanize("FREQ=WEEKLY;BYDAY=TU")
    assert_equal "Every Tuesday", humanize("FREQ=WEEKLY;BYDAY=TU", :en)
  end

  test "weekly two days" do
    assert_equal "Jeden Mittwoch und Freitag", humanize("FREQ=WEEKLY;BYDAY=WE,FR")
    assert_equal "Every Wednesday and Friday", humanize("FREQ=WEEKLY;BYDAY=WE,FR", :en)
  end

  test "weekly no byday" do
    assert_equal "Wöchentlich", humanize("FREQ=WEEKLY")
    assert_equal "Weekly", humanize("FREQ=WEEKLY", :en)
  end

  test "biweekly with day" do
    assert_equal "Alle zwei Wochen donnerstags", humanize("FREQ=WEEKLY;INTERVAL=2;BYDAY=TH")
    assert_equal "Every other Thursday", humanize("FREQ=WEEKLY;INTERVAL=2;BYDAY=TH", :en)
  end

  test "biweekly no day" do
    assert_equal "Alle zwei Wochen", humanize("FREQ=WEEKLY;INTERVAL=2")
    assert_equal "Every other week", humanize("FREQ=WEEKLY;INTERVAL=2", :en)
  end

  test "monthly nth weekday" do
    assert_equal "Jeden ersten Dienstag im Monat", humanize("FREQ=MONTHLY;BYDAY=1TU")
    assert_equal "Jeden zweiten Freitag im Monat", humanize("FREQ=MONTHLY;BYDAY=2FR")
    assert_equal "Jeden dritten Sonntag im Monat", humanize("FREQ=MONTHLY;BYDAY=3SU")
    assert_equal "Jeden letzten Mittwoch im Monat", humanize("FREQ=MONTHLY;BYDAY=-1WE")
  end

  test "monthly nth weekday english" do
    assert_equal "Every first Tuesday of the month", humanize("FREQ=MONTHLY;BYDAY=1TU", :en)
    assert_equal "Every last Wednesday of the month", humanize("FREQ=MONTHLY;BYDAY=-1WE", :en)
  end

  test "monthly second-to-last" do
    assert_equal "Jeden vorletzten Donnerstag im Monat", humanize("FREQ=MONTHLY;BYDAY=-2TH")
    assert_equal "Every second-to-last Thursday of the month", humanize("FREQ=MONTHLY;BYDAY=-2TH", :en)
  end

  test "monthly selected weeks" do
    assert_equal "Jeden ersten, zweiten und fünften Dienstag im Monat",
      humanize("FREQ=MONTHLY;BYDAY=1TU,2TU,5TU")
    assert_equal "Every first, second and fifth Tuesday of the month",
      humanize("FREQ=MONTHLY;BYDAY=1TU,2TU,5TU", :en)
  end

  test "monthly fifth weekday" do
    assert_equal "Jeden fünften Dienstag im Monat", humanize("FREQ=MONTHLY;BYDAY=5TU")
    assert_equal "Every fifth Tuesday of the month", humanize("FREQ=MONTHLY;BYDAY=5TU", :en)
  end

  test "monthly weekday excluding one ordinal" do
    rrule = "FREQ=MONTHLY;BYDAY=1TU,3TU,4TU,5TU"
    assert_equal "Jeden Dienstag im Monat, außer dem zweiten Dienstag", humanize(rrule)
    assert_equal "Every Tuesday of the month, except the second Tuesday", humanize(rrule, :en)
  end

  test "monthly no byday" do
    assert_equal "Monatlich", humanize("FREQ=MONTHLY")
    assert_equal "Monthly", humanize("FREQ=MONTHLY", :en)
  end

  test "monthly with single excluded month" do
    assert_equal "Jeden letzten Donnerstag im Monat, außer im Dezember",
      humanize("FREQ=MONTHLY;BYDAY=-1TH;BYMONTH=1,2,3,4,5,6,7,8,9,10,11")
    assert_equal "Every last Thursday of the month, except in December",
      humanize("FREQ=MONTHLY;BYDAY=-1TH;BYMONTH=1,2,3,4,5,6,7,8,9,10,11", :en)
  end

  test "monthly excluding january" do
    assert_equal "Jeden zweiten Mittwoch im Monat, außer im Januar",
      humanize("FREQ=MONTHLY;BYMONTH=2,3,4,5,6,7,8,9,10,11,12;BYDAY=2WE")
  end

  test "blank rrule returns nil" do
    assert_nil humanize(nil)
    assert_nil humanize("")
  end

  test "count and until are not guessed at" do
    assert_nil humanize("FREQ=MONTHLY;BYDAY=1WE;COUNT=36")
    assert_nil humanize("FREQ=MONTHLY;BYDAY=1WE;UNTIL=20050105T222222Z")
  end

  test "unrecognized freq returns nil" do
    assert_nil humanize("FREQ=YEARLY;BYMONTH=12")
  end

  test "falls back to english for unknown locale" do
    assert_equal "Every Tuesday", humanize("FREQ=WEEKLY;BYDAY=TU", :fr)
  end

  test "wday_abbr returns the correct German abbreviation for each day" do
    monday = Time.parse("2026-07-06") # confirmed Monday
    assert_equal "Mo", RruleHumanizer.wday_abbr(monday, :de)
    assert_equal "Di", RruleHumanizer.wday_abbr(monday + 1.day, :de)
    assert_equal "Mi", RruleHumanizer.wday_abbr(monday + 2.days, :de)
    assert_equal "Do", RruleHumanizer.wday_abbr(monday + 3.days, :de)
    assert_equal "Fr", RruleHumanizer.wday_abbr(monday + 4.days, :de)
    assert_equal "Sa", RruleHumanizer.wday_abbr(monday + 5.days, :de)
    assert_equal "So", RruleHumanizer.wday_abbr(monday + 6.days, :de)
  end

  test "wday_abbr falls back to :de for an unrecognized locale" do
    assert_equal "Mo", RruleHumanizer.wday_abbr(Time.parse("2026-07-06"), :fr)
  end

  test "monthly two selected weeks" do
    assert_equal "Jeden ersten und dritten Dienstag im Monat",
      humanize("FREQ=MONTHLY;BYDAY=1TU,3TU")
    assert_equal "Every first and third Tuesday of the month",
      humanize("FREQ=MONTHLY;BYDAY=1TU,3TU", :en)
  end
end