2011-08-23 19:53:49 -04:00
|
|
|
# coding: US-ASCII
|
2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2011-08-23 19:53:49 -04:00
|
|
|
|
2019-08-08 10:09:24 -04:00
|
|
|
require_relative 'helper'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
class TestRDocEncoding < RDoc::TestCase
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
def setup
|
2012-11-26 23:28:14 -05:00
|
|
|
super
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
@tempfile = Tempfile.new 'test_rdoc_encoding'
|
|
|
|
end
|
|
|
|
|
2014-05-26 09:33:01 -04:00
|
|
|
def teardown
|
|
|
|
@tempfile.close!
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def test_class_read_file
|
|
|
|
@tempfile.write "hi everybody"
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
assert_equal "hi everybody", RDoc::Encoding.read_file(@tempfile.path, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_read_file_encoding
|
|
|
|
expected = "# coding: utf-8\nhi everybody"
|
|
|
|
|
|
|
|
@tempfile.write expected
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal "# coding: utf-8\nhi everybody", contents
|
2010-12-19 22:22:49 -05:00
|
|
|
assert_equal Encoding::UTF_8, contents.encoding
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_read_file_encoding_convert
|
|
|
|
content = ""
|
2017-11-27 05:45:24 -05:00
|
|
|
content = RDoc::Encoding.change_encoding content, 'ISO-8859-1'
|
2010-12-19 22:22:49 -05:00
|
|
|
content << "# coding: ISO-8859-1\nhi \xE9verybody"
|
|
|
|
|
|
|
|
@tempfile.write content
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
|
|
|
|
assert_equal Encoding::UTF_8, contents.encoding
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal "# coding: ISO-8859-1\nhi \u00e9verybody", contents.sub("\r", '')
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
|
2011-02-07 02:07:12 -05:00
|
|
|
def test_class_read_file_encoding_fail
|
|
|
|
@tempfile.write "# coding: utf-8\n\317\200" # pi
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = :junk
|
|
|
|
|
2019-08-10 17:12:46 -04:00
|
|
|
_, err = verbose_capture_output do
|
2011-02-07 02:07:12 -05:00
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::US_ASCII
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nil contents
|
|
|
|
|
|
|
|
assert_match %r%^unable to convert%, err
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def test_class_read_file_encoding_fancy
|
|
|
|
expected = "# -*- coding: utf-8; fill-column: 74 -*-\nhi everybody"
|
2018-03-26 01:56:26 -04:00
|
|
|
expected = RDoc::Encoding.change_encoding expected, Encoding::UTF_8
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
@tempfile.write expected
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal "# -*- coding: utf-8; fill-column: 74 -*-\nhi everybody", contents
|
2010-12-19 22:22:49 -05:00
|
|
|
assert_equal Encoding::UTF_8, contents.encoding
|
|
|
|
end
|
|
|
|
|
2011-02-07 02:07:12 -05:00
|
|
|
def test_class_read_file_encoding_force_transcode
|
|
|
|
@tempfile.write "# coding: utf-8\n\317\200" # pi
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::US_ASCII, true
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal "# coding: utf-8\n?", contents
|
2011-02-07 02:07:12 -05:00
|
|
|
assert_equal Encoding::US_ASCII, contents.encoding
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def test_class_read_file_encoding_guess
|
|
|
|
path = File.expand_path '../test.ja.txt', __FILE__
|
|
|
|
content = RDoc::Encoding.read_file path, Encoding::UTF_8
|
|
|
|
|
|
|
|
assert_equal Encoding::UTF_8, content.encoding
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def test_class_read_file_encoding_invalid
|
|
|
|
@tempfile.write "# coding: ascii\nM\xE4r"
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = :junk
|
2019-08-10 17:12:46 -04:00
|
|
|
_, err = verbose_capture_output do
|
2012-11-26 23:28:14 -05:00
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "unable to convert \"\\xE4\" on US-ASCII for #{@tempfile.path}, skipping\n", err
|
|
|
|
|
|
|
|
assert_nil contents
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def test_class_read_file_encoding_with_signature
|
|
|
|
@tempfile.write "\xEF\xBB\xBFhi everybody"
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
bug3360 = '[ruby-dev:41452]'
|
|
|
|
content = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
|
|
|
|
assert_equal Encoding::UTF_8, content.encoding, bug3360
|
|
|
|
assert_equal "hi everybody", content, bug3360
|
|
|
|
end
|
|
|
|
|
2014-09-04 21:41:25 -04:00
|
|
|
def test_class_read_file_encoding_iso_2022_jp
|
|
|
|
input = "# coding: ISO-2022-JP\n:\e$B%3%^%s%I\e(B:"
|
|
|
|
|
|
|
|
@tempfile.write input
|
|
|
|
@tempfile.flush
|
|
|
|
|
|
|
|
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
expected = "# coding: ISO-2022-JP\n:\xe3\x82\xb3\xe3\x83\x9e\xe3\x83\xb3\xe3\x83\x89:"
|
2017-11-27 05:45:24 -05:00
|
|
|
expected = RDoc::Encoding.change_encoding expected, Encoding::UTF_8
|
2014-09-04 21:41:25 -04:00
|
|
|
|
|
|
|
assert_equal expected, contents
|
|
|
|
assert_equal Encoding::UTF_8, contents.encoding
|
|
|
|
end
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
def test_class_detect_encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
s = "# coding: UTF-8\n"
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
# sanity check for 1.8
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal Encoding::UTF_8, encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
s = "#!/bin/ruby\n# coding: UTF-8\n"
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal Encoding::UTF_8, encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
s = "<?xml version='1.0' encoding='UTF-8'?>\n"
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal Encoding::UTF_8, encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
s = "<?xml version='1.0' encoding=\"UTF-8\"?>\n"
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-28 17:08:56 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_equal Encoding::UTF_8, encoding
|
2010-12-28 17:08:56 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def test_class_set_encoding_bad
|
|
|
|
s = ""
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_nil encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
s = "# vim:set fileencoding=utf-8:\n"
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_nil encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
s = "# vim:set fileencoding=utf-8:\n"
|
2018-03-26 01:56:26 -04:00
|
|
|
encoding = RDoc::Encoding.detect_encoding s
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
assert_nil encoding
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2018-03-26 01:56:26 -04:00
|
|
|
s = RDoc::Encoding.detect_encoding "# -*- encoding: undecided -*-\n"
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-28 17:08:56 -05:00
|
|
|
def test_sanity
|
|
|
|
assert_equal Encoding::US_ASCII, ''.encoding,
|
|
|
|
'If this file is not ASCII tests may incorrectly pass'
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|