Commit d0683f78 by Mark Wunsch

Ensure that built-up codepoints are written in encoding traversal

parent a20768b9
......@@ -26,14 +26,12 @@ module Rumoji
sequence = if possible_emoji.empty?
# If this codepoint is not part of an emoji
# just write it back out, along with any previously stored codepoints.
sequence_from_codepoints(previous_codepoints, codepoint).tap do
previous_codepoints.clear
end
dump_codepoints_to_string(previous_codepoints << codepoint)
elsif !possible_emoji.any?(&:multiple?)
# If this codepoint is part of an emoji, but not one
# that contains multiple codepoints, write out the
# first possiblity's cheat code.
possible_emoji.first.code
# first possiblity's cheat code and the previously stored codepoints
dump_codepoints_to_string(previous_codepoints) << possible_emoji.first.code
else
# This codepoint is a part of an emoji with multiple
# codepoints, so push it onto the stack.
......@@ -51,7 +49,7 @@ module Rumoji
end
# Write any remaining codepoints.
writeable.write sequence_from_codepoints(previous_codepoints) if previous_codepoints.any?
writeable.write dump_codepoints_to_string(previous_codepoints) if previous_codepoints.any?
writeable
end
......@@ -65,9 +63,10 @@ module Rumoji
private
def sequence_from_codepoints(*codepoints)
compacted = codepoints.flatten.compact
compacted.pack("U*")
def dump_codepoints_to_string(codepoints)
codepoints.pack("U*").tap do
codepoints.clear
end
end
end
......@@ -94,7 +94,7 @@ module Rumoji
self.new("\u{1F3B5}", [:musical_note]), # "music, being in good mood"
self.new("\u{1F525}", [:fire], "FIRE"),
# Poop
self.new("\u{1F4A9}", [:hankey, :poop, :shit], "PILE OF POO"), # "dog dirt"
self.new("\u{1F4A9}", [:poop, :hankey, :shit], "PILE OF POO"), # "dog dirt"
self.new("\u{1F44D}", [:thumbsup, :"+1"], "THUMBS UP SIGN"),
self.new("\u{1F44E}", [:thumbsdown, :"-1"], "THUMBS DOWN SIGN"),
self.new("\u{1F44C}", [:ok_hand], "OK HAND SIGN"),
......
......@@ -16,6 +16,7 @@ describe Rumoji do
it "transforms emoji into cheat-sheet form" do
key = :smile
Rumoji.encode(@smile).must_equal ":smile:"
Rumoji.encode("#{@smile}").must_equal ":smile:"
end
end
......@@ -38,7 +39,6 @@ describe Rumoji do
it "keeps codepoints that match the beginnings of multi-codepoint emoji" do
text = "i like #hashtags and 1direction they are the #1 band. end with 9"
io = StringIO.new(text)
Rumoji.encode_io(io).string.must_equal text
end
......@@ -68,6 +68,13 @@ describe Rumoji do
Rumoji.encode_io(io).string.must_equal "An example of a multipoint emoji is the :us: flag."
end
end
describe "with trailing emoji" do
it "writes characters that are in a multipoint emoji followed by an emoji" do
io = StringIO.new "I would like 0#{@poop}"
Rumoji.encode_io(io).string.must_equal "I would like 0:poop:"
end
end
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment