Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signature and tests for Encoding::*Error #898

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions core/encoding.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,171 @@ end
# contains a byte invalid for the either the source or target encoding.
#
class Encoding::InvalidByteSequenceError < EncodingError
public

# <!--
# rdoc-file=transcode.c
# - ecerr.destination_encoding -> string
# -->
# Returns the destination encoding as an encoding object.
#
def destination_encoding: () -> Encoding

# <!--
# rdoc-file=transcode.c
# - ecerr.destination_encoding_name -> string
# -->
# Returns the destination encoding name as a string.
#
def destination_encoding_name: () -> String

# <!--
# rdoc-file=transcode.c
# - ecerr.error_bytes -> string
# -->
# Returns the discarded bytes when Encoding::InvalidByteSequenceError occurs.
#
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
# begin
# ec.convert("abc\xA1\xFFdef")
# rescue Encoding::InvalidByteSequenceError
# p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP>
# puts $!.error_bytes.dump #=> "\xA1"
# puts $!.readagain_bytes.dump #=> "\xFF"
# end
#
def error_bytes: () -> String

# <!--
# rdoc-file=transcode.c
# - ecerr.incomplete_input? -> true or false
# -->
# Returns true if the invalid byte sequence error is caused by premature end of
# string.
#
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
#
# begin
# ec.convert("abc\xA1z")
# rescue Encoding::InvalidByteSequenceError
# p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "z" on EUC-JP>
# p $!.incomplete_input? #=> false
# end
#
# begin
# ec.convert("abc\xA1")
# ec.finish
# rescue Encoding::InvalidByteSequenceError
# p $! #=> #<Encoding::InvalidByteSequenceError: incomplete "\xA1" on EUC-JP>
# p $!.incomplete_input? #=> true
# end
#
def incomplete_input?: () -> bool

# <!--
# rdoc-file=transcode.c
# - ecerr.readagain_bytes -> string
# -->
# Returns the bytes to be read again when Encoding::InvalidByteSequenceError
# occurs.
#
def readagain_bytes: () -> String

# <!--
# rdoc-file=transcode.c
# - ecerr.source_encoding -> encoding
# -->
# Returns the source encoding as an encoding object.
#
# Note that the result may not be equal to the source encoding of the encoding
# converter if the conversion has multiple steps.
#
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
# begin
# ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
# rescue Encoding::UndefinedConversionError
# p $!.source_encoding #=> #<Encoding:UTF-8>
# p $!.destination_encoding #=> #<Encoding:EUC-JP>
# p $!.source_encoding_name #=> "UTF-8"
# p $!.destination_encoding_name #=> "EUC-JP"
# end
#
def source_encoding: () -> Encoding

# <!--
# rdoc-file=transcode.c
# - ecerr.source_encoding_name -> string
# -->
# Returns the source encoding name as a string.
#
def source_encoding_name: () -> String
end

# <!-- rdoc-file=transcode.c -->
# Raised by Encoding and String methods when a transcoding operation fails.
#
class Encoding::UndefinedConversionError < EncodingError
public

# <!--
# rdoc-file=transcode.c
# - ecerr.destination_encoding -> string
# -->
# Returns the destination encoding as an encoding object.
#
def destination_encoding: () -> Encoding

# <!--
# rdoc-file=transcode.c
# - ecerr.destination_encoding_name -> string
# -->
# Returns the destination encoding name as a string.
#
def destination_encoding_name: () -> String

# <!--
# rdoc-file=transcode.c
# - ecerr.error_char -> string
# -->
# Returns the one-character string which cause
# Encoding::UndefinedConversionError.
#
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP")
# begin
# ec.convert("\xa0")
# rescue Encoding::UndefinedConversionError
# puts $!.error_char.dump #=> "\xC2\xA0"
# p $!.error_char.encoding #=> #<Encoding:UTF-8>
# end
#
def error_char: () -> String

# <!--
# rdoc-file=transcode.c
# - ecerr.source_encoding -> encoding
# -->
# Returns the source encoding as an encoding object.
#
# Note that the result may not be equal to the source encoding of the encoding
# converter if the conversion has multiple steps.
#
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
# begin
# ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
# rescue Encoding::UndefinedConversionError
# p $!.source_encoding #=> #<Encoding:UTF-8>
# p $!.destination_encoding #=> #<Encoding:EUC-JP>
# p $!.source_encoding_name #=> "UTF-8"
# p $!.destination_encoding_name #=> "EUC-JP"
# end
#
def source_encoding: () -> Encoding

# <!--
# rdoc-file=transcode.c
# - ecerr.source_encoding_name -> string
# -->
# Returns the source encoding name as a string.
#
def source_encoding_name: () -> String
end
52 changes: 52 additions & 0 deletions test/stdlib/Encoding_InvalidByteSequenceError_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require_relative "test_helper"


class Encoding::InvalidByteSequenceErrorTest < Test::Unit::TestCase
include TypeAssertions

# library "pathname", "set", "securerandom" # Declare library signatures to load
testing "::Encoding::InvalidByteSequenceError"

def test_destination_encoding
assert_send_type "() -> ::Encoding",
error_object, :destination_encoding
end

def test_destination_encoding_name
assert_send_type "() -> ::String",
error_object, :destination_encoding_name
end

def test_error_bytes
assert_send_type "() -> ::String",
error_object, :error_bytes
end

def test_incomplete_input?
assert_send_type "() -> bool",
error_object, :incomplete_input?
end

def test_readagain_bytes
assert_send_type "() -> ::String",
error_object, :readagain_bytes
end

def test_source_encoding
assert_send_type "() -> ::Encoding",
error_object, :source_encoding
end

def test_source_encoding_name
assert_send_type "() -> ::String",
error_object, :source_encoding_name
end

private

def error_object
ec = Encoding::Converter.new("UTF-8", "ISO-8859-1")
ec.primitive_convert("\xf1abcd", "")
ec.last_error
end
end
41 changes: 41 additions & 0 deletions test/stdlib/Encoding_UndefinedConversionError_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative "test_helper"

class Encoding::UndefinedConversionErrorTest < Test::Unit::TestCase
include TypeAssertions

# library "pathname", "set", "securerandom" # Declare library signatures to load
testing "::Encoding::UndefinedConversionError"

def test_destination_encoding
assert_send_type "() -> ::Encoding",
error_object, :destination_encoding
end

def test_destination_encoding_name
assert_send_type "() -> ::String",
error_object, :destination_encoding_name
end

def test_error_char
assert_send_type "() -> ::String",
error_object, :error_char
end

def test_source_encoding
assert_send_type "() -> ::Encoding",
error_object, :source_encoding
end

def test_source_encoding_name
assert_send_type "() -> ::String",
error_object, :source_encoding_name
end

private

def error_object
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
ec.primitive_convert("\xa4\xa2", "")
ec.last_error
end
end