X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/b4ef61a9f31ac8bb911c094bc2fb00a9fa2102a6..d73a5d4bc015d304e7b8863a0e927ac0b134e07b:/test/validators/characters_validator_test.rb
diff --git a/test/validators/characters_validator_test.rb b/test/validators/characters_validator_test.rb
new file mode 100644
index 000000000..e1409a5dc
--- /dev/null
+++ b/test/validators/characters_validator_test.rb
@@ -0,0 +1,66 @@
+require "test_helper"
+
+class InvalidCharsValidatable
+ include ActiveModel::Validations
+ validates :chars, :characters => true
+ attr_accessor :chars
+end
+
+class InvalidUrlCharsValidatable
+ include ActiveModel::Validations
+ validates :chars, :characters => { :url_safe => true }
+ attr_accessor :chars
+end
+
+class CharactersValidatorTest < ActiveSupport::TestCase
+ include Rails::Dom::Testing::Assertions::SelectorAssertions
+
+ def test_with_valid_chars
+ c = InvalidCharsValidatable.new
+
+ valid = ["Name.", "'me", "he\"", "
", "*ho", "\"help\"@",
+ "vergröÃern", "ã«ã·ã¹ãã ã«ã対å¿ãã¾ã", "è¼è§¸ææçéæ²", "/;.,?%#"]
+
+ valid.each do |v|
+ c.chars = v
+ assert c.valid?, "'#{v}' should be valid"
+ end
+ end
+
+ def test_with_invalid_chars
+ c = InvalidCharsValidatable.new
+
+ invalid = ["\x7f
", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe",
+ "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-ã対å¿ãã¾ã/", "\x0c#ping",
+ "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"]
+
+ invalid.each do |v|
+ c.chars = v
+ assert_not c.valid?, "'#{v}' should not be valid"
+ end
+ end
+
+ def test_with_valid_url_chars
+ c = InvalidUrlCharsValidatable.new
+
+ valid = ["Name", "'me", "he\"", "
", "*ho", "\"help\"@",
+ "vergröÃern", "ã«ã·ã¹ãã ã«ã対å¿ãã¾ã", "è¼è§¸ææçéæ²"]
+
+ valid.each do |v|
+ c.chars = v
+ assert c.valid?, "'#{v}' should be valid"
+ end
+ end
+
+ def test_with_invalid_url_chars
+ c = InvalidUrlCharsValidatable.new
+
+ invalid = ["Name.", "you;me", "he\"#", "
", "50%", "good?",
+ "vergröÃern,deutsche", "ã«ã·ã¹ãã ã«;.ã対å¿ãã¾ã", "è¼è§¸æ/æçéæ²", "/;.,?%#"]
+
+ invalid.each do |v|
+ c.chars = v
+ assert_not c.valid?, "'#{v}' should not be valid"
+ end
+ end
+end