Skip to content

Commit

Permalink
Add unit test for nlohmann#3388
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Apr 6, 2022
1 parent 6529557 commit d58ee35
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion test/src/unit-alt-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class alt_string
public:
using value_type = std::string::value_type;

static constexpr auto npos = static_cast<std::size_t>(-1);

alt_string(const char* str): str_impl(str) {}
alt_string(const char* str, std::size_t count): str_impl(str, count) {}
alt_string(size_t count, char chr): str_impl(count, chr) {}
Expand Down Expand Up @@ -144,17 +146,50 @@ class alt_string
str_impl.clear();
}

const value_type* data()
const value_type* data() const
{
return str_impl.data();
}

bool empty() const
{
return str_impl.empty();
}

std::size_t find(const alt_string& str, std::size_t pos = 0) const
{
return str_impl.find(str.str_impl, pos);
}

std::size_t find_first_of(char c, std::size_t pos = 0) const
{
return str_impl.find_first_of(c, pos);
}

alt_string substr(std::size_t pos = 0, std::size_t count = npos) const
{
std::string s = str_impl.substr(pos, count);
return {s.data(), s.size()};
}

alt_string& replace(std::size_t pos, std::size_t count, const alt_string& str)
{
str_impl.replace(pos, count, str.str_impl);
return *this;
}

private:
std::string str_impl {};

friend bool ::operator<(const char* /*op1*/, const alt_string& /*op2*/) noexcept;
friend unsigned long long stoull(const alt_string& /*str*/, std::size_t* /*pos*/, int /*base*/);
};

unsigned long long stoull(const alt_string& str, std::size_t* pos = nullptr, int base = 10)
{
return std::stoull(str.str_impl, pos, base);
}

void int_to_string(alt_string& target, std::size_t value)
{
target = std::to_string(value).c_str();
Expand Down Expand Up @@ -296,4 +331,20 @@ TEST_CASE("alternative string type")
CHECK_FALSE(const_doc["Who are you?"] == "I'm Bruce Wayne");
}
}

SECTION("JSON pointer")
{
// conversion from json to alt_json fails to compile;
// attempted fix(*) produces: [[['b','a','r'],['b','a','z']]] (with each char being an integer)
// (*) disable implicit conversion for json_refs of any basic_json type
// alt_json j = R"(
// {
// "foo": ["bar", "baz"]
// }
// )"_json;
auto j = alt_json::parse(R"({"foo": ["bar", "baz"]})");

CHECK(j.at(alt_json::json_pointer("/foo/0")) == j["foo"][0]);
CHECK(j.at(alt_json::json_pointer("/foo/1")) == j["foo"][1]);
}
}

0 comments on commit d58ee35

Please sign in to comment.