Skip to content

Commit

Permalink
Initial Commit with simple spec
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Feb 25, 2009
0 parents commit a0465aa
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Rakefile
@@ -0,0 +1,4 @@
namespace :gembox do


end
8 changes: 8 additions & 0 deletions config.ru
@@ -0,0 +1,8 @@
# To use with thin
# thin start -p PORT -R config.ru

require File.join(File.dirname(__FILE__), 'gembox.rb')

disable :run
set :env, :production
run Sinatra.application
1 change: 1 addition & 0 deletions config.yml
@@ -0,0 +1 @@
---
32 changes: 32 additions & 0 deletions gembox.rb
@@ -0,0 +1,32 @@
require 'rubygems'
require 'sinatra'
require File.join(File.dirname(__FILE__), 'lib', 'gembox')

Gembox::Gems.load

helpers do
def link_to(text, link = nil)
link ||= text
"<a href=\"#{link}\">#{text}</a>"
end

def escape(text)
CGI.escapeHTML(text)
end
end

set :public, 'public'
set :views, 'views'

get '/' do
@gems = Gembox::Gems.local_gems
haml :index
end

get '/gems/?' do

end

get '/gems/:name' do

end
27 changes: 27 additions & 0 deletions lib/gembox.rb
@@ -0,0 +1,27 @@
module Gembox
class Gems

class << self
attr_accessor :source_index

def load
@source_index ||= ::Gem.source_index
local_gems
end

def local_gems
@local_gems ||= group_gems
end

protected
def group_gems
gem_hash = Hash.new {|h,k| h[k] = [] }
source_index.gems.each do |gem_with_version, spec|
gem_hash[spec.name] << spec
end
gem_hash
end
end

end
end
56 changes: 56 additions & 0 deletions test/test_gembox_app.rb
@@ -0,0 +1,56 @@
require 'test_helper'

describe "Gembox App" do

describe 'getting /' do
before do
get '/'
end

should 'load the index' do
should.be.ok
end

should "display gem list" do
body.should have_element('#gems')
end

should "display list of installed gems" do
body.should have_element('.gem', /sinatra/)
end
end

describe 'getting gems/ with layout = false' do
before do
get '/gems/?layout=false'
end

should "load" do
should.be.ok
end

should "not display layout" do
body.should.not have_element('html')
end

should "display gem list" do
body.should have_element('#gems')
end
end

describe 'getting gems/:name' do
before do
get '/gems/sinatra'
end

should "display only specific gem" do
body.should.not have_element('.gem', /rack/)
end

should "display link to gems website" do
body.should have_element('a', 'http://sinatra.rubyforge.org')
end

end

end
40 changes: 40 additions & 0 deletions test/test_gembox_gems.rb
@@ -0,0 +1,40 @@
require 'test_helper'

describe 'Gembox::Gems' do

describe '#load' do
before do
Gembox::Gems.load
end

should "load local gems" do
Gembox::Gems.local_gems.should.be.an instance_of(Hash)
end

should "load source index into source_index" do
Gembox::Gems.source_index.should.be.an instance_of(Gem::SourceIndex)
end
end

describe "#local_gems" do
before do
Gembox::Gems.load
@gems = Gembox::Gems.local_gems
end

should "return hash of gems" do
@gems.should.be.an instance_of(Hash)
end

should "only have one entry per gem" do
@gems.should.has_key? 'sinatra'
@gems.should.not.has_key? 'sinatra-0.9.0.5'
end

should "have an array of spec versions per gem name" do
@gems['sinatra'].should.be.an instance_of(Array)
@gems['sinatra'].first.should.be.an instance_of(Gem::Specification)
end
end

end
51 changes: 51 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,51 @@
require 'rubygems'
require 'sinatra'
require 'sinatra/test/bacon'

require File.join(File.dirname(__FILE__), '..', 'gembox.rb')

require 'nokogiri'


module TestHelper

def instance_of(klass)
lambda {|obj| obj.is_a?(klass) }
end

# HTML matcher for bacon
#
# it 'should display document' do
# body.should have_element('#document')
# end
#
# With content matching:
#
# it 'should display loaded document' do
# body.should have_element('#document .title', /My Document/)
# end
def have_element(search, content = nil)
lambda do |obj|
doc = Nokogiri.parse(obj.to_s)
node_set = doc.search(search)
if node_set.empty?
false
else
collected_content = node_set.collect {|t| t.content }.join(' ')
case content
when Regexp
collected_content =~ content
when String
collected_content.include?(content)
when nil
true
end
end
end
end


end

Bacon::Context.send(:include, TestHelper)

7 changes: 7 additions & 0 deletions views/index.haml
@@ -0,0 +1,7 @@
%h2 Installed Gems
#gems
-@gems.sort.each do |gem_name, spec|
.gem
=link_to(gem_name, "/gems/#{gem_name}")
.versions
%span Versions
6 changes: 6 additions & 0 deletions views/layout.haml
@@ -0,0 +1,6 @@
%html{:xmlns=> "http://www.w3.org/1999/xhtml", 'xml:lang' => "en", :lang => "en"}
%head
%meta{'http-equiv' => "Content-Type", 'content' => "text/html; charset=utf-8"}
%title gembox
%body
= yield

0 comments on commit a0465aa

Please sign in to comment.