Replaced rfpdf with prawn and prawnto. Start to convert pdf-views.

This commit is contained in:
Benjamin Meichsner 2009-01-07 15:29:23 +01:00
parent da309f03b0
commit a6c7b04e33
165 changed files with 723 additions and 28123 deletions

View file

@ -0,0 +1,38 @@
require 'rubygems'
require 'action_controller'
require 'action_controller/test_process'
require 'action_view'
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/prawnto'
class ActionControllerTest < Test::Unit::TestCase
class PrawntoController < ActionController::Base
prawnto :inline=>true, :prawn=>{:page_orientation=>:landscape}
def test
prawnto :inline=>false, :prawn=>{:page_size=>'A4'}
end
end
def test_inheritable_options
assert_equal({:page_orientation=>:landscape}, PrawntoController.read_inheritable_attribute(:prawn))
assert_equal({:inline=>true}, PrawntoController.read_inheritable_attribute(:prawnto))
end
def test_computed_options
controller = PrawntoController.new
test_process(controller)
assert_equal({:inline=>false, :prawn=>{:page_orientation=>:landscape, :page_size=>'A4'}}, controller.send(:compute_prawnto_options))
end
protected
def test_process(controller, action = "test")
request = ActionController::TestRequest.new
request.action = action
controller.process(request, ActionController::TestResponse.new)
end
end

View file

@ -0,0 +1,39 @@
require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
require File.dirname(__FILE__) + '/../lib/prawnto'
#require File.dirname(__FILE__) + '/../init'
#TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
class BaseTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Base.new(@view)
@controller = @view.controller
end
def test_headers_disposition_inline_and_filename
@controller.prawnto :filename=>'xxx.pdf', :inline=>true
@handler.pull_prawnto_options
@handler.set_disposition
assert_equal 'inline;filename=xxx.pdf', @view.headers['Content-Disposition']
end
def test_headers_disposition_attachment_and_filename
@controller.prawnto :filename=>'xxx.pdf', :inline=>false
@handler.pull_prawnto_options
@handler.set_disposition
assert_equal 'attachment;filename=xxx.pdf', @view.headers['Content-Disposition']
end
def test_headers_disposition_default
@handler.pull_prawnto_options
@handler.set_disposition
assert_equal 'inline', @view.headers['Content-Disposition']
end
end

View file

@ -0,0 +1,40 @@
require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
require File.dirname(__FILE__) + '/../lib/prawnto'
#require File.dirname(__FILE__) + '/../init'
#TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
class DslTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Dsl.new(@view)
@controller = @view.controller
end
def test_prawnto_options_dsl_hash
@y = 3231; @x = 5322
@controller.prawnto :dsl=> {'x'=>:@x, :y=>'@y'}
@handler.pull_prawnto_options
source = @handler.build_source_to_establish_locals(Template.new(""))
assert_equal @x, eval(source + "\nx")
assert_equal @y, eval(source + "\ny")
end
def test_prawnto_options_dsl_array
@y = 3231; @x = 5322
@controller.prawnto :dsl=> ['x', :@y]
@handler.pull_prawnto_options
source = @handler.build_source_to_establish_locals(Template.new(""))
assert_equal @x, eval(source + "\nx")
assert_equal @y, eval(source + "\ny")
end
end

View file

@ -0,0 +1,163 @@
# uncomment and edit below if you want to get off gem version
#$LOAD_PATH.unshift '~/cracklabs/vendor/gems/prawn-0.0.0.1/lib/' #to force picup of latest prawn (instead of stable gem)
require 'rubygems'
require 'action_controller'
require 'action_view'
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/prawnto'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
class RawTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
class ::ApplicationHelper
end
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Raw.new(@view)
end
def test_massage_template_source_header_comments
expected_commented_lines = [0,2,3]
source = <<EOS
require 'prawn'
require 'hello'
require "rubygems"
$LOAD_PATH.unshift blah blah
LOAD_PATH.unshift blah blah
EOS
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.each_with_index do |line, i|
method = expected_commented_lines.include?(i) ? :assert_match : :assert_no_match
self.send method, /^\s*\#/, line
end
end
def test_massage_template_source_generate
@handler.pull_prawnto_options
changed_lines = [0,2,3]
source = <<EOS
Prawn::Document.generate('hello.pdf') do |pdf|
end
EOS
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
assert_match(/^\s*(\S+)\s*\=\s*Prawn\:\:Document\.new\(?\s*\)?\s*do\s*\|pdf\|/, output_lines.first)
variable = $1
assert_match(/^\s*\[(\S+)\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
assert_equal variable, $1
end
def test_massage_template_source_new
@handler.pull_prawnto_options
unchanged_lines = [0,1,2]
source = <<EOS
x = Prawn::Document.new do |pdf|
text.blah blah blah
end
x.render_file('hello.pdf')
EOS
source_lines = source.split("\n")
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.each_with_index do |line, i|
method = unchanged_lines.include?(i) ? :assert_equal : :assert_not_equal
self.send method, source_lines[i], line
end
assert_match(/^\s*\#\s*x\.render\_file\(\'hello.pdf\'\)/, output_lines[3])
assert_match(/^\s*\[\s*x\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
end
def test_massage_template_source_classes_methods
source = <<EOS
class Foo
def initialize
@foo = true
end
end
def bar(*args)
if args[0]==true
z = false
end
end
EOS
@handler.send :setup_run_environment
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.pop
output_lines.each {|l| assert_match(/^\s*$/, l)}
assert @handler.run_environment.methods(false).include?('bar')
assert class <<@handler.run_environment; self; end.constants.include?('Foo')
end
CURRENT_PATH = Pathname('.').realpath
PRAWN_PATH = Pathname(Prawn::BASEDIR).realpath
REFERENCE_PATH = Pathname('reference_pdfs').realpath
INPUT_PATH = PRAWN_PATH + 'examples'
IGNORE_LIST = %w(table_bench ruport_formatter page_geometry)
INPUTS = INPUT_PATH.children.select {|p| p.extname==".rb" && !IGNORE_LIST.include?(p.basename('.rb').to_s)}
def self.ensure_reference_pdfs_are_recent
head_lines = (INPUT_PATH + "../.git/HEAD").read.split("\n")
head_hash = Hash[*head_lines.map {|line| line.split(':').map{|v| v.strip}}.flatten]
head_version = (INPUT_PATH + "../.git" + head_hash['ref'])
REFERENCE_PATH.mkpath
current_version = REFERENCE_PATH + 'HEAD'
if !current_version.exist? || current_version.read!=head_version.read
puts "\n!!!! reference pdfs are determined to be old-- repopulating...\n\n"
require 'fileutils'
FileUtils.instance_eval do
rm REFERENCE_PATH + '*', :force=>true
INPUTS.each do |path|
pre_brood = INPUT_PATH.children
cd INPUT_PATH
system("ruby #{path.basename}")
post_brood = INPUT_PATH.children
new_kids = post_brood - pre_brood
new_kids.each {|p| mv p, REFERENCE_PATH + p.basename}
cd CURRENT_PATH
end
cp head_version, current_version
end
else
puts "\n reference pdfs are current-- continuing...\n"
end
end
#TODO: ruby 1.9: uncomment below line when on 1.9
#ensure_reference_pdfs_are_recent
def assert_renders_correctly(name, path)
input_source = path.read
output_source = @handler.compile(Template.new(input_source))
value = @view.instance_eval output_source
reference = (REFERENCE_PATH + @view.prawnto_options[:filename]).read
message = "template: #{name}\n"
message += ">"*30 + " original template: " + ">"*20 + "\n"
message += input_source + "\n"*2
message += ">"*30 + " manipulated template: " + ">"*20 + "\n"
message += output_source + "\n" + "<"*60 + "\n"
assert_equal reference, value, message
end
#!!! Can't actually verify pdf equality until ruby 1.9
# (cuz hash orders are messed up otherwise and no other way to test equality at the moment)
INPUTS.each do |path|
name = path.basename('.rb')
define_method "test_template_should_render_correctly [template: #{name}] " do
# assert_renders_correctly name, path
assert true
end
end
end

View file

@ -0,0 +1,77 @@
require 'rubygems'
require File.dirname(__FILE__) + '/../lib/prawnto'
module TemplateHandlerTestMocks
class Template
attr_reader :source, :locals, :filename
def initialize(source, locals={})
@source = source
@locals = locals
@filename = "blah.pdf"
end
end
class Response
def initialize
@headers = {}
end
def headers
@headers
end
def content_type=(value)
end
end
class Request
def env
{}
end
end
class ActionController
include Prawnto::ActionController
def response
@response ||= Response.new
end
def request
@request ||= Request.new
end
def headers
response.headers
end
end
class ActionView
def controller
@controller ||= ActionController.new
end
def response
controller.response
end
def request
controller.request
end
def headers
controller.headers
end
def prawnto_options
controller.get_instance_variable(:@prawnto_options)
end
end
end