1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00
CLI11/tests/app_helper.hpp
Andrey Zhukov a227cd10fc
feat: unicode support (#804)
* Add unicode support tests

* Add unicode parse tests

* Implement #14

* Slim down Windows.h

* Fix documentation comments

* Fix clang-tidy and cpplint

* Update README

* Fix clang-tidy

* Fix to_path not being available on linux

* Add roundtrip encoding tests

* style: pre-commit.ci fixes

* Fix pre-commit.ci

* Fix codacy

* Exclude parse_unicode which should not contain a newline from pre-commit

* Remove a test which breaks CI

* Fix build in CI

* Replace broken execute_with tests

* Fix wide string conversions on all systems

* Fix system args on apple

* style: pre-commit.ci fixes

* Fix some includes

* Fix wrong size calculation and comments

* Add guards around codecvt

* Fix _Pragma not recognized on MSVC

* Fix bad macro check

* Fix include

* Fix narrow and widen when codecvt is missing

* Fix some weird bug in old MSVC

* Add dependent applications to meson-build

* Fix precompilation

* Fix lint

* Fix coverage

* Update README

* style: pre-commit.ci fixes

* Fix lint

* Fix coverage

* Fix optional braces offending clang

* Remove copied comments from Windows.h

* Suppress flawfinder detects

* Fix cmake config tests failing because of a missing lib

* chore: update copyright on new files to 2023

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* style: pre-commit.ci fixes

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2023-01-12 15:03:20 -05:00

124 lines
3.6 KiB
C++

// Copyright (c) 2017-2021, 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
#pragma once
#ifdef CLI11_SINGLE_FILE
#include "CLI11.hpp"
#else
#include "CLI/CLI.hpp"
#endif
#include "catch.hpp"
#include <array>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using input_t = std::vector<std::string>;
class TApp {
public:
CLI::App app{"My Test Program"};
input_t args{};
virtual ~TApp() = default;
void run() {
// It is okay to re-parse - clear is called automatically before a parse.
input_t newargs = args;
std::reverse(std::begin(newargs), std::end(newargs));
app.parse(newargs);
}
};
CLI11_INLINE int fileClear(const std::string &name) { return std::remove(name.c_str()); }
class TempFile {
std::string _name{};
public:
explicit TempFile(std::string name) : _name(std::move(name)) {
if(!CLI::NonexistentPath(_name).empty())
throw std::runtime_error(_name);
}
~TempFile() {
std::remove(_name.c_str()); // Doesn't matter if returns 0 or not
}
operator const std::string &() const { return _name; } // NOLINT(google-explicit-constructor)
CLI11_NODISCARD const char *c_str() const { return _name.c_str(); }
};
inline void put_env(std::string name, std::string value) {
#ifdef _WIN32
_putenv_s(name.c_str(), value.c_str());
#else
setenv(name.c_str(), value.c_str(), 1);
#endif
}
inline void unset_env(std::string name) {
#ifdef _WIN32
_putenv_s(name.c_str(), "");
#else
unsetenv(name.c_str());
#endif
}
CLI11_INLINE void check_identical_files(const char *path1, const char *path2) {
std::string err1 = CLI::ExistingFile(path1);
if(!err1.empty()) {
FAIL("Could not open " << path1 << ": " << err1);
}
std::string err2 = CLI::ExistingFile(path2);
if(!err2.empty()) {
FAIL("Could not open " << path2 << ": " << err2);
}
// open files at the end to compare size first
std::ifstream file1(path1, std::ifstream::ate | std::ifstream::binary);
std::ifstream file2(path2, std::ifstream::ate | std::ifstream::binary);
if(!file1.good()) {
FAIL("File " << path1 << " is corrupted");
}
if(!file2.good()) {
FAIL("File " << path2 << " is corrupted");
}
if(file1.tellg() != file2.tellg()) {
FAIL("Different file sizes:\n " << file1.tellg() << " bytes in " << path1 << "\n " << file2.tellg()
<< " bytes in " << path2);
}
// rewind files
file1.seekg(0);
file2.seekg(0);
std::array<uint8_t, 10240> buffer1;
std::array<uint8_t, 10240> buffer2;
for(size_t ibuffer = 0; file1.good(); ++ibuffer) {
// Flawfinder: ignore
file1.read(reinterpret_cast<char *>(buffer1.data()), static_cast<std::streamsize>(buffer1.size()));
// Flawfinder: ignore
file2.read(reinterpret_cast<char *>(buffer2.data()), static_cast<std::streamsize>(buffer2.size()));
for(size_t i = 0; i < static_cast<size_t>(file1.gcount()); ++i) {
if(buffer1[i] != buffer2[i]) {
FAIL(std::hex << std::setfill('0') << "Different bytes at position " << (ibuffer * 10240 + i) << ":\n "
<< "0x" << std::setw(2) << static_cast<int>(buffer1[i]) << " in " << path1 << "\n "
<< "0x" << std::setw(2) << static_cast<int>(buffer2[i]) << " in " << path2);
}
}
}
}