5#include <snowhouse/snowhouse.h>
6#include <string_format.h>
16 std::optional<std::filesystem::path> WalkUpToFindFilePath(
const std::string& path) {
21 if (std::filesystem::exists(path))
return path;
23 std::filesystem::path currentPath = std::filesystem::current_path();
24 if (std::filesystem::exists(currentPath / path))
return currentPath / path;
26 while (currentPath.has_parent_path()) {
27 currentPath = currentPath.parent_path();
28 if (std::filesystem::exists(currentPath / path))
return currentPath / path;
34 std::string ReadLineFromFile(
const std::string& filename,
unsigned int linenumber) {
35 auto path = WalkUpToFindFilePath(filename);
36 if (!path.has_value())
return "";
38 std::ifstream file(path.value());
39 if (!file.is_open())
return "";
42 unsigned int currentLineNumber = 0;
43 while (std::getline(file, line)) {
45 if (currentLineNumber == linenumber) {
47 line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](
int ch) {
48 return !std::isspace(ch);
62 std::rethrow_exception(*exception);
63 }
catch (
const snowhouse::AssertionException& e) {
64 if (failureMessageCallback) {
66 "{}\n[{}:{}] {}", e.what(), e.file(), e.line(),
67 ReadLineFromFile(e.file(), e.line())
69 failureMessageCallback->invoke(&failureMessage);
bool handle_exception(std::exception_ptr *exception, IExceptionMessageCallbackFn *failureMessageCallback) override
ILocalSpecExceptionHandler::LocalSpecExceptionFailureMessageCallbackFn IExceptionMessageCallbackFn