Sha256: c7e29cf67eefda0a906e4e85b01b96a0c35ee136c81e5d1c7c6a8c503034caf4

Contents?: true

Size: 1.15 KB

Versions: 18

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8
require 'test_helper'

class BytesizeValidatorTest < Minitest::Test

  class Model
    include ActiveModel::Validations

    attr_accessor :data
    validates :data, bytesize: { maximum: 100, encoding: 'utf-8' }
  end

  def setup
    @model = Model.new
  end

  def test_nil_is_valid
    @model.data = nil
    assert @model.valid?
  end

  def test_fitting_strings_are_valid
    @model.data = ''
    assert @model.valid?

    @model.data = 'a' * 100
    assert @model.valid?

    @model.data = 'ü' * 50
    assert @model.valid?

    @model.data = "\0" * 100
    assert @model.valid?
  end

  def test_too_large_binary_values_are_invalid
    @model.data = "\0" * 101
    assert @model.invalid?
    assert_equal ["is too long (maximum is 100 bytes)"], @model.errors[:data]
  end

  def test_too_large_unicode_values_are_invalid
    @model.data = '💩' * 26
    assert @model.invalid?
    assert_equal ["is too long (maximum is 100 bytes)"], @model.errors[:data]
  end

  def test_transcoding
    @model.data = ('ü' * 51).encode('ISO-8859-15')
    assert @model.invalid?
    assert_equal ["is too long (maximum is 100 bytes)"], @model.errors[:data]
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
activerecord-databasevalidations-1.0.1 test/bytesize_validator_test.rb
activerecord-databasevalidations-1.0.0 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.5.1 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.5.0 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.4.0 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.3.1 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.3.0 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.6 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.5 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.4 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.3 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.2 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.1 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.2.0 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.1.3 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.1.2 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.1.1 test/bytesize_validator_test.rb
activerecord-databasevalidations-0.1.0 test/bytesize_validator_test.rb