Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/extension/schemaconfig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME schemaconfig
PRIVATE_HEADERS error.h SOURCES parse.cc find.cc)
PRIVATE_HEADERS error.h SOURCES parse.cc schemaconfig.cc)

if(SOURCEMETA_CORE_INSTALL)
sourcemeta_library_install(NAMESPACE sourcemeta PROJECT core NAME schemaconfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ struct SOURCEMETA_CORE_SCHEMACONFIG_EXPORT SchemaConfig {
std::unordered_map<JSON::String, JSON::String> resolve;
JSON extra = JSON::make_object();

/// Check if the given path represents a schema described by this
/// configuration
[[nodiscard]]
auto applies_to(const std::filesystem::path &path) const -> bool;

/// Parse a configuration file from its contents
[[nodiscard]]
static auto from_json(const JSON &value,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <sourcemeta/core/io.h>
#include <sourcemeta/core/schemaconfig.h>

#include <cassert> // assert
#include <algorithm> // std::ranges::any_of
#include <cassert> // assert
#include <string> // std::string

namespace sourcemeta::core {

Expand Down Expand Up @@ -31,4 +33,11 @@ auto SchemaConfig::find(const std::filesystem::path &path)
return std::nullopt;
}

auto SchemaConfig::applies_to(const std::filesystem::path &path) const -> bool {
const std::string filename{path.filename().string()};
return std::ranges::any_of(this->extension, [&filename](const auto &suffix) {
return filename.ends_with(suffix);
});
}

} // namespace sourcemeta::core
1 change: 1 addition & 0 deletions test/schemaconfig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sourcemeta_googletest(NAMESPACE sourcemeta PROJECT core NAME schemaconfig
SOURCES
schemaconfig_applies_to_test.cc
schemaconfig_find_test.cc
schemaconfig_from_json_test.cc
schemaconfig_read_json_test.cc)
Expand Down
102 changes: 102 additions & 0 deletions test/schemaconfig/schemaconfig_applies_to_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <gtest/gtest.h>

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/schemaconfig.h>

TEST(SchemaConfig_applies_to, default_extensions) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com"
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_TRUE(config.applies_to("foo.json"));
EXPECT_TRUE(config.applies_to("bar.yaml"));
EXPECT_TRUE(config.applies_to("baz.yml"));
EXPECT_FALSE(config.applies_to("qux.schema"));
EXPECT_FALSE(config.applies_to("no_extension"));
}

TEST(SchemaConfig_applies_to, single_extension_match) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com",
"extension": ".json"
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_TRUE(config.applies_to("foo.json"));
EXPECT_TRUE(config.applies_to("/path/to/schema.json"));
}

TEST(SchemaConfig_applies_to, single_extension_no_match) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com",
"extension": ".json"
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_FALSE(config.applies_to("foo.yaml"));
EXPECT_FALSE(config.applies_to("foo.schema"));
EXPECT_FALSE(config.applies_to("no_extension"));
}

TEST(SchemaConfig_applies_to, multiple_extensions_match) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com",
"extension": [ ".json", ".yaml", ".yml" ]
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_TRUE(config.applies_to("foo.json"));
EXPECT_TRUE(config.applies_to("bar.yaml"));
EXPECT_TRUE(config.applies_to("baz.yml"));
}

TEST(SchemaConfig_applies_to, multiple_extensions_no_match) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com",
"extension": [ ".json", ".yaml" ]
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_FALSE(config.applies_to("foo.schema"));
EXPECT_FALSE(config.applies_to("bar.txt"));
EXPECT_FALSE(config.applies_to("no_extension"));
}

TEST(SchemaConfig_applies_to, double_extension_match) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com",
"extension": ".json"
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_TRUE(config.applies_to("foo.schema.json"));
EXPECT_TRUE(config.applies_to("/path/to/bar.test.json"));
}

TEST(SchemaConfig_applies_to, compound_extension_match) {
const auto input{sourcemeta::core::parse_json(R"JSON({
"baseUri": "https://example.com",
"extension": ".schema.json"
})JSON")};

const auto config{
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};

EXPECT_TRUE(config.applies_to("foo.schema.json"));
EXPECT_TRUE(config.applies_to("/path/to/bar.schema.json"));
EXPECT_FALSE(config.applies_to("baz.json"));
EXPECT_FALSE(config.applies_to("qux.schema.yaml"));
}