Skip to content
Snippets Groups Projects
Commit ae84f8f7 authored by Brett Walker's avatar Brett Walker :octagonal_sign:
Browse files

Add additional specs

parent 78b2b8f3
No related branches found
No related tags found
1 merge request!65Update support for Unicode 15.1
Pipeline #1477543148 passed
...@@ -45,6 +45,9 @@ module TanukiEmoji ...@@ -45,6 +45,9 @@ module TanukiEmoji
# #
# @param [String] codepoints # @param [String] codepoints
def add_codepoints(codepoints) def add_codepoints(codepoints)
return if @codepoints == codepoints
return if codepoints_alternates.include?(codepoints)
codepoints_alternates << codepoints codepoints_alternates << codepoints
end end
...@@ -52,15 +55,20 @@ module TanukiEmoji ...@@ -52,15 +55,20 @@ module TanukiEmoji
# #
# @param [String] alpha_code # @param [String] alpha_code
def add_alias(alpha_code) def add_alias(alpha_code)
return if aliases.include?(alpha_code) formatted_code = self.class.format_alpha_code(alpha_code)
return if @alpha_code == alpha_code
return if aliases.include?(formatted_code)
aliases << self.class.format_alpha_code(alpha_code) aliases << formatted_code
end end
# Add alternative ASCII aliases to this character # Add alternative ASCII aliases to this character
# #
# @param [String] ascii_string # @param [String] ascii_string
def add_ascii_alias(ascii_string) def add_ascii_alias(ascii_string)
return if ascii_aliases.include?(ascii_string)
ascii_aliases << ascii_string ascii_aliases << ascii_string
end end
...@@ -78,6 +86,8 @@ module TanukiEmoji ...@@ -78,6 +86,8 @@ module TanukiEmoji
# Noto doesn't ship flags as part of regular hex-named files # Noto doesn't ship flags as part of regular hex-named files
# Flags are stored in a separate third-party folder and follow ISO-3166-1 codes # Flags are stored in a separate third-party folder and follow ISO-3166-1 codes
# @see http://en.wikipedia.org/wiki/ISO_3166-1 # @see http://en.wikipedia.org/wiki/ISO_3166-1
# also see https://www.unicode.org/reports/tr51/#flag-emoji-tag-sequences for
# regional flags.
if flag? if flag?
name = noto_image name = noto_image
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
require 'strscan' require 'strscan'
require 'date' require 'date'
require 'pathname'
require 'i18n' require 'i18n'
require_relative 'emoji_data' require_relative 'emoji_data'
...@@ -26,7 +27,7 @@ module TanukiEmoji ...@@ -26,7 +27,7 @@ module TanukiEmoji
# #
# @return [Pathname] path to the default data file # @return [Pathname] path to the default data file
def self.data_file def self.data_file
Pathname.new(File.expand_path(File.join(__dir__, '../../../', DATA_FILE))) ::Pathname.new(File.expand_path(File.join(__dir__, '../../../', DATA_FILE)))
end end
attr_reader :data_file attr_reader :data_file
...@@ -74,37 +75,26 @@ module TanukiEmoji ...@@ -74,37 +75,26 @@ module TanukiEmoji
alpha_code = I18n.transliterate(emoji_data.description).gsub(/[^a-zA-Z#*\d]+/, '_').downcase alpha_code = I18n.transliterate(emoji_data.description).gsub(/[^a-zA-Z#*\d]+/, '_').downcase
end end
emoji = @index.find_by_codepoints(emoji_data.codepoints) # This might be a different qualified version, basically same emoji but slightly different
# codepoint. Search on the alpha code and pull that. If found, add as alternate codepoint.
# "smiling face" is one example.
emoji = @index.find_by_alpha_code(alpha_code)
if emoji if emoji
if emoji.alpha_code != alpha_code emoji.add_codepoints(emoji_data.codepoints)
# code might have been renamed, add as an alias
emoji.add_alias(alpha_code)
@index.update(emoji) @index.update(emoji)
end
else else
# This might be a different qualified version, basically same emoji but slightly different # not found, add a new emoji
# codepoint. Search on the alpha code and pull that. If found, add as alternate codepoint. emoji = Character.new(alpha_code,
# "smiling face" is one example. codepoints: emoji_data.codepoints,
emoji = @index.find_by_alpha_code(alpha_code) alpha_code: alpha_code,
description: emoji_data.description,
if emoji category: emoji_data.group_category)
emoji.add_codepoints(emoji_data.codepoints)
emoji.unicode_version = emoji_data.version
@index.update(emoji)
else @index.add(emoji)
# not found, add a new emoji
emoji = Character.new(alpha_code,
codepoints: emoji_data.codepoints,
alpha_code: alpha_code,
description: emoji_data.description,
category: emoji_data.group_category)
emoji.unicode_version = emoji_data.version
@index.add(emoji)
end
end end
end end
end end
......
...@@ -24,6 +24,13 @@ RSpec.describe TanukiEmoji::Character do ...@@ -24,6 +24,13 @@ RSpec.describe TanukiEmoji::Character do
description: 'brazil', description: 'brazil',
category: 'flags') category: 'flags')
end end
let(:england_flag) do
described_class.new('flag_england',
codepoints: "\u{1f3f4}\u{e0067}\u{e0062}\u{e0065}\u{e006e}\u{e0067}\u{e007f}",
alpha_code: ':flag_england:',
description: 'flag: England',
category: 'flags')
end
describe '#initialize' do describe '#initialize' do
it 'formats the name' do it 'formats the name' do
...@@ -70,6 +77,11 @@ RSpec.describe TanukiEmoji::Character do ...@@ -70,6 +77,11 @@ RSpec.describe TanukiEmoji::Character do
expect { subject.add_codepoints("\u{1f434}\u{fe0f}") }.to change { subject.codepoints_alternates.count }.by(1) expect { subject.add_codepoints("\u{1f434}\u{fe0f}") }.to change { subject.codepoints_alternates.count }.by(1)
expect(subject.codepoints_alternates).to include("\u{1f434}\u{fe0f}") expect(subject.codepoints_alternates).to include("\u{1f434}\u{fe0f}")
end end
it 'does not add duplicates' do
expect { subject.add_codepoints("\u{1f434}\u{fe0f}") }.to change(subject.codepoints_alternates, :count).by(1)
expect { subject.add_codepoints("\u{1f434}\u{fe0f}") }.not_to change(subject.codepoints_alternates, :count)
end
end end
context '#add_alias' do context '#add_alias' do
...@@ -83,6 +95,11 @@ RSpec.describe TanukiEmoji::Character do ...@@ -83,6 +95,11 @@ RSpec.describe TanukiEmoji::Character do
expect(subject.aliases).to include(':horse_face:') expect(subject.aliases).to include(':horse_face:')
end end
it 'does not add duplicates' do
expect { mage_emoji.add_alias('wizard') }.to change(mage_emoji.aliases, :count)
expect { mage_emoji.add_alias('wizard') }.not_to change(mage_emoji.aliases, :count)
end
end end
context '#add_ascii_alias' do context '#add_ascii_alias' do
...@@ -90,6 +107,11 @@ RSpec.describe TanukiEmoji::Character do ...@@ -90,6 +107,11 @@ RSpec.describe TanukiEmoji::Character do
expect { subject.add_ascii_alias(':)') }.to change { subject.ascii_aliases.count }.by(1) expect { subject.add_ascii_alias(':)') }.to change { subject.ascii_aliases.count }.by(1)
expect(subject.ascii_aliases).to include(':)') expect(subject.ascii_aliases).to include(':)')
end end
it 'does not add duplicates' do
expect { subject.add_ascii_alias(':)') }.to change(subject.ascii_aliases, :count).by(1)
expect { subject.add_ascii_alias(':)') }.not_to change(subject.ascii_aliases, :count)
end
end end
context '#hex' do context '#hex' do
...@@ -104,6 +126,12 @@ RSpec.describe TanukiEmoji::Character do ...@@ -104,6 +126,12 @@ RSpec.describe TanukiEmoji::Character do
expect(mage_emoji.hex).to eq('1f9d9-1f3fb') expect(mage_emoji.hex).to eq('1f9d9-1f3fb')
end end
end end
context 'with a passed in codepoint' do
it 'returns a formatted hex representation of the codepoint' do
expect(subject.hex(mage_emoji.codepoints)).to eq('1f9d9-1f3fb')
end
end
end end
context '#image_name' do context '#image_name' do
...@@ -150,6 +178,10 @@ RSpec.describe TanukiEmoji::Character do ...@@ -150,6 +178,10 @@ RSpec.describe TanukiEmoji::Character do
it 'returns false when character is not made of flag-like A-Z characters' do it 'returns false when character is not made of flag-like A-Z characters' do
expect(mage_emoji.flag?).to be_falsey expect(mage_emoji.flag?).to be_falsey
end end
it 'returns true when character is made with regional formatting' do
expect(england_flag.flag?).to be_truthy
end
end end
context '#==' do context '#==' do
......
# frozen_string_literal: true
require 'rspec'
RSpec.describe TanukiEmoji::Db::AdditionalAliases do
subject { described_class.new(index: TanukiEmoji.index) }
before do
TanukiEmoji.index.reset!(reload: false)
TanukiEmoji::Db::EmojiTestParser.new(index: TanukiEmoji.index).load!
end
describe '#load!' do
it 'populates indexed emojis with aliases' do
emoji = TanukiEmoji.find_by_alpha_code('flag_england')
expect(emoji.aliases).not_to include(':england:')
expect(emoji.noto_image).to be_nil
subject.load!
emoji = TanukiEmoji.find_by_alpha_code('flag_england')
expect(emoji.aliases).to include(':england:')
expect(emoji.noto_image).to eq 'gb-eng'
end
end
end
# frozen_string_literal: true
require 'rspec'
RSpec.describe TanukiEmoji::Db::EmojiTestParser do
describe '#data' do
subject(:emoji_data) { described_class.new(index: TanukiEmoji.index).data }
before do
TanukiEmoji.index.reset!(reload: false)
end
it 'returns an array of EmojiData' do
is_expected.to be_a(Array)
is_expected.to all(be_an(TanukiEmoji::Db::EmojiTestData))
end
context 'for each EmojiTestData item' do
it 'has codepoints filled' do
emoji_data.each do |data|
expect(data.codepoints).to_not be_empty
end
end
it 'has emoji filled' do
emoji_data.each do |data|
expect(data.emoji).to_not be_empty
end
end
it 'has version filled and following a known version pattern' do
emoji_data.each do |data|
expect(data.version).to_not be_empty
expect(data.version).to match(/[0-9]+\.[0-9]+/)
end
end
it 'has description filled' do
emoji_data.each do |data|
expect(data.description).to_not be_empty
end
end
it 'has qualification filled' do
emoji_data.each do |data|
expect(data.qualification).to_not be_empty
end
end
it 'has group_category filled' do
emoji_data.each do |data|
expect(data.group_category).to_not be_empty
end
end
end
end
describe '#load!' do
before do
TanukiEmoji.index.reset!(reload: false)
described_class.new(index: TanukiEmoji.index).load!
end
it 'adds emojis' do
emoji = TanukiEmoji.find_by_alpha_code('smiling_face')
expect(emoji.codepoints).to eq '☺️'
expect(emoji.unicode_version).to eq '6.0'
expect(emoji.description).to eq 'smiling face'
expect(emoji.category).to eq 'Smileys & Emotion'
expect(emoji.codepoints_alternates.first).to eq '☺'
emoji = TanukiEmoji.find_by_alpha_code('palm_down_hand_medium_dark_skin_tone')
expect(emoji.codepoints).to eq '🫳🏾'
expect(emoji.unicode_version).to eq '14.0'
expect(emoji.description).to eq 'palm down hand: medium-dark skin tone'
expect(emoji.category).to eq 'People & Body'
expect(emoji.codepoints_alternates.first).to be_nil
end
end
end
...@@ -56,6 +56,39 @@ RSpec.describe TanukiEmoji::Index do ...@@ -56,6 +56,39 @@ RSpec.describe TanukiEmoji::Index do
end end
end end
context '#update' do
let(:modified_horse_emoji) do
TanukiEmoji::Character.new('horse_face',
codepoints: "\u{1f434}",
alpha_code: ':horse_face:',
description: 'horse face',
category: 'Animals & Nature')
.tap do |emoji|
emoji.add_alias('horse')
emoji.add_alias('tall_horse')
emoji.add_codepoints("\u{1F984}")
end
end
before do
subject.reset!(reload: false)
end
context 'update the index' do
it 'adds a new item to the index' do
subject.add(horse_emoji)
expect { subject.update(modified_horse_emoji) }.not_to change(subject.all, :size)
emoji = subject.find_by_alpha_code('horse_face')
expect(emoji.aliases).to include(':tall_horse:')
expect(subject.find_by_alpha_code('tall_horse')).to eq emoji
expect(subject.find_by_codepoints("\u{1F984}")).to eq emoji
end
end
end
context '#find_by_alpha_code' do context '#find_by_alpha_code' do
before do before do
subject.reset! subject.reset!
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment