Skip to content

Commit

Permalink
Manifest: add parseProfiles tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Jan 21, 2025
1 parent e4772d8 commit d69c364
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 13 deletions.
110 changes: 103 additions & 7 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ validatePackageName(const std::string_view name) noexcept {

#ifdef CABIN_TEST

# include "Rustify/Tests.hpp"

# include <climits>
# include <fmt/ranges.h>
# include <toml11/fwd/literal_fwd.hpp>
Expand Down Expand Up @@ -799,7 +801,7 @@ testPackageTryFromToml() {
name = "test-pkg"
edition = "20"
version = "1.2.3"
)"_toml;
)"_toml;

auto pkg = Package::tryFromToml(val).unwrap();
assertEq(pkg.name, "test-pkg");
Expand All @@ -811,7 +813,7 @@ testPackageTryFromToml() {
{
const toml::value val = R"(
[package]
)"_toml;
)"_toml;

assertEq(
Package::tryFromToml(val).unwrap_err()->what(),
Expand All @@ -826,7 +828,7 @@ testPackageTryFromToml() {
const toml::value val = R"(
[package]
name = "test-pkg"
)"_toml;
)"_toml;

assertEq(
Package::tryFromToml(val).unwrap_err()->what(),
Expand All @@ -842,7 +844,7 @@ testPackageTryFromToml() {
[package]
name = "test-pkg"
edition = "20"
)"_toml;
)"_toml;

assertEq(
Package::tryFromToml(val).unwrap_err()->what(),
Expand All @@ -861,7 +863,7 @@ testPackageTryFromToml() {
name = "test-pkg"
edition = "invalid"
version = "1.2.3"
)"_toml;
)"_toml;

assertEq(Package::tryFromToml(val).unwrap_err()->what(), "invalid edition");
}
Expand All @@ -871,7 +873,7 @@ testPackageTryFromToml() {
name = "test-pkg"
edition = "20"
version = "invalid"
)"_toml;
)"_toml;

assertEq(
Package::tryFromToml(val).unwrap_err()->what(),
Expand All @@ -884,6 +886,99 @@ invalid
pass();
}

static void
testParseProfiles() {
const Profile devProfileDefault(
/*cxxflags=*/{}, /*ldflags=*/{}, /*lto=*/false, /*debug=*/true,
/*compDb=*/false, /*optLevel=*/0
);
const Profile relProfileDefault(
/*cxxflags=*/{}, /*ldflags=*/{}, /*lto=*/false, /*debug=*/false,
/*compDb=*/false, /*optLevel=*/3
);

{
const toml::value empty = ""_toml;

const auto profiles = parseProfiles(empty).unwrap();
assertEq(profiles.size(), 2UL);
assertEq(profiles.at("dev"), devProfileDefault);
assertEq(profiles.at("release"), relProfileDefault);
}
{
const toml::value profOnly = "[profile]"_toml;

const auto profiles = parseProfiles(profOnly).unwrap();
assertEq(profiles.size(), 2UL);
assertEq(profiles.at("dev"), devProfileDefault);
assertEq(profiles.at("release"), relProfileDefault);
}
{
const toml::value baseOnly = R"(
[profile]
cxxflags = ["-fno-rtti"]
ldflags = ["-lm"]
lto = true
debug = true
comp-db = true
opt-level = 2
)"_toml;

const Profile expected(
/*cxxflags=*/{ "-fno-rtti" }, /*ldflags=*/{ "-lm" }, /*lto=*/true,
/*debug=*/true,
/*compDb=*/true, /*optLevel=*/2
);

const auto profiles = parseProfiles(baseOnly).unwrap();
assertEq(profiles.size(), 2UL);
assertEq(profiles.at("dev"), expected);
assertEq(profiles.at("release"), expected);
}
{
const toml::value overwrite = R"(
[profile]
cxxflags = ["-fno-rtti"]
[profile.dev]
cxxflags = []
[profile.release]
cxxflags = []
)"_toml;

const auto profiles = parseProfiles(overwrite).unwrap();
assertEq(profiles.size(), 2UL);
assertEq(profiles.at("dev"), devProfileDefault);
assertEq(profiles.at("release"), relProfileDefault);
}
{
const toml::value overwrite = R"(
[profile]
opt-level = 2
[profile.dev]
opt-level = 1
)"_toml;

const Profile devExpected(
/*cxxflags=*/{}, /*ldflags=*/{}, /*lto=*/false,
/*debug=*/true,
/*compDb=*/false, /*optLevel=*/1
);
const Profile relExpected(
/*cxxflags=*/{}, /*ldflags=*/{}, /*lto=*/false,
/*debug=*/false,
/*compDb=*/false, /*optLevel=*/2 // here, the default is 3
);

const auto profiles = parseProfiles(overwrite).unwrap();
assertEq(profiles.size(), 2UL);
assertEq(profiles.at("dev"), devExpected);
assertEq(profiles.at("release"), relExpected);
}
}

static void
testLintTryFromToml() {
// Basic lint config
Expand All @@ -894,7 +989,7 @@ testLintTryFromToml() {
"+filter1",
"-filter2"
]
)"_toml;
)"_toml;

auto lint = Lint::tryFromToml(val).unwrap();
assertEq(
Expand Down Expand Up @@ -988,6 +1083,7 @@ main() {
tests::testEditionTryFromString();
tests::testEditionComparison();
tests::testPackageTryFromToml();
tests::testParseProfiles();
tests::testLintTryFromToml();
tests::testValidateDepName();
}
Expand Down
22 changes: 22 additions & 0 deletions src/Manifest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ struct Profile {
) noexcept
: cxxflags(std::move(cxxflags)), ldflags(std::move(ldflags)), lto(lto),
debug(debug), compDb(compDb), optLevel(optLevel) {}

bool operator==(const Profile& other) const {
return cxxflags == other.cxxflags && ldflags == other.ldflags
&& lto == other.lto && debug == other.debug && compDb == other.compDb
&& optLevel == other.optLevel;
}

friend std::ostream& operator<<(std::ostream& os, const Profile& p) {
const std::string str = fmt::format(
R"(Profile {{
cxxflags: {},
ldflags: {},
lto: {},
debug: {},
compDb: {},
optLevel: {},
}})",
p.cxxflags, p.ldflags, p.lto, p.debug, p.compDb, p.optLevel
);
os << str;
return os;
}
};

struct Cpplint {
Expand Down
15 changes: 9 additions & 6 deletions src/Rustify/Tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ error(const std::source_location& loc, Display auto&&... msgs) {
<< std::boolalpha;
(oss << ... << std::forward<decltype(msgs)>(msgs))
<< "', " << loc.file_name() << ':' << loc.line() << '\n';
throw std::logic_error(oss.str());

std::string str = oss.str();
std::cerr << str;
throw std::logic_error(str);
}

inline void
assertTrue(
const bool cond, const std::string_view msg = "",
const std::source_location& loc = std::source_location::current()
) noexcept {
) {
if (cond) {
return; // OK
}
Expand All @@ -108,7 +111,7 @@ inline void
assertFalse(
const bool cond, const std::string_view msg = "",
const std::source_location& loc = std::source_location::current()
) noexcept {
) {
if (!cond) {
return; // OK
}
Expand All @@ -126,7 +129,7 @@ inline void
assertEq(
Lhs&& lhs, Rhs&& rhs, const std::string_view msg = "",
const std::source_location& loc = std::source_location::current()
) noexcept {
) {
if (lhs == rhs) {
return; // OK
}
Expand All @@ -148,7 +151,7 @@ inline void
assertNe(
Lhs&& lhs, Rhs&& rhs, const std::string_view msg = "",
const std::source_location& loc = std::source_location::current()
) noexcept {
) {
if (lhs != rhs) {
return; // OK
}
Expand All @@ -170,7 +173,7 @@ inline void
assertLt(
Lhs&& lhs, Rhs&& rhs, const std::string_view msg = "",
const std::source_location& loc = std::source_location::current()
) noexcept {
) {
if (lhs < rhs) {
return; // OK
}
Expand Down

0 comments on commit d69c364

Please sign in to comment.