1+ /*
2+ * Copyright © 2026 Alain M. (https://github.com/alainm23/planify)
3+ *
4+ * This program is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 3 of the License, or (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public
15+ * License along with this program; if not, write to the
16+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+ * Boston, MA 02110-1301 USA
18+ */
19+
20+ /**
21+ * ArgumentParser Tests
22+ *
23+ * Tests for command-line argument parsing including:
24+ * - Command recognition (add, list, update, list-projects)
25+ * - Option parsing (short and long forms)
26+ * - Error handling (missing args, invalid values)
27+ */
28+
29+ namespace PlanifyCLI.Tests.ArgumentParser {
30+ void test_no_command () {
31+ print (" Testing: No command provided\n " );
32+ int exit_code;
33+ string [] args = {" planify-cli" };
34+
35+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
36+
37+ assert (parsed == null );
38+ assert (exit_code == 1 );
39+ print (" ✓ Returns null with exit code 1\n ---\n " );
40+ }
41+
42+ void test_unknown_command () {
43+ print (" Testing: Unknown command\n " );
44+ int exit_code;
45+ string [] args = {" planify-cli" , " invalid-command" };
46+
47+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
48+
49+ assert (parsed == null );
50+ assert (exit_code == 1 );
51+ print (" ✓ Returns null with exit code 1\n ---\n " );
52+ }
53+
54+ void test_list_projects () {
55+ print (" Testing: 'list-projects' command\n " );
56+ int exit_code;
57+ string [] args = {" planify-cli" , " list-projects" };
58+
59+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
60+
61+ assert (parsed != null );
62+ assert (exit_code == 0 );
63+ assert (parsed. command_type == PlanifyCLI . CommandType . LIST_PROJECTS );
64+ print (" ✓ Parses list-projects command\n ---\n " );
65+ }
66+
67+ void test_add_minimal () {
68+ print (" Testing: 'add' command with minimal args\n " );
69+ int exit_code;
70+ string [] args = {" planify-cli" , " add" , " -c" , " Test task" };
71+
72+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
73+
74+ assert (parsed != null );
75+ assert (exit_code == 0 );
76+ assert (parsed. command_type == PlanifyCLI . CommandType . ADD );
77+ assert (parsed. task_args != null );
78+ assert (parsed. task_args. content == " Test task" );
79+ print (" ✓ Parses add command with content\n ---\n " );
80+ }
81+
82+ void test_add_full () {
83+ print (" Testing: 'add' command with all options\n " );
84+ int exit_code;
85+ string [] args = {
86+ " planify-cli" , " add" ,
87+ " -c" , " Complete task" ,
88+ " -d" , " Task description" ,
89+ " -p" , " Work" ,
90+ " -s" , " In Progress" ,
91+ " -P" , " 1" ,
92+ " -D" , " 2024-12-31" ,
93+ " -l" , " urgent,important" ,
94+ " --pin" , " true"
95+ };
96+
97+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
98+
99+ assert (parsed != null );
100+ assert (exit_code == 0 );
101+ assert (parsed. command_type == PlanifyCLI . CommandType . ADD );
102+ assert (parsed. task_args != null );
103+ assert (parsed. task_args. content == " Complete task" );
104+ assert (parsed. task_args. description == " Task description" );
105+ assert (parsed. task_args. project_name == " Work" );
106+ assert (parsed. task_args. section_name == " In Progress" );
107+ assert (parsed. task_args. priority == 1 );
108+ assert (parsed. task_args. due_date == " 2024-12-31" );
109+ assert (parsed. task_args. labels == " urgent,important" );
110+ assert (parsed. task_args. pinned == 1 );
111+ print (" ✓ Parses all add options correctly\n ---\n " );
112+ }
113+
114+ void test_list_with_project () {
115+ print (" Testing: 'list' command with project\n " );
116+ int exit_code;
117+ string [] args = {" planify-cli" , " list" , " -p" , " Work" };
118+
119+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
120+
121+ assert (parsed != null );
122+ assert (exit_code == 0 );
123+ assert (parsed. command_type == PlanifyCLI . CommandType . LIST );
124+ assert (parsed. list_args != null );
125+ assert (parsed. list_args. project_name == " Work" );
126+ print (" ✓ Parses list command with project name\n ---\n " );
127+ }
128+
129+ void test_list_with_project_id () {
130+ print (" Testing: 'list' command with project ID\n " );
131+ int exit_code;
132+ string [] args = {" planify-cli" , " list" , " -i" , " project-123" };
133+
134+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
135+
136+ assert (parsed != null );
137+ assert (exit_code == 0 );
138+ assert (parsed. command_type == PlanifyCLI . CommandType . LIST );
139+ assert (parsed. list_args != null );
140+ assert (parsed. list_args. project_id == " project-123" );
141+ print (" ✓ Parses list command with project ID\n ---\n " );
142+ }
143+
144+ void test_update_minimal () {
145+ print (" Testing: 'update' command with minimal args\n " );
146+ int exit_code;
147+ string [] args = {" planify-cli" , " update" , " -t" , " task-456" , " -c" , " Updated content" };
148+
149+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
150+
151+ assert (parsed != null );
152+ assert (exit_code == 0 );
153+ assert (parsed. command_type == PlanifyCLI . CommandType . UPDATE );
154+ assert (parsed. update_args != null );
155+ assert (parsed. update_args. task_id == " task-456" );
156+ assert (parsed. update_args. content == " Updated content" );
157+ print (" ✓ Parses update command with task ID and content\n ---\n " );
158+ }
159+
160+ void test_update_completion () {
161+ print (" Testing: 'update' command with completion flags\n " );
162+ int exit_code;
163+
164+ // Test --complete
165+ string [] args_complete = {" planify-cli" , " update" , " -t" , " task-123" , " --complete" };
166+ var parsed = PlanifyCLI . ArgumentParser . parse (args_complete, out exit_code);
167+
168+ assert (parsed != null );
169+ assert (exit_code == 0 );
170+ assert (parsed. update_args. checked == 1 );
171+ print (" ✓ Parses --complete flag\n " );
172+
173+ // Test --uncomplete
174+ string [] args_uncomplete = {" planify-cli" , " update" , " -t" , " task-123" , " --uncomplete" };
175+ parsed = PlanifyCLI . ArgumentParser . parse (args_uncomplete, out exit_code);
176+
177+ assert (parsed != null );
178+ assert (exit_code == 0 );
179+ assert (parsed. update_args. checked == 0 );
180+ print (" ✓ Parses --uncomplete flag\n ---\n " );
181+ }
182+
183+ void test_missing_required_arg () {
184+ print (" Testing: Missing required argument value\n " );
185+ int exit_code;
186+ string [] args = {" planify-cli" , " add" , " -c" }; // -c without value
187+
188+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
189+
190+ assert (parsed == null );
191+ assert (exit_code == 1 );
192+ print (" ✓ Returns error for missing argument value\n ---\n " );
193+ }
194+
195+ void test_invalid_pin_value () {
196+ print (" Testing: Invalid --pin value\n " );
197+ int exit_code;
198+ string [] args = {" planify-cli" , " add" , " -c" , " Task" , " --pin" , " invalid" };
199+
200+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
201+
202+ assert (parsed == null );
203+ assert (exit_code == 1 );
204+ print (" ✓ Returns error for invalid pin value\n ---\n " );
205+ }
206+
207+ void test_unknown_option () {
208+ print (" Testing: Unknown option\n " );
209+ int exit_code;
210+ string [] args = {" planify-cli" , " add" , " -c" , " Task" , " --unknown-option" };
211+
212+ var parsed = PlanifyCLI . ArgumentParser . parse (args, out exit_code);
213+
214+ assert (parsed == null );
215+ assert (exit_code == 1 );
216+ print (" ✓ Returns error for unknown option\n ---\n " );
217+ }
218+
219+ public void register_tests () {
220+ Test . add_func (" /cli/argument_parser/no_command" , test_no_command);
221+ Test . add_func (" /cli/argument_parser/unknown_command" , test_unknown_command);
222+ Test . add_func (" /cli/argument_parser/list_projects" , test_list_projects);
223+ Test . add_func (" /cli/argument_parser/add_minimal" , test_add_minimal);
224+ Test . add_func (" /cli/argument_parser/add_full" , test_add_full);
225+ Test . add_func (" /cli/argument_parser/list_with_project" , test_list_with_project);
226+ Test . add_func (" /cli/argument_parser/list_with_project_id" , test_list_with_project_id);
227+ Test . add_func (" /cli/argument_parser/update_minimal" , test_update_minimal);
228+ Test . add_func (" /cli/argument_parser/update_completion" , test_update_completion);
229+ Test . add_func (" /cli/argument_parser/missing_required_arg" , test_missing_required_arg);
230+ Test . add_func (" /cli/argument_parser/invalid_pin_value" , test_invalid_pin_value);
231+ Test . add_func (" /cli/argument_parser/unknown_option" , test_unknown_option);
232+ }
233+ }
0 commit comments