From e96e6074e37e73517bfad1dec1c11a9f17f96de5 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 3 Mar 2022 21:29:15 +0000 Subject: [PATCH 1/3] Add String#intern spec --- spec/core/string/intern_spec.rb | 7 +++ spec/core/string/shared/to_sym.rb | 74 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 spec/core/string/intern_spec.rb create mode 100644 spec/core/string/shared/to_sym.rb diff --git a/spec/core/string/intern_spec.rb b/spec/core/string/intern_spec.rb new file mode 100644 index 000000000..cd7dad435 --- /dev/null +++ b/spec/core/string/intern_spec.rb @@ -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 diff --git a/spec/core/string/shared/to_sym.rb b/spec/core/string/shared/to_sym.rb new file mode 100644 index 000000000..bada46633 --- /dev/null +++ b/spec/core/string/shared/to_sym.rb @@ -0,0 +1,74 @@ +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 + + it "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 + + it "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 + + it "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 + + it "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 + + it "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 From 9aaca2b112c234a0b8673a7dc4a590531bcfeccb Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 3 Mar 2022 21:30:52 +0000 Subject: [PATCH 2/3] Add String#intern binding --- lib/natalie/compiler/binding_gen.rb | 1 + spec/core/string/shared/to_sym.rb | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/natalie/compiler/binding_gen.rb b/lib/natalie/compiler/binding_gen.rb index a5e8712cf..c97e836ac 100644 --- a/lib/natalie/compiler/binding_gen.rb +++ b/lib/natalie/compiler/binding_gen.rb @@ -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) diff --git a/spec/core/string/shared/to_sym.rb b/spec/core/string/shared/to_sym.rb index bada46633..2a362ba96 100644 --- a/spec/core/string/shared/to_sym.rb +++ b/spec/core/string/shared/to_sym.rb @@ -35,27 +35,31 @@ sym.should equal :"foobar" end - it "returns a UTF-8 Symbol for a UTF-8 String containing non US-ASCII characters" do + # 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 - it "returns a UTF-16LE Symbol for a UTF-16LE String containing non US-ASCII characters" do + # 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 - it "returns a binary Symbol for a binary String containing non US-ASCII characters" do + # 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 - it "ignores exising symbols with different encoding" do + # 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) @@ -64,7 +68,8 @@ binary_symbol.encoding.should == Encoding::BINARY end - it "raises an EncodingError for UTF-8 String containing invalid bytes" do + # 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? -> { From 60001b0156cb5c7c40a5b372baf73ffa7af33d4d Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 3 Mar 2022 21:31:20 +0000 Subject: [PATCH 3/3] Add String#to_sym spec --- spec/core/string/to_sym_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spec/core/string/to_sym_spec.rb diff --git a/spec/core/string/to_sym_spec.rb b/spec/core/string/to_sym_spec.rb new file mode 100644 index 000000000..f9135211c --- /dev/null +++ b/spec/core/string/to_sym_spec.rb @@ -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