test/unit/report/test_internal.rb in thinreports-0.7.5 vs test/unit/report/test_internal.rb in thinreports-0.7.6

- old
+ new

@@ -6,229 +6,242 @@ include ThinReports::TestHelpers # Alias Report = ThinReports::Report - def setup - # Setting for instance of Layout::Base as mock. - layout = flexmock('layout_base') - layout.should_receive(:config).and_return(flexmock('layout_configuration')) - - flexmock(ThinReports::Layout). - should_receive(:new).and_return(layout) + def report + Report::Base.new end - - def test_initialize_with_default_layout - report = init_report(:layout => 'default.tlf') - assert_equal report.default_layout.flexmock_name, 'layout_base' + + def sample_layout1 + data_file('basic_layout1.tlf') end - - def test_register_layout_properly_set_the_default_layout - report = init_report - report.register_layout('default.tlf') - - assert_equal report.default_layout.flexmock_name, 'layout_base' + + def sample_layout2 + data_file('basic_layout2.tlf') end + + def test_layout_specified_in_new_method_should_be_defined_as_default_layout + internal = Report::Internal.new(report, :layout => sample_layout1) + assert_equal internal.default_layout.filename, sample_layout1 + end + + def test_register_layout_should_be_set_as_default_layout_when_options_are_omitted + internal = Report::Internal.new(report, {}) + internal.register_layout(sample_layout1) + + assert_equal internal.default_layout.filename, sample_layout1 + end - def test_register_layout_properly_set_the_default_layout_with_option - report = init_report - report.register_layout('default.tlf', :default => true) + def test_register_layout_should_be_set_as_default_layout_when_default_option_is_true + internal = Report::Internal.new(report, {}) + internal.register_layout(sample_layout1, :default => true) - assert_equal report.default_layout.flexmock_name, 'layout_base' + assert_equal internal.default_layout.filename, sample_layout1 end - def test_register_layout_raise_when_default_layout_is_already_set - assert_raises ArgumentError do - init_report(:layout => 'default.tlf').register_layout('other.tlf') - end + def test_register_layout_should_be_able_to_change_the_default_layout + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.register_layout(sample_layout2, :default => true) + + assert_equal internal.default_layout.filename, sample_layout2 end - def test_register_layout_properly_set_the_layout - report = init_report - report.register_layout('foo.tlf', :id => :foo) - - assert_equal report.layout_registry[:foo].flexmock_name, 'layout_base' + def test_register_layout_should_be_set_as_with_id_when_id_option_is_set + internal = Report::Internal.new(report, {}) + internal.register_layout(sample_layout1, :id => :foo) + + assert_equal internal.layout_registry[:foo].filename, sample_layout1 end - def test_register_layout_raise_when_specified_id_is_already_registered - report = init_report - report.register_layout('foo.tlf', :id => :foo) + def test_register_layout_should_raise_an_error_when_id_is_already_registered + internal = Report::Internal.new(report, {}) + internal.register_layout(sample_layout2, :id => :foo) assert_raises ArgumentError do - report.register_layout('hoge.tlf', :id => :foo) + internal.register_layout(sample_layout1, :id => :foo) end end - def test_register_layout_call_layout_config_method_with_block - report = init_report - assert_equal report.register_layout('layout.tlf').flexmock_name, - 'layout_configuration' + def test_register_layout_should_return_the_instance_of_LayoutConfiguration + internal = Report::Internal.new(report, {}) + + assert_instance_of ThinReports::Layout::Configuration, + internal.register_layout(sample_layout1) end - def test_add_page_count_up_the_page_count - new_page = proc { - mock_page.should_receive(:finalize).mock - } - - report = init_report - assert_equal report.page_count, 0 - - report.add_page(new_page.call) - assert_equal report.page_count, 1 + def test_add_page_should_finalize_the_current_page + layout = ThinReports::Layout.new(sample_layout1) - report.add_page(new_page.call) - assert_equal report.page_count, 2 + internal = Report::Internal.new(report, :layout => sample_layout1) + page = internal.add_page(ThinReports::Core::Page.new(report, layout)) + internal.add_page(ThinReports::Core::Page.new(report, layout)) + + assert_equal page.finalized?, true end - - def test_add_page_finalize_current_page - report = init_report - - report.add_page(mock_page.should_receive(:finalize).once.mock) - report.add_page(mock_page) + + def test_add_page_should_return_the_current_page + layout = ThinReports::Layout.new(sample_layout1) + new_page = ThinReports::Core::Page.new(report, layout) + + internal = Report::Internal.new(report, :layout => sample_layout1) + + assert_same new_page, internal.add_page(new_page) end - - def test_add_page_switch_currently_page_of_report - new_page = proc {|name| - mock_page(name).should_receive(:finalize).mock - } - - report = init_report - - report.add_page(new_page.call('page1')) - assert_equal report.page.flexmock_name, 'page1' - - report.add_page(new_page.call('page2')) - assert_equal report.page.flexmock_name, 'page2' + + def test_add_page_should_add_the_initialized_page + layout = ThinReports::Layout.new(sample_layout1) + new_page = ThinReports::Core::Page.new(report, layout) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(new_page) + + assert_same new_page, internal.pages.last end - - def test_add_page_store_specified_page_to_report_pages - report = init_report - report.add_page(new_page = mock_page) + + def test_add_page_should_count_up_the_total_page_count + layout = ThinReports::Layout.new(sample_layout1) - assert_includes report.pages, new_page + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(ThinReports::Core::Page.new(report, layout)) + + assert_equal internal.page_count, 1 end - def test_add_page_dispatch_the_page_create_event - pass = false - report = init_report - report.events.on(:page_create) {|e| pass = true } - - report.add_page(mock_page) - - assert_equal pass, true - end - - def test_add_blank_page_not_dispatch_the_page_create_event - pass = true - report = init_report - report.events.on(:page_create) {|e| pass = false } - - report.add_page(mock_blank_page) - - assert_equal pass, true + def test_add_page_should_switch_to_a_reference_to_the_current_page + layout = ThinReports::Layout.new(sample_layout1) + new_pages = (1..2).inject([]) do |pages, i| + pages << ThinReports::Core::Page.new(report, layout) + end + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(new_pages[0]) + + assert_same internal.page, new_pages[0] + + internal.add_page(new_pages[1]) + + assert_same internal.page, new_pages[1] end - - def test_add_blank_page_not_count_up_the_page_count_when_count_is_disabled - report = init_report - report.add_page(mock_not_counting_blank_page) - report.add_page(mock_not_counting_blank_page) - assert_equal report.page_count, 0 + def test_add_page_should_dispatch_the_event_page_creation + dispatched = false + layout = ThinReports::Layout.new(sample_layout1) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.events.on(:page_create) {|e| dispatched = true } + internal.add_page(ThinReports::Core::Page.new(report, layout)) + + assert dispatched end - - def test_finalize - pass = false - - report = init_report - report.events.on(:generate) {|e| pass = true } - - report.add_page(mock_page.should_receive(:finalize).once.mock) - report.finalize - - assert_equal report.finalized?, true - assert_equal pass, true + + def test_add_blank_page_should_not_dispatch_the_event_page_creation + dispatched = false + layout = ThinReports::Layout.new(sample_layout1) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.events.on(:page_create) {|e| dispatched = true } + internal.add_page(ThinReports::Core::BlankPage.new) + + refute dispatched end - def test_finalize_never_work_when_report_is_already_finalized - report = init_report - report.finalize - - flexmock(report).should_receive(:finalize_current_page).times(0) - report.finalize + def test_add_blank_page_should_not_count_up_the_total_page_count_when_count_is_disabled + layout = ThinReports::Layout.new(sample_layout1) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(ThinReports::Core::BlankPage.new(false)) + + assert_equal internal.page_count, 0 end - - def test_finalize_not_finalize_current_page_when_page_is_blank - report = init_report - report.add_page(mock_blank_page.should_receive(:finalize).times(0).mock) - report.finalize + + def test_add_blank_page_should_count_up_the_total_page_count_when_count_is_enabled + layout = ThinReports::Layout.new(sample_layout1) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(ThinReports::Core::BlankPage.new) + + assert_equal internal.page_count, 1 end - def test_load_layout_with_String - report = init_report - assert_equal report.load_layout('layout.tlf').flexmock_name, 'layout_base' + def test_finalize_should_dispatch_the_event_report_generation + dispatched = false + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.events.on(:generate) {|e| dispatched = true } + internal.finalize + + assert dispatched end - - def test_load_layout_with_id - report = init_report - report.register_layout('layout.tlf', :id => :foo) - - assert_equal report.load_layout(:foo).flexmock_name, 'layout_base' + + def test_finalize_should_finalize_the_report + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.finalize + + assert internal.finalized? end - - def test_load_layout_with_unknown_id - report = init_report - assert_nil report.load_layout(:unknown) + + def test_finalize_should_not_work_when_report_is_already_finalized + internal = Report::Internal.new(report, :layout => sample_layout1) + + flexmock(internal). + should_receive(:finalize_current_page).once + + internal.finalize + internal.finalize end - - def test_load_layout_raise_when_invalid_value_set - report = init_report - assert_raises ArgumentError do - report.load_layout(1) - end - assert_raises ArgumentError do - report.load_layout(ThinReports::Layout.new('layout.tlf')) - end + + def test_finalized_should_return_true_when_report_is_already_finalized + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.finalize + + assert internal.finalized? end - def test_load_layout_with_nil - report = init_report - report.register_layout('default.tlf', :default => true) + def test_load_layout_with_String + internal = Report::Internal.new(report, :layout => sample_layout1) - assert_equal report.load_layout(nil).flexmock_name, 'layout_base' + assert_equal internal.load_layout(sample_layout2).filename, + sample_layout2 end - def test_copy_page - page = mock_page - - flexmock(page). - should_receive(:finalize).with(:at => :copy).once. - should_receive(:copy).and_return(mock_page('copied_page')).once - - report = init_report - report.add_page(page) - report.copy_page - - assert_equal report.page.flexmock_name, 'copied_page' + def test_load_layout_with_id + internal = Report::Internal.new(report, {}) + internal.register_layout(sample_layout1, :id => :sample) + + assert_equal internal.load_layout(:sample).filename, + sample_layout1 end - - def init_report(options = {}) - Report::Internal.new(flexmock('report_base'), options) - end - def mock_page(page_name = 'page') - flexmock(page_name). - should_receive(:count? => true, :blank? => false). - should_receive(:no=).with(::Numeric).once.mock + def test_load_layout_with_unknown_id + internal = Report::Internal.new(report, {}) + assert_nil internal.load_layout(:unknown) end - def mock_blank_page - flexmock('blank_page'). - should_receive(:count? => true, :blank? => true). - should_receive(:no=).with(::Numeric).once.mock + def test_load_layout_should_raise_error_when_invalid_value_set + internal = Report::Internal.new(report, {}) + + assert_raises ThinReports::Errors::LayoutFileNotFound do + internal.load_layout('/path/to/unkown.tlf') + end end - def mock_not_counting_blank_page - flexmock('blank_page'). - should_receive(:count? => false, :blank? => true). - should_receive(:no=).with(::Numeric).times(0).mock + def test_copy_page_should_finalize_current_page + layout = ThinReports::Layout.new(sample_layout1) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(page = ThinReports::Core::Page.new(report, layout)) + internal.copy_page + + assert page.finalized? + end + + def test_copy_page_should_add_the_copied_page + layout = ThinReports::Layout.new(sample_layout1) + + internal = Report::Internal.new(report, :layout => sample_layout1) + internal.add_page(page = ThinReports::Core::Page.new(report, layout)) + internal.copy_page + + assert_equal internal.page_count, 2 end end