Skip to content

Commit 62b9ee4

Browse files
committed
feat: refactor arg_parser to be less context aware
1 parent 4521d03 commit 62b9ee4

File tree

10 files changed

+273
-527
lines changed

10 files changed

+273
-527
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
name: MSBuild
77

8-
on:
9-
push:
10-
branches: ["master"]
11-
pull_request:
12-
branches: ["master"]
8+
on: push
139

1410
env:
1511
# Path to the solution file relative to the root of the project.

parser-tests/mock-arg-parser.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,4 @@ class mock_arg_parser : public arg_parser
1111
L"C:\\Program\\sample.exe",
1212
L"C:\\Program\\sample.pdb"
1313
};
14-
15-
protected:
16-
void check_file_path(std::wstring file_path) override
17-
{
18-
if (existing_files.find(file_path) == existing_files.end())
19-
{
20-
// Simulate file does not exist
21-
throw_invalid_arg(file_path, L"Mock: File does not exist.");
22-
}
23-
// Else, do nothing (file exists)
24-
}
2514
};

parser-tests/parser-tests.cpp

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace parsertests
6363
}
6464
);
6565
}
66-
// Test parsing the 'help' command with no arguments
66+
// Test parsing the "help" command with no arguments
6767
TEST_METHOD(TEST_HELP_COMMAND)
6868
{
6969
const wchar_t* argv[] = { L"wperf", L"--help" };
@@ -99,19 +99,6 @@ namespace parsertests
9999
Assert::IsTrue(COMMAND_CLASS::LIST == parser.command);
100100
}
101101

102-
// Test parsing the 'sample' command with various flags
103-
TEST_METHOD(TEST_SAMPLE_COMMAND_WITH_MULTIPLE_CORES)
104-
{
105-
const wchar_t* argv[] = { L"wperf", L"sample", L"--annotate", L"--timeout", L"5s", L"-c", L"0,1,2" };
106-
int argc = 7;
107-
mock_arg_parser parser;
108-
109-
Assert::ExpectException<std::invalid_argument>([&parser, argc, &argv]() {
110-
parser.parse(argc, argv);
111-
}
112-
);
113-
}
114-
115102
// Test parsing the 'record' command with command line separator and arguments
116103
TEST_METHOD(TEST_RECORD_COMMAND_WITH_PROCESS)
117104
{
@@ -121,9 +108,8 @@ namespace parsertests
121108
parser.parse(argc, argv);
122109

123110
Assert::IsTrue(parser.do_record.get());
124-
Assert::AreEqual(std::wstring(L"notepad.exe test_arg"), parser.record_commandline);
111+
Assert::AreEqual(std::wstring(L"notepad.exe test_arg"), parser.record_commandline.get());
125112
Assert::AreEqual(std::wstring(L"notepad.exe"), parser.sample_pe_file.get());
126-
Assert::AreEqual(std::wstring(L"notepad.pdb"), parser.sample_pdb_file.get());
127113
Assert::IsTrue(COMMAND_CLASS::RECORD == parser.command);
128114
}
129115

@@ -159,20 +145,9 @@ namespace parsertests
159145
mock_arg_parser parser;
160146
parser.parse(argc, argv);
161147

162-
Assert::AreEqual(120.0, parser.count_duration.get()); // 2 minutes in seconds
148+
Assert::AreEqual(std::wstring(L"2m"), parser.count_duration.get()); // 2 minutes in seconds
163149
}
164150

165-
// Test that invalid timeout format causes exception
166-
TEST_METHOD(TEST_INVALID_TIMEOUT_WITH_WRONG_UNIT)
167-
{
168-
const wchar_t* argv[] = { L"wperf", L"sample", L"--timeout", L"5x" };
169-
int argc = 4;
170-
mock_arg_parser parser;
171-
Assert::ExpectException<std::invalid_argument>([&parser, argc, &argv]() {
172-
parser.parse(argc, argv);
173-
}
174-
);
175-
}
176151
TEST_METHOD(TEST_INVALID_TIMEOUT_WITH_WRONG_FORMAT)
177152
{
178153
const wchar_t* argv[] = { L"wperf", L"sample", L"--timeout", L"5.4", L"ms"};
@@ -230,7 +205,7 @@ namespace parsertests
230205
mock_arg_parser parser;
231206
parser.parse(argc, argv);
232207

233-
Assert::AreEqual((uint32_t)100, parser.sample_display_row.get());
208+
Assert::AreEqual(std::wstring(L"100"), parser.sample_display_row.get());
234209
Assert::IsTrue(COMMAND_CLASS::SAMPLE == parser.command);
235210
}
236211

@@ -244,8 +219,6 @@ namespace parsertests
244219
parser.parse(argc, argv);
245220

246221
Assert::AreEqual(std::wstring(L"C:\\Program\\sample.exe"), parser.sample_pe_file.get());
247-
Assert::AreEqual(std::wstring(L"C:\\Program\\sample.pdb"), parser.sample_pdb_file.get());
248-
Assert::AreEqual(std::wstring(L"C:\\Program\\sample.exe"), parser.sample_image_name.get());
249222
Assert::IsTrue(COMMAND_CLASS::SAMPLE == parser.command);
250223
}
251224

@@ -310,5 +283,35 @@ namespace parsertests
310283
}
311284
);
312285
}
286+
287+
// Test parsing with no command
288+
TEST_METHOD(TEST_NO_COMMAND)
289+
{
290+
const wchar_t* argv[] = { L"wperf", L"--annotate", L"--json" };
291+
int argc = 3;
292+
mock_arg_parser parser;
293+
Assert::ExpectException<std::invalid_argument>([&parser, argc, &argv]() {
294+
parser.parse(argc, argv);
295+
}
296+
);
297+
}
298+
299+
// Test complex stat command
300+
TEST_METHOD(TEST_FULL_STAT_COMMAND)
301+
{
302+
const wchar_t* argv[] = { L"wperf", L"stat", L"--output", L"_output_02.json", L"-e", L"inst_spec,vfp_spec,ase_spec,dp_spec,ld_spec,st_spec,br_immed_spec,crypto_spec", L"-c", L"0", L"sleep", L"5" };
303+
int argc = 10;
304+
mock_arg_parser parser;
305+
parser.parse(argc, argv);
306+
Assert::IsTrue(COMMAND_CLASS::STAT == parser.command);
307+
Assert::IsTrue(parser.raw_events.is_set());
308+
Assert::IsTrue(parser.output_filename.is_set());
309+
Assert::IsTrue(parser.cores_idx.is_set());
310+
Assert::IsTrue(parser.count_duration.is_set());
311+
Assert::AreEqual(wstring(L"5"), parser.count_duration.get());
312+
Assert::AreEqual(wstring(L"0"), parser.cores_idx.get());
313+
Assert::AreEqual(wstring(L"_output_02.json"),parser.output_filename.get());
314+
Assert::AreEqual(wstring(L"inst_spec,vfp_spec,ase_spec,dp_spec,ld_spec,st_spec,br_immed_spec,crypto_spec"), parser.raw_events.get());
315+
}
313316
};
314317
}

parser-tests/parser-tests.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
<Link>
104104
<SubSystem>Windows</SubSystem>
105105
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories);$(SolutionDir)parser\$(Platform)\$(Configuration)\</AdditionalLibraryDirectories>
106-
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);$(SolutionDir)parser\$(Platform)\$(Configuration)\arg-parser.obj;$(SolutionDir)parser\$(Platform)\$(Configuration)\utils.obj</AdditionalDependencies>
106+
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);$(SolutionDir)parser\$(Platform)\$(Configuration)\arg-parser.obj</AdditionalDependencies>
107107
</Link>
108108
</ItemDefinitionGroup>
109109
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -158,7 +158,7 @@
158158
<EnableCOMDATFolding>true</EnableCOMDATFolding>
159159
<OptimizeReferences>true</OptimizeReferences>
160160
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories);$(SolutionDir)parser\$(Platform)\$(Configuration)\</AdditionalLibraryDirectories>
161-
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);$(SolutionDir)parser\$(Platform)\$(Configuration)\arg-parser.obj;$(SolutionDir)parser\$(Platform)\$(Configuration)\utils.obj</AdditionalDependencies>
161+
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);$(SolutionDir)parser\$(Platform)\$(Configuration)\arg-parser.obj</AdditionalDependencies>
162162
</Link>
163163
</ItemDefinitionGroup>
164164
<ItemGroup>

0 commit comments

Comments
 (0)