Skip to content

Commit

Permalink
Merge pull request #8 from ruedap/refactor
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
ruedap committed Aug 26, 2013
2 parents 71e8629 + 437a287 commit 4e26ba5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 32 deletions.
18 changes: 18 additions & 0 deletions workflow/lib/font_awesome.rb
Expand Up @@ -5,4 +5,22 @@ def self.icons
(md && md[1]) ? md[1] : nil
}.compact.sort
end

def self.select!(icons, queries)
queries.each do |q|
# use reject! for ruby 1.8 compatible
icons.reject! { |i| i.index(q.downcase) ? false : true }
end
end

def self.item_hash(icon)
{
:uid => '',
:title => icon,
:subtitle => "Copy to clipboard: icon-#{icon}",
:arg => icon,
:icon => { :type => 'default', :name => "icon-#{icon}.png" },
:valid => 'yes',
}
end
end
19 changes: 3 additions & 16 deletions workflow/main.rb
Expand Up @@ -11,22 +11,10 @@
def generate_feedback(alfred, query)
feedback = alfred.feedback
queries = query.split
icons = FontAwesome.icons
icons = FontAwesome.icons

queries.each do |q|
icons.reject! { |i| i.index(q.downcase) ? false : true } # select!...
end

icons.each do |icon|
feedback.add_item({
:uid => '',
:title => icon,
:subtitle => "Copy to clipboard: icon-#{icon}",
:arg => icon,
:icon => { :type => 'default', :name => "icon-#{icon}.png" },
:valid => 'yes',
})
end
FontAwesome.select!(icons, queries)
icons.each { |icon| feedback.add_item(FontAwesome.item_hash(icon)) }

puts feedback.to_alfred
end
Expand All @@ -36,4 +24,3 @@ def generate_feedback(alfred, query)
query = ARGV.join(' ').strip
generate_feedback(alfred, query)
end

95 changes: 79 additions & 16 deletions workflow/test/font_awesome_test.rb
Expand Up @@ -2,38 +2,101 @@

describe FontAwesome do
it 'does not cause an error' do
assert_equal false, require('bundle/bundler/setup')
require('bundle/bundler/setup').must_equal false
end

describe '.icons' do
before do
@icons = FontAwesome.icons
end
before { @icons = FontAwesome.icons }
after { @icons = nil }

it 'is 378 pieces' do
@icons.count.must_equal 378
end
it { @icons.count.must_equal 378 }
it { @icons.first.must_equal 'adjust' }
it { @icons.last.must_equal 'zoom-out' }

it 'includes these strings' do
strings = %w(user repeat copy code-fork)
strings.each do |str|
@icons.include?(str).must_equal true
end
strings.each { |str| @icons.must_include str }
end

it 'does not includes these strings' do
strings = %w(icon)
strings.each do |str|
@icons.include?(str).must_equal false
strings.each { |str| @icons.wont_include str }
end
end

describe '.select!' do
before { @icons = FontAwesome.icons }
after { @icons = nil }

describe 'with `apple`' do
before do
@queries = %w(apple)
FontAwesome.select!(@icons, @queries)
end
after { @queries = nil }

it { @icons.must_equal @queries }
it { @icons.count.must_equal 1 }
end

describe 'with `left arr`' do
before do
@queries = %w(left arr)
FontAwesome.select!(@icons, @queries)
end
after { @queries = nil }

it { @icons.must_equal %w(arrow-left circle-arrow-left long-arrow-left) }
it { @icons.count.must_equal 3 }
end

describe 'with `arr left` (reverse)' do
before do
@queries = %w(arr left)
FontAwesome.select!(@icons, @queries)
end
after { @queries = nil }

it { @icons.must_equal %w(arrow-left circle-arrow-left long-arrow-left) }
it { @icons.count.must_equal 3 }
end

it 'is "adjust" is the first element' do
@icons.first.must_equal 'adjust'
describe 'with `ruedap` (does not match)' do
before do
@queries = %w(ruedap)
FontAwesome.select!(@icons, @queries)
end
after { @queries = nil }

it { @icons.must_equal %w() }
it { @icons.must_be_empty }
end

it 'is "zoom-out" is the last element' do
@icons.last.must_equal 'zoom-out'
describe 'with unknown arguments' do
before do
@queries = %w()
FontAwesome.select!(@icons, @queries)
end
after { @queries = nil }

it { @icons.must_equal FontAwesome.icons }
it { @icons.count.must_equal 378 }
end
end

describe '.item_hash' do
before do
@icon = 'apple'
@item_hash = FontAwesome.item_hash(@icon)
end

it { @item_hash[:uid].must_equal '' }
it { @item_hash[:title].must_equal @icon }
it { @item_hash[:subtitle].must_equal "Copy to clipboard: icon-#{@icon}" }
it { @item_hash[:arg].must_equal @icon }
it { @item_hash[:icon][:type].must_equal 'default' }
it { @item_hash[:icon][:name].must_equal "icon-#{@icon}.png" }
it { @item_hash[:valid].must_equal 'yes' }
it { @item_hash.count.must_equal 6 }
end
end

0 comments on commit 4e26ba5

Please sign in to comment.