Skip to content

Commit

Permalink
Merge pull request #4 from pikesley/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
Sam Pikesley committed Dec 18, 2014
2 parents f8012c4 + 4997481 commit 878bf06
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--require spec_helper
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -13,4 +13,6 @@ group :test do
gem 'cucumber'
gem 'capybara'
gem 'rspec'
gem 'guard-cucumber'
gem 'guard-rspec'
end
39 changes: 39 additions & 0 deletions Gemfile.lock
Expand Up @@ -8,6 +8,9 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
celluloid (0.16.0)
timers (~> 4.0.0)
coderay (1.1.0)
coveralls (0.7.1)
multi_json (~> 1.3)
rest-client
Expand All @@ -23,24 +26,55 @@ GEM
curb (0.8.6)
diff-lcs (1.2.5)
docile (1.1.5)
ffi (1.9.6)
formatador (0.2.5)
gherkin (2.12.2)
multi_json (~> 1.3)
guard (2.10.2)
formatador (>= 0.2.4)
listen (~> 2.7)
lumberjack (~> 1.0)
pry (>= 0.9.12)
thor (>= 0.18.1)
guard-compat (1.1.0)
guard-cucumber (1.5.2)
cucumber (>= 1.3.0)
guard (>= 2.0.0)
guard-compat (~> 1.0)
guard-rspec (4.5.0)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
haml (4.0.6)
tilt
hitimes (1.2.2)
kramdown (1.5.0)
listen (2.8.3)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.9)
method_source (0.8.2)
mime-types (2.4.3)
mini_portile (0.6.1)
multi_json (1.10.1)
multi_test (0.1.1)
netrc (0.10.1)
nokogiri (1.6.5)
mini_portile (~> 0.6.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.5.2)
rack-protection (1.5.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
rake (10.4.2)
rb-fsevent (0.9.4)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rest-client (1.7.2)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
Expand All @@ -65,10 +99,13 @@ GEM
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.6.0)
term-ansicolor (1.3.0)
tins (~> 1.0)
thor (0.19.1)
tilt (1.4.1)
timers (4.0.1)
hitimes
tins (1.3.3)
xpath (2.0.0)
nokogiri (~> 1.3)
Expand All @@ -81,6 +118,8 @@ DEPENDENCIES
coveralls
cucumber
curb
guard-cucumber
guard-rspec
haml
kramdown
rake
Expand Down
17 changes: 17 additions & 0 deletions Guardfile
@@ -0,0 +1,17 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %(app lib config test spec feature)

## Uncomment to clear the screen before every task
# clearing :on

guard "cucumber" do
watch(%r{^features/.+\.feature$})
watch(%r{^features/support/.+$}) { "features" }

watch(%r{^features/step_definitions/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "features"
end
end
4 changes: 3 additions & 1 deletion Rakefile
@@ -1,8 +1,10 @@
require File.join(File.dirname(__FILE__), 'lib/github_badges.rb')
require 'cucumber/rake/task'
require 'rspec/core/rake_task'
require 'coveralls/rake/task'

Coveralls::RakeTask.new
Cucumber::Rake::Task.new
RSpec::Core::RakeTask.new

task :default => [:cucumber, 'coveralls:push']
task :default => [:spec, :cucumber, 'coveralls:push']
73 changes: 53 additions & 20 deletions lib/github_badges.rb
Expand Up @@ -11,6 +11,8 @@
}

class GithubBadges < Sinatra::Base
attr_accessor :count

get '/' do
haml :index, locals: {
title: 'Github Badges',
Expand All @@ -19,42 +21,73 @@ class GithubBadges < Sinatra::Base

get '/:user/:repo/:thing' do
thing_parts = params[:thing].split('.')
thing = thing_parts[0...-1].join('.')
extension = thing_parts[-1]
@thing = thing_parts[0...-1].join('.')
@extension = thing_parts[-1]

c = Curl::Easy.new("https://api.github.com/repos/#{params[:user]}/#{params[:repo]}/#{thing}")
c = Curl::Easy.new("https://api.github.com/repos/#{params[:user]}/#{params[:repo]}/#{@thing}")
c.headers = {
'Accept' => 'application/json',
'User-agent' => 'GithubBadges'
}
c.perform
j = JSON.parse c.body_str
count = j.count ||= 'NaN'

colour = case count
when 0
'brightgreen'
when 1..3
'blue'
when 4..6
'orange'
else
'red'
end
@count = j.count ||= 'NaN'
bounce
end

# start the server if ruby file executed directly
run! if app_file == $0

def bounce
redirect Badgerise.target @thing, @count, @extension
end
end

module Badgerise
def Badgerise.colour count
case count
when 0
'brightgreen'
when 1..3
'blue'
when 4..6
'orange'
else
'red'
end
end

thing = case params[:thing]
def Badgerise.label source
case source
when 'issues'
'open%20issues'
when 'pulls'
'pending%20pull--requests'
else
thing
source
end
end

def Badgerise.get_extension text
parts = text.split('.')
if ['svg', 'png'].include? parts[-1]
return parts[-1]
end

redirect "http://img.shields.io/badge/#{thing}-#{j.count}-#{colour}.#{extension}"
'svg'
end

# start the server if ruby file executed directly
run! if app_file == $0
def Badgerise.without_extension text
parts = text.split('.')

if ['svg', 'png'].include? parts[-1]
return parts[0...-1].join('.')
end

text
end

def Badgerise.target type, count, extension = 'svg'
"http://img.shields.io/badge/#{label type}-#{count}-#{Badgerise.colour count}.#{extension}"
end
end
36 changes: 36 additions & 0 deletions spec/github_badges_spec.rb
@@ -0,0 +1,36 @@
require 'spec_helper.rb'

module Badgerise
context 'colour' do
it 'sets a colour' do
expect(Badgerise.colour 0).to eq 'brightgreen'
expect(Badgerise.colour 2).to eq 'blue'
expect(Badgerise.colour 4).to eq 'orange'
expect(Badgerise.colour 8).to eq 'red'
expect(Badgerise.colour 'derp').to eq 'red'
end

it 'sets a label' do
expect(Badgerise.label 'issues').to eq 'open%20issues'
expect(Badgerise.label 'pulls').to eq 'pending%20pull--requests'
expect(Badgerise.label 'unchanged').to eq 'unchanged'
end

it 'generates a target url' do
expect(Badgerise.target 'issues', 4).to eq 'http://img.shields.io/badge/open%20issues-4-orange.svg'
expect(Badgerise.target 'pulls', 1).to eq 'http://img.shields.io/badge/pending%20pull--requests-1-blue.svg'
end

it 'extracts an extension' do
expect(Badgerise.get_extension 'this.svg').to eq 'svg'
expect(Badgerise.get_extension 'that.png').to eq 'png'
expect(Badgerise.get_extension 'this.has.no.extension').to eq 'svg'
end

it 'extracts the non-extension string' do
expect(Badgerise.without_extension 'some.name'). to eq 'some.name'
expect(Badgerise.without_extension 'some.other.name.png'). to eq 'some.other.name'
expect(Badgerise.without_extension 'more-name.svg'). to eq 'more-name'
end
end
end
93 changes: 93 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,93 @@
require 'github_badges'

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

config.order = :random

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Limits the available syntax to the non-monkey patched syntax that is recommended.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

0 comments on commit 878bf06

Please sign in to comment.