3 class LeadingWhitespaceValidatable
4 include ActiveModel::Validations
5 validates :string, :whitespace => { :leading => false }
9 class TrailingWhitespaceValidatable
10 include ActiveModel::Validations
11 validates :string, :whitespace => { :trailing => false }
15 class WhitespaceValidatorTest < ActiveSupport::TestCase
16 include Rails::Dom::Testing::Assertions::SelectorAssertions
18 def test_with_leading_whitespace
19 validator = LeadingWhitespaceValidatable.new
21 strings = [" ", " test", " ", "\ttest"]
25 assert_not_predicate validator, :valid?, "'#{v}' should not be valid"
29 def test_without_leading_whitespace
30 validator = LeadingWhitespaceValidatable.new
32 strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
36 assert_predicate validator, :valid?, "'#{v}' should be valid"
40 def test_with_trailing_whitespace
41 validator = TrailingWhitespaceValidatable.new
43 strings = [" ", "test ", " ", "test\t", "_test_ "]
47 assert_not_predicate validator, :valid?, "'#{v}' should not be valid"
51 def test_without_trailing_whitespace
52 validator = TrailingWhitespaceValidatable.new
54 strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
58 assert_predicate validator, :valid?, "'#{v}' should be valid"