File: document_parsing_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 (107 lines) | stat: -rw-r--r-- 3,297 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
require 'spec_helper'

# Tests parsing of multiple lines, in context, using #parse.

describe 'Parsing a document' do
  describe 'when a comment preceeds a single section and option' do
    before(:all) do
      @doc = IniParse::Parser.new(fixture(:comment_before_section)).parse
    end

    it 'should have a comment as the first line' do
      expect(@doc.lines.to_a.first).to be_kind_of(IniParse::Lines::Comment)
    end

    it 'should have one section' do
      expect(@doc.lines.keys).to eq(['first_section'])
    end

    it 'should have one option belonging to `first_section`' do
      expect(@doc['first_section']['key']).to eq('value')
    end
  end

  it 'should allow blank lines to preceed the first section' do
    expect {
      @doc = IniParse::Parser.new(fixture(:blank_before_section)).parse
    }.not_to raise_error

    expect(@doc.lines.to_a.first).to be_kind_of(IniParse::Lines::Blank)
  end

  it 'should allow a blank line to belong to a section' do
    expect {
      @doc = IniParse::Parser.new(fixture(:blank_in_section)).parse
    }.not_to raise_error

    expect(@doc['first_section'].lines.to_a.first).to be_kind_of(IniParse::Lines::Blank)
  end

  it 'should permit comments on their own line' do
    expect {
      @doc = IniParse::Parser.new(fixture(:comment_line)).parse
    }.not_to raise_error

    line = @doc['first_section'].lines.to_a.first
    expect(line.comment).to eql('; block comment ;')
  end

  it 'should permit options before the first section' do
    doc = IniParse::Parser.new(fixture(:option_before_section)).parse

    expect(doc.lines).to have_key('__anonymous__')
    expect(doc['__anonymous__']['foo']).to eql('bar')
    expect(doc['foo']['another']).to eql('thing')
  end

  it 'should raise ParseError if a line could not be parsed' do
    expect { IniParse::Parser.new(fixture(:invalid_line)).parse }.to \
      raise_error(IniParse::ParseError)
  end

  describe 'when a section name contains "="' do
    before(:all) do
      @doc = IniParse::Parser.new(fixture(:section_with_equals)).parse
    end

    it 'should have two sections' do
      expect(@doc.lines.to_a.length).to eq(2)
    end

    it 'should have one section' do
      expect(@doc.lines.keys).to eq(['first_section = name',
                                 'another_section = a name'])
    end

    it 'should have one option belonging to `first_section = name`' do
      expect(@doc['first_section = name']['key']).to eq('value')
    end

    it 'should have one option belonging to `another_section = a name`' do
      expect(@doc['another_section = a name']['another']).to eq('thing')
    end
  end

  describe 'when a document contains a section multiple times' do
    before(:all) do
      @doc = IniParse::Parser.new(fixture(:duplicate_section)).parse
    end

    it 'should only add the section once' do
      # "first_section" and "second_section".
      expect(@doc.lines.to_a.length).to eq(2)
    end

    it 'should retain values from the first time' do
      expect(@doc['first_section']['key']).to eq('value')
    end

    it 'should add new keys' do
      expect(@doc['first_section']['third']).to eq('fourth')
    end

    it 'should merge in duplicate keys' do
      expect(@doc['first_section']['another']).to eq(%w( thing again ))
    end
  end
end