1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2023 Christos Margiolis <christos@FreeBSD.org>
5#
6
7get_months_fmt()
8{
9	rm -f in
10        for i in $(seq 12 1); do
11                printf "2000-%02d-01\n" ${i} | xargs -I{} \
12                date -jf "%Y-%m-%d" {} "${1}" >>in
13        done
14}
15
16atf_test_case monthsort_english
17monthsort_english_head()
18{
19	atf_set "descr" "Test the -M flag with English months"
20}
21monthsort_english_body()
22{
23	export LC_TIME="en_US.UTF-8"
24
25	cat >expout <<EOF
26January
27February
28March
29April
30May
31June
32July
33August
34September
35October
36November
37December
38EOF
39
40	# No need to test the rest of the formats (%b and %OB) as %b is a
41	# substring of %B and %OB is the same as %B.
42	get_months_fmt '+%B'
43	atf_check -o file:expout sort -M in
44}
45
46atf_test_case monthsort_all_formats_greek
47monthsort_all_formats_greek_head()
48{
49	atf_set "descr" "Test the -M flag with all possible Greek month formats"
50}
51monthsort_all_formats_greek_body()
52{
53	# Test with the Greek locale, since, unlike English, the
54	# abbreviation/full-name and standalone formats are different.
55	export LC_TIME="el_GR.UTF-8"
56
57	# Abbreviation format (e.g Jan, ������)
58	cat >expout <<EOF
59??????
60??????
61??????
62??????
63??????
64????????
65????????
66??????
67??????
68??????
69??????
70??????
71EOF
72	get_months_fmt '+%b'
73	atf_check -o file:expout sort -M in
74
75	# Full-name format (e.g January, ��������������������)
76	cat >expout <<EOF
77????????????????????
78??????????????????????
79??????????????
80????????????????
81??????????
82??????????????
83??????????????
84??????????????????
85??????????????????????
86??????????????????
87??????????????????
88????????????????????
89EOF
90	get_months_fmt '+%B'
91	atf_check -o file:expout sort -M in
92
93	# Standalone format (e.g January, ��������������������)
94	cat >expout <<EOF
95????????????????????
96??????????????????????
97??????????????
98????????????????
99??????????
100??????????????
101??????????????
102??????????????????
103??????????????????????
104??????????????????
105??????????????????
106????????????????????
107EOF
108	get_months_fmt '+%OB'
109	atf_check -o file:expout sort -M in
110}
111
112atf_test_case monthsort_mixed_formats_greek
113monthsort_mixed_formats_greek_head()
114{
115	atf_set "descr" "Test the -M flag with mixed Greek month formats"
116}
117monthsort_mixed_formats_greek_body()
118{
119	export LC_TIME="el_GR.UTF-8"
120
121	cat >in <<EOF
122????????????????????
123??????????????????
124??????
125??????
126??????
127??????????????
128??????????????
129??????????
130????????????????
131??????????????
132??????????????????????
133????????????????????
134EOF
135
136	cat >expout <<EOF
137????????????????????
138??????????????????????
139??????????????
140????????????????
141??????????
142??????????????
143??????????????
144??????
145??????
146??????
147??????????????????
148????????????????????
149EOF
150
151	atf_check -o file:expout sort -M in
152}
153
154atf_init_test_cases()
155{
156	atf_add_test_case monthsort_english
157	atf_add_test_case monthsort_all_formats_greek
158	atf_add_test_case monthsort_mixed_formats_greek
159}
160