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