]> git.openstreetmap.org Git - rails.git/blob - test/validators/whitespace_validator_test.rb
Exclude `opening_hours` from semicolon splitting in tags helper (#6968)
[rails.git] / test / validators / whitespace_validator_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class WhitespaceValidatorTest < ActiveSupport::TestCase
6   include Rails::Dom::Testing::Assertions::SelectorAssertions
7
8   class LeadingWhitespaceValidatable
9     include ActiveModel::Validations
10
11     validates :string, :whitespace => { :leading => false }
12     attr_accessor :string
13   end
14
15   class TrailingWhitespaceValidatable
16     include ActiveModel::Validations
17
18     validates :string, :whitespace => { :trailing => false }
19     attr_accessor :string
20   end
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