Setup for inclusion in the Bazel Central Registry

By adding a MODULE.bazel file, this library becomes usable with Bazel's
new external dependency management system Bzlmod.

The new tests are run when a new version is included in the Bazel
Central Registry and verify that the basic functionality works with the
provided Bazel setup.

See:
https://bazel.build/build/bzlmod
This commit is contained in:
Fabian Meumertzheim 2022-11-01 09:19:13 +01:00
parent a3e6e26dc8
commit cec4630c2d
9 changed files with 44 additions and 0 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@
# build directories (vscode-cmake-tools, user-defined, ...)
/build*/
bazel-*
# fuzzers
/tests/corpus_*

5
MODULE.bazel Normal file
View File

@ -0,0 +1,5 @@
module(
name = "nlohmann_json",
compatibility_level = 1,
version = "3.11.2",
)

1
tests/bazel/.bazelrc Normal file
View File

@ -0,0 +1 @@
common --enable_bzlmod

View File

@ -0,0 +1 @@
6.0.0rc1

5
tests/bazel/BUILD.bazel Normal file
View File

@ -0,0 +1,5 @@
cc_test(
name = "json_example_test",
srcs = ["json_example_test.cpp"],
deps = ["@json"],
)

5
tests/bazel/MODULE.bazel Normal file
View File

@ -0,0 +1,5 @@
bazel_dep(name = "nlohmann_json", repo_name = "json")
local_path_override(
module_name = "nlohmann_json",
path = "../.."
)

View File

View File

@ -0,0 +1,2 @@
# The presence of this file ensures that Bazel ignores WORKSPACE.bazel and
# does not implicitly add any dependency declarations.

View File

@ -0,0 +1,24 @@
#include <iostream>
#include <string>
#include <nlohmann/json.hpp>
int main()
{
nlohmann::json j;
j["pi"] = 3.141;
j["happy"] = true;
j["name"] = "Niels";
j["nothing"] = nullptr;
j["answer"]["everything"] = 42;
j["list"] = {1, 0, 2};
j["object"] = {{"currency", "USD"}, {"value", 42.99}};
std::string s = j.dump();
if (s != R"({"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141})")
{
std::cerr << "Unexpected JSON serialization: " << s << std::endl;
return 1;
}
return 0;
}