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

stdlib: Add types for kconv #1857

Merged
merged 2 commits into from
Sep 15, 2024
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
166 changes: 166 additions & 0 deletions stdlib/kconv/0/kconv.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# Kanji Converter for Ruby.
#
module Kconv
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# ASCII
#
ASCII: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# Auto-Detect
#
AUTO: nil

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# BINARY
#
BINARY: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# EUC-JP
#
EUC: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# ISO-2022-JP
#
JIS: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# NOCONV
#
NOCONV: nil

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# Shift_JIS
#
SJIS: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# UNKNOWN
#
UNKNOWN: nil

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# UTF-16
#
UTF16: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# UTF-32
#
UTF32: Encoding

# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
# UTF-8
#
UTF8: Encoding

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.guess(str) => encoding
# -->
# Guess input encoding by NKF.guess
#
def self.guess: (String str) -> Encoding

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.iseuc(str) => true or false
# -->
# Returns whether input encoding is EUC-JP or not.
#
# **Note** don't expect this return value is MatchData.
#
def self.iseuc: (String str) -> bool

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.isjis(str) => true or false
# -->
# Returns whether input encoding is ISO-2022-JP or not.
#
def self.isjis: (String str) -> bool

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.issjis(str) => true or false
# -->
# Returns whether input encoding is Shift_JIS or not.
#
def self.issjis: (String str) -> bool

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.isutf8(str) => true or false
# -->
# Returns whether input encoding is UTF-8 or not.
#
def self.isutf8: (String str) -> bool

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.kconv(str, to_enc, from_enc=nil)
# -->
# Convert `str` to `to_enc`. `to_enc` and `from_enc` are given as constants of
# Kconv or Encoding objects.
#
def self.kconv: (String str, Encoding? out_code, ?Encoding? in_code) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.toeuc(str) => string
# -->
# Convert `str` to EUC-JP
#
def self.toeuc: (String str) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.tojis(str) => string
# -->
# Convert `str` to ISO-2022-JP
#
def self.tojis: (String str) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.tolocale => string
# -->
# Convert `self` to locale encoding
#
def self.tolocale: (String str) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.tosjis(str) => string
# -->
# Convert `str` to Shift_JIS
#
def self.tosjis: (String str) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.toutf16(str) => string
# -->
# Convert `str` to UTF-16
#
def self.toutf16: (String str) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.toutf32(str) => string
# -->
# Convert `str` to UTF-32
#
def self.toutf32: (String str) -> String

# <!--
# rdoc-file=ext/nkf/lib/kconv.rb
# - Kconv.toutf8(str) => string
# -->
# Convert `str` to UTF-8
#
def self.toutf8: (String str) -> String
end
118 changes: 118 additions & 0 deletions test/stdlib/Kconv_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require_relative "test_helper"
require "kconv"

class KconvSingletonTest < Test::Unit::TestCase
include TestHelper

library "kconv"
testing "singleton(::Kconv)"

def test_ASCII
assert_const_type "::Encoding", "Kconv::ASCII"
end

def test_AUTO
assert_const_type "nil", "Kconv::AUTO"
end

def test_BINARY
assert_const_type "::Encoding", "Kconv::BINARY"
end

def test_EUC
assert_const_type "::Encoding", "Kconv::EUC"
end

def test_JIS
assert_const_type "::Encoding", "Kconv::JIS"
end

def test_NOCONV
assert_const_type "nil", "Kconv::NOCONV"
end

def test_SJIS
assert_const_type "::Encoding", "Kconv::SJIS"
end

def test_UNKNOWN
assert_const_type "nil", "Kconv::UNKNOWN"
end

def test_UTF16
assert_const_type "::Encoding", "Kconv::UTF16"
end

def test_UTF32
assert_const_type "::Encoding", "Kconv::UTF32"
end

def test_UTF8
assert_const_type "::Encoding", "Kconv::UTF8"
end

def test_guess
assert_send_type "(::String str) -> ::Encoding?",
Kconv, :guess, ""
end

def test_iseuc
assert_send_type "(::String str) -> bool",
Kconv, :iseuc, ""
end

def test_isjis
assert_send_type "(::String str) -> bool",
Kconv, :isjis, ""
end

def test_issjis
assert_send_type "(::String str) -> bool",
Kconv, :issjis, ""
end

def test_isutf8
assert_send_type "(::String str) -> bool",
Kconv, :isutf8, ""
end

def test_kconv
assert_send_type "(::String str, ::Encoding? out_code, ?::Encoding? in_code) -> ::String",
Kconv, :kconv, "", Kconv::UTF8
end

def test_toeuc
assert_send_type "(::String str) -> ::String",
Kconv, :toeuc, ""
end

def test_tojis
assert_send_type "(::String str) -> ::String",
Kconv, :tojis, ""
end

def test_tolocale
assert_send_type "(::String str) -> ::String",
Kconv, :tolocale, ""
end

def test_tosjis
assert_send_type "(::String str) -> ::String",
Kconv, :tosjis, ""
end

def test_toutf16
assert_send_type "(::String str) -> ::String",
Kconv, :toutf16, ""
end

def test_toutf32
assert_send_type "(::String str) -> ::String",
Kconv, :toutf32, ""
end

def test_toutf8
assert_send_type "(::String str) -> ::String",
Kconv, :toutf8, ""
end
end