Skip to content

Instantly share code, notes, and snippets.

@tadd

tadd/test_undump.rb

Last active Dec 9, 2017
Embed
What would you like to do?
Tests for String#undump (based on the testcases in `string_undump` gem)
require 'test-unit'
class TestUndump < Test::Unit::TestCase
def test_undump
assert_equal('foo', '"foo"'.undump)
assert_equal('foo#$bar#@baz#{quxx}', '"foo\#$bar\#@baz\#{quxx}"'.undump)
assert_equal('\\', '"\\\\"'.undump)
assert_equal(%(\\"), '"\\\\\""'.undump)
assert_equal("\n", '"\n"'.undump)
assert_equal(%(\\"\n), '"\\\\\"\n"'.undump)
assert_equal('すごーい', '"\u3059\u3054\u30FC\u3044"'.undump)
assert_equal('\\a\\', '"\\\\a\\\\"'.undump)
assert_equal("\nn", '"\nn"'.undump)
assert_equal("\u30593059", '"\u30593059"'.undump)
assert_equal('たのしー', '"\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC"'.undump)
assert_equal('🐾', '"\u{1F43E}"'.undump)
assert_equal(%(すごーい\\🐾たのしー\n\#{foo}),
('"\u3059\u3054\u30FC\u3044\\\\\u{1F43E}' +
'\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC\\n\#{foo}"').undump)
end
def test_undump_force_encoding
assert_equal("abc".encode(Encoding::UTF_16LE),
'"a\x00b\x00c\x00".force_encoding("UTF-16LE")'.undump)
end
def test_undump_error
assert_raise(ArgumentError.new('invalid Unicode escape')) {'"\u"'.undump}
assert_raise(ArgumentError.new('unterminated Unicode escape')) {'"\u{"'.undump}
assert_raise(ArgumentError.new('unterminated Unicode escape')) {'"\u{3042"'.undump}
assert_raise(ArgumentError.new('invalid hex escape')) {'"\x"'.undump}
assert_equal('#', '"\#"'.undump)
assert_raise(ArgumentError.new('invalid escape')) {'"\\"'.undump}
assert_raise(ArgumentError.new('unknown encoding name - UNKNOWN')) {
'"".force_encoding("UNKNOWN")'.undump
}
end
def test_undump_taintedness
assert_true('""'.taint.undump.tainted?)
end
end
# encoding: euc-jp
# test for multibyte and non-Unicode string
require 'test-unit'
class TestUndumpEucJp < Test::Unit::TestCase
def test_undump
assert_equal('すごーい', '\xA4\xB9\xA4\xB4\xA1\xBC\xA4\xA4'.undump)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.