-
Notifications
You must be signed in to change notification settings - Fork 116
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
Assume symbols are either ASCII or UTF-8 #248
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# encoding: ascii-8bit | ||
require 'spec_helper' | ||
|
||
describe MessagePack::Factory do | ||
|
@@ -255,29 +254,25 @@ def to_msgpack_ext | |
subject { factory.packer.pack(value).to_s } | ||
before { stub_const('Value', Class.new{ include Mod }) } | ||
let(:value) { Value.new } | ||
it { is_expected.to eq "\xC7\x0F\x01value_msgpacked" } | ||
it { is_expected.to eq "\xC7\x0F\x01value_msgpacked".force_encoding(Encoding::BINARY) } | ||
end | ||
|
||
describe "packing an object which has been extended by the module" do | ||
subject { factory.packer.pack(object).to_s } | ||
let(:object) { Object.new.extend Mod } | ||
it { is_expected.to eq "\xC7\x0F\x01value_msgpacked" } | ||
it { is_expected.to eq "\xC7\x0F\x01value_msgpacked".force_encoding(Encoding::BINARY) } | ||
end | ||
|
||
describe "unpacking with the module" do | ||
subject { factory.unpacker.feed("\xC7\x06\x01module").unpack } | ||
subject { factory.unpacker.feed("\xC7\x06\x01module".force_encoding(Encoding::BINARY)).unpack } | ||
it { is_expected.to eq "unpacked module" } | ||
end | ||
end | ||
end | ||
|
||
describe 'the special treatment of symbols with ext type' do | ||
let(:packer) { subject.packer } | ||
let(:unpacker) { subject.unpacker } | ||
|
||
def symbol_after_roundtrip | ||
packed_symbol = packer.pack(:symbol).to_s | ||
unpacker.feed(packed_symbol).unpack | ||
def roundtrip(object) | ||
subject.load(subject.dump(object)) | ||
end | ||
|
||
context 'using the optimized symbol unpacker' do | ||
|
@@ -293,13 +288,25 @@ def symbol_after_roundtrip | |
end | ||
|
||
it 'lets symbols survive a roundtrip' do | ||
expect(symbol_after_roundtrip).to be :symbol | ||
expect(roundtrip(:symbol)).to be :symbol | ||
end | ||
|
||
it 'preserves encoding for ASCII symbols' do | ||
expect(:symbol.encoding).to be Encoding::US_ASCII | ||
expect(roundtrip(:symbol)).to be :symbol | ||
expect(roundtrip(:symbol).encoding).to be Encoding::US_ASCII | ||
end | ||
|
||
it 'preserves encoding for UTF-8 symbols' do | ||
expect(:"fée".encoding).to be Encoding::UTF_8 | ||
expect(roundtrip(:"fée").encoding).to be Encoding::UTF_8 | ||
expect(roundtrip(:"fée")).to be :"fée" | ||
end | ||
end | ||
|
||
context 'if no ext type is registered for symbols' do | ||
it 'converts symbols to string' do | ||
expect(symbol_after_roundtrip).to eq 'symbol' | ||
expect(roundtrip(:symbol)).to eq 'symbol' | ||
end | ||
end | ||
|
||
|
@@ -308,7 +315,33 @@ def symbol_after_roundtrip | |
before { subject.register_type(0x00, ::Symbol) } | ||
|
||
it 'lets symbols survive a roundtrip' do | ||
expect(symbol_after_roundtrip).to be :symbol | ||
expect(roundtrip(:symbol)).to be :symbol | ||
end | ||
|
||
it 'preserves encoding for ASCII symbols' do | ||
expect(:symbol.encoding).to be Encoding::US_ASCII | ||
expect(roundtrip(:symbol)).to be :symbol | ||
expect(roundtrip(:symbol).encoding).to be Encoding::US_ASCII | ||
end | ||
|
||
it 'preserves encoding for UTF-8 symbols' do | ||
expect(:"fée".encoding).to be Encoding::UTF_8 | ||
expect(roundtrip(:"fée")).to be :"fée" | ||
expect(roundtrip(:"fée").encoding).to be Encoding::UTF_8 | ||
end | ||
|
||
it 'does not handle symbols in other encodings' do | ||
symbol = "fàe".encode(Encoding::ISO_8859_1).to_sym | ||
expect(symbol.encoding).to be Encoding::ISO_8859_1 | ||
|
||
if IS_JRUBY | ||
# JRuby doesn't quite behave like MRI here. | ||
# "fàe".force_encoding(Encoding::BINARY).to_sym is able to lookup the existing ISO-8859-1 symbol | ||
# It likely is a JRuby bug. | ||
expect(roundtrip(symbol).encoding).to be Encoding::ISO_8859_1 | ||
Comment on lines
+337
to
+341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RUby Spec for this ruby/spec#919, so it's likely that future versions of JRuby will have this fixed. |
||
else | ||
expect(roundtrip(symbol).encoding).to be Encoding::BINARY | ||
end | ||
end | ||
end | ||
|
||
|
@@ -332,7 +365,7 @@ def from_msgpack_ext(data) | |
before { subject.register_type(0x00, ::Symbol) } | ||
|
||
it 'lets symbols survive a roundtrip' do | ||
expect(symbol_after_roundtrip).to be :symbol | ||
expect(roundtrip(:symbol)).to be :symbol | ||
end | ||
|
||
after do | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the solution. I first try what should be the happy path, and deal with the error in a
rescue
as this should basically never happen ever, so no reason the happy path should pay an extra cost for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix and awesome code comment!