Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add tests for generate pdf private methods

  • Loading branch information...
commit b241031f33623fc204fd12f2ba153c9b57b13303 1 parent acc77b9
Aaron Baldwin aaron authored
8 lib/princess/generate_pdf.rb
@@ -82,10 +82,10 @@ def make_pdf(options = {}) #template_path, pdf_name, landscape=false)
82 82
83 83 #appends the filename with the given suffix if necessary.
84 84 # Examples:
85   - # * append_suffix(:tps_report,:pdf) => 'tps_report.pdf'
86   - # * append_suffix('my_styles.css',:css) => 'my_styles.css'
87   - def append_suffix(filename,suffix)
88   - return filename.match(/\.#{suffix}$/) ? filename.to_s : "#{filename}.#{suffix}"
  85 + # * append_suffix(:tps_report, :pdf) => 'tps_report.pdf'
  86 + # * append_suffix('my_styles.css', :css) => 'my_styles.css'
  87 + def append_suffix(filename, suffix)
  88 + return filename.match(/\.#{suffix}$/) ? filename.to_s : "#{filename}.#{suffix}"
89 89 end
90 90
91 91 def princess_default_stylesheet
32 test/erb_views_test.rb
@@ -3,45 +3,21 @@
3 3
4 4 require 'test_helper'
5 5
6   -#Controller who's views are all in the .html.erb format
7   -class ErbArticlesController < ActionController::Base
8   -
9   - def show
10   - @article = Article.find(params[:id])
11   -
12   - respond_to do |format|
13   - format.html
14   - format.pdf { render :pdf => true }
15   - end
16   - end
17   -
18   - def custom_one
19   - @article = Article.find(params[:id])
20   - respond_to do |format|
21   - format.html
22   - format.pdf { render :pdf => 'alternate_custom_one', :filename => "custom_one.pdf" }
23   - end
24   - end
25   -end
26   -
27   -class ErbViewsTest < ActionController::TestCase
  6 +class ErbArticlesControllerTest < ActionController::TestCase
28 7
29 8 def setup
30 9 prepop_db
31   - @controller = ErbArticlesController.new
32   - @request = ActionController::TestRequest.new
33   - @response = ActionController::TestResponse.new
34 10 end
35 11
36 12
37 13 # show
38   - def test_should_get_show_and_return_html
  14 + test "should get show and return html" do
39 15 get :show, :id => 1
40 16 assert_response :success
41 17 assert !@response.body.match(/^%PDF/)
42 18 end
43 19
44   - def test_should_generate_pdf_with_no_argugments
  20 + test "should generate pdf with no argugments" do
45 21 get :show, :id => 1, :format => 'pdf'
46 22 assert_response :success
47 23 assert @response.body.match(/^%PDF/)
@@ -49,7 +25,7 @@ def test_should_generate_pdf_with_no_argugments
49 25
50 26
51 27 # custom_one
52   - def test_articles_custom_one
  28 + test "should get alternate_custom_one template" do
53 29 get :custom_one, :id => 1
54 30 assert_select 'h1', 'Article1'
55 31 get :custom_one, {:id => 1, :format => 'pdf'}
54 test/generate_pdf_test.rb
... ... @@ -0,0 +1,54 @@
  1 +#!/usr/bin/env ruby
  2 +# encoding: UTF-8
  3 +
  4 +require 'test_helper'
  5 +
  6 +class Foo
  7 + include Princess::GeneratePdf
  8 +
  9 + def params
  10 + @params ||= Hash.new
  11 + end
  12 +end
  13 +
  14 +class GeneratePdfTest < ActiveSupport::TestCase
  15 +
  16 + # append_suffix
  17 + test "should append suffix" do
  18 + instance = Foo.new
  19 +
  20 + assert_equal 'foo.bar', instance.send(:append_suffix, 'foo', :bar)
  21 + end
  22 +
  23 + test "should not append when suffix is already present" do
  24 + instance = Foo.new
  25 +
  26 + assert_equal 'foo.bar', instance.send(:append_suffix, 'foo.bar', :bar)
  27 + end
  28 +
  29 +
  30 + # princess_default_stylesheet
  31 + test "should return application pdf stylesheet" do
  32 + instance = Foo.new
  33 + instance.params[:controller] = 'does_not_exist'
  34 +
  35 + assert_equal 'application_default_pdf', instance.send(:princess_default_stylesheet)
  36 + end
  37 +
  38 + test "should return controller specific pdf stylesheet" do
  39 + instance = Foo.new
  40 + instance.params[:controller] = 'reports'
  41 +
  42 + assert_equal 'reports_default_pdf', instance.send(:princess_default_stylesheet)
  43 + end
  44 +
  45 +
  46 + # princess_default_filename
  47 + test "should return princess_default_filename" do
  48 + instance = Foo.new
  49 + instance.params[:controller] = 'foo'
  50 + instance.params[:action] = 'bar'
  51 +
  52 + assert_equal 'foo_bar.pdf', instance.send(:princess_default_filename)
  53 + end
  54 +end
20 test/princess/app/controllers/erb_articles_controller.rb
... ... @@ -0,0 +1,20 @@
  1 +#Controller who's views are all in the .html.erb format
  2 +class ErbArticlesController < ApplicationController
  3 +
  4 + def show
  5 + @article = Article.find(params[:id])
  6 +
  7 + respond_to do |format|
  8 + format.html
  9 + format.pdf { render :pdf => true }
  10 + end
  11 + end
  12 +
  13 + def custom_one
  14 + @article = Article.find(params[:id])
  15 + respond_to do |format|
  16 + format.html
  17 + format.pdf { render :pdf => 'alternate_custom_one', :filename => "custom_one.pdf" }
  18 + end
  19 + end
  20 +end
27 test/princess/app/controllers/reports_controller.rb
... ... @@ -0,0 +1,27 @@
  1 +class ReportsController < ApplicationController
  2 +
  3 + REPMAPS = {
  4 + 'address_labels_avery_5160' => {
  5 + :template => '/princess/avery_5160',
  6 + :partial => 'address_label',
  7 + :filename => 'address_labels',
  8 + :stylesheet => 'address_labels',
  9 + :layout => false
  10 + },
  11 + 'nametags_avery_5390' => {
  12 + :template => '/princess/avery_5390',
  13 + :partial => 'nametag',
  14 + :filename => 'nametags',
  15 + :stylesheet => 'nametags',
  16 + :layout => false
  17 + }
  18 + }
  19 +
  20 + def create
  21 + map = REPMAPS[params[:report][:format]]
  22 +
  23 + respond_to do |format|
  24 + format.pdf { render :pdf => map.merge(:collection => %w(one two three four)) }
  25 + end
  26 + end
  27 +end
1  test/princess/public/stylesheets/application_default_pdf.css
... ... @@ -0,0 +1 @@
  1 +/* USED FOR TESTING */
0  test/princess/public/stylesheets/reports_default_pdf.css
No changes.
30 test/templates_test.rb
@@ -3,36 +3,8 @@
3 3
4 4 require 'test_helper'
5 5
6   -class ReportsController < ApplicationController
7   -
8   - REPMAPS = {
9   - 'address_labels_avery_5160' => {
10   - :template => '/princess/avery_5160',
11   - :partial => 'address_label',
12   - :filename => 'address_labels',
13   - :stylesheet => 'address_labels',
14   - :layout => false
15   - },
16   - 'nametags_avery_5390' => {
17   - :template => '/princess/avery_5390',
18   - :partial => 'nametag',
19   - :filename => 'nametags',
20   - :stylesheet => 'nametags',
21   - :layout => false
22   - }
23   - }
24   -
25   - def create
26   - map = REPMAPS[params[:report][:format]]
27   -
28   - respond_to do |format|
29   - format.pdf { render :pdf => map.merge(:collection => %w(one two three four)) }
30   - end
31   - end
32   -end
33   -
34   -
35 6 class ReportsControllerTest < ActionController::TestCase
  7 +
36 8 test "should create address labels report" do
37 9 post :create, :report => {:format => 'address_labels_avery_5160'}, :format => 'pdf'
38 10 assert_response :success

0 comments on commit b241031

Please sign in to comment.
Something went wrong with that request. Please try again.