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

Progress on String#intern #520

Merged
merged 3 commits into from
Mar 4, 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
1 change: 1 addition & 0 deletions lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ def generate_name
gen.binding('String', 'index', 'StringObject', 'index', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', 'initialize', 'StringObject', 'initialize', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', 'inspect', 'StringObject', 'inspect', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', 'intern', 'StringObject', 'to_sym', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', 'length', 'StringObject', 'size', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', 'ljust', 'StringObject', 'ljust', argc: 1..2, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', 'lstrip', 'StringObject', 'lstrip', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
Expand Down
7 changes: 7 additions & 0 deletions spec/core/string/intern_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/to_sym'

describe "String#intern" do
it_behaves_like :string_to_sym, :intern
end
79 changes: 79 additions & 0 deletions spec/core/string/shared/to_sym.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
describe :string_to_sym, shared: true do
it "returns the symbol corresponding to self" do
"Koala".send(@method).should equal :Koala
'cat'.send(@method).should equal :cat
'@cat'.send(@method).should equal :@cat
'cat and dog'.send(@method).should equal :"cat and dog"
"abc=".send(@method).should equal :abc=
end

it "does not special case +(binary) and -(binary)" do
"+(binary)".send(@method).should equal :"+(binary)"
"-(binary)".send(@method).should equal :"-(binary)"
end

it "does not special case certain operators" do
"!@".send(@method).should equal :"!@"
"~@".send(@method).should equal :"~@"
"!(unary)".send(@method).should equal :"!(unary)"
"~(unary)".send(@method).should equal :"~(unary)"
"+(unary)".send(@method).should equal :"+(unary)"
"-(unary)".send(@method).should equal :"-(unary)"
end

# NATFIXME: Add back once we have encoding.
xit "returns a US-ASCII Symbol for a UTF-8 String containing only US-ASCII characters" do
sym = "foobar".send(@method)
sym.encoding.should == Encoding::US_ASCII
sym.should equal :"foobar"
end

# NATFIXME: Add back once we have encoding.
xit "returns a US-ASCII Symbol for a binary String containing only US-ASCII characters" do
sym = "foobar".b.send(@method)
sym.encoding.should == Encoding::US_ASCII
sym.should equal :"foobar"
end

# NATFIXME: Add back once we have encoding.
xit "returns a UTF-8 Symbol for a UTF-8 String containing non US-ASCII characters" do
sym = "il était une fois".send(@method)
sym.encoding.should == Encoding::UTF_8
sym.should equal :"il était une #{'fois'}"
end

# NATFIXME: Add back once we have encoding.
xit "returns a UTF-16LE Symbol for a UTF-16LE String containing non US-ASCII characters" do
utf16_str = "UtéF16".encode(Encoding::UTF_16LE)
sym = utf16_str.send(@method)
sym.encoding.should == Encoding::UTF_16LE
sym.to_s.should == utf16_str
end

# NATFIXME: Add back once we have encoding.
xit "returns a binary Symbol for a binary String containing non US-ASCII characters" do
binary_string = "binarí".b
sym = binary_string.send(@method)
sym.encoding.should == Encoding::BINARY
sym.to_s.should == binary_string
end

# NATFIXME: Add back once we have encoding.
xit "ignores exising symbols with different encoding" do
source = "fée"

iso_symbol = source.force_encoding(Encoding::ISO_8859_1).send(@method)
iso_symbol.encoding.should == Encoding::ISO_8859_1
binary_symbol = source.force_encoding(Encoding::BINARY).send(@method)
binary_symbol.encoding.should == Encoding::BINARY
end

# NATFIXME: Add back once we have encoding and `String#valid_encoding?`
xit "raises an EncodingError for UTF-8 String containing invalid bytes" do
invalid_utf8 = "\xC3"
invalid_utf8.should_not.valid_encoding?
-> {
invalid_utf8.send(@method)
}.should raise_error(EncodingError, /invalid/)
end
end
7 changes: 7 additions & 0 deletions spec/core/string/to_sym_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/to_sym'

describe "String#to_sym" do
it_behaves_like :string_to_sym, :to_sym
end