Skip to content

Commit

Permalink
Started adding tests, cleared up documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 26, 2009
1 parent 21f51c5 commit cb39d5a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
@@ -1,6 +1,8 @@
# Sinatra Flash

Simple flash hash implementation for Sinatra.
Simple flash hash implementation for Sinatra. My implementation has slightly
different behavior than Rails in that it doesn't delete entries until they used.
I think it's pretty rad, but I'm happy to hear thoughts to the contrary.

## Usage

Expand Down
6 changes: 3 additions & 3 deletions lib/sinatra-flash.rb
Expand Up @@ -2,8 +2,8 @@ module Sinatra
module Flash
# Implements bracket accessors for storing and retrieving flash entries.
class FlashHash
def initialize(env)
@session = env['rack.session']
def initialize(session)
@session = session
@session[:__FLASH__] ||= {}
end

Expand Down Expand Up @@ -41,7 +41,7 @@ def values
end

def flash
@flash ||= Sinatra::Flash::FlashHash.new(env)
@flash ||= Sinatra::Flash::FlashHash.new(session)
end
end
end
25 changes: 25 additions & 0 deletions test/helper.rb
@@ -0,0 +1,25 @@
require 'rubygems'
require 'sinatra/base'
require 'bacon'
require 'sinatra/test'
require 'sinatra/test/bacon'
require File.join(File.dirname(__FILE__), *%w[.. lib sinatra-flash])

class String
[:green, :yellow, :red].each { |c| define_method(c) { self } }
end if ENV['TM_RUBY']

# bacon swallows errors alive
def err_explain
begin
yield
rescue => e
puts e.inspect
puts e.backtrace
raise e
end
end

def mock_app(&block)
@app = Sinatra.new(&block)
end
29 changes: 29 additions & 0 deletions test/test_flash.rb
@@ -0,0 +1,29 @@
require File.dirname(__FILE__) + '/helper'

describe 'Sinatra::Flash' do
before do
mock_app {
include Sinatra::Flash

set :sessions, false

get '/view' do
flash[:notice]
end

post '/set' do
flash[:notice] = params[:q]
redirect '/view'
end
}
end

it 'is empty by default' do
err_explain do
get '/view'
body.should.be.empty
end
end

# Testing sessions is a royal pain in the ass.
end

0 comments on commit cb39d5a

Please sign in to comment.