File: without_section_blocks_spec.rb

package info (click to toggle)
ruby-iniparse 1.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 472 kB
  • sloc: ruby: 2,409; makefile: 3
file content (136 lines) | stat: -rw-r--r-- 4,407 bytes parent folder | download | duplicates (3)
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
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