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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
require 'spec_helper'
# Tests use of the Generator when used like so:
#
# IniParse::Generator.gen do |doc|
# doc.comment('My very own comment')
# doc.section('my_section') do |section|
# section.option('my_option', 'my value')
# end
# end
#
describe 'When generating a document using Generator with section blocks,' do
it 'should be able to compile an empty document' do
expect { IniParse::Generator.gen { |doc| } }.not_to raise_error
end
it 'should raise LocalJumpError if no block is given' do
expect { IniParse::Generator.gen }.to raise_error(LocalJumpError)
end
it "should yield an object with generator methods" do
IniParse::Generator.gen do |doc|
%w( section option comment blank ).each do |meth|
expect(doc).to respond_to(meth)
end
end
end
# --
# ==========================================================================
# SECTION LINES
# ==========================================================================
# ++
describe 'adding a section' do
it 'should yield an object with generator methods' do
IniParse::Generator.gen do |doc|
doc.section("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.section("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.section("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.section("a section") { |section| }
expect(doc.context).to be_kind_of(IniParse::Document)
end
end
it "should use the parent document's options as a base" do
document = IniParse::Generator.gen(:indent => ' ') do |doc|
doc.section("a section") { |section| }
end
expect(document["a section"].to_ini).to match(/\A /)
end
it 'should pass extra options to the Section instance' do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :indent => ' ') { |section| }
end
expect(document["a section"].to_ini).to match(/\A /)
end
it 'should append a blank line to the document, after the section' do
expect(IniParse::Generator.gen do |doc|
doc.section("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.section("a section") do |section_one|
section_one.section("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 "should add the option to an __anonymous__ section" do
document = IniParse::Generator.gen do |doc|
doc.option("my option", "a value")
end
expect(document['__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.section("a section") do |section|
section.option("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
it 'should pass extra options to the Option instance' do
document = IniParse::Generator.gen do |doc|
doc.section("a section") do |section|
section.option("my option", "a value", :indent => " ")
end
end
expect(document["a section"].option("my option").to_ini).to match(/^ /)
end
it "should use the parent document's options as a base" do
document = IniParse::Generator.gen(:indent => " ") do |doc|
doc.section("a section") do |section|
section.option("my option", "a value")
end
end
expect(document["a section"].option("my option").to_ini).to match(/^ /)
end
it "should use the parent section's options as a base" do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :indent => " ") do |section|
section.option("my option", "a value")
end
end
expect(document["a section"].option("my option").to_ini).to match(/^ /)
end
it "should allow customisation of the parent's options" do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :indent => " ") do |section|
section.option("my option", "a value", {
:comment_sep => "#", :comment => 'a comment'
})
end
end
option_ini = document["a section"].option("my option").to_ini
expect(option_ini).to match(/^ /)
expect(option_ini).to match(/ # a comment/)
end
it "should not use the parent section's comment when setting line options" do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :comment => "My section") do |section|
section.option("my option", "a value")
end
end
expect(document["a section"].option("my option").to_ini).not_to match(/My section$/)
end
end
end
# --
# ==========================================================================
# COMMENT LINES
# ==========================================================================
# ++
describe 'adding a comment' do
it 'should pass extra options to the Option instance' do
document = IniParse::Generator.gen do |doc|
doc.comment("My comment", :indent => ' ')
end
expect(document.lines.to_a.first.to_ini).to match(/\A /)
end
it 'should ignore any extra :comment option' do
document = IniParse::Generator.gen do |doc|
doc.comment("My comment", :comment => 'Ignored')
end
expect(document.lines.to_a.first.to_ini).to match(/My comment/)
expect(document.lines.to_a.first.to_ini).not_to match(/Ignored/)
end
describe 'when the context is a Document' do
it 'should add a comment to the document' do
document = IniParse::Generator.gen do |doc|
doc.comment("My comment")
end
comment = document.lines.to_a.first
expect(comment).to be_kind_of(IniParse::Lines::Comment)
expect(comment.to_ini).to match(/My comment/)
end
it 'should use the default line options as a base' do
document = IniParse::Generator.gen do |doc|
doc.comment("My comment")
end
comment_ini = document.lines.to_a.first.to_ini
# Match separator (;) and offset (0).
expect(comment_ini).to eq('; My comment')
end
end
describe 'when the context is a Section' do
it 'should add a comment to the section' do
document = IniParse::Generator.gen do |doc|
doc.section("a section") do |section|
section.comment("My comment")
end
end
comment = document['a section'].lines.to_a.first
expect(comment).to be_kind_of(IniParse::Lines::Comment)
expect(comment.to_ini).to match(/My comment/)
end
it "should use the parent document's line options as a base" do
document = IniParse::Generator.gen(:comment_offset => 5) do |doc|
doc.section("a section") do |section|
section.comment("My comment")
end
end
expect(document['a section'].lines.to_a.first.to_ini).to match(/^ ;/)
end
it "should use the parent section's line options as a base" do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :comment_offset => 5) do |section|
section.comment("My comment")
end
end
expect(document['a section'].lines.to_a.first.to_ini).to match(/^ ;/)
end
it "should allow customisation of the parent's options" do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :comment_offset => 5) do |section|
section.comment("My comment", :comment_sep => "#")
end
end
# Match separator (#) and offset (5)
expect(document['a section'].lines.to_a.first.to_ini).to \
eq(' # My comment')
end
it "should not use the parent section's comment when setting line options" do
document = IniParse::Generator.gen do |doc|
doc.section("a section", :comment => "My section") do |section|
section.comment("My comment")
end
end
comment_ini = document['a section'].lines.to_a.first.to_ini
expect(comment_ini).to match(/My comment/)
expect(comment_ini).not_to match(/My section/)
end
end
end
# --
# ==========================================================================
# BLANK LINES
# ==========================================================================
# ++
describe 'adding a blank line' do
it 'should add a blank line to the document when it is the context' do
document = IniParse::Generator.gen do |doc|
doc.blank
end
expect(document.lines.to_a.first).to be_kind_of(IniParse::Lines::Blank)
end
it 'should add a blank line to the section when it is the context' do
document = IniParse::Generator.gen do |doc|
doc.section("a section") do |section|
section.blank
end
end
expect(document['a section'].lines.to_a.first).to be_kind_of(IniParse::Lines::Blank)
end
end
end
|