spec/unit/template_spec.rb in pdf_ravager-0.1.0 vs spec/unit/template_spec.rb in pdf_ravager-0.2.0

- old
+ new

@@ -1,61 +1,111 @@ require File.dirname(__FILE__) + '/unit_helper' -require 'pdf_ravager/template' -class TestTemplate < MiniTest::Unit::TestCase +describe PDFRavager::Template do - def setup - @template = PDFRavager::Template.new do |t| - t.text 'text', 'foo' - t.rich_text 'rich_text', '<b>foo</b>' - t.check 'checkbox1' - t.uncheck 'checkbox2' - t.checkbox_group 'cbox_group' do |cb| - cb.check 'checked' - cb.uncheck 'unchecked' + context 'initialization' do + context 'template with a name' do + let(:template) { PDFRavager::Template.new('template') } + + it 'sets the name' do + expect(template.name).to eq('template') end - t.fill 'radio_group', 'button' - t.radio_group 'better_radio_group' do |rg| - rg.fill 'button' + end + + context 'template without a name' do + let(:template) { PDFRavager::Template.new } + + it 'does not set the name' do + expect(template.name).to be_nil end end - @template_with_name = PDFRavager::Template.new('template'){} - end + context 'setting strategy' do + context 'with a valid strategy' do + let(:template) { PDFRavager::Template.new(:strategy => :xfa) } - def test_that_text_is_set - assert_includes @template.fields, PDFRavager::Fields::Text.new('text', 'foo') - end + it 'sets the strategy' do + expect(template.strategy).to eq(:xfa) + end + end - def test_that_rich_text_is_set - assert_includes @template.fields, PDFRavager::Fields::RichText.new('rich_text', '<b>foo</b>') + context 'with an invalid strategy' do + it 'raises an error' do + expect { PDFRavager::Template.new(:strategy => :foo) }.to raise_error + end + end + end end - def test_that_checkbox_is_set - assert_includes @template.fields, PDFRavager::Fields::Checkbox.new('checkbox1', true) - end + context 'setting template values' do - def test_that_checkbox_is_unset - assert_includes @template.fields, PDFRavager::Fields::Checkbox.new('checkbox2', false) - end + let(:template) { + PDFRavager::Template.new do |t| + t.text 'text', 'foo' + t.rich_text 'rich_text', '<b>foo</b>' + t.check 'checkbox1' + t.uncheck 'checkbox2' + t.checkbox_group 'cbox_group' do |cb| + cb.check 'checked' + cb.uncheck 'unchecked' + end + t.fill 'radio_group', 'button' + t.radio_group 'better_radio_group' do |rg| + rg.fill 'button' + end + end + } - def test_that_radio_button_is_filled - assert_includes @template.fields, PDFRavager::Fields::Radio.new('radio_group', 'button') - end + context 'text' do + it 'is set' do + expect(template.fields).to include(PDFRavager::Fields::Text.new('text', 'foo')) + end + end - def test_that_name_is_set - assert_equal @template_with_name.name, 'template' - end + context 'rich_text' do + it 'is set' do + expect(template.fields).to include(PDFRavager::Fields::RichText.new('rich_text', '<b>foo</b>')) + end + end - def test_that_checkbox_group_box_is_checked - assert_includes @template.fields, PDFRavager::Fields::Checkbox.new('cbox_group.checked', true) - end + context 'checkbox1' do + it 'is set' do + expect(template.fields).to include(PDFRavager::Fields::Checkbox.new('checkbox1', true)) + end + end - def test_that_checkbox_group_box_is_unchecked - assert_includes @template.fields, PDFRavager::Fields::Checkbox.new('cbox_group.unchecked', false) - end + context 'checkbox2' do + it 'is unset' do + expect(template.fields).to include(PDFRavager::Fields::Checkbox.new('checkbox2', false)) + end + end - def test_that_radio_group_button_is_filled - assert_includes @template.fields, PDFRavager::Fields::Radio.new('better_radio_group', 'button') + context 'cbox_group' do + context '.checked' do + it 'is checked' do + expect(template.fields).to include(PDFRavager::Fields::Checkbox.new('cbox_group.checked', true)) + end + end + + context '.unchecked' do + it 'is unchecked' do + expect(template.fields).to include(PDFRavager::Fields::Checkbox.new('cbox_group.unchecked', false)) + end + end + end + + context 'radio buttons' do + context 'one-line syntax' do + it 'fills the radio_group button' do + expect(template.fields).to include(PDFRavager::Fields::Radio.new('radio_group', 'button')) + end + end + + context 'block syntax' do + it 'fills the better_radio_group button' do + expect(template.fields).to include(PDFRavager::Fields::Radio.new('better_radio_group', 'button')) + end + end + end end end