Permalink
Browse files

add tests for generate pdf private methods

  • Loading branch information...
1 parent acc77b9 commit b241031f33623fc204fd12f2ba153c9b57b13303 @aaron aaron committed May 16, 2011
@@ -82,10 +82,10 @@ def make_pdf(options = {}) #template_path, pdf_name, landscape=false)
#appends the filename with the given suffix if necessary.
# Examples:
- # * append_suffix(:tps_report,:pdf) => 'tps_report.pdf'
- # * append_suffix('my_styles.css',:css) => 'my_styles.css'
- def append_suffix(filename,suffix)
- return filename.match(/\.#{suffix}$/) ? filename.to_s : "#{filename}.#{suffix}"
+ # * append_suffix(:tps_report, :pdf) => 'tps_report.pdf'
+ # * append_suffix('my_styles.css', :css) => 'my_styles.css'
+ def append_suffix(filename, suffix)
+ return filename.match(/\.#{suffix}$/) ? filename.to_s : "#{filename}.#{suffix}"
end
def princess_default_stylesheet
View
@@ -3,53 +3,29 @@
require 'test_helper'
-#Controller who's views are all in the .html.erb format
-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
+class ErbArticlesControllerTest < ActionController::TestCase
def setup
prepop_db
- @controller = ErbArticlesController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
end
# show
- def test_should_get_show_and_return_html
+ test "should get show and return html" do
get :show, :id => 1
assert_response :success
assert !@response.body.match(/^%PDF/)
end
- def test_should_generate_pdf_with_no_argugments
+ test "should generate pdf with no argugments" do
get :show, :id => 1, :format => 'pdf'
assert_response :success
assert @response.body.match(/^%PDF/)
end
# custom_one
- def test_articles_custom_one
+ test "should get alternate_custom_one template" do
get :custom_one, :id => 1
assert_select 'h1', 'Article1'
get :custom_one, {:id => 1, :format => 'pdf'}
View
@@ -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
@@ -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
@@ -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 */
View
@@ -3,36 +3,8 @@
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
+
test "should create address labels report" do
post :create, :report => {:format => 'address_labels_avery_5160'}, :format => 'pdf'
assert_response :success

0 comments on commit b241031

Please sign in to comment.