glue 0.20.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/CHANGELOG +161 -110
  2. data/INSTALL +12 -12
  3. data/README +1 -1
  4. data/Rakefile +43 -45
  5. data/doc/AUTHORS +5 -5
  6. data/doc/LICENSE +3 -3
  7. data/doc/RELEASES +32 -24
  8. data/install.rb +7 -17
  9. data/lib/facet/object/alias_class.rb +12 -0
  10. data/lib/glue.rb +35 -35
  11. data/lib/glue/array.rb +46 -46
  12. data/lib/glue/aspects.rb +199 -209
  13. data/lib/glue/attribute.rb +15 -15
  14. data/lib/glue/autoreload.rb +1 -1
  15. data/lib/glue/builder.rb +48 -0
  16. data/lib/glue/builder/xml.rb +114 -0
  17. data/lib/glue/cache.rb +189 -0
  18. data/lib/glue/configuration.rb +108 -90
  19. data/lib/glue/flexob.rb +17 -17
  20. data/lib/glue/hash.rb +71 -71
  21. data/lib/glue/helper.rb +12 -12
  22. data/lib/glue/idgen.rb +9 -0
  23. data/lib/glue/idgen/md5.rb +24 -0
  24. data/lib/glue/idgen/sequential.rb +15 -0
  25. data/lib/glue/literal_method.rb +44 -0
  26. data/lib/glue/localization.rb +130 -0
  27. data/lib/glue/logger.rb +98 -98
  28. data/lib/glue/misc.rb +7 -7
  29. data/lib/glue/mixins.rb +19 -19
  30. data/lib/glue/number.rb +8 -8
  31. data/lib/glue/object.rb +2 -2
  32. data/lib/glue/pool.rb +43 -43
  33. data/lib/glue/property.rb +392 -392
  34. data/lib/glue/sanitize.rb +34 -34
  35. data/lib/glue/settings.rb +1 -1
  36. data/lib/glue/snapshot.rb +104 -0
  37. data/lib/glue/string.rb +129 -129
  38. data/lib/glue/time.rb +53 -53
  39. data/lib/glue/uri.rb +162 -162
  40. data/lib/glue/validation.rb +421 -421
  41. data/lib/vendor/blankslate.rb +53 -0
  42. data/test/glue/builder/tc_xml.rb +56 -0
  43. data/test/glue/tc_aspects.rb +90 -90
  44. data/test/glue/tc_attribute.rb +11 -11
  45. data/test/glue/tc_builder.rb +30 -0
  46. data/test/glue/tc_configuration.rb +97 -97
  47. data/test/glue/tc_flexob.rb +10 -10
  48. data/test/glue/tc_hash.rb +23 -23
  49. data/test/glue/tc_localization.rb +49 -0
  50. data/test/glue/tc_logger.rb +31 -31
  51. data/test/glue/tc_numbers.rb +9 -9
  52. data/test/glue/tc_property.rb +67 -67
  53. data/test/glue/tc_property_mixins.rb +17 -17
  54. data/test/glue/tc_property_type_checking.rb +13 -13
  55. data/test/glue/tc_strings.rb +94 -94
  56. data/test/glue/tc_uri.rb +65 -65
  57. data/test/glue/tc_validation.rb +196 -196
  58. metadata +26 -4
@@ -10,26 +10,26 @@ Glue::Property.type_checking = true
10
10
  module Test # :nodoc: all
11
11
 
12
12
  class Person
13
- prop_accessor :age, Fixnum
14
-
15
- def initialize(age = nil)
16
- @age = age
17
- end
13
+ prop_accessor :age, Fixnum
14
+
15
+ def initialize(age = nil)
16
+ @age = age
17
+ end
18
18
 
19
19
  end
20
20
 
21
21
  class TC_PropertiesTypeChecking < Test::Unit::TestCase
22
22
 
23
- def test_all
24
- per = Person.new
23
+ def test_all
24
+ per = Person.new
25
25
  =begin
26
- FIXME: does not work when run in the full test suite.
27
- assert_raises(RuntimeError) {
28
- per.age = 'Hello'
29
- }
26
+ FIXME: does not work when run in the full test suite.
27
+ assert_raises(RuntimeError) {
28
+ per.age = 'Hello'
29
+ }
30
30
  =end
31
- end
32
-
31
+ end
32
+
33
33
  end
34
34
 
35
35
  end
@@ -3,100 +3,100 @@ require 'test/unit'
3
3
  require 'glue/string'
4
4
 
5
5
  class TCStringUtilsUtils < Test::Unit::TestCase # :nodoc: all
6
- include Glue
7
-
8
- def setup
9
- @s = "this is any cool test"
10
- end
11
-
12
- def test_valid?
13
- assert_equal(true, StringUtils.valid?("test"))
14
- assert_equal(false, StringUtils.valid?(""))
15
- assert_equal(false, StringUtils.valid?(nil))
16
- end
17
-
18
- def test_head
19
- assert_equal(nil, StringUtils.head(nil))
20
- assert_equal("this...", StringUtils.head(@s, 3))
21
- assert_equal("this+++", StringUtils.head(@s, 3, false, "+++"))
22
- assert_equal("thi...", StringUtils.head(@s, 3, true))
23
- assert_equal("this is...", StringUtils.head(@s, 8))
24
- assert_equal(nil, StringUtils.head(@s, 0))
25
- assert_equal(nil, StringUtils.head(@s, 0, true))
26
- assert_equal("this is...", StringUtils.head(@s, 8, true))
27
- end
28
-
29
- def test_rewrite
30
- rules = [
31
- [/\/\$/, "../n1"],
32
- [/\$/, "../n1"],
33
- [/^games/, "../../games"],
34
- [/^tmp/, "/n/theseas/var/tmp"],
35
- ]
36
- string = StringUtils.rewrite("games/arkanoid.html", rules)
37
- assert_equal("../../games/arkanoid.html", string)
38
- string = StringUtils.rewrite("tmp/garbage.html", rules)
39
- assert_equal("/n/theseas/var/tmp/garbage.html", string)
40
- string = StringUtils.rewrite("root/index.html", rules)
41
- assert_equal("root/index.html", string)
42
- assert_equal(nil, StringUtils.rewrite(nil, rules))
43
- assert_equal("", StringUtils.rewrite("", rules))
44
-
45
- assert_raises(ArgumentError) {
46
- assert_equal("koko", StringUtils.rewrite("koko", nil))
47
- }
48
-
49
- # bug: should keep order
50
- s = StringUtils.rewrite("/$/koko.html", rules)
51
- assert_equal("../n1/koko.html", s)
52
- end
53
-
54
- def test_wrap
55
- s = "1234567890abcdefghijklmnopqrstu"
56
- r = "1234 5678 90ab cdef ghij klmn opqr stu"
57
- assert_equal(r, StringUtils::wrap(s, 4, " "))
58
-
59
- s = "111111111111111111111111111111111111111111111111"
60
- r = "1111111111 1111111111 1111111111 1111111111 11111111"
61
- assert_equal(r, StringUtils::wrap(s, 10, " "))
62
-
63
- s = "jackdaws love my big sphinx of quartz"
64
- r = "jackdaws love my big sphinx of quartz"
65
- assert_equal(r, StringUtils::wrap(s, 10, " "))
66
-
67
- s = "jackdaws love my big sphinx of quartz"
68
- r = "jack daws love my big sphi nx of quar tz"
69
- assert_equal(r, StringUtils::wrap(s, 4, " "))
70
-
71
- s = "jack.daws love my big sphinx of quartz"
72
- r = "jack .daw s love my big sphi nx of quar tz"
73
- assert_equal(r, StringUtils::wrap(s, 4, " "))
74
-
75
- assert_equal("", StringUtils::wrap("", 4, " "))
76
- assert_equal(nil, StringUtils::wrap(nil, 4, " "))
77
- end
6
+ include Glue
7
+
8
+ def setup
9
+ @s = "this is any cool test"
10
+ end
11
+
12
+ def test_valid?
13
+ assert_equal(true, StringUtils.valid?("test"))
14
+ assert_equal(false, StringUtils.valid?(""))
15
+ assert_equal(false, StringUtils.valid?(nil))
16
+ end
17
+
18
+ def test_head
19
+ assert_equal(nil, StringUtils.head(nil))
20
+ assert_equal("this...", StringUtils.head(@s, 3))
21
+ assert_equal("this+++", StringUtils.head(@s, 3, false, "+++"))
22
+ assert_equal("thi...", StringUtils.head(@s, 3, true))
23
+ assert_equal("this is...", StringUtils.head(@s, 8))
24
+ assert_equal(nil, StringUtils.head(@s, 0))
25
+ assert_equal(nil, StringUtils.head(@s, 0, true))
26
+ assert_equal("this is...", StringUtils.head(@s, 8, true))
27
+ end
28
+
29
+ def test_rewrite
30
+ rules = [
31
+ [/\/\$/, "../n1"],
32
+ [/\$/, "../n1"],
33
+ [/^games/, "../../games"],
34
+ [/^tmp/, "/n/theseas/var/tmp"],
35
+ ]
36
+ string = StringUtils.rewrite("games/arkanoid.html", rules)
37
+ assert_equal("../../games/arkanoid.html", string)
38
+ string = StringUtils.rewrite("tmp/garbage.html", rules)
39
+ assert_equal("/n/theseas/var/tmp/garbage.html", string)
40
+ string = StringUtils.rewrite("root/index.html", rules)
41
+ assert_equal("root/index.html", string)
42
+ assert_equal(nil, StringUtils.rewrite(nil, rules))
43
+ assert_equal("", StringUtils.rewrite("", rules))
44
+
45
+ assert_raises(ArgumentError) {
46
+ assert_equal("koko", StringUtils.rewrite("koko", nil))
47
+ }
48
+
49
+ # bug: should keep order
50
+ s = StringUtils.rewrite("/$/koko.html", rules)
51
+ assert_equal("../n1/koko.html", s)
52
+ end
53
+
54
+ def test_wrap
55
+ s = "1234567890abcdefghijklmnopqrstu"
56
+ r = "1234 5678 90ab cdef ghij klmn opqr stu"
57
+ assert_equal(r, StringUtils::wrap(s, 4, " "))
58
+
59
+ s = "111111111111111111111111111111111111111111111111"
60
+ r = "1111111111 1111111111 1111111111 1111111111 11111111"
61
+ assert_equal(r, StringUtils::wrap(s, 10, " "))
62
+
63
+ s = "jackdaws love my big sphinx of quartz"
64
+ r = "jackdaws love my big sphinx of quartz"
65
+ assert_equal(r, StringUtils::wrap(s, 10, " "))
66
+
67
+ s = "jackdaws love my big sphinx of quartz"
68
+ r = "jack daws love my big sphi nx of quar tz"
69
+ assert_equal(r, StringUtils::wrap(s, 4, " "))
70
+
71
+ s = "jack.daws love my big sphinx of quartz"
72
+ r = "jack .daw s love my big sphi nx of quar tz"
73
+ assert_equal(r, StringUtils::wrap(s, 4, " "))
74
+
75
+ assert_equal("", StringUtils::wrap("", 4, " "))
76
+ assert_equal(nil, StringUtils::wrap(nil, 4, " "))
77
+ end
78
78
  =begin
79
- def test_rationalize_filename
80
- filename = StringUtils.rationalize_filename("hello my friend!.gif")
81
- assert_equal("hello-my-friend.gif", filename)
82
- filename = StringUtils.rationalize_filename("���� ����.gif")
83
- assert_equal("pame-pali.gif", filename)
84
- filename = StringUtils.rationalize_filename("�� ���.gif")
85
- assert_equal("ti-les.gif", filename)
86
-
87
- # bug:
88
- filename = StringUtils.rationalize_filename("image-(10).gif")
89
- assert_equal("image-10.gif", filename)
90
- end
79
+ def test_rationalize_filename
80
+ filename = StringUtils.rationalize_filename("hello my friend!.gif")
81
+ assert_equal("hello-my-friend.gif", filename)
82
+ filename = StringUtils.rationalize_filename("���� ����.gif")
83
+ assert_equal("pame-pali.gif", filename)
84
+ filename = StringUtils.rationalize_filename("�� ���.gif")
85
+ assert_equal("ti-les.gif", filename)
86
+
87
+ # bug:
88
+ filename = StringUtils.rationalize_filename("image-(10).gif")
89
+ assert_equal("image-10.gif", filename)
90
+ end
91
91
  =end
92
- def test_random_string
93
- s1 = StringUtils.random()
94
- s2 = StringUtils.random()
95
- assert_not_equal(s1, s2)
96
- assert(s1.size == s2.size)
97
- end
98
-
99
- def teardown
100
- @s = nil
101
- end
92
+ def test_random_string
93
+ s1 = StringUtils.random()
94
+ s2 = StringUtils.random()
95
+ assert_not_equal(s1, s2)
96
+ assert(s1.size == s2.size)
97
+ end
98
+
99
+ def teardown
100
+ @s = nil
101
+ end
102
102
  end
data/test/glue/tc_uri.rb CHANGED
@@ -2,96 +2,96 @@ require 'test/unit'
2
2
  require 'glue/uri'
3
3
 
4
4
  class Dummy # :nodoc: all
5
- attr_accessor :test1, :test2
5
+ attr_accessor :test1, :test2
6
6
  end
7
7
 
8
8
  class TC_Uri < Test::Unit::TestCase # :nodoc: all
9
- include Glue
9
+ include Glue
10
10
 
11
- def test_query_string_to_hash
12
- # bad query string
11
+ def test_query_string_to_hash
12
+ # bad query string
13
13
 
14
- assert_equal(0, UriUtils.query_string_to_hash("").length)
15
- assert_equal(0, UriUtils.query_string_to_hash(nil).length)
14
+ assert_equal(0, UriUtils.query_string_to_hash("").length)
15
+ assert_equal(0, UriUtils.query_string_to_hash(nil).length)
16
16
 
17
- # single valued
17
+ # single valued
18
18
 
19
- parameters = UriUtils.query_string_to_hash("koko=2&lala=3")
20
- assert_equal("2", parameters["koko"])
21
- assert_equal("3", parameters["lala"])
19
+ parameters = UriUtils.query_string_to_hash("koko=2&lala=3")
20
+ assert_equal("2", parameters["koko"])
21
+ assert_equal("3", parameters["lala"])
22
22
 
23
- parameters = UriUtils.query_string_to_hash("koko=2;lala=3")
24
- assert_equal("2", parameters["koko"])
25
- assert_equal("3", parameters["lala"])
23
+ parameters = UriUtils.query_string_to_hash("koko=2;lala=3")
24
+ assert_equal("2", parameters["koko"])
25
+ assert_equal("3", parameters["lala"])
26
26
 
27
- # multivalued
27
+ # multivalued
28
28
 
29
- parameters = UriUtils.query_string_to_hash("koko=2;lala=3&koko=5")
30
- assert_equal(2, parameters["koko"].length)
31
- assert_equal("2", parameters["koko"][0])
32
- assert_equal("3", parameters["lala"])
33
- assert_equal("5", parameters["koko"][1])
29
+ parameters = UriUtils.query_string_to_hash("koko=2;lala=3&koko=5")
30
+ assert_equal(2, parameters["koko"].length)
31
+ assert_equal("2", parameters["koko"][0])
32
+ assert_equal("3", parameters["lala"])
33
+ assert_equal("5", parameters["koko"][1])
34
34
 
35
- # non existing value
36
- assert_equal(nil, parameters["non-existing-value"])
37
- end
35
+ # non existing value
36
+ assert_equal(nil, parameters["non-existing-value"])
37
+ end
38
38
 
39
- def test_hash_to_query_string
40
- hash = { "koko" => 1, "lala" => 2}
41
- qs = "koko=1;lala=2"
42
- assert_equal(qs, UriUtils.hash_to_query_string(hash))
39
+ def test_hash_to_query_string
40
+ hash = { "koko" => 1, "lala" => 2}
41
+ qs = "koko=1;lala=2"
42
+ assert_equal(qs, UriUtils.hash_to_query_string(hash))
43
43
 
44
- assert_equal(nil, UriUtils.hash_to_query_string(nil))
44
+ assert_equal(nil, UriUtils.hash_to_query_string(nil))
45
45
 
46
- # bug: dont encode complex objects
47
- hash = { "a" => 2, "b" => 3, "c" => Dummy.new }
48
- qs = "a=2;b=3"
49
- assert_equal(qs, UriUtils.hash_to_query_string(hash))
50
- end
46
+ # bug: dont encode complex objects
47
+ hash = { "a" => 2, "b" => 3, "c" => Dummy.new }
48
+ qs = "a=2;b=3"
49
+ assert_equal(qs, UriUtils.hash_to_query_string(hash))
50
+ end
51
51
 
52
- def test_get_query_string
53
- uri = "people/gmosx.sx?koko=1;lala=2"
54
- assert_equal("koko=1;lala=2", UriUtils.get_query_string(uri))
52
+ def test_get_query_string
53
+ uri = "people/gmosx.sx?koko=1;lala=2"
54
+ assert_equal("koko=1;lala=2", UriUtils.get_query_string(uri))
55
55
 
56
- uri = "http://www.navel.gr/people/gmosx.sx?koko=1&lala=2"
57
- assert_equal("koko=1&lala=2", UriUtils.get_query_string(uri))
56
+ uri = "http://www.navel.gr/people/gmosx.sx?koko=1&lala=2"
57
+ assert_equal("koko=1&lala=2", UriUtils.get_query_string(uri))
58
58
 
59
- uri = "http://www.navel.gr:8080/people/gmosx.sx?koko=1;lala=2"
60
- assert_equal("koko=1;lala=2", UriUtils.get_query_string(uri))
61
- end
59
+ uri = "http://www.navel.gr:8080/people/gmosx.sx?koko=1;lala=2"
60
+ assert_equal("koko=1;lala=2", UriUtils.get_query_string(uri))
61
+ end
62
62
 
63
- def test_chomp_query_string
64
- uri = "people/gmosx.sx"
65
- assert_equal("people/gmosx.sx", UriUtils.chomp_query_string(uri))
63
+ def test_chomp_query_string
64
+ uri = "people/gmosx.sx"
65
+ assert_equal("people/gmosx.sx", UriUtils.chomp_query_string(uri))
66
66
 
67
- uri = "people/gmosx.sx?koko=1;lala=2"
68
- assert_equal("people/gmosx.sx", UriUtils.chomp_query_string(uri))
67
+ uri = "people/gmosx.sx?koko=1;lala=2"
68
+ assert_equal("people/gmosx.sx", UriUtils.chomp_query_string(uri))
69
69
 
70
- uri = "http://www.navel.gr/people/gmosx.sx?koko=1&lala=2"
71
- assert_equal("http://www.navel.gr/people/gmosx.sx", UriUtils.chomp_query_string(uri))
70
+ uri = "http://www.navel.gr/people/gmosx.sx?koko=1&lala=2"
71
+ assert_equal("http://www.navel.gr/people/gmosx.sx", UriUtils.chomp_query_string(uri))
72
72
 
73
- uri = "http://www.navel.gr:8080/people/gmosx.sx?koko=1;lala=2"
74
- assert_equal("http://www.navel.gr:8080/people/gmosx.sx", UriUtils.chomp_query_string(uri))
73
+ uri = "http://www.navel.gr:8080/people/gmosx.sx?koko=1;lala=2"
74
+ assert_equal("http://www.navel.gr:8080/people/gmosx.sx", UriUtils.chomp_query_string(uri))
75
75
 
76
- assert_equal(nil, UriUtils.chomp_query_string(nil))
77
- end
76
+ assert_equal(nil, UriUtils.chomp_query_string(nil))
77
+ end
78
78
 
79
- def test_update_query_string
80
- uri = "ko/index.sx?koko=1"
81
- hash = {"lala" => 2, "kaka" => 3}
82
- assert_equal("ko/index.sx?koko=1;lala=2;kaka=3", UriUtils.update_query_string(uri, hash))
79
+ def test_update_query_string
80
+ uri = "ko/index.sx?koko=1"
81
+ hash = {"lala" => 2, "kaka" => 3}
82
+ assert_equal("ko/index.sx?koko=1;lala=2;kaka=3", UriUtils.update_query_string(uri, hash))
83
83
 
84
- uri = "http://www.navel.gr:8080/ko/index.sx?koko=1"
85
- hash = {"lala" => 2, "kaka" => 3}
86
- assert_equal("http://www.navel.gr:8080/ko/index.sx?koko=1;lala=2;kaka=3", UriUtils.update_query_string(uri, hash))
84
+ uri = "http://www.navel.gr:8080/ko/index.sx?koko=1"
85
+ hash = {"lala" => 2, "kaka" => 3}
86
+ assert_equal("http://www.navel.gr:8080/ko/index.sx?koko=1;lala=2;kaka=3", UriUtils.update_query_string(uri, hash))
87
87
 
88
- uri = "http://www.navel.gr"
89
- hash = {"lala" => 2, "kaka" => 3}
90
- assert_equal("http://www.navel.gr?lala=2;kaka=3", UriUtils.update_query_string(uri, hash))
88
+ uri = "http://www.navel.gr"
89
+ hash = {"lala" => 2, "kaka" => 3}
90
+ assert_equal("http://www.navel.gr?lala=2;kaka=3", UriUtils.update_query_string(uri, hash))
91
91
 
92
- # bug: no ? when passed an empty hash
93
- uri = "http://www.navel.gr"
94
- assert_equal("http://www.navel.gr", UriUtils.update_query_string(uri, {}))
95
- end
92
+ # bug: no ? when passed an empty hash
93
+ uri = "http://www.navel.gr"
94
+ assert_equal("http://www.navel.gr", UriUtils.update_query_string(uri, {}))
95
+ end
96
96
 
97
97
  end
@@ -9,207 +9,207 @@ require 'glue/validation'
9
9
  Glue::Property.type_checking = false
10
10
 
11
11
  class TC_Validation < Test::Unit::TestCase # :nodoc: all
12
- include Glue
13
-
14
- # Override the default error message
15
- Validation::Errors.invalid_format = 'INVALID'
16
-
17
- module Mixin
18
- prop_accessor :value, String
19
- validate_format :value, :format => /123/, :msg => 'mixin error'
20
- end
21
-
22
- class User
23
- include Mixin
24
-
25
- prop_accessor :name, String
26
- validate_format :name, :format => /L.o/, :msg => 'per class override'
27
- end
28
-
29
- class Article
30
- prop_accessor :name, String
31
- validate_format :name, :format => /news/
32
- end
33
-
34
- class Dummy1
35
- prop_accessor :test, Fixnum
36
- prop_accessor :str, String
37
- validate_value :test, :str
38
- end
39
-
40
- class Dummy2
41
- prop_accessor :password, String
42
- validate_confirmation :password
43
- end
44
-
45
- class Dummy3
46
- prop_accessor :vmin, String
47
- validate_length :vmin, :min => 2
48
-
49
- prop_accessor :vmax, String
50
- validate_length :vmax, :max => 3, :msg => 'LOOONG'
51
-
52
- prop_accessor :vran, String
53
- validate_length :vran, :range => 2..4, :msg_long => 'argh'
54
-
55
- prop_accessor :vlen, String
56
- validate_length :vlen, :length => 3, :msg => 'argh'
57
- end
58
-
59
- class Dummy4
60
- prop_accessor :sex, String
61
- validate_inclusion :sex, :in => %w{ Male Female }, :msg => 'huh?'
62
-
63
- prop_accessor :age, Fixnum
64
- validate_inclusion :age, :in => 5..99
65
- end
66
-
67
- class Dummy5
68
- property :age, Fixnum
69
- validate_numeric :age, :integer => true
70
-
71
- property :rate, Float
72
- validate_numeric :rate
73
- end
74
-
75
- # bug
76
-
77
- class NoProp
78
- include Mixin
79
- validate_length :value, :min => 3
80
- end
81
-
82
- def test_validate_value
83
- d = Dummy1.new
84
-
85
- assert !d.valid?
86
- assert_equal 2, d.errors.count
87
- d.test = 1
88
- assert !d.valid?
89
- assert_equal 1, d.errors.count
90
- d.str = 'yeah'
91
- assert d.valid?
92
- end
93
-
94
- def test_validate_confirmation
95
- d = Dummy2.new
96
-
97
- assert_respond_to d, :password_confirmation
98
-
99
- d.password = 'hello'
100
- assert !d.valid?
101
- d.password_confirmation = 'hel'
102
- assert !d.valid?
103
- d.password_confirmation = 'hello'
104
- assert d.valid?
105
- end
106
-
107
- def test_validate_format
108
- u = User.new
109
-
110
- u.value = '1234'
111
-
112
- u.name = 'George'
113
- assert !u.valid?
114
-
115
- assert_equal 'per class override', u.errors[:name].first
116
-
117
- u.name = 'Leonard'
118
- assert u.valid?
119
-
120
- a = Article.new
121
-
122
- a.name = 'In the news'
123
- assert a.valid?
124
-
125
- a.name = 'Sports'
126
- assert !a.valid?
127
-
128
- assert_equal 1, a.errors.count
129
- assert_equal 1, a.errors.on(:name).size
130
- assert_kind_of Array, a.errors[:name]
131
-
132
- assert_equal 'INVALID', a.errors[:name].first
133
-
134
- # test mixin
135
-
136
- u.value = '543'
137
- assert !u.valid?
138
- end
139
-
140
- def test_validate_length
141
- d = Dummy3.new
142
-
143
- d.vmin = 'q'
144
- d.vmax = '2'
145
- d.vlen = '123'
146
- d.vran = '123'
147
-
148
- assert !d.valid?
149
- assert_equal 'Too short, must be more than 2 characters long', d.errors.on(:vmin).first
150
- d.vmin = 'longer'
151
- assert d.valid?
152
-
153
- d.vmax = 'loooooooooooong'
154
- assert !d.valid?
155
- assert_equal 'LOOONG', d.errors.on(:vmax).first
156
-
157
- d.vmax = nil
158
- assert !d.valid?
159
- assert_equal 'No value provided', d.errors.on(:vmax).first
160
-
161
- d.vlen = '12'
162
- assert !d.valid?
163
- assert_equal 'argh', d.errors.on(:vlen).first
164
-
165
- d.vlen = '1234565'
166
- assert !d.valid?
167
-
168
- d.vran = '1'
169
- assert !d.valid?
170
- assert_equal 'Too short, must be more than 2 characters long', d.errors.on(:vran).first
171
-
172
- d.vran = '11111111'
173
- assert !d.valid?
174
- assert_equal 'argh', d.errors.on(:vran).first
175
- end
176
-
177
- def test_validate_inclusion
178
- d = Dummy4.new
179
-
180
- d.sex = 'Alien'
181
- d.age = 1
182
-
183
- assert !d.valid?
184
- assert_equal 2, d.errors.count
185
-
186
- d.sex = 'Male'
187
- assert !d.valid?
188
- assert_equal 1, d.errors.count
12
+ include Glue
13
+
14
+ # Override the default error message
15
+ Validation::Errors.invalid_format = 'INVALID'
16
+
17
+ module Mixin
18
+ prop_accessor :value, String
19
+ validate_format :value, :format => /123/, :msg => 'mixin error'
20
+ end
21
+
22
+ class User
23
+ include Mixin
24
+
25
+ prop_accessor :name, String
26
+ validate_format :name, :format => /L.o/, :msg => 'per class override'
27
+ end
28
+
29
+ class Article
30
+ prop_accessor :name, String
31
+ validate_format :name, :format => /news/
32
+ end
33
+
34
+ class Dummy1
35
+ prop_accessor :test, Fixnum
36
+ prop_accessor :str, String
37
+ validate_value :test, :str
38
+ end
39
+
40
+ class Dummy2
41
+ prop_accessor :password, String
42
+ validate_confirmation :password
43
+ end
44
+
45
+ class Dummy3
46
+ prop_accessor :vmin, String
47
+ validate_length :vmin, :min => 2
48
+
49
+ prop_accessor :vmax, String
50
+ validate_length :vmax, :max => 3, :msg => 'LOOONG'
51
+
52
+ prop_accessor :vran, String
53
+ validate_length :vran, :range => 2..4, :msg_long => 'argh'
54
+
55
+ prop_accessor :vlen, String
56
+ validate_length :vlen, :length => 3, :msg => 'argh'
57
+ end
58
+
59
+ class Dummy4
60
+ prop_accessor :sex, String
61
+ validate_inclusion :sex, :in => %w{ Male Female }, :msg => 'huh?'
62
+
63
+ prop_accessor :age, Fixnum
64
+ validate_inclusion :age, :in => 5..99
65
+ end
66
+
67
+ class Dummy5
68
+ property :age, Fixnum
69
+ validate_numeric :age, :integer => true
70
+
71
+ property :rate, Float
72
+ validate_numeric :rate
73
+ end
74
+
75
+ # bug
76
+
77
+ class NoProp
78
+ include Mixin
79
+ validate_length :value, :min => 3
80
+ end
81
+
82
+ def test_validate_value
83
+ d = Dummy1.new
84
+
85
+ assert !d.valid?
86
+ assert_equal 2, d.errors.count
87
+ d.test = 1
88
+ assert !d.valid?
89
+ assert_equal 1, d.errors.count
90
+ d.str = 'yeah'
91
+ assert d.valid?
92
+ end
93
+
94
+ def test_validate_confirmation
95
+ d = Dummy2.new
96
+
97
+ assert_respond_to d, :password_confirmation
98
+
99
+ d.password = 'hello'
100
+ assert !d.valid?
101
+ d.password_confirmation = 'hel'
102
+ assert !d.valid?
103
+ d.password_confirmation = 'hello'
104
+ assert d.valid?
105
+ end
106
+
107
+ def test_validate_format
108
+ u = User.new
109
+
110
+ u.value = '1234'
111
+
112
+ u.name = 'George'
113
+ assert !u.valid?
114
+
115
+ assert_equal 'per class override', u.errors[:name].first
116
+
117
+ u.name = 'Leonard'
118
+ assert u.valid?
119
+
120
+ a = Article.new
121
+
122
+ a.name = 'In the news'
123
+ assert a.valid?
124
+
125
+ a.name = 'Sports'
126
+ assert !a.valid?
127
+
128
+ assert_equal 1, a.errors.count
129
+ assert_equal 1, a.errors.on(:name).size
130
+ assert_kind_of Array, a.errors[:name]
131
+
132
+ assert_equal 'INVALID', a.errors[:name].first
133
+
134
+ # test mixin
135
+
136
+ u.value = '543'
137
+ assert !u.valid?
138
+ end
139
+
140
+ def test_validate_length
141
+ d = Dummy3.new
142
+
143
+ d.vmin = 'q'
144
+ d.vmax = '2'
145
+ d.vlen = '123'
146
+ d.vran = '123'
147
+
148
+ assert !d.valid?
149
+ assert_equal 'Too short, must be more than 2 characters long', d.errors.on(:vmin).first
150
+ d.vmin = 'longer'
151
+ assert d.valid?
152
+
153
+ d.vmax = 'loooooooooooong'
154
+ assert !d.valid?
155
+ assert_equal 'LOOONG', d.errors.on(:vmax).first
156
+
157
+ d.vmax = nil
158
+ assert !d.valid?
159
+ assert_equal 'No value provided', d.errors.on(:vmax).first
160
+
161
+ d.vlen = '12'
162
+ assert !d.valid?
163
+ assert_equal 'argh', d.errors.on(:vlen).first
164
+
165
+ d.vlen = '1234565'
166
+ assert !d.valid?
167
+
168
+ d.vran = '1'
169
+ assert !d.valid?
170
+ assert_equal 'Too short, must be more than 2 characters long', d.errors.on(:vran).first
171
+
172
+ d.vran = '11111111'
173
+ assert !d.valid?
174
+ assert_equal 'argh', d.errors.on(:vran).first
175
+ end
176
+
177
+ def test_validate_inclusion
178
+ d = Dummy4.new
179
+
180
+ d.sex = 'Alien'
181
+ d.age = 1
182
+
183
+ assert !d.valid?
184
+ assert_equal 2, d.errors.count
185
+
186
+ d.sex = 'Male'
187
+ assert !d.valid?
188
+ assert_equal 1, d.errors.count
189
189
 
190
- d.age = 110
191
- assert !d.valid?
192
- assert_equal 1, d.errors.count
190
+ d.age = 110
191
+ assert !d.valid?
192
+ assert_equal 1, d.errors.count
193
193
 
194
- d.age = 90
195
- assert d.valid?
196
- end
194
+ d.age = 90
195
+ assert d.valid?
196
+ end
197
197
 
198
- def test_validate_numeric
199
- d = Dummy5.new
198
+ def test_validate_numeric
199
+ d = Dummy5.new
200
200
 
201
- d.rate = 'Alien'
202
- d.age = 'World'
201
+ d.rate = 'Alien'
202
+ d.age = 'World'
203
203
 
204
- assert !d.valid?
205
- assert_equal 2, d.errors.count
204
+ assert !d.valid?
205
+ assert_equal 2, d.errors.count
206
206
 
207
- d.rate = 3
208
- assert !d.valid?
209
- assert_equal 1, d.errors.count
207
+ d.rate = 3
208
+ assert !d.valid?
209
+ assert_equal 1, d.errors.count
210
210
 
211
- d.rate = 1.2
212
- d.age = 123
213
- assert d.valid?
214
- end
211
+ d.rate = 1.2
212
+ d.age = 123
213
+ assert d.valid?
214
+ end
215
215
  end