10000 fix REXML::Formatters::Pretty#write_document · ruby/rexml@df89ee2 · GitHub
[go: up one dir, main page]

Skip to content

Commit df89ee2

Browse files
committed
fix REXML::Formatters::Pretty#write_document
## Why? Fix REXML::Formatters::Pretty#write_document, which implicitly assumes that the XML file ends with a newline. If the XML file does not end with a newline, a space is added to the end of the first line. ``` Failure: test_indent(REXMLTests::TestDocument::WriteTest::ArgumentsTest) /Users/naitoh/ghq/github.com/naitoh/rexml/test/test_document.rb:270:in `test_indent' 267: output = "" 268: indent = 2 269: @document.write(output, indent) => 270: assert_equal(<<-EOX.chomp, output) 271: <?xml version='1.0' encoding='UTF-8'?> 272: <message> 273: Hello world! <"<?xml version='1.0' encoding='UTF-8'?>\n" + "<message>\n" + " Hello world!\n" + "</message>"> expected but was <"<?xml version='1.0' encoding='UTF-8'?> \n" + "<message>\n" + " Hello world!\n" + "</message>"> diff: ? <?xml version='1.0' encoding='UTF-8'?> <message> Hello world! </message> ```
1 parent ebc3e85 commit df89ee2

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lib/rexml/formatters/pretty.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def write_document( node, output )
111111
# itself, then we don't need a carriage return... which makes this
112112
# logic more complex.
113113
node.children.each { |child|
114-
next if child == node.children[-1] and child.instance_of?(Text)
114+
next if child == "\n" and child.instance_of?(Text)
115115
unless child == node.children[0] or child.instance_of?(Text) or
116116
(child == node.children[1] and !node.children[0].writethis)
117117
output << "\n"

test/test_document.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_each_recursive
247247

248248
class WriteTest < Test::Unit::TestCase
249249
def setup
250-
@document = REXML::Document.new(<<-EOX)
250+
@document = REXML::Document.new(<<-EOX.chomp)
251251
<?xml version="1.0" encoding="UTF-8"?>
252252
<message>Hello world!</message>
253253
EOX
@@ -257,7 +257,7 @@ class ArgumentsTest < self
257257
def test_output
258258
output = ""
259259
@document.write(output)
260-
assert_equal(<<-EOX, output)
260+
assert_equal(<<-EOX.chomp, output)
261261
<?xml version='1.0' encoding='UTF-8'?>
262262
<message>Hello world!</message>
263263
EOX
@@ -280,7 +280,7 @@ def test_transitive
280280
indent = 2
281281
transitive = true
282282
@document.write(output, indent, transitive)
283-
assert_equal(<<-EOX, output)
283+
assert_equal(<<-EOX.chomp, output)
284284
<?xml version='1.0' encoding='UTF-8'?>
285285
<message
286286
>Hello world!</message
@@ -309,7 +309,7 @@ def test_encoding
309309
japanese_text = "こんにちは"
310310
@document.root.text = japanese_text
311311
@document.write(output, indent, transitive, ie_hack, encoding)
312-
assert_equal(<<-EOX.encode(encoding), output)
312+
assert_equal(<<-EOX.chomp.encode(encoding), output)
313313
<?xml version='1.0' encoding='SHIFT_JIS'?>
314314
<message>#{japanese_text}</message>
315315
EOX
@@ -320,7 +320,7 @@ class OptionsTest < self
320320
def test_output
321321
output = ""
322322
@document.write(:output => output)
323-
assert_equal(<<-EOX, output)
323+
assert_equal(<<-EOX.chomp, output)
324324
<?xml version='1.0' encoding='UTF-8'?>
325325
<message>Hello world!</message>
326326
EOX
@@ -340,7 +340,7 @@ def test_indent
340340
def test_transitive
341341
output = ""
342342
@document.write(:output => output, :indent => 2, :transitive => true)
343-
assert_equal(<<-EOX, output)
343+
assert_equal(<<-EOX.chomp, output)
344344
<?xml version='1.0' encoding='UTF-8'?>
345345
<message
346346
>Hello world!</message
@@ -362,7 +362,7 @@ def test_encoding
362362
japanese_text = "こんにちは"
363363
@document.root.text = japanese_text
364364
@document.write(:output => output, :encoding => encoding)
365-
assert_equal(<<-EOX.encode(encoding), output)
365+
assert_equal(<<-EOX.chomp.encode(encoding), output)
366366
<?xml version='1.0' encoding='SHIFT_JIS'?>
367367
<message>#{japanese_text}</message>
368368
EOX

0 commit comments

Comments
 (0)
0