Skip to content

Commit

Permalink
Show tag count on main page
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewVos committed Nov 24, 2011
1 parent 70e6f17 commit ccf6f18
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 13 deletions.
4 changes: 2 additions & 2 deletions example-features/has-lots-of-tags.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@tag1 @tag2
@batman @robin
Feature: Has lots of tags

@tag @another_tag
@green_lantern @spider_man
Scenario: Also has some tags
20 changes: 20 additions & 0 deletions features/list_tags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Feature: List Tags
In order to be informed of all tags
As a stakeholder
I want a list of tags to be displayed

Scenario: Multiple tags
Given a feature file named "sample.feature" with the contents:
"""
@tag1
Feature: Tag Feature
@tag2 @multiple
Scenario: Tag Scenario 1
@tag3 @multiple
Scenario: Tag Scenario 2
"""
When I visit the home page
Then I should see a link to "@tag1 (1)" with the url "/search?q=@tag1"
And I should see a link to "@tag2 (1)" with the url "/search?q=@tag2"
And I should see a link to "@tag3 (1)" with the url "/search?q=@tag3"
And I should see a link to "@multiple (2)" with the url "/search?q=@multiple"
1 change: 1 addition & 0 deletions lib/wally.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
require "wally/application"
require "wally/lists_features"
require "wally/search_features"
require "wally/counts_tags"
15 changes: 5 additions & 10 deletions lib/wally/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
set :haml, { :ugly=>true }
end

def features_path
ARGV.first || "features"
end

def lists_features
Wally::ListsFeatures.new(features_path)
end

before do
@features = lists_features.features
features_path = ARGV.first || "features"
@lists_features = Wally::ListsFeatures.new(features_path)
@features = @lists_features.features
@tag_count = Wally::CountsTags.new(@lists_features).count_tags
end

get '/?' do
Expand All @@ -32,7 +27,7 @@ def lists_features

get '/search/?' do
if params[:q]
@search_results = Wally::SearchFeatures.new(lists_features).find(:query => params[:q])
@search_results = Wally::SearchFeatures.new(@lists_features).find(:query => params[:q])
end
haml :search
end
Expand Down
27 changes: 27 additions & 0 deletions lib/wally/counts_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Wally
class CountsTags
def initialize lists_features
@lists_features = lists_features
end

def count_tags
@lists_features.features.inject(Hash.new(0)) do |tag_count, feature|
if feature["tags"]
feature["tags"].each do |tag|
tag_count[tag["name"]] += 1
end
end
if feature["elements"]
feature["elements"].each do |element|
if element["tags"]
element["tags"].each do |tag|
tag_count[tag["name"]] += 1
end
end
end
end
tag_count
end
end
end
end
2 changes: 1 addition & 1 deletion lib/wally/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wally
VERSION = "0.0.15"
VERSION = "0.0.16"
end
1 change: 1 addition & 0 deletions lib/wally/views/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions lib/wally/views/layout.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
%input.btn{:type => "submit", :id => "search", :value => "Search"}
%div.container-fluid
%div.sidebar
- if @tag_count.any?
%h2 Tags
- @tag_count.each do |tag, count|
%span.label.notice
%a{:href => "/search?q=#{tag}"}
= "#{tag} (#{count})"
%h2
Features
%ul
Expand Down
42 changes: 42 additions & 0 deletions spec/wally/counts_tags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require File.join(File.dirname(__FILE__), "..", "spec_helper")
require 'fileutils'
require "wally/counts_tags"

module Wally
describe CountsTags do
before do
FileUtils.mkdir_p "application-features"
end

after do
FileUtils.rm_rf "application-features"
end

def write_feature(name, contents)
File.open("application-features/#{name}", "w") do |file|
file.write(contents)
end
end

it "counts feature tags" do
write_feature("feature-1.feature", "@tag1 @tag2\nFeature: Feature 1")
write_feature("feature-2.feature", "@tag2 @tag2\nFeature: Feature 2")
lists_features = ListsFeatures.new("application-features")

CountsTags.new(lists_features).count_tags.should == {
"@tag1" => 1,
"@tag2" => 3
}
end

it "counts scenario tags" do
write_feature("feature-1.feature", "Feature: Feature 1\n@tag1@tag1\nScenario: Scenario 1")
write_feature("feature-2.feature", "Feature: Feature 2\n@tag3@tag1\nScenario: Scenario 1")
lists_features = ListsFeatures.new("application-features")
CountsTags.new(lists_features).count_tags.should == {
"@tag1" => 3,
"@tag3" => 1
}
end
end
end

0 comments on commit ccf6f18

Please sign in to comment.