Skip to content

Commit faca316

Browse files
authored
Merge pull request #1857 from tk0miya/kconv
stdlib: Add types for kconv
2 parents 65f4290 + 1603ea6 commit faca316

File tree

2 files changed

+284
-0
lines changed

2 files changed

+284
-0
lines changed

stdlib/kconv/0/kconv.rbs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
2+
# Kanji Converter for Ruby.
3+
#
4+
module Kconv
5+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
6+
# ASCII
7+
#
8+
ASCII: Encoding
9+
10+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
11+
# Auto-Detect
12+
#
13+
AUTO: nil
14+
15+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
16+
# BINARY
17+
#
18+
BINARY: Encoding
19+
20+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
21+
# EUC-JP
22+
#
23+
EUC: Encoding
24+
25+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
26+
# ISO-2022-JP
27+
#
28+
JIS: Encoding
29+
30+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
31+
# NOCONV
32+
#
33+
NOCONV: nil
34+
35+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
36+
# Shift_JIS
37+
#
38+
SJIS: Encoding
39+
40+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
41+
# UNKNOWN
42+
#
43+
UNKNOWN: nil
44+
45+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
46+
# UTF-16
47+
#
48+
UTF16: Encoding
49+
50+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
51+
# UTF-32
52+
#
53+
UTF32: Encoding
54+
55+
# <!-- rdoc-file=ext/nkf/lib/kconv.rb -->
56+
# UTF-8
57+
#
58+
UTF8: Encoding
59+
60+
# <!--
61+
# rdoc-file=ext/nkf/lib/kconv.rb
62+
# - Kconv.guess(str) => encoding
63+
# -->
64+
# Guess input encoding by NKF.guess
65+
#
66+
def self.guess: (String str) -> Encoding
67+
68+
# <!--
69+
# rdoc-file=ext/nkf/lib/kconv.rb
70+
# - Kconv.iseuc(str) => true or false
71+
# -->
72+
# Returns whether input encoding is EUC-JP or not.
73+
#
74+
# **Note** don't expect this return value is MatchData.
75+
#
76+
def self.iseuc: (String str) -> bool
77+
78+
# <!--
79+
# rdoc-file=ext/nkf/lib/kconv.rb
80+
# - Kconv.isjis(str) => true or false
81+
# -->
82+
# Returns whether input encoding is ISO-2022-JP or not.
83+
#
84+
def self.isjis: (String str) -> bool
85+
86+
# <!--
87+
# rdoc-file=ext/nkf/lib/kconv.rb
88+
# - Kconv.issjis(str) => true or false
89+
# -->
90+
# Returns whether input encoding is Shift_JIS or not.
91+
#
92+
def self.issjis: (String str) -> bool
93+
94+
# <!--
95+
# rdoc-file=ext/nkf/lib/kconv.rb
96+
# - Kconv.isutf8(str) => true or false
97+
# -->
98+
# Returns whether input encoding is UTF-8 or not.
99+
#
100+
def self.isutf8: (String str) -> bool
101+
102+
# <!--
103+
# rdoc-file=ext/nkf/lib/kconv.rb
104+
# - Kconv.kconv(str, to_enc, from_enc=nil)
105+
# -->
106+
# Convert `str` to `to_enc`. `to_enc` and `from_enc` are given as constants of
107+
# Kconv or Encoding objects.
108+
#
109+
def self.kconv: (String str, Encoding? out_code, ?Encoding? in_code) -> String
110+
111+
# <!--
112+
# rdoc-file=ext/nkf/lib/kconv.rb
113+
# - Kconv.toeuc(str) => string
114+
# -->
115+
# Convert `str` to EUC-JP
116+
#
117+
def self.toeuc: (String str) -> String
118+
119+
# <!--
120+
# rdoc-file=ext/nkf/lib/kconv.rb
121+
# - Kconv.tojis(str) => string
122+
# -->
123+
# Convert `str` to ISO-2022-JP
124+
#
125+
def self.tojis: (String str) -> String
126+
127+
# <!--
128+
# rdoc-file=ext/nkf/lib/kconv.rb
129+
# - Kconv.tolocale => string
130+
# -->
131+
# Convert `self` to locale encoding
132+
#
133+
def self.tolocale: (String str) -> String
134+
135+
# <!--
136+
# rdoc-file=ext/nkf/lib/kconv.rb
137+
# - Kconv.tosjis(str) => string
138+
# -->
139+
# Convert `str` to Shift_JIS
140+
#
141+
def self.tosjis: (String str) -> String
142+
143+
# <!--
144+
# rdoc-file=ext/nkf/lib/kconv.rb
145+
# - Kconv.toutf16(str) => string
146+
# -->
147+
# Convert `str` to UTF-16
148+
#
149+
def self.toutf16: (String str) -> String
150+
151+
# <!--
152+
# rdoc-file=ext/nkf/lib/kconv.rb
153+
# - Kconv.toutf32(str) => string
154+
# -->
155+
# Convert `str` to UTF-32
156+
#
157+
def self.toutf32: (String str) -> String
158+
159+
# <!--
160+
# rdoc-file=ext/nkf/lib/kconv.rb
161+
# - Kconv.toutf8(str) => string
162+
# -->
163+
# Convert `str` to UTF-8
164+
#
165+
def self.toutf8: (String str) -> String
166+
end

test/stdlib/Kconv_test.rb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
require_relative "test_helper"
2+
require "kconv"
3+
4+
class KconvSingletonTest < Test::Unit::TestCase
5+
include TestHelper
6+
7+
library "kconv"
8+
testing "singleton(::Kconv)"
9+
10+
def test_ASCII
11+
assert_const_type "::Encoding", "Kconv::ASCII"
12+
end
13+
14+
def test_AUTO
15+
assert_const_type "nil", "Kconv::AUTO"
16+
end
17+
18+
def test_BINARY
19+
assert_const_type "::Encoding", "Kconv::BINARY"
20+
end
21+
22+
def test_EUC
23+
assert_const_type "::Encoding", "Kconv::EUC"
24+
end
25+
26+
def test_JIS
27+
assert_const_type "::Encoding", "Kconv::JIS"
28+
end
29+
30+
def test_NOCONV
31+
assert_const_type "nil", "Kconv::NOCONV"
32+
end
33+
34+
def test_SJIS
35+
assert_const_type "::Encoding", "Kconv::SJIS"
36+
end
37+
38+
def test_UNKNOWN
39+
assert_const_type "nil", "Kconv::UNKNOWN"
40+
end
41+
42+
def test_UTF16
43+
assert_const_type "::Encoding", "Kconv::UTF16"
44+
end
45+
46+
def test_UTF32
47+
assert_const_type "::Encoding", "Kconv::UTF32"
48+
end
49+
50+
def test_UTF8
51+
assert_const_type "::Encoding", "Kconv::UTF8"
52+
end
53+
54+
def test_guess
55+
assert_send_type "(::String str) -> ::Encoding?",
56+
Kconv, :guess, ""
57+
end
58+
59+
def test_iseuc
60+
assert_send_type "(::String str) -> bool",
61+
Kconv, :iseuc, ""
62+
end
63+
64+
def test_isjis
65+
assert_send_type "(::String str) -> bool",
66+
Kconv, :isjis, ""
67+
end
68+
69+
def test_issjis
70+
assert_send_type "(::String str) -> bool",
71+
Kconv, :issjis, ""
72+
end
73+
74+
def test_isutf8
75+
assert_send_type "(::String str) -> bool",
76+
Kconv, :isutf8, ""
77+
end
78+
79+
def test_kconv
80+
assert_send_type "(::String str, ::Encoding? out_code, ?::Encoding? in_code) -> ::String",
81+
Kconv, :kconv, "", Kconv::UTF8
82+
end
83+
84+
def test_toeuc
85+
assert_send_type "(::String str) -> ::String",
86+
Kconv, :toeuc, ""
87+
end
88+
89+
def test_tojis
90+
assert_send_type "(::String str) -> ::String",
91+
Kconv, :tojis, ""
92+
end
93+
94+
def test_tolocale
95+
assert_send_type "(::String str) -> ::String",
96+
Kconv, :tolocale, ""
97+
end
98+
99+
def test_tosjis
100+
assert_send_type "(::String str) -> ::String",
101+
Kconv, :tosjis, ""
102+
end
103+
104+
def test_toutf16
105+
assert_send_type "(::String str) -> ::String",
106+
Kconv, :toutf16, ""
107+
end
108+
109+
def test_toutf32
110+
assert_send_type "(::String str) -> ::String",
111+
Kconv, :toutf32, ""
112+
end
113+
114+
def test_toutf8
115+
assert_send_type "(::String str) -> ::String",
116+
Kconv, :toutf8, ""
117+
end
118+
end

0 commit comments

Comments
 (0)