1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
# Copyright (C) 2015-2019 Ruby-GNOME Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
class TestEnum < Test::Unit::TestCase
sub_test_case(".try_convert") do
def test_nil
assert_nil(GLib::NormalizeMode.try_convert(nil))
end
def test_enum
assert_equal(GLib::NormalizeMode::NFD,
GLib::NormalizeMode.try_convert(GLib::NormalizeMode::NFD))
end
def test_integer
assert_equal(GLib::NormalizeMode::NFD,
GLib::NormalizeMode.try_convert(GLib::NormalizeMode::NFD.to_i))
end
def test_string
assert_equal(GLib::NormalizeMode::NFD,
GLib::NormalizeMode.try_convert("nfd"))
end
def test_symbol
assert_equal(GLib::NormalizeMode::NFD,
GLib::NormalizeMode.try_convert(:nfd))
end
def test_nonexistent
assert_nil(GLib::NormalizeMode.try_convert(:nonexistent))
end
def test_unconvertable
assert_nil(GLib::NormalizeMode.try_convert([]))
end
end
def test_enum_by_symbol
original = [0x00c1].pack("U*") # A with acute
assert_equal(GLib::UTF8.normalize(original, GLib::NormalizeMode::NFD),
GLib::UTF8.normalize(original, :nfd))
assert_equal(GLib::UTF8.normalize(original, GLib::NormalizeMode::NFD),
GLib::UTF8.normalize(original, :NFD))
assert_raise(TypeError) do
GLib::UTF8.normalize(original, :unknown)
end
end
def test_enum_by_string
original = [0x00c1].pack("U*") # A with acute
assert_equal(GLib::UTF8.normalize(original, GLib::NormalizeMode::NFD),
GLib::UTF8.normalize(original, "nfd"))
assert_equal(GLib::UTF8.normalize(original, GLib::NormalizeMode::NFD),
GLib::UTF8.normalize(original, "NFD"))
assert_raise(TypeError) do
GLib::UTF8.normalize(original, "unknown")
end
end
def test_flags_simple
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, :keep_comments)
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, :KEEP_COMMENTS)
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, "keep_comments")
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, "KEEP_COMMENTS")
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, "keep COMMENTS")
assert_raise(ArgumentError) do
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, :unknown)
end
assert_raise(ArgumentError) do
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS, "UNKNOWN")
end
end
def test_flags_by_array
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS |
GLib::KeyFile::KEEP_TRANSLATIONS,
[:keep_comments, :keep_translations])
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS |
GLib::KeyFile::KEEP_TRANSLATIONS,
[:keep_COMMENTS, "KEEP_TRANSLATIONS"])
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS |
GLib::KeyFile::KEEP_TRANSLATIONS,
["keep_comments", "KEEP_translations"])
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS |
GLib::KeyFile::KEEP_TRANSLATIONS,
[:keep_comments, GLib::KeyFile::KEEP_TRANSLATIONS])
assert_key_file_load(GLib::KeyFile::KEEP_COMMENTS |
GLib::KeyFile::KEEP_TRANSLATIONS,
[:keep_comments, nil, :keep_translations])
end
def test_flags_or
assert_equal(GLib::KeyFile::KEEP_COMMENTS,
GLib::KeyFile::KEEP_COMMENTS | [])
assert_equal(GLib::KeyFile::KEEP_COMMENTS |
GLib::KeyFile::KEEP_TRANSLATIONS ,
GLib::KeyFile::KEEP_COMMENTS | [:keep_translations])
end
private
def assert_key_file_load(flags, convenience_flags)
data = <<-EOD
[SECTION]
KEY=VALUE
# comment
KEY[ja]=値
EOD
expected_key_file = GLib::KeyFile.new
expected_key_file.load_from_data(data, flags)
actual_key_file = GLib::KeyFile.new
actual_key_file.load_from_data(data, convenience_flags)
assert_equal(expected_key_file.get_value("SECTION", "KEY"),
actual_key_file.get_value("SECTION", "KEY"))
assert_equal(expected_key_file.to_data,
actual_key_file.to_data)
end
end
|