| 1 |
# test_buffer.rb: Test suites of Edmaru::Buffer based on Test::Unit. |
| 2 |
# |
| 3 |
# Copyright (C) 2007 Takashi Nakamoto |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301 USA. |
| 18 |
# |
| 19 |
require "test/unit" |
| 20 |
require "buffer" |
| 21 |
|
| 22 |
module EdmaruTest |
| 23 |
class TC_Buffer < Test::Unit::TestCase |
| 24 |
def setup |
| 25 |
@obj = Edmaru::Buffer.new("test buffer") |
| 26 |
end |
| 27 |
|
| 28 |
def test_initilization |
| 29 |
assert_instance_of(Edmaru::Buffer, @obj) |
| 30 |
assert_instance_of(Array, @obj.lines) |
| 31 |
assert_equal(1, @obj.lines.size) |
| 32 |
assert_equal("", @obj.lines[0]) |
| 33 |
end |
| 34 |
|
| 35 |
def test_manipulation |
| 36 |
# Tests for appending a string. |
| 37 |
@obj.append("test") |
| 38 |
assert_equal(1, @obj.lines.size) |
| 39 |
assert_equal("test", @obj.lines[0]) |
| 40 |
|
| 41 |
# Tests for appending "\n". |
| 42 |
@obj.append("\n") |
| 43 |
assert_equal(2, @obj.lines.size) |
| 44 |
assert_equal("test", @obj.lines[0]) |
| 45 |
assert_equal("", @obj.lines[1]) |
| 46 |
|
| 47 |
# Tests for appending a string including "\n" |
| 48 |
@obj.append("abc\ndef") |
| 49 |
assert_equal(3, @obj.lines.size) |
| 50 |
assert_equal("test", @obj.lines[0]) |
| 51 |
assert_equal("abc", @obj.lines[1]) |
| 52 |
assert_equal("def", @obj.lines[2]) |
| 53 |
|
| 54 |
# Now the buffer contains the following text: |
| 55 |
#test |
| 56 |
#abc |
| 57 |
#def |
| 58 |
|
| 59 |
# Validations for the specified position. |
| 60 |
assert_equal(true, @obj.valid_position?(0, 0)) |
| 61 |
assert_equal(true, @obj.valid_position?(0, 4)) |
| 62 |
assert_equal(true, @obj.valid_position?(2, 0)) |
| 63 |
assert_equal(true, @obj.valid_position?(2, 3)) |
| 64 |
assert_equal(false, @obj.valid_position?(-1, 0)) |
| 65 |
assert_equal(false, @obj.valid_position?(0, -1)) |
| 66 |
assert_equal(false, @obj.valid_position?(0, 5)) |
| 67 |
assert_equal(false, @obj.valid_position?(3, 0)) |
| 68 |
assert_equal(false, @obj.valid_position?(2, 4)) |
| 69 |
|
| 70 |
# Tests for inserting an empty string. |
| 71 |
@obj.insert(0, 0, "") |
| 72 |
assert_equal(3, @obj.lines.size) |
| 73 |
assert_equal("test", @obj.lines[0]) |
| 74 |
assert_equal("abc", @obj.lines[1]) |
| 75 |
assert_equal("def", @obj.lines[2]) |
| 76 |
|
| 77 |
# Tests for inserting a string that doesn't contain "\n". |
| 78 |
@obj.insert(1, 2, "ghi") |
| 79 |
assert_equal(3, @obj.lines.size) |
| 80 |
assert_equal("test", @obj.lines[0]) |
| 81 |
assert_equal("abghic", @obj.lines[1]) |
| 82 |
assert_equal("def", @obj.lines[2]) |
| 83 |
|
| 84 |
@obj.insert(2, 3, "ine") |
| 85 |
assert_equal(3, @obj.lines.size) |
| 86 |
assert_equal("test", @obj.lines[0]) |
| 87 |
assert_equal("abghic", @obj.lines[1]) |
| 88 |
assert_equal("define", @obj.lines[2]) |
| 89 |
|
| 90 |
# Tests for inserting a string that contains "\n". |
| 91 |
@obj.insert(2, 3, "ine\nf") |
| 92 |
assert_equal(4, @obj.lines.size) |
| 93 |
assert_equal("test", @obj.lines[0]) |
| 94 |
assert_equal("abghic", @obj.lines[1]) |
| 95 |
assert_equal("define", @obj.lines[2]) |
| 96 |
assert_equal("fine", @obj.lines[3]) |
| 97 |
|
| 98 |
# Tests for inserting a string at an invalid position. |
| 99 |
assert_raise(RuntimeError){ |
| 100 |
@obj.insert(4, 0, " ") |
| 101 |
} |
| 102 |
|
| 103 |
# Now the buffer contains the following text: |
| 104 |
#test |
| 105 |
#abghic |
| 106 |
#define |
| 107 |
#fine |
| 108 |
|
| 109 |
# Tests for clear all strings. |
| 110 |
@obj.clear |
| 111 |
assert_equal(1, @obj.lines.size) |
| 112 |
assert_equal("", @obj.lines[0]) |
| 113 |
end |
| 114 |
end |
| 115 |
end |