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