1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00
CLI11/tests/TimerTest.cpp
Philip Top 5a03ee5838
Allow non standard option names like -option (#1078)
This has been bounced around for a couple years now 

#474 and a few others have expressed desire to work with non-standard
option names. We have been somewhat resistant to that but I think it can
be done now. This PR adds a modifier `allow_non_standard_option_names()`
It is purposely long, it is purposely off by default. But what it does
is allow option names with a single `-` to act like a short option name.
With this modifier enabled no single letter short option names are
allowed to start with the same letter as a non-standard names. For
example `-s` and `-single` would not be allowed.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2024-10-23 05:14:29 -07:00

70 lines
2.0 KiB
C++

// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#include "CLI/Timer.hpp"
#include "catch.hpp"
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
TEST_CASE("Timer: MSTimes", "[timer]") {
CLI::Timer timer{"My Timer"};
std::this_thread::sleep_for(std::chrono::milliseconds(123));
std::string output = timer.to_string();
std::string new_output = (timer / 1000000).to_string();
CHECK_THAT(output, Contains("My Timer"));
CHECK_THAT(output, Contains(" ms"));
CHECK_THAT(new_output, Contains(" ns"));
}
/* Takes too long
TEST_CASE("Timer: STimes", "[timer]") {
CLI::Timer timer;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string output = timer.to_string();
CHECK_THAT (output, Contains(" s"));
}
*/
// Fails on Windows
// TEST_CASE("Timer: UStimes", "[timer]") {
// CLI::Timer timer;
// std::this_thread::sleep_for(std::chrono::microseconds(2));
// std::string output = timer.to_string();
// CHECK_THAT (output, Contains(" ms"));
//}
TEST_CASE("Timer: BigTimer", "[timer]") {
CLI::Timer timer{"My Timer", CLI::Timer::Big};
std::string output = timer.to_string();
CHECK_THAT(output, Contains("Time ="));
CHECK_THAT(output, Contains("-----------"));
}
TEST_CASE("Timer: AutoTimer", "[timer]") {
CLI::AutoTimer timer;
std::string output = timer.to_string();
CHECK_THAT(output, Contains("Timer"));
}
TEST_CASE("Timer: PrintTimer", "[timer]") {
std::stringstream out;
CLI::AutoTimer timer;
out << timer;
std::string output = out.str();
CHECK_THAT(output, Contains("Timer"));
}
TEST_CASE("Timer: TimeItTimer", "[timer]") {
CLI::Timer timer;
std::string output = timer.time_it([]() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }, .1);
std::cout << output << '\n';
CHECK_THAT(output, Contains("ms"));
}