utf-8 wide-char testing of message title - this will come in useful for other things...
authorAndy Allan <gravitystorm@gmail.com>
Wed, 15 Oct 2008 13:17:10 +0000 (13:17 +0000)
committerAndy Allan <gravitystorm@gmail.com>
Wed, 15 Oct 2008 13:17:10 +0000 (13:17 +0000)
test/unit/message_test.rb

index 4de1a7b295557420a114e711a89b1b5a36791698..362bcc4307d965308c7247fd56ea9a46fc2d7a93 100644 (file)
@@ -3,6 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper'
 class MessageTest < Test::Unit::TestCase
   fixtures :messages, :users
 
+  EURO = "\xe2\x82\xac" #euro symbol
+
   # This needs to be updated when new fixtures are added
   # or removed.
   def test_check_message_count
@@ -31,4 +33,30 @@ class MessageTest < Test::Unit::TestCase
     message.recipient = nil
     assert !message.valid?
   end
+
+  def test_utf8_roundtrip
+    (1..255).each do |i|
+      assert_message_ok('c', i)
+      assert_message_ok(EURO, i)
+    end
+  end
+
+  def test_length_oversize
+    assert_raise(ActiveRecord::RecordInvalid) { make_message('c', 256).save! }
+    assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
+  end
+
+  def make_message(char, count)
+    message = messages(:one)
+    message.title = char * count
+    return message
+  end
+
+  def assert_message_ok(char, count)
+    message = make_message(char, count)
+    assert message.save!
+    response = message.class.find(message.id) # stand by for some ├╝ber-generalisation...
+    assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length*count} bytes) fails"
+  end
+
 end