To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 45 / 45daf4d07a686bd1866f0fdb820fe646d660b21c.svn-base @ 1298:4f746d8966dd
History | View | Annotate | Download (9.8 KB)
| 1 | 1295:622f24f53b42 | Chris | # Redmine - project management software |
|---|---|---|---|
| 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
||
| 3 | # |
||
| 4 | # This program is free software; you can redistribute it and/or |
||
| 5 | # modify it under the terms of the GNU General Public License |
||
| 6 | # as published by the Free Software Foundation; either version 2 |
||
| 7 | # of the License, or (at your option) any later version. |
||
| 8 | # |
||
| 9 | # This program is distributed in the hope that it will be useful, |
||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | # GNU 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 02110-1301, USA. |
||
| 17 | |||
| 18 | require File.expand_path('../../../../test_helper', __FILE__)
|
||
| 19 | |||
| 20 | class Redmine::UnifiedDiffTest < ActiveSupport::TestCase |
||
| 21 | def test_subversion_diff |
||
| 22 | diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'))
|
||
| 23 | # number of files |
||
| 24 | assert_equal 4, diff.size |
||
| 25 | assert diff.detect {|file| file.file_name =~ %r{^config/settings.yml}}
|
||
| 26 | end |
||
| 27 | |||
| 28 | def test_truncate_diff |
||
| 29 | diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'), :max_lines => 20)
|
||
| 30 | assert_equal 2, diff.size |
||
| 31 | end |
||
| 32 | |||
| 33 | def test_inline_partials |
||
| 34 | diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'))
|
||
| 35 | assert_equal 1, diff.size |
||
| 36 | diff = diff.first |
||
| 37 | assert_equal 43, diff.size |
||
| 38 | |||
| 39 | assert_equal [51, -1], diff[0].offsets |
||
| 40 | assert_equal [51, -1], diff[1].offsets |
||
| 41 | assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line |
||
| 42 | assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[1].html_line |
||
| 43 | |||
| 44 | assert_nil diff[2].offsets |
||
| 45 | assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[2].html_line |
||
| 46 | |||
| 47 | assert_equal [0, -14], diff[3].offsets |
||
| 48 | assert_equal [0, -14], diff[4].offsets |
||
| 49 | assert_equal '<span>Ut sed</span> auctor justo', diff[3].html_line |
||
| 50 | assert_equal '<span>xxx</span> auctor justo', diff[4].html_line |
||
| 51 | |||
| 52 | assert_equal [13, -19], diff[6].offsets |
||
| 53 | assert_equal [13, -19], diff[7].offsets |
||
| 54 | |||
| 55 | assert_equal [24, -8], diff[9].offsets |
||
| 56 | assert_equal [24, -8], diff[10].offsets |
||
| 57 | |||
| 58 | assert_equal [37, -1], diff[12].offsets |
||
| 59 | assert_equal [37, -1], diff[13].offsets |
||
| 60 | |||
| 61 | assert_equal [0, -38], diff[15].offsets |
||
| 62 | assert_equal [0, -38], diff[16].offsets |
||
| 63 | end |
||
| 64 | |||
| 65 | def test_side_by_side_partials |
||
| 66 | diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'), :type => 'sbs')
|
||
| 67 | assert_equal 1, diff.size |
||
| 68 | diff = diff.first |
||
| 69 | assert_equal 32, diff.size |
||
| 70 | |||
| 71 | assert_equal [51, -1], diff[0].offsets |
||
| 72 | assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line_left |
||
| 73 | assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[0].html_line_right |
||
| 74 | |||
| 75 | assert_nil diff[1].offsets |
||
| 76 | assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_left |
||
| 77 | assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_right |
||
| 78 | |||
| 79 | assert_equal [0, -14], diff[2].offsets |
||
| 80 | assert_equal '<span>Ut sed</span> auctor justo', diff[2].html_line_left |
||
| 81 | assert_equal '<span>xxx</span> auctor justo', diff[2].html_line_right |
||
| 82 | |||
| 83 | assert_equal [13, -19], diff[4].offsets |
||
| 84 | assert_equal [24, -8], diff[6].offsets |
||
| 85 | assert_equal [37, -1], diff[8].offsets |
||
| 86 | assert_equal [0, -38], diff[10].offsets |
||
| 87 | |||
| 88 | end |
||
| 89 | |||
| 90 | def test_partials_with_html_entities |
||
| 91 | raw = <<-DIFF |
||
| 92 | --- test.orig.txt Wed Feb 15 16:10:39 2012 |
||
| 93 | +++ test.new.txt Wed Feb 15 16:11:25 2012 |
||
| 94 | @@ -1,5 +1,5 @@ |
||
| 95 | Semicolons were mysteriously appearing in code diffs in the repository |
||
| 96 | |||
| 97 | -void DoSomething(std::auto_ptr<MyClass> myObj) |
||
| 98 | +void DoSomething(const MyClass& myObj) |
||
| 99 | |||
| 100 | DIFF |
||
| 101 | |||
| 102 | diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs') |
||
| 103 | assert_equal 1, diff.size |
||
| 104 | assert_equal 'void DoSomething(<span>std::auto_ptr<MyClass></span> myObj)', diff.first[2].html_line_left |
||
| 105 | assert_equal 'void DoSomething(<span>const MyClass&</span> myObj)', diff.first[2].html_line_right |
||
| 106 | |||
| 107 | diff = Redmine::UnifiedDiff.new(raw, :type => 'inline') |
||
| 108 | assert_equal 1, diff.size |
||
| 109 | assert_equal 'void DoSomething(<span>std::auto_ptr<MyClass></span> myObj)', diff.first[2].html_line |
||
| 110 | assert_equal 'void DoSomething(<span>const MyClass&</span> myObj)', diff.first[3].html_line |
||
| 111 | end |
||
| 112 | |||
| 113 | def test_line_starting_with_dashes |
||
| 114 | diff = Redmine::UnifiedDiff.new(<<-DIFF |
||
| 115 | --- old.txt Wed Nov 11 14:24:58 2009 |
||
| 116 | +++ new.txt Wed Nov 11 14:25:02 2009 |
||
| 117 | @@ -1,8 +1,4 @@ |
||
| 118 | -Lines that starts with dashes: |
||
| 119 | - |
||
| 120 | ------------------------- |
||
| 121 | --- file.c |
||
| 122 | ------------------------- |
||
| 123 | +A line that starts with dashes: |
||
| 124 | |||
| 125 | and removed. |
||
| 126 | |||
| 127 | @@ -23,4 +19,4 @@ |
||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | -Another chunk of change |
||
| 132 | +Another chunk of changes |
||
| 133 | |||
| 134 | DIFF |
||
| 135 | ) |
||
| 136 | assert_equal 1, diff.size |
||
| 137 | end |
||
| 138 | |||
| 139 | def test_one_line_new_files |
||
| 140 | diff = Redmine::UnifiedDiff.new(<<-DIFF |
||
| 141 | diff -r 000000000000 -r ea98b14f75f0 README1 |
||
| 142 | --- /dev/null |
||
| 143 | +++ b/README1 |
||
| 144 | @@ -0,0 +1,1 @@ |
||
| 145 | +test1 |
||
| 146 | diff -r 000000000000 -r ea98b14f75f0 README2 |
||
| 147 | --- /dev/null |
||
| 148 | +++ b/README2 |
||
| 149 | @@ -0,0 +1,1 @@ |
||
| 150 | +test2 |
||
| 151 | diff -r 000000000000 -r ea98b14f75f0 README3 |
||
| 152 | --- /dev/null |
||
| 153 | +++ b/README3 |
||
| 154 | @@ -0,0 +1,3 @@ |
||
| 155 | +test4 |
||
| 156 | +test5 |
||
| 157 | +test6 |
||
| 158 | diff -r 000000000000 -r ea98b14f75f0 README4 |
||
| 159 | --- /dev/null |
||
| 160 | +++ b/README4 |
||
| 161 | @@ -0,0 +1,3 @@ |
||
| 162 | +test4 |
||
| 163 | +test5 |
||
| 164 | +test6 |
||
| 165 | DIFF |
||
| 166 | ) |
||
| 167 | assert_equal 4, diff.size |
||
| 168 | assert_equal "README1", diff[0].file_name |
||
| 169 | end |
||
| 170 | |||
| 171 | def test_both_git_diff |
||
| 172 | diff = Redmine::UnifiedDiff.new(<<-DIFF |
||
| 173 | # HG changeset patch |
||
| 174 | # User test |
||
| 175 | # Date 1348014182 -32400 |
||
| 176 | # Node ID d1c871b8ef113df7f1c56d41e6e3bfbaff976e1f |
||
| 177 | # Parent 180b6605936cdc7909c5f08b59746ec1a7c99b3e |
||
| 178 | modify test1.txt |
||
| 179 | |||
| 180 | diff -r 180b6605936c -r d1c871b8ef11 test1.txt |
||
| 181 | --- a/test1.txt |
||
| 182 | +++ b/test1.txt |
||
| 183 | @@ -1,1 +1,1 @@ |
||
| 184 | -test1 |
||
| 185 | +modify test1 |
||
| 186 | DIFF |
||
| 187 | ) |
||
| 188 | assert_equal 1, diff.size |
||
| 189 | assert_equal "test1.txt", diff[0].file_name |
||
| 190 | end |
||
| 191 | |||
| 192 | def test_include_a_b_slash |
||
| 193 | diff = Redmine::UnifiedDiff.new(<<-DIFF |
||
| 194 | --- test1.txt |
||
| 195 | +++ b/test02.txt |
||
| 196 | @@ -1 +0,0 @@ |
||
| 197 | -modify test1 |
||
| 198 | DIFF |
||
| 199 | ) |
||
| 200 | assert_equal 1, diff.size |
||
| 201 | assert_equal "b/test02.txt", diff[0].file_name |
||
| 202 | |||
| 203 | diff = Redmine::UnifiedDiff.new(<<-DIFF |
||
| 204 | --- a/test1.txt |
||
| 205 | +++ a/test02.txt |
||
| 206 | @@ -1 +0,0 @@ |
||
| 207 | -modify test1 |
||
| 208 | DIFF |
||
| 209 | ) |
||
| 210 | assert_equal 1, diff.size |
||
| 211 | assert_equal "a/test02.txt", diff[0].file_name |
||
| 212 | |||
| 213 | diff = Redmine::UnifiedDiff.new(<<-DIFF |
||
| 214 | --- a/test1.txt |
||
| 215 | +++ test02.txt |
||
| 216 | @@ -1 +0,0 @@ |
||
| 217 | -modify test1 |
||
| 218 | DIFF |
||
| 219 | ) |
||
| 220 | assert_equal 1, diff.size |
||
| 221 | assert_equal "test02.txt", diff[0].file_name |
||
| 222 | end |
||
| 223 | |||
| 224 | def test_utf8_ja |
||
| 225 | ja = " text_tip_issue_end_day: " |
||
| 226 | ja += "\xe3\x81\x93\xe3\x81\xae\xe6\x97\xa5\xe3\x81\xab\xe7\xb5\x82\xe4\xba\x86\xe3\x81\x99\xe3\x82\x8b<span>\xe3\x82\xbf\xe3\x82\xb9\xe3\x82\xaf</span>" |
||
| 227 | ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
|
||
| 228 | with_settings :repositories_encodings => '' do |
||
| 229 | diff = Redmine::UnifiedDiff.new(read_diff_fixture('issue-12641-ja.diff'), :type => 'inline')
|
||
| 230 | assert_equal 1, diff.size |
||
| 231 | assert_equal 12, diff.first.size |
||
| 232 | assert_equal ja, diff.first[4].html_line_left |
||
| 233 | end |
||
| 234 | end |
||
| 235 | |||
| 236 | def test_utf8_ru |
||
| 237 | ru = " other: "\xd0\xbe\xd0\xba\xd0\xbe\xd0\xbb\xd0\xbe %{count} \xd1\x87\xd0\xb0\xd1\x81<span>\xd0\xb0</span>""
|
||
| 238 | ru.force_encoding('UTF-8') if ru.respond_to?(:force_encoding)
|
||
| 239 | with_settings :repositories_encodings => '' do |
||
| 240 | diff = Redmine::UnifiedDiff.new(read_diff_fixture('issue-12641-ru.diff'), :type => 'inline')
|
||
| 241 | assert_equal 1, diff.size |
||
| 242 | assert_equal 8, diff.first.size |
||
| 243 | assert_equal ru, diff.first[3].html_line_left |
||
| 244 | end |
||
| 245 | end |
||
| 246 | |||
| 247 | def test_offset_range_ascii_1 |
||
| 248 | raw = <<-DIFF |
||
| 249 | --- a.txt 2013-04-05 14:19:39.000000000 +0900 |
||
| 250 | +++ b.txt 2013-04-05 14:19:51.000000000 +0900 |
||
| 251 | @@ -1,3 +1,3 @@ |
||
| 252 | aaaa |
||
| 253 | -abc |
||
| 254 | +abcd |
||
| 255 | bbbb |
||
| 256 | DIFF |
||
| 257 | diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs') |
||
| 258 | assert_equal 1, diff.size |
||
| 259 | assert_equal 3, diff.first.size |
||
| 260 | assert_equal "abc<span></span>", diff.first[1].html_line_left |
||
| 261 | assert_equal "abc<span>d</span>", diff.first[1].html_line_right |
||
| 262 | end |
||
| 263 | |||
| 264 | def test_offset_range_ascii_2 |
||
| 265 | raw = <<-DIFF |
||
| 266 | --- a.txt 2013-04-05 14:19:39.000000000 +0900 |
||
| 267 | +++ b.txt 2013-04-05 14:19:51.000000000 +0900 |
||
| 268 | @@ -1,3 +1,3 @@ |
||
| 269 | aaaa |
||
| 270 | -abc |
||
| 271 | +zabc |
||
| 272 | bbbb |
||
| 273 | DIFF |
||
| 274 | diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs') |
||
| 275 | assert_equal 1, diff.size |
||
| 276 | assert_equal 3, diff.first.size |
||
| 277 | assert_equal "<span></span>abc", diff.first[1].html_line_left |
||
| 278 | assert_equal "<span>z</span>abc", diff.first[1].html_line_right |
||
| 279 | end |
||
| 280 | |||
| 281 | def test_offset_range_japanese_1 |
||
| 282 | ja1 = "\xe6\x97\xa5\xe6\x9c\xac<span></span>" |
||
| 283 | ja1.force_encoding('UTF-8') if ja1.respond_to?(:force_encoding)
|
||
| 284 | ja2 = "\xe6\x97\xa5\xe6\x9c\xac<span>\xe8\xaa\x9e</span>" |
||
| 285 | ja2.force_encoding('UTF-8') if ja2.respond_to?(:force_encoding)
|
||
| 286 | with_settings :repositories_encodings => '' do |
||
| 287 | diff = Redmine::UnifiedDiff.new( |
||
| 288 | read_diff_fixture('issue-13644-1.diff'), :type => 'sbs')
|
||
| 289 | assert_equal 1, diff.size |
||
| 290 | assert_equal 3, diff.first.size |
||
| 291 | assert_equal ja1, diff.first[1].html_line_left |
||
| 292 | assert_equal ja2, diff.first[1].html_line_right |
||
| 293 | end |
||
| 294 | end |
||
| 295 | |||
| 296 | def test_offset_range_japanese_2 |
||
| 297 | ja1 = "<span></span>\xe6\x97\xa5\xe6\x9c\xac" |
||
| 298 | ja1.force_encoding('UTF-8') if ja1.respond_to?(:force_encoding)
|
||
| 299 | ja2 = "<span>\xe3\x81\xab\xe3\x81\xa3\xe3\x81\xbd\xe3\x82\x93</span>\xe6\x97\xa5\xe6\x9c\xac" |
||
| 300 | ja2.force_encoding('UTF-8') if ja2.respond_to?(:force_encoding)
|
||
| 301 | with_settings :repositories_encodings => '' do |
||
| 302 | diff = Redmine::UnifiedDiff.new( |
||
| 303 | read_diff_fixture('issue-13644-2.diff'), :type => 'sbs')
|
||
| 304 | assert_equal 1, diff.size |
||
| 305 | assert_equal 3, diff.first.size |
||
| 306 | assert_equal ja1, diff.first[1].html_line_left |
||
| 307 | assert_equal ja2, diff.first[1].html_line_right |
||
| 308 | end |
||
| 309 | end |
||
| 310 | |||
| 311 | private |
||
| 312 | |||
| 313 | def read_diff_fixture(filename) |
||
| 314 | File.new(File.join(File.dirname(__FILE__), '/../../../fixtures/diffs', filename)).read |
||
| 315 | end |
||
| 316 | end |