| | 617 | def kcode_scope(kcode) |
|---|
| | 618 | orig_kcode = $KCODE |
|---|
| | 619 | $KCODE = kcode |
|---|
| | 620 | begin |
|---|
| | 621 | yield |
|---|
| | 622 | ensure |
|---|
| | 623 | $KCODE = orig_kcode |
|---|
| | 624 | end |
|---|
| | 625 | end |
|---|
| | 626 | |
|---|
| | 627 | def test_validates_length_of_using_minimum_utf8 |
|---|
| | 628 | kcode_scope('UTF8') do |
|---|
| | 629 | Topic.validates_length_of :title, :minimum => 5 |
|---|
| | 630 | |
|---|
| | 631 | t = Topic.create("title" => "äžäºäžåäº", "content" => "whatever") |
|---|
| | 632 | assert t.valid? |
|---|
| | 633 | |
|---|
| | 634 | t.title = "äžäºäžå" |
|---|
| | 635 | assert !t.valid? |
|---|
| | 636 | assert t.errors.on(:title) |
|---|
| | 637 | assert_equal "is too short (min is 5 characters)", t.errors["title"] |
|---|
| | 638 | end |
|---|
| | 639 | end |
|---|
| | 640 | |
|---|
| | 641 | def test_validates_length_of_using_maximum_utf8 |
|---|
| | 642 | kcode_scope('UTF8') do |
|---|
| | 643 | Topic.validates_length_of :title, :maximum => 5 |
|---|
| | 644 | |
|---|
| | 645 | t = Topic.create("title" => "äžäºäžåäº", "content" => "whatever") |
|---|
| | 646 | assert t.valid? |
|---|
| | 647 | |
|---|
| | 648 | t.title = "äžäº34äºå |
|---|
| | 649 | " |
|---|
| | 650 | assert !t.valid? |
|---|
| | 651 | assert t.errors.on(:title) |
|---|
| | 652 | assert_equal "is too long (max is 5 characters)", t.errors["title"] |
|---|
| | 653 | end |
|---|
| | 654 | end |
|---|
| | 655 | |
|---|
| | 656 | def test_validates_length_of_using_within_utf8 |
|---|
| | 657 | kcode_scope('UTF8') do |
|---|
| | 658 | Topic.validates_length_of(:title, :content, :within => 3..5) |
|---|
| | 659 | |
|---|
| | 660 | t = Topic.new("title" => "äžäº", "content" => "12äžåäºå |
|---|
| | 661 | äž") |
|---|
| | 662 | assert !t.valid? |
|---|
| | 663 | assert_equal "is too short (min is 3 characters)", t.errors.on(:title) |
|---|
| | 664 | assert_equal "is too long (max is 5 characters)", t.errors.on(:content) |
|---|
| | 665 | t.title = "äžäºäž" |
|---|
| | 666 | t.content = "12äž" |
|---|
| | 667 | assert t.valid? |
|---|
| | 668 | end |
|---|
| | 669 | end |
|---|
| | 670 | |
|---|
| | 671 | def test_optionally_validates_length_of_using_within_utf8 |
|---|
| | 672 | kcode_scope('UTF8') do |
|---|
| | 673 | Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true |
|---|
| | 674 | |
|---|
| | 675 | t = Topic.create('title' => 'äžäºäž', 'content' => 'äžäºäžåäº') |
|---|
| | 676 | assert t.valid? |
|---|
| | 677 | |
|---|
| | 678 | t.title = nil |
|---|
| | 679 | assert t.valid? |
|---|
| | 680 | end |
|---|
| | 681 | end |
|---|
| | 682 | |
|---|
| | 683 | def test_optionally_validates_length_of_using_within_on_create_utf8 |
|---|
| | 684 | kcode_scope('UTF8') do |
|---|
| | 685 | Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "é·ãããŸã: %d" |
|---|
| | 686 | |
|---|
| | 687 | t = Topic.create("title" => "äžäºäžåäºå |
|---|
| | 688 | äžå |
|---|
| | 689 | «ä¹åA", "content" => "whatever") |
|---|
| | 690 | assert !t.save |
|---|
| | 691 | assert t.errors.on(:title) |
|---|
| | 692 | assert_equal "é·ãããŸã: 10", t.errors[:title] |
|---|
| | 693 | |
|---|
| | 694 | t.title = "äžäºäžåäºå |
|---|
| | 695 | äžå |
|---|
| | 696 | «ä¹" |
|---|
| | 697 | assert t.save |
|---|
| | 698 | |
|---|
| | 699 | t.title = "äžäº3" |
|---|
| | 700 | assert t.save |
|---|
| | 701 | |
|---|
| | 702 | t.content = "äžäºäžåäºå |
|---|
| | 703 | äžå |
|---|
| | 704 | «ä¹å" |
|---|
| | 705 | assert t.save |
|---|
| | 706 | |
|---|
| | 707 | t.content = t.title = "äžäºäžåäºå |
|---|
| | 708 | " |
|---|
| | 709 | assert t.save |
|---|
| | 710 | end |
|---|
| | 711 | end |
|---|
| | 712 | |
|---|
| | 713 | def test_optionally_validates_length_of_using_within_on_update_utf8 |
|---|
| | 714 | kcode_scope('UTF8') do |
|---|
| | 715 | Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "çãããŸã: %d" |
|---|
| | 716 | |
|---|
| | 717 | t = Topic.create("title" => "äžäºäž4", "content" => "whatever") |
|---|
| | 718 | assert !t.save |
|---|
| | 719 | assert t.errors.on(:title) |
|---|
| | 720 | |
|---|
| | 721 | t.title = "1äºäž4" |
|---|
| | 722 | assert !t.save |
|---|
| | 723 | assert t.errors.on(:title) |
|---|
| | 724 | assert_equal "çãããŸã: 5", t.errors[:title] |
|---|
| | 725 | |
|---|
| | 726 | t.title = "valid" |
|---|
| | 727 | t.content = "äžäºäžåäºå |
|---|
| | 728 | äžå |
|---|
| | 729 | «ä¹åA" |
|---|
| | 730 | assert !t.save |
|---|
| | 731 | assert t.errors.on(:content) |
|---|
| | 732 | |
|---|
| | 733 | t.content = "äžäº345" |
|---|
| | 734 | assert t.save |
|---|
| | 735 | end |
|---|
| | 736 | end |
|---|
| | 737 | |
|---|
| | 738 | def test_validates_length_of_using_is_utf8 |
|---|
| | 739 | kcode_scope('UTF8') do |
|---|
| | 740 | Topic.validates_length_of :title, :is => 5 |
|---|
| | 741 | |
|---|
| | 742 | t = Topic.create("title" => "äžäº345", "content" => "whatever") |
|---|
| | 743 | assert t.valid? |
|---|
| | 744 | |
|---|
| | 745 | t.title = "äžäº345å |
|---|
| | 746 | " |
|---|
| | 747 | assert !t.valid? |
|---|
| | 748 | assert t.errors.on(:title) |
|---|
| | 749 | assert_equal "is the wrong length (should be 5 characters)", t.errors["title"] |
|---|
| | 750 | end |
|---|
| | 751 | end |
|---|
| | 752 | |
|---|
| | 753 | def test_validates_size_of_association_utf8 |
|---|
| | 754 | kcode_scope('UTF8') do |
|---|
| | 755 | assert_nothing_raised { Topic.validates_size_of :replies, :minimum => 1 } |
|---|
| | 756 | t = Topic.new('title' => 'ããããã', 'content' => 'ããããã') |
|---|
| | 757 | assert !t.save |
|---|
| | 758 | assert t.errors.on(:replies) |
|---|
| | 759 | t.replies.create('title' => 'ããããã', 'content' => 'ããããã') |
|---|
| | 760 | assert t.valid? |
|---|
| | 761 | end |
|---|
| | 762 | end |
|---|
| | 763 | |
|---|