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
|
require 'spec_helper'
# Tests use of the Generator when used like so:
#
# IniParse::Generator.gen do |doc|
# doc.comment 'My very own comment'
# doc.my_section do |section|
# section.my_option = 'my value'
# end
# end
#
describe 'When generating a document using Generator with section blocks using method_missing,' do
# --
# ==========================================================================
# SECTION LINES
# ==========================================================================
# ++
describe 'adding a section' do
it 'should yield an object with generator methods' do
IniParse::Generator.gen do |doc|
doc.a_section do |section|
%w( option comment blank ).each do |meth|
expect(section).to respond_to(meth)
end
end
end
end
it 'should add a Section to the document' do
expect(IniParse::Generator.gen do |doc|
doc.a_section { |section| }
end).to have_section("a_section")
end
it 'should change the Generator context to the section during the section block' do
IniParse::Generator.gen do |doc|
doc.a_section do |section|
expect(section.context).to be_kind_of(IniParse::Lines::Section)
expect(section.context.key).to eq("a_section")
end
end
end
it 'should reset the Generator context to the document after the section block' do
IniParse::Generator.gen do |doc|
doc.a_section { |section| }
expect(doc.context).to be_kind_of(IniParse::Document)
end
end
it 'should append a blank line to the document, after the section' do
expect(IniParse::Generator.gen do |doc|
doc.a_section { |section| }
end.lines.to_a.last).to be_kind_of(IniParse::Lines::Blank)
end
it 'should raise a LineNotAllowed if you attempt to nest a section' do
expect do
IniParse::Generator.gen do |doc|
doc.a_section do |section_one|
section_one.another_section { |section_two| }
end
end
end.to raise_error(IniParse::LineNotAllowed)
end
end
# --
# ==========================================================================
# OPTION LINES
# ==========================================================================
# ++
describe 'adding a option' do
describe 'when the context is a Document' do
it "adds the option to an __anonymous__ section" do
doc = IniParse::Generator.gen { |doc| doc.my_option = "a value" }
expect(doc['__anonymous__']['my_option']).to eql('a value')
end
end
describe 'when the context is a Section' do
it 'should add the option to the section' do
document = IniParse::Generator.gen do |doc|
doc.a_section do |section|
section.my_option = "a value"
end
end
section = document["a_section"]
expect(section).to have_option("my_option")
expect(section["my_option"]).to eq("a value")
end
end
end
# Comments and blanks are added in the same way as in the
# 'with_section_block_spec.rb' specification.
end
|