File: test_length_validator.rb

package info (click to toggle)
ruby-client-side-validations 3.2.6%2Bgh-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: javascript: 3,019; ruby: 2,959; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'active_model/cases/test_base'

class ActiveModel::LengthValidatorTest < ClientSideValidations::ActiveModelTestBase

  def test_length_client_side_hash
    expected_hash = {
      :messages => {
        :is => "is the wrong length (should be 10 characters)"
      },
      :is => 10
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10).client_side_hash(@person, :first_name)
  end

  def test_length_client_side_hash_with_custom_message
    expected_hash = {
      :messages => {
        :is => "is the wrong length (should be 10 words)"
      },
      :is => 10
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10, :wrong_length => "is the wrong length (should be %{count} words)").client_side_hash(@person, :first_name)
  end

  def test_length_client_side_hash_with_custom_general_message
    expected_hash = {
      :messages => {
        :minimum => "is not the correct length",
        :maximum => "is way too long"
      },
      :minimum => 4,
      :maximum => 10
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :minimum => 4, :maximum => 10, :message => "is not the correct length", :too_long => "is way too long").client_side_hash(@person, :first_name)
  end

  def test_length_client_side_hash_with_js_tokenizer
    expected_hash = {
      :messages => {
        :is => "is the wrong length (should be 10 characters)"
      },
      :is => 10,
      :js_tokenizer => %q{match(/\w+/g)}
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10, :tokenizer => proc { |value| value.split(/\w+/) }, :js_tokenizer => %q{match(/\w+/g)}).client_side_hash(@person, :first_name)
  end

  def test_length_client_side_hash_with_minimum_and_maximum
    expected_hash = {
      :messages => {
        :minimum => "is too short (minimum is 5 characters)",
        :maximum => "is too long (maximum is 10 characters)"
      },
      :minimum => 5,
      :maximum => 10
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :minimum => 5, :maximum => 10).client_side_hash(@person, :first_name)
  end

  def test_length_client_side_hash_with_range
    expected_hash = {
      :messages => {
        :minimum => "is too short (minimum is 5 characters)",
        :maximum => "is too long (maximum is 10 characters)"
      },
      :minimum => 5,
      :maximum => 10
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :within => 5..10).client_side_hash(@person, :first_name)
  end

  def test_length_client_side_hash
    expected_hash = {
      :messages => {
        :is => "is the wrong length (should be 10 characters)"
      },
      :is => 10
    }
    assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10).client_side_hash(@person, :first_name)
  end
end