<commit_before>// cpsm - fuzzy path matcher
// Copyright (C) 2015 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <algorithm>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <vector>

#include "match.h"
#include "matcher.h"
#include "str_util.h"

namespace cpsm {

class TestAssertionFailure : public std::exception {
 public:
  TestAssertionFailure() : msg_("test assertion failed") {}

  template <typename... Args>
  explicit TestAssertionFailure(Args... args)
      : msg_(str_cat("test assertion failed: ", args...)) {}

  char const* what() const noexcept override { return msg_.c_str(); }

 private:
  std::string msg_;
};

void test_match_order() {
  std::vector<std::string> items({
      "barfoo", "fbar", "foo/bar", "foo/fbar", "foo/foobar", "foo/foo_bar",
      "foo/foo_bar_test", "foo/FooBar", "foo/abar", "foo/qux",
  });

  Matcher matcher("fb");
  std::vector<Match<std::string>> match_objs;
  std::vector<std::string> matches;
  for (auto const& item : items) {
    Match<std::string> match(item);
    if (matcher.match(item, match)) {
      match_objs.emplace_back(std::move(match));
    }
  }
  sort_limit(match_objs);
  std::printf("matches:");
  for (auto& m : match_objs) {
    std::printf(" %s", m.item.c_str());
    matches.emplace_back(std::move(m.item));
   }
   std::printf("\n");

  auto const match_it =
      [&](boost::string_ref const item) -> std::vector<std::string>::iterator {
        return std::find_if(
            matches.begin(), matches.end(),
            [item](std::string const& match) -> bool { return item == match; });
      };
  auto const matched = [&](boost::string_ref const item)
                           -> bool { return match_it(item) != matches.end(); };
  auto const assert_matched = [&](boost::string_ref const item) {
    if (!matched(item)) {
      throw TestAssertionFailure("incorrectly matched '", item, "'");
    }
  };
  auto const assert_not_matched = [&](boost::string_ref const item) {
    if (matched(item)) {
      throw TestAssertionFailure("incorrectly failed to match '", item, "'");
    }
  };
  assert_not_matched("barfoo");
  assert_matched("fbar");
  assert_matched("foo/bar");
  assert_matched("foo/fbar");<commit_msg>Fix incorrect test assertion text<commit_after>// cpsm - fuzzy path matcher
// Copyright (C) 2015 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <algorithm>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <vector>

#include "match.h"
#include "matcher.h"
#include "str_util.h"

namespace cpsm {

class TestAssertionFailure : public std::exception {
 public:
  TestAssertionFailure() : msg_("test assertion failed") {}

  template <typename... Args>
  explicit TestAssertionFailure(Args... args)
      : msg_(str_cat("test assertion failed: ", args...)) {}

  char const* what() const noexcept override { return msg_.c_str(); }

 private:
  std::string msg_;
};

void test_match_order() {
  std::vector<std::string> items({
      "barfoo", "fbar", "foo/bar", "foo/fbar", "foo/foobar", "foo/foo_bar",
      "foo/foo_bar_test", "foo/FooBar", "foo/abar", "foo/qux",
  });

  Matcher matcher("fb");
  std::vector<Match<std::string>> match_objs;
  std::vector<std::string> matches;
  for (auto const& item : items) {
    Match<std::string> match(item);
    if (matcher.match(item, match)) {
      match_objs.emplace_back(std::move(match));
    }
  }
  sort_limit(match_objs);
  std::printf("matches:");
  for (auto& m : match_objs) {
    std::printf(" %s", m.item.c_str());
    matches.emplace_back(std::move(m.item));
   }
   std::printf("\n");

  auto const match_it =
      [&](boost::string_ref const item) -> std::vector<std::string>::iterator {
        return std::find_if(
            matches.begin(), matches.end(),
            [item](std::string const& match) -> bool { return item == match; });
      };
  auto const matched = [&](boost::string_ref const item)
                           -> bool { return match_it(item) != matches.end(); };
  auto const assert_matched = [&](boost::string_ref const item) {
    if (!matched(item)) {
      throw TestAssertionFailure("incorrectly failed to match '", item, "'");
    }
  };
  auto const assert_not_matched = [&](boost::string_ref const item) {
    if (matched(item)) {
      throw TestAssertionFailure("incorrectly matched '", item, "'");
    }
  };
  assert_not_matched("barfoo");
  assert_matched("fbar");
  assert_matched("foo/bar");
  assert_matched("foo/fbar");
{'fraction_non_alphanumeric': 0.15904139433551198, 'fraction_numerical': 0.002904865649963689, 'mean_word_length': 3.6646909398814564, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '191717', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1724, 'n_words': 473}