Skip to content
This repository has been archived by the owner on Aug 14, 2018. It is now read-only.

Commit

Permalink
add tests for generate pdf private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron committed May 16, 2011
1 parent acc77b9 commit b241031
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 61 deletions.
8 changes: 4 additions & 4 deletions lib/princess/generate_pdf.rb
Expand Up @@ -82,10 +82,10 @@ def make_pdf(options = {}) #template_path, pdf_name, landscape=false)


#appends the filename with the given suffix if necessary. #appends the filename with the given suffix if necessary.
# Examples: # Examples:
# * append_suffix(:tps_report,:pdf) => 'tps_report.pdf' # * append_suffix(:tps_report, :pdf) => 'tps_report.pdf'
# * append_suffix('my_styles.css',:css) => 'my_styles.css' # * append_suffix('my_styles.css', :css) => 'my_styles.css'
def append_suffix(filename,suffix) def append_suffix(filename, suffix)
return filename.match(/\.#{suffix}$/) ? filename.to_s : "#{filename}.#{suffix}" return filename.match(/\.#{suffix}$/) ? filename.to_s : "#{filename}.#{suffix}"
end end


def princess_default_stylesheet def princess_default_stylesheet
Expand Down
32 changes: 4 additions & 28 deletions test/erb_views_test.rb
Expand Up @@ -3,53 +3,29 @@


require 'test_helper' require 'test_helper'


#Controller who's views are all in the .html.erb format class ErbArticlesControllerTest < ActionController::TestCase
class ErbArticlesController < ActionController::Base

def show
@article = Article.find(params[:id])

respond_to do |format|
format.html
format.pdf { render :pdf => true }
end
end

def custom_one
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.pdf { render :pdf => 'alternate_custom_one', :filename => "custom_one.pdf" }
end
end
end

class ErbViewsTest < ActionController::TestCase


def setup def setup
prepop_db prepop_db
@controller = ErbArticlesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end end




# show # show
def test_should_get_show_and_return_html test "should get show and return html" do
get :show, :id => 1 get :show, :id => 1
assert_response :success assert_response :success
assert !@response.body.match(/^%PDF/) assert !@response.body.match(/^%PDF/)
end end


def test_should_generate_pdf_with_no_argugments test "should generate pdf with no argugments" do
get :show, :id => 1, :format => 'pdf' get :show, :id => 1, :format => 'pdf'
assert_response :success assert_response :success
assert @response.body.match(/^%PDF/) assert @response.body.match(/^%PDF/)
end end




# custom_one # custom_one
def test_articles_custom_one test "should get alternate_custom_one template" do
get :custom_one, :id => 1 get :custom_one, :id => 1
assert_select 'h1', 'Article1' assert_select 'h1', 'Article1'
get :custom_one, {:id => 1, :format => 'pdf'} get :custom_one, {:id => 1, :format => 'pdf'}
Expand Down
54 changes: 54 additions & 0 deletions test/generate_pdf_test.rb
@@ -0,0 +1,54 @@
#!/usr/bin/env ruby
# encoding: UTF-8

require 'test_helper'

class Foo
include Princess::GeneratePdf

def params
@params ||= Hash.new
end
end

class GeneratePdfTest < ActiveSupport::TestCase

# append_suffix
test "should append suffix" do
instance = Foo.new

assert_equal 'foo.bar', instance.send(:append_suffix, 'foo', :bar)
end

test "should not append when suffix is already present" do
instance = Foo.new

assert_equal 'foo.bar', instance.send(:append_suffix, 'foo.bar', :bar)
end


# princess_default_stylesheet
test "should return application pdf stylesheet" do
instance = Foo.new
instance.params[:controller] = 'does_not_exist'

assert_equal 'application_default_pdf', instance.send(:princess_default_stylesheet)
end

test "should return controller specific pdf stylesheet" do
instance = Foo.new
instance.params[:controller] = 'reports'

assert_equal 'reports_default_pdf', instance.send(:princess_default_stylesheet)
end


# princess_default_filename
test "should return princess_default_filename" do
instance = Foo.new
instance.params[:controller] = 'foo'
instance.params[:action] = 'bar'

assert_equal 'foo_bar.pdf', instance.send(:princess_default_filename)
end
end
20 changes: 20 additions & 0 deletions test/princess/app/controllers/erb_articles_controller.rb
@@ -0,0 +1,20 @@
#Controller who's views are all in the .html.erb format
class ErbArticlesController < ApplicationController

def show
@article = Article.find(params[:id])

respond_to do |format|
format.html
format.pdf { render :pdf => true }
end
end

def custom_one
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.pdf { render :pdf => 'alternate_custom_one', :filename => "custom_one.pdf" }
end
end
end
27 changes: 27 additions & 0 deletions test/princess/app/controllers/reports_controller.rb
@@ -0,0 +1,27 @@
class ReportsController < ApplicationController

REPMAPS = {
'address_labels_avery_5160' => {
:template => '/princess/avery_5160',
:partial => 'address_label',
:filename => 'address_labels',
:stylesheet => 'address_labels',
:layout => false
},
'nametags_avery_5390' => {
:template => '/princess/avery_5390',
:partial => 'nametag',
:filename => 'nametags',
:stylesheet => 'nametags',
:layout => false
}
}

def create
map = REPMAPS[params[:report][:format]]

respond_to do |format|
format.pdf { render :pdf => map.merge(:collection => %w(one two three four)) }
end
end
end
@@ -0,0 +1 @@
/* USED FOR TESTING */
Empty file.
30 changes: 1 addition & 29 deletions test/templates_test.rb
Expand Up @@ -3,36 +3,8 @@


require 'test_helper' require 'test_helper'


class ReportsController < ApplicationController

REPMAPS = {
'address_labels_avery_5160' => {
:template => '/princess/avery_5160',
:partial => 'address_label',
:filename => 'address_labels',
:stylesheet => 'address_labels',
:layout => false
},
'nametags_avery_5390' => {
:template => '/princess/avery_5390',
:partial => 'nametag',
:filename => 'nametags',
:stylesheet => 'nametags',
:layout => false
}
}

def create
map = REPMAPS[params[:report][:format]]

respond_to do |format|
format.pdf { render :pdf => map.merge(:collection => %w(one two three four)) }
end
end
end


class ReportsControllerTest < ActionController::TestCase class ReportsControllerTest < ActionController::TestCase

test "should create address labels report" do test "should create address labels report" do
post :create, :report => {:format => 'address_labels_avery_5160'}, :format => 'pdf' post :create, :report => {:format => 'address_labels_avery_5160'}, :format => 'pdf'
assert_response :success assert_response :success
Expand Down

0 comments on commit b241031

Please sign in to comment.