]> git.openstreetmap.org Git - rails.git/blob - test/validators/whitespace_validator_test.rb
Merge remote-tracking branch 'upstream/pull/6305'
[rails.git] / test / validators / whitespace_validator_test.rb
1 require "test_helper"
2
3 class LeadingWhitespaceValidatable
4   include ActiveModel::Validations
5
6   validates :string, :whitespace => { :leading => false }
7   attr_accessor :string
8 end
9
10 class TrailingWhitespaceValidatable
11   include ActiveModel::Validations
12
13   validates :string, :whitespace => { :trailing => false }
14   attr_accessor :string
15 end
16
17 class WhitespaceValidatorTest < ActiveSupport::TestCase
18   include Rails::Dom::Testing::Assertions::SelectorAssertions
19
20   def test_with_leading_whitespace
21     validator = LeadingWhitespaceValidatable.new
22
23     strings = [" ", " test", "  ", "\ttest"]
24
25     strings.each do |v|
26       validator.string = v
27       assert_not_predicate validator, :valid?, "'#{v}' should not be valid"
28     end
29   end
30
31   def test_without_leading_whitespace
32     validator = LeadingWhitespaceValidatable.new
33
34     strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
35
36     strings.each do |v|
37       validator.string = v
38       assert_predicate validator, :valid?, "'#{v}' should be valid"
39     end
40   end
41
42   def test_with_trailing_whitespace
43     validator = TrailingWhitespaceValidatable.new
44
45     strings = [" ", "test ", "  ", "test\t", "_test_ "]
46
47     strings.each do |v|
48       validator.string = v
49       assert_not_predicate validator, :valid?, "'#{v}' should not be valid"
50     end
51   end
52
53   def test_without_trailing_whitespace
54     validator = TrailingWhitespaceValidatable.new
55
56     strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
57
58     strings.each do |v|
59       validator.string = v
60       assert_predicate validator, :valid?, "'#{v}' should be valid"
61     end
62   end
63 end