<commit_before>require 'test_helper'

# Should not be an Action Controller test, but a Unit Test instead.
class RailsExtentionsTest < ActionController::TestCase

  def setup
    @a_hash = {a: 1, some_str: "a string", inner_hash: { 'b' => 2, c: 3 } }
  end

  test "any Object should respond to :allow" do
    assert_equal nil,       nil.allow(nil)
    assert_equal nil,       nil.allow(1)
    assert_equal nil,       nil.allow(:a)
    assert_equal nil,       nil.allow([])
    assert_equal nil,       nil.allow([:a, :b])
    assert_equal nil,       :c.allow(:a)
    assert_equal :a,        :a.allow(:a)
    assert_equal nil,       :c.allow([:a, :b])
    assert_equal :a,        :a.allow([:a, :b])
    assert_equal nil,       [:a, :b].allow([:a, :b])
    assert_equal [:a, :b],  [:a, :b].allow([[:a, :b]])
    assert_equal nil,       'a'.allow([:a, :b])
    assert_equal 'a',       'a'.allow([:a, :b, 'a'])
    assert_equal 'a',       'a'.allow(Set[:a, :b, 'a'])
    assert_equal nil,       ['a'].allow(Set[:a, :b, 'a'])
    assert_equal ['a'],     ['a'].allow(Set[:a, :b, 'a', ['a']])
  end

  test "it should be possible to get the closest element in an array" do
    a = (1..10).to_a
    assert_equal 3, a.find_closest_to(3)
    assert_equal 3, a.find_closest_to(3.3)
    assert_equal 3, a.find_closest_to(3.5)
    assert_equal 4, a.find_closest_to(3.51)
    assert_equal 4, a.find_closest_to(3.6)

    assert_equal 3,
      a.find_closest_to(3,   closest_type: Array::ClosestType::ABSOLUT)
    assert_equal 3,
      a.find_closest_to(3,   closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal a.find_equal_or_greater_closest_to(3),
      a.find_closest_to(3,   closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal 4,
      a.find_closest_to(3,   closest_type: Array::ClosestType::GREATER)
    assert_equal 3,
      a.find_closest_to(3,   closest_type: Array::ClosestType::EQUAL_OR_LESS)
    assert_equal 2,
      a.find_closest_to(3,   closest_type: Array::ClosestType::LESS)

    assert_equal 3,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::ABSOLUT)
    assert_equal 4,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal a.find_equal_or_greater_closest_to(3.1),
      a.find_closest_to(3.1, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal 4,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::GREATER)
    assert_equal 3,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::EQUAL_OR_LESS)
    assert_equal 3,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::LESS)

    assert_equal 3,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::ABSOLUT)
    assert_equal 3,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal a.find_equal_or_greater_closest_to(2.9),
      a.find_closest_to(2.9, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal 3,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::GREATER)
    assert_equal 2,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::EQUAL_OR_LESS)
    assert_equal 2,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::LESS)
  end

  test "it should be possible to get the closest element in an date array" do
    date = Time.zone.local(2016, 1, 1).to_date
    dates = (date.beginning_of_year..date.end_of_year).select(&:monday?)

    some_monday                 = Time.zone.local(2016, 2, 1).to_date
    some_monday_plus_one_day    = some_monday + 1.day
    some_monday_minus_one_day   = some_monday - 1.day
    some_monday_plus_one_week   = some_monday + 1.week
    some_monday_minus_one_week  = some_monday - 1.week
    some_monday_plus_one_hour   = some_monday.to_time + 1.hour
    some_monday_minus_one_hour  = some_monday.to_time - 1.hour

    assert_equal some_monday,
      dates.find_closest_to(some_monday)

    assert_raise do<commit_msg>Revert "fix: date related test"<commit_after>require 'test_helper'

# Should not be an Action Controller test, but a Unit Test instead.
class RailsExtentionsTest < ActionController::TestCase

  def setup
    @a_hash = {a: 1, some_str: "a string", inner_hash: { 'b' => 2, c: 3 } }
  end

  test "any Object should respond to :allow" do
    assert_equal nil,       nil.allow(nil)
    assert_equal nil,       nil.allow(1)
    assert_equal nil,       nil.allow(:a)
    assert_equal nil,       nil.allow([])
    assert_equal nil,       nil.allow([:a, :b])
    assert_equal nil,       :c.allow(:a)
    assert_equal :a,        :a.allow(:a)
    assert_equal nil,       :c.allow([:a, :b])
    assert_equal :a,        :a.allow([:a, :b])
    assert_equal nil,       [:a, :b].allow([:a, :b])
    assert_equal [:a, :b],  [:a, :b].allow([[:a, :b]])
    assert_equal nil,       'a'.allow([:a, :b])
    assert_equal 'a',       'a'.allow([:a, :b, 'a'])
    assert_equal 'a',       'a'.allow(Set[:a, :b, 'a'])
    assert_equal nil,       ['a'].allow(Set[:a, :b, 'a'])
    assert_equal ['a'],     ['a'].allow(Set[:a, :b, 'a', ['a']])
  end

  test "it should be possible to get the closest element in an array" do
    a = (1..10).to_a
    assert_equal 3, a.find_closest_to(3)
    assert_equal 3, a.find_closest_to(3.3)
    assert_equal 3, a.find_closest_to(3.5)
    assert_equal 4, a.find_closest_to(3.51)
    assert_equal 4, a.find_closest_to(3.6)

    assert_equal 3,
      a.find_closest_to(3,   closest_type: Array::ClosestType::ABSOLUT)
    assert_equal 3,
      a.find_closest_to(3,   closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal a.find_equal_or_greater_closest_to(3),
      a.find_closest_to(3,   closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal 4,
      a.find_closest_to(3,   closest_type: Array::ClosestType::GREATER)
    assert_equal 3,
      a.find_closest_to(3,   closest_type: Array::ClosestType::EQUAL_OR_LESS)
    assert_equal 2,
      a.find_closest_to(3,   closest_type: Array::ClosestType::LESS)

    assert_equal 3,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::ABSOLUT)
    assert_equal 4,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal a.find_equal_or_greater_closest_to(3.1),
      a.find_closest_to(3.1, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal 4,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::GREATER)
    assert_equal 3,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::EQUAL_OR_LESS)
    assert_equal 3,
      a.find_closest_to(3.1, closest_type: Array::ClosestType::LESS)

    assert_equal 3,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::ABSOLUT)
    assert_equal 3,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal a.find_equal_or_greater_closest_to(2.9),
      a.find_closest_to(2.9, closest_type: Array::ClosestType::EQUAL_OR_GREATER)
    assert_equal 3,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::GREATER)
    assert_equal 2,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::EQUAL_OR_LESS)
    assert_equal 2,
      a.find_closest_to(2.9, closest_type: Array::ClosestType::LESS)
  end

  test "it should be possible to get the closest element in an date array" do
    date = Date.new(2016, 1, 1)
    dates = (date.beginning_of_year..date.end_of_year).select(&:monday?)

    some_monday                 = Date.new(2016, 2, 1)
    some_monday_plus_one_day    = some_monday + 1.day
    some_monday_minus_one_day   = some_monday - 1.day
    some_monday_plus_one_week   = some_monday + 1.week
    some_monday_minus_one_week  = some_monday - 1.week
    some_monday_plus_one_hour   = some_monday.to_time + 1.hour
    some_monday_minus_one_hour  = some_monday.to_time - 1.hour

    assert_equal some_monday,
      dates.find_closest_to(some_monday)

    assert_raise do
{'fraction_non_alphanumeric': 0.12740384615384615, 'fraction_numerical': 0.02277327935222672, 'mean_word_length': 3.582608695652174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2743589', 'n_tokens_mistral': 3222, 'n_tokens_neox': 3079, 'n_words': 555}