1 /*
2  * PROJECT:     ReactOS API tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Test for CommandLineToArgvW
5  * COPYRIGHT:   Copyright 2023 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6  */
7 
8 #include "shelltest.h"
9 #include <atlstr.h>
10 
11 static VOID
12 DoEntry(INT lineno, LPCWSTR cmdline, INT expected_argc, ...)
13 {
14     va_list va;
15     va_start(va, expected_argc);
16 
17     INT real_argc;
18     LPWSTR *real_argv = CommandLineToArgvW(cmdline, &real_argc);
19 
20     INT common = min(expected_argc, real_argc);
21     for (INT i = 1; i < common; ++i)
22     {
23         CStringA str1 = wine_dbgstr_w(va_arg(va, LPCWSTR));
24         CStringA str2 = wine_dbgstr_w(real_argv[i]);
25         ok(str1 == str2, "Line %d: %s != %s\n", lineno, (LPCSTR)str1, (LPCSTR)str2);
26     }
27 
28     INT diff = abs(expected_argc - real_argc);
29     while (diff-- > 0)
30     {
31         ok(expected_argc == real_argc,
32            "Line %d: expected_argc %d and real_argc %d are different\n",
33            lineno, expected_argc, real_argc);
34     }
35 
36     LocalFree(real_argv);
37     va_end(va);
38 }
39 
40 START_TEST(CommandLineToArgvW)
41 {
42     DoEntry(__LINE__, L"", 1);
43     DoEntry(__LINE__, L"test.exe", 1);
44     DoEntry(__LINE__, L"test.exe ", 1);
45     DoEntry(__LINE__, L"test.exe\t", 1);
46     DoEntry(__LINE__, L"test.exe\r", 1);
47     DoEntry(__LINE__, L"test.exe\n", 1);
48     DoEntry(__LINE__, L"test.exe\v", 1);
49     DoEntry(__LINE__, L"test.exe\f", 1);
50     DoEntry(__LINE__, L"test.exe\u3000", 1);
51     DoEntry(__LINE__, L"\"This is a test.exe\"", 1);
52     DoEntry(__LINE__, L"\"This is a test.exe\" ", 1);
53     DoEntry(__LINE__, L"\"This is a test.exe\"\t", 1);
54     DoEntry(__LINE__, L"\"This is a test.exe\"\r", 2, L"\r");
55     DoEntry(__LINE__, L"\"This is a test.exe\"\n", 2, L"\n");
56     DoEntry(__LINE__, L"\"This is a test.exe\"\v", 2, L"\v");
57     DoEntry(__LINE__, L"\"This is a test.exe\"\f", 2, L"\f");
58     DoEntry(__LINE__, L"\"This is a test.exe\"\u3000", 2, L"\u3000");
59     DoEntry(__LINE__, L"test.exe a", 2, L"a");
60     DoEntry(__LINE__, L"test.exe\ta", 2, L"a");
61     DoEntry(__LINE__, L"test.exe\ra", 2, L"a");
62     DoEntry(__LINE__, L"test.exe\na", 2, L"a");
63     DoEntry(__LINE__, L"test.exe\va", 2, L"a");
64     DoEntry(__LINE__, L"test.exe\fa", 2, L"a");
65     DoEntry(__LINE__, L"test.exe\u3000" L"a", 1);
66     DoEntry(__LINE__, L"test.exe  a", 2, L"a");
67     DoEntry(__LINE__, L"test.exe \ta", 2, L"a");
68     DoEntry(__LINE__, L"test.exe \ra", 2, L"\ra");
69     DoEntry(__LINE__, L"test.exe \na", 2, L"\na");
70     DoEntry(__LINE__, L"test.exe \va", 2, L"\va");
71     DoEntry(__LINE__, L"test.exe \fa", 2, L"\fa");
72     DoEntry(__LINE__, L"test.exe a ", 2, L"a");
73     DoEntry(__LINE__, L"test.exe a\t", 2, L"a");
74     DoEntry(__LINE__, L"test.exe a\r", 2, L"a\r");
75     DoEntry(__LINE__, L"test.exe a\n", 2, L"a\n");
76     DoEntry(__LINE__, L"test.exe a\v", 2, L"a\v");
77     DoEntry(__LINE__, L"test.exe a\f", 2, L"a\f");
78     DoEntry(__LINE__, L"test.exe \"a\" ", 2, L"a");
79     DoEntry(__LINE__, L"test.exe \"a\"\t", 2, L"a");
80     DoEntry(__LINE__, L"test.exe \"a\"\r", 2, L"a\r");
81     DoEntry(__LINE__, L"test.exe \"a\"\n", 2, L"a\n");
82     DoEntry(__LINE__, L"test.exe \"a\"\v", 2, L"a\v");
83     DoEntry(__LINE__, L"test.exe \"a\"\f", 2, L"a\f");
84     DoEntry(__LINE__, L"test.exe \u3000" L"a", 2, L"\u3000" L"a");
85     DoEntry(__LINE__, L"test.exe \"a b\"", 2, L"a b");
86     DoEntry(__LINE__, L"test.exe \"a\tb\"", 2, L"a\tb");
87     DoEntry(__LINE__, L"test.exe \"a\rb\"", 2, L"a\rb");
88     DoEntry(__LINE__, L"test.exe \"a\nb\"", 2, L"a\nb");
89     DoEntry(__LINE__, L"test.exe \"a\vb\"", 2, L"a\vb");
90     DoEntry(__LINE__, L"test.exe \"a\fb\"", 2, L"a\fb");
91     DoEntry(__LINE__, L"test.exe \"a\u3000" L"b\"", 2, L"a\u3000" L"b");
92     DoEntry(__LINE__, L"test.exe a b c", 4, L"a", L"b", L"c");
93     DoEntry(__LINE__, L"test.exe a b \"c", 4, L"a", L"b", L"c");
94     DoEntry(__LINE__, L"test.exe \"a b\" \"c d\"", 3, L"a b", L"c d");
95     DoEntry(__LINE__, L"test.exe \"a \" d\"", 3, L"a ", L"d");
96     DoEntry(__LINE__, L"test.exe \"0 1\"\" 2", 3, L"0 1\"", L"2");
97     DoEntry(__LINE__, L"test.exe \"0 1\"\"\" 2", 2, L"0 1\" 2");
98 }
99