require 'spec_helper'

# Tests use of the Generator when used like so:
#
#   @gen = IniParse::Generator.new
#   @gen.comment('My very own comment')
#   @gen.section('my_section')
#   @gen.option('my_option', 'my value')
#   ...
#
# Or
#
#   IniParse::Generator.gen do |doc|
#     doc.comment('My very own comment')
#     doc.section('my_section')
#     doc.option('my_option', 'my value')
#   end
#

describe 'When generating a document using Generator without section blocks,' do
  before(:each) { @gen = IniParse::Generator.new }

  # --
  # ==========================================================================
  #   SECTION LINES
  # ==========================================================================
  # ++

  describe 'adding a section' do
    it 'should add a Section to the document' do
      @gen.section("a section")
      expect(@gen.document).to have_section("a section")
    end

    it 'should change the Generator context to the section' do
      @gen.section("a section")
      expect(@gen.context).to eq(@gen.document['a section'])
    end

    it 'should pass extra options to the Section instance' do
      @gen.section("a section", :indent => '    ')
      expect(@gen.document["a section"].to_ini).to match(/\A    /)
    end
  end

  # --
  # ==========================================================================
  #   OPTION LINES
  # ==========================================================================
  # ++

  describe 'adding a option' do
    it 'should pass extra options to the Option instance' do
      @gen.section("a section")
      @gen.option("my option", "a value", :indent => '    ')
      expect(@gen.document["a section"].option("my option").to_ini).to match(/^    /)
    end

    describe 'when the context is a Document' do
      it "should add the option to an __anonymous__ section" do
        @gen.option("key", "value")
        expect(@gen.document['__anonymous__']['key']).to eql('value')
      end
    end

    describe 'when the context is a Section' do
      it 'should add the option to the section' do
        @gen.section("a section")
        @gen.option("my option", "a value")
        expect(@gen.document["a section"]).to have_option("my option")
        expect(@gen.document["a section"]["my option"]).to eq("a value")
      end
    end
  end

  # --
  # ==========================================================================
  #   COMMENT LINES
  # ==========================================================================
  # ++

  describe 'adding a comment' do
    it 'should pass extra options to the Option instance' do
      @gen.comment("My comment", :indent => '    ')
      expect(@gen.document.lines.to_a.first.to_ini).to match(/^    /)
    end

    it 'should ignore any extra :comment option' do
      @gen.comment("My comment", :comment => 'Ignored')
      comment_ini = @gen.document.lines.to_a.first.to_ini
      expect(comment_ini).to match(/My comment/)
      expect(comment_ini).not_to match(/Ignored/)
    end

    describe 'when the context is a Document' do
      it 'should add a comment to the document' do
        @gen.comment('My comment')
        comment = @gen.document.lines.to_a.first
        expect(comment).to be_kind_of(IniParse::Lines::Comment)
        expect(comment.to_ini).to match(/; My comment/)
      end
    end

    describe 'when the context is a Section' do
      it 'should add a comment to the section' do
        @gen.section('a section')
        @gen.comment('My comment')
        comment = @gen.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
    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
      @gen.blank
      comment = @gen.document.lines.to_a.first
      expect(comment).to be_kind_of(IniParse::Lines::Blank)
    end

    it 'should add a blank line to the section when it is the context' do
      @gen.section('a section')
      @gen.blank
      comment = @gen.document['a section'].lines.to_a.first
      expect(comment).to be_kind_of(IniParse::Lines::Blank)
    end
  end

end
