# frozen_string_literal: true
require 'test_helper'

class AnswerFormatValidatorTest < ActiveSupport::TestCase
  test "text should PASS with string" do
    obj = AnswerValidatable.new('text')
    obj.answer = "this is a valid answer"

    assert obj.valid?
    assert obj.errors.messages.empty?
  end

  test "text should FAIL with nil" do
    obj = AnswerValidatable.new('text')
    obj.answer = nil

    refute obj.valid?
    assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
  end

  test "text should FAIL with empry string" do
    obj = AnswerValidatable.new('text')
    obj.answer = "   "

    refute obj.valid?
    assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
  end

  test "text should PASS with 999 character" do
    obj = AnswerValidatable.new('text')
    obj.answer = long_string_with_returns

    assert obj.valid?
    assert obj.errors.messages.empty?
  end

  test "text should FAIL with more than 1000 character" do
    obj = AnswerValidatable.new('text')
    obj.answer = long_string_with_returns + " - to long now "

    refute obj.valid?
    assert_match(/char.*limit.*1000.$/, obj.errors.messages[:answer][0])
  end

  def long_string_with_returns
    # returns 999 chars, after \r is stripped.
    "Some rando input\r\n\rYo. Making this up.\r\n" * 27
  end
end
