$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib') require 'test/unit' require 'glue' require 'glue/validation' Property.type_checking = false class TC_Validation < Test::Unit::TestCase # :nodoc: all # Override the default error message N::Validation::Errors.invalid_format = 'INVALID' module Mixin prop_accessor :value, String validate_format :value, :format => /123/, :msg => 'mixin error' end class User include Mixin prop_accessor :name, String validate_format :name, :format => /Leo/, :msg => 'per class override' end class Article prop_accessor :name, String validate_format :name, :format => /news/ end class Dummy1 prop_accessor :test, Fixnum prop_accessor :str, String validate_value :test, :str end class Dummy2 prop_accessor :password, String validate_confirmation :password end class Dummy3 prop_accessor :vmin, String validate_length :vmin, :min => 2 prop_accessor :vmax, String validate_length :vmax, :max => 3, :msg => 'LOOONG' prop_accessor :vran, String validate_length :vran, :range => 2..4, :msg_long => 'argh' prop_accessor :vlen, String validate_length :vlen, :length => 3, :msg => 'argh' end class Dummy4 prop_accessor :sex, String validate_inclusion :sex, :in => %w{ Male Female }, :msg => 'huh?' prop_accessor :age, Fixnum validate_inclusion :age, :in => 5..99 end # bug class NoProp include Mixin validate_length :value, :min => 3 end def test_validate_value d = Dummy1.new assert !d.valid? assert_equal 2, d.errors.count d.test = 1 assert !d.valid? assert_equal 1, d.errors.count d.str = 'yeah' assert d.valid? end def test_validate_confirmation d = Dummy2.new assert_respond_to d, :password_confirmation d.password = 'hello' assert !d.valid? d.password_confirmation = 'hel' assert !d.valid? d.password_confirmation = 'hello' assert d.valid? end def test_validate_format u = User.new u.value = '1234' u.name = 'George' assert !u.valid? assert_equal 'per class override', u.errors[:name].first u.name = 'Leonard' assert u.valid? a = Article.new a.name = 'In the news' assert a.valid? a.name = 'Sports' assert !a.valid? assert_equal 1, a.errors.count assert_equal 1, a.errors.on(:name).size assert_kind_of Array, a.errors[:name] assert_equal 'INVALID', a.errors[:name].first # test mixin u.value = '543' assert !u.valid? end def test_validate_length d = Dummy3.new d.vmin = 'q' d.vmax = '2' d.vlen = '123' d.vran = '123' assert !d.valid? assert_equal 'Too short, must be more than 2 characters long', d.errors.on(:vmin).first d.vmin = 'longer' assert d.valid? d.vmax = 'loooooooooooong' assert !d.valid? assert_equal 'LOOONG', d.errors.on(:vmax).first d.vmax = nil assert !d.valid? assert_equal 'No value provided', d.errors.on(:vmax).first d.vlen = '12' assert !d.valid? assert_equal 'argh', d.errors.on(:vlen).first d.vlen = '1234565' assert !d.valid? d.vran = '1' assert !d.valid? assert_equal 'Too short, must be more than 2 characters long', d.errors.on(:vran).first d.vran = '11111111' assert !d.valid? assert_equal 'argh', d.errors.on(:vran).first end def test_validate_inclusion d = Dummy4.new d.sex = 'Alien' d.age = 1 assert !d.valid? assert_equal 2, d.errors.count d.sex = 'Male' assert !d.valid? assert_equal 1, d.errors.count d.age = 110 assert !d.valid? assert_equal 1, d.errors.count d.age = 90 assert d.valid? end end