|
| 1 | +#ifdef RYML_SAVE_TEST_YAML |
| 2 | + |
| 3 | +#include <c4/yml/common.hpp> |
| 4 | +#include <c4/yml/error.hpp> |
| 5 | +#include <c4/error.hpp> |
| 6 | +#include <c4/format.hpp> |
| 7 | +#include <c4/std/vector.hpp> |
| 8 | +#include <c4/std/string.hpp> |
| 9 | +#include <c4/fs/fs.hpp> |
| 10 | +#include <gtest/gtest.h> |
| 11 | +#include <map> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +C4_SUPPRESS_WARNING_GCC_CLANG_PUSH |
| 15 | +C4_SUPPRESS_WARNING_GCC_CLANG("-Wold-style-cast") |
| 16 | + |
| 17 | +namespace c4 { |
| 18 | +namespace yml { |
| 19 | + |
| 20 | +static char savedir[] = "./yamldump\0"; |
| 21 | + |
| 22 | +struct savehelper |
| 23 | +{ |
| 24 | + std::vector<std::string> sources; |
| 25 | + std::vector<char> basename; |
| 26 | + std::vector<char> fullname; |
| 27 | + size_t indexpos; |
| 28 | + void reset_from_notest(csubstr filename) |
| 29 | + { |
| 30 | + char buf[1024]; |
| 31 | + ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf)-1); |
| 32 | + _RYML_CHECK_BASIC(ret > 0); |
| 33 | + _RYML_CHECK_BASIC(ret < (int)sizeof(buf)); |
| 34 | + csubstr exe = {buf, (size_t)ret}; |
| 35 | + exe = exe.basename(); |
| 36 | + static size_t count = 0; |
| 37 | + if(filename.empty()) |
| 38 | + filename = "nofilename"; |
| 39 | + filename = filename.basename(); |
| 40 | + c4::formatrs(&basename, "{}_{}_{}", exe, filename, c4::fmt::zpad(count, 3)); |
| 41 | + to_substr(basename).replace('/', '_'); |
| 42 | + ++count; |
| 43 | + prepare_fullname(); |
| 44 | + } |
| 45 | + void reset_from_gtest(testing::TestInfo const* curr) |
| 46 | + { |
| 47 | + csubstr filename = to_csubstr(curr->file()); |
| 48 | + csubstr suitename = to_csubstr(curr->test_suite_name()); |
| 49 | + csubstr testname = to_csubstr(curr->name()); |
| 50 | + // handle group tests differently: |
| 51 | + if(filename.ends_with("test_group.def.hpp")) |
| 52 | + { |
| 53 | + filename = suitename; |
| 54 | + size_t pos = filename.find('/'); |
| 55 | + _RYML_ASSERT_BASIC(pos != npos); |
| 56 | + filename = filename.first(pos); |
| 57 | + suitename = suitename.sub(pos + 1); |
| 58 | + if(suitename.begins_with("YmlTestCase")) |
| 59 | + { |
| 60 | + pos = testname.last_of('/'); |
| 61 | + _RYML_ASSERT_BASIC(pos != npos); |
| 62 | + suitename = testname.sub(pos+1); |
| 63 | + } |
| 64 | + } |
| 65 | + size_t pos = filename.find("test/"); |
| 66 | + if(pos != npos) |
| 67 | + filename = filename.sub(pos + 5); |
| 68 | + if(filename.ends_with(".cpp") || filename.ends_with(".hpp")) |
| 69 | + filename = filename.offs(0, 4); |
| 70 | + c4::formatrs(&basename, "{}_{}-{}_{}", filename, fmt::zpad(curr->line(), 3), suitename, testname); |
| 71 | + to_substr(basename).replace('/', '_'); |
| 72 | + prepare_fullname(); |
| 73 | + } |
| 74 | + void prepare_fullname() |
| 75 | + { |
| 76 | + printf("new test! %.*s\n", (int)basename.size(), &basename.front()); |
| 77 | + c4::formatrs(&fullname, "{}/{}--", savedir, basename); |
| 78 | + indexpos = fullname.size(); |
| 79 | + fullname.resize(fullname.size() + 32); |
| 80 | + sources.clear(); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +static void save_impl(csubstr filename, csubstr extension, csubstr src) |
| 85 | +{ |
| 86 | + static savehelper h = {}; |
| 87 | + static testing::TestInfo const* prev = nullptr; |
| 88 | + testing::TestInfo const* curr = testing::UnitTest::GetInstance()->current_test_info(); |
| 89 | + if(prev == nullptr) |
| 90 | + c4::fs::mkdirs(savedir); |
| 91 | + if(curr != prev || !curr) |
| 92 | + { |
| 93 | + prev = curr; |
| 94 | + if(curr) |
| 95 | + h.reset_from_gtest(curr); |
| 96 | + else |
| 97 | + h.reset_from_notest(filename); |
| 98 | + } |
| 99 | + if(to_csubstr(h.basename).find("FilterTest_filter") != npos) |
| 100 | + return; // this case is not interesting, and very noisy |
| 101 | + // have we already saved this source? |
| 102 | + for(std::string const& s : h.sources) |
| 103 | + if(to_csubstr(s) == src) |
| 104 | + return; |
| 105 | + // we'll place the source in this position: |
| 106 | + size_t index = h.sources.size(); // it is also the index we'll use for the savename |
| 107 | + h.sources.emplace_back(src.begin(), src.end()); |
| 108 | + // now form the savename |
| 109 | + substr buf = to_substr(h.fullname).sub(h.indexpos); |
| 110 | + _RYML_ASSERT_BASIC(buf.len > 0); |
| 111 | + size_t len = c4::cat(buf, c4::fmt::zpad(index, 3), extension, '\0'); |
| 112 | + _RYML_ASSERT_BASIC(len < buf.len); |
| 113 | + csubstr savename = to_substr(h.fullname).first(h.indexpos + len); |
| 114 | + // done! dump the file |
| 115 | + printf("saving %.*s\n", (int)savename.len, savename.str); |
| 116 | + c4::fs::file_put_contents(savename.str, src); |
| 117 | +} |
| 118 | + |
| 119 | +void ryml_save_test_yaml(csubstr filename, csubstr src) |
| 120 | +{ |
| 121 | + save_impl(filename, ".yaml", src); |
| 122 | +} |
| 123 | +void ryml_save_test_json(csubstr filename, csubstr src) |
| 124 | +{ |
| 125 | + save_impl(filename, ".json", src); |
| 126 | +} |
| 127 | +} // namespace yml |
| 128 | +} // namespace c4 |
| 129 | + |
| 130 | +C4_SUPPRESS_WARNING_GCC_CLANG_POP |
| 131 | + |
| 132 | +#endif // RYML_SAVE_TEST_YAML |
0 commit comments