]> git.openstreetmap.org Git - rails.git/blob - test/validators/characters_validator_test.rb
Merge remote-tracking branch 'upstream/pull/2083'
[rails.git] / test / validators / characters_validator_test.rb
1 require "test_helper"
2
3 class InvalidCharsValidatable
4   include ActiveModel::Validations
5   validates :chars, :characters => true
6   attr_accessor :chars
7 end
8
9 class InvalidUrlCharsValidatable
10   include ActiveModel::Validations
11   validates :chars, :characters => { :url_safe => true }
12   attr_accessor :chars
13 end
14
15 class CharactersValidatorTest < ActiveSupport::TestCase
16   include Rails::Dom::Testing::Assertions::SelectorAssertions
17
18   def test_with_valid_chars
19     c = InvalidCharsValidatable.new
20
21     valid = ["Name.", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
22              "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "/;.,?%#"]
23
24     valid.each do |v|
25       c.chars = v
26       assert c.valid?, "'#{v}' should be valid"
27     end
28   end
29
30   def test_with_invalid_chars
31     c = InvalidCharsValidatable.new
32
33     invalid = ["\x7f<hr/>", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe",
34                "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-も対応します/", "\x0c#ping",
35                "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"]
36
37     invalid.each do |v|
38       c.chars = v
39       assert_not c.valid?, "'#{v}' should not be valid"
40     end
41   end
42
43   def test_with_valid_url_chars
44     c = InvalidUrlCharsValidatable.new
45
46     valid = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
47              "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
48
49     valid.each do |v|
50       c.chars = v
51       assert c.valid?, "'#{v}' should be valid"
52     end
53   end
54
55   def test_with_invalid_url_chars
56     c = InvalidUrlCharsValidatable.new
57
58     invalid = ["Name.", "you;me", "he\"#", "<hr/>", "50%", "good?",
59                "vergrößern,deutsche", "ルシステムに;.も対応します", "輕觸搖/晃的遊戲", "/;.,?%#"]
60
61     invalid.each do |v|
62       c.chars = v
63       assert_not c.valid?, "'#{v}' should not be valid"
64     end
65   end
66 end