<commit_before># coding:utf-8

require_relative '../test_helper'
require 'ostruct'

module SmartAnswer
  class DateQuestionTest < ActiveSupport::TestCase
    def setup
      @initial_state = State.new(:example)
    end

    test "dates are parsed from Hash into Date before being saved" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
      end

      new_state = q.transition(@initial_state, {year: "2011", month: '2', day: '1'})
      assert_equal Date.parse('2011-02-01'), new_state.date
    end

    test "incomplete dates raise an error" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
      end

      assert_raise SmartAnswer::InvalidResponse do
        q.transition(@initial_state, {year: "", month: '2', day: '1'})
      end
    end

    test "define allowable range of dates" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
        from { Date.parse('2011-01-01') }
        to { Date.parse('2011-01-03') }
      end
      assert_equal ::Date.parse('2011-01-01')..::Date.parse('2011-01-03'), q.range
    end

    test "a day before the allowed range is invalid" do
      assert_raises(InvalidResponse) do
        date_question_2011.transition(@initial_state, '2010-12-31')
      end
    end

    test "a day after the allowed range is invalid" do
      assert_raises(InvalidResponse) do
        date_question_2011.transition(@initial_state, '2012-01-01')
      end
    end

    test "the first day of the allowed range is valid" do
      new_state = date_question_2011.transition(@initial_state, '2011-01-01')
      assert @initial_state != new_state
    end

    test "the last day of the allowed range is valid" do
      new_state = date_question_2011.transition(@initial_state, '2011-12-31')
      assert @initial_state != new_state
    end

    test "do not complain when the input is within the allowed range when the dates are in descending order" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
        from { Date.parse('2011-01-03') }
        to { Date.parse('2011-01-01') }
        validate_in_range
      end

      q.transition(@initial_state, '2011-01-02')
    end

    test "define default day" do
      q = Question::Date.new(nil, :example) do
        default_day { 11 }
      end
      assert_equal 11, q.default_day
    end

    test "define default month" do
      q = Question::Date.new(nil, :example) do
        default_month 2
      end
      assert_equal 2, q.default_month
    end

    test "define default year" do
      q = Question::Date.new(nil, :example) do
        default_year 2013
      end
      assert_equal 2013, q.default_year
    end

    test "incomplete dates are accepted if appropriate defaults are defined" do
      q = Question::Date.new(nil, :example) do
        default_day { 11 }
        default_month 2
        default_year 2013
        save_input_as :date
        next_node :done
      end

      new_state = q.transition(@initial_state, {year: "", month: "", day: ""})
      assert_equal Date.parse('2013-02-11'), new_state.date
    end

    test "default the day to the last in the month of an incomplete date" do
      q = Question::Date.new(nil, :example) do<commit_msg>Always pass blocks to Question::Date#default_month<commit_after># coding:utf-8

require_relative '../test_helper'
require 'ostruct'

module SmartAnswer
  class DateQuestionTest < ActiveSupport::TestCase
    def setup
      @initial_state = State.new(:example)
    end

    test "dates are parsed from Hash into Date before being saved" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
      end

      new_state = q.transition(@initial_state, {year: "2011", month: '2', day: '1'})
      assert_equal Date.parse('2011-02-01'), new_state.date
    end

    test "incomplete dates raise an error" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
      end

      assert_raise SmartAnswer::InvalidResponse do
        q.transition(@initial_state, {year: "", month: '2', day: '1'})
      end
    end

    test "define allowable range of dates" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
        from { Date.parse('2011-01-01') }
        to { Date.parse('2011-01-03') }
      end
      assert_equal ::Date.parse('2011-01-01')..::Date.parse('2011-01-03'), q.range
    end

    test "a day before the allowed range is invalid" do
      assert_raises(InvalidResponse) do
        date_question_2011.transition(@initial_state, '2010-12-31')
      end
    end

    test "a day after the allowed range is invalid" do
      assert_raises(InvalidResponse) do
        date_question_2011.transition(@initial_state, '2012-01-01')
      end
    end

    test "the first day of the allowed range is valid" do
      new_state = date_question_2011.transition(@initial_state, '2011-01-01')
      assert @initial_state != new_state
    end

    test "the last day of the allowed range is valid" do
      new_state = date_question_2011.transition(@initial_state, '2011-12-31')
      assert @initial_state != new_state
    end

    test "do not complain when the input is within the allowed range when the dates are in descending order" do
      q = Question::Date.new(nil, :example) do
        save_input_as :date
        next_node :done
        from { Date.parse('2011-01-03') }
        to { Date.parse('2011-01-01') }
        validate_in_range
      end

      q.transition(@initial_state, '2011-01-02')
    end

    test "define default day" do
      q = Question::Date.new(nil, :example) do
        default_day { 11 }
      end
      assert_equal 11, q.default_day
    end

    test "define default month" do
      q = Question::Date.new(nil, :example) do
        default_month { 2 }
      end
      assert_equal 2, q.default_month
    end

    test "define default year" do
      q = Question::Date.new(nil, :example) do
        default_year 2013
      end
      assert_equal 2013, q.default_year
    end

    test "incomplete dates are accepted if appropriate defaults are defined" do
      q = Question::Date.new(nil, :example) do
        default_day { 11 }
        default_month { 2 }
        default_year 2013
        save_input_as :date
        next_node :done
      end

      new_state = q.transition(@initial_state, {year: "", month: "", day: ""})
      assert_equal Date.parse('2013-02-11'), new_state.date
    end

    test "default the day to the last in the month of an incomplete date" do
      q = Question::Date.new(nil, :example) do
{'fraction_non_alphanumeric': 0.09930236010093513, 'fraction_numerical': 0.0445302063232893, 'mean_word_length': 2.54445028932141, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4417432', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2018, 'n_words': 617}