1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
# encoding: utf-8
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
describe "Prawn::Document#transaction" do
it "should properly commit if no error is raised" do
pdf = Prawn::Document.new do
transaction do
text "This is shown"
end
end
text = PDF::Inspector::Text.analyze(pdf.render)
text.strings.should == ["This is shown"]
end
it "should not display text if transaction is rolled back" do
pdf = Prawn::Document.new do
transaction do
text "This is not shown"
rollback
end
end
text = PDF::Inspector::Text.analyze(pdf.render)
text.strings.should == []
end
it "should return true/false value indicating success of the transaction" do
Prawn::Document.new do
success = transaction { }
success.should == true
success = transaction { rollback }
success.should == false
end
end
it "should support nested transactions" do
pdf = Prawn::Document.new do
transaction do
text "This is shown"
transaction do
text "and this is not"
rollback
end
text "and this is"
end
end
text = PDF::Inspector::Text.analyze(pdf.render)
text.strings.should == ["This is shown", "and this is"]
end
it "should allow rollback of multiple pages" do
pdf = Prawn::Document.new do
transaction do
5.times { start_new_page }
text "way out there and will never be shown"
rollback
end
text "This is the real text, only one page"
end
pages = PDF::Inspector::Page.analyze(pdf.render).pages
pages.size.should == 1
end
it "should not propagate a RollbackTransaction outside its bounds" do
def add_lines(pdf)
100.times { |i| pdf.text "Line #{i}" }
end
Prawn::Document.new do |pdf|
lambda do
begin
pdf.group { add_lines(pdf) }
rescue Prawn::Errors::CannotGroup
add_lines(pdf)
end
end.should.not.raise#(Prawn::Document::Snapshot::RollbackTransaction)
end
end
# Because the Pages object, when restored, points to the snapshotted pages
# by identifier, we have to restore the snapshot into the same page objects,
# or else old pages will appear in the post-rollback document.
it "should restore the pages into the same objects" do
Prawn::Document.new do
old_page_object_id = state.page.dictionary.identifier
old_page_content_id = state.page.content.identifier
transaction do
start_new_page
rollback
end
state.page.dictionary.identifier.should == old_page_object_id
state.page.content.identifier.should == old_page_content_id
end
end
it "page object should refer to the page_content object after restore" do
Prawn::Document.new do
transaction do
start_new_page
rollback
end
# should be the exact same object, not a clone
state.page.dictionary.data[:Contents].should == state.page.content
end
end
it "should restore bounds on rollback" do
Prawn::Document.new(:page_layout => :landscape) do
size = [bounds.width, bounds.height]
transaction do
start_new_page :layout => :portrait
rollback
end
[bounds.width, bounds.height].should == size
end
end
it "should set new bounding box on start_new_page with different layout" do
Prawn::Document.new(:page_layout => :landscape) do
size = [bounds.width, bounds.height]
transaction do
start_new_page
rollback
end
start_new_page :layout => :portrait
[bounds.width, bounds.height].should == size.reverse
end
end
it "should work with dests" do
Prawn::Document.new do |pdf|
pdf.add_dest("dest", pdf.dest_fit_horizontally(pdf.cursor, pdf.page))
pdf.text("Hello world")
lambda { pdf.transaction{} }.should.not.raise
end
end
describe "with a stamp dictionary present" do
it "should properly commit if no error is raised" do
pdf = Prawn::Document.new do
create_stamp("test_stamp") { draw_text "This is shown", :at => [0,0] }
transaction do
stamp("test_stamp")
end
end
pdf.render.should =~ /\/Stamp1 Do/
end
it "should properly rollback when #rollback is called" do
pdf = Prawn::Document.new do
create_stamp("test_stamp") { draw_text "This is not shown", :at => [0,0] }
transaction do
stamp("test_stamp")
rollback
end
end
pdf.render.should.not =~ /\/Stamp1 Do/
end
end
it "should restore page_number on rollback" do
Prawn::Document.new do
transaction do
5.times { start_new_page }
rollback
end
page_number.should == 1
end
end
end
|