Import upstream version 1.5.0
Debian Janitor
2 years ago
0 | 0 | language: ruby |
1 | 1 | script: "bundle exec rspec" |
2 | 2 | bundler_args: "--without extras" |
3 | before_install: | |
4 | - gem update --system | |
5 | - gem update bundler | |
6 | - gem cleanup bundler | |
3 | 7 | rvm: |
4 | - "2.1.0" | |
5 | - "1.9.3" | |
6 | - "jruby-19mode" | |
8 | - 2.4 | |
9 | - 2.3 | |
10 | - 2.2 |
0 | ### 1.5.0 | |
1 | ||
2 | * OptionCollection no longer yields duplicate keys as an array, but instead yields each key in turn. | |
3 | ||
4 | For example, given an INI file: | |
5 | ||
6 | [test] | |
7 | a = 1 | |
8 | a = 2 | |
9 | b = 3 | |
10 | ||
11 | IniParse would previously yield a single "a" key: an array containing two `Line`s: | |
12 | ||
13 | doc['test'].map { |line| line } | |
14 | # => [[<a = 1>, <a = 2>], <b = 3>] | |
15 | ||
16 | Instead, each key/value pair will be yielded in turn: | |
17 | ||
18 | doc['test'].map { |line| line } | |
19 | # => [<a = 1>, <a = 2>, <b = 3>] | |
20 | ||
21 | Directly accessing values via `[]` will still return an array of values as before: | |
22 | ||
23 | doc['test']['a'] | |
24 | # => [1, 2] | |
25 | ||
26 | * LineCollection#each may be called without a block, returning an Enumerator. | |
27 | ||
28 | doc = IniParse.parse(<<~EOF) | |
29 | [test] | |
30 | a = x | |
31 | b = y | |
32 | EOF | |
33 | ||
34 | doc[test].each | |
35 | # => #<Enumerator: ...> | |
36 | ||
37 | This allows for chaining as in the standard library: | |
38 | ||
39 | doc['test'].map.with_index { |a, i| { index: i, value: a.value } } | |
40 | # => [{ index: 0, value: 'x' }, { index: 1, value: 'y' }] |
20 | 20 | |
21 | 21 | def date |
22 | 22 | Date.today.to_s |
23 | end | |
24 | ||
25 | def rubyforge_project | |
26 | name | |
27 | 23 | end |
28 | 24 | |
29 | 25 | def gemspec_file |
76 | 72 | replace_header(head, :name) |
77 | 73 | replace_header(head, :version) |
78 | 74 | replace_header(head, :date) |
79 | #comment this out if your rubyforge_project has a different name | |
80 | replace_header(head, :rubyforge_project) | |
81 | 75 | |
82 | 76 | # determine file list from git ls-files |
83 | 77 | files = `git ls-files`. |
11 | 11 | ## If your rubyforge_project name is different, then edit it and comment out |
12 | 12 | ## the sub! line in the Rakefile |
13 | 13 | s.name = 'iniparse' |
14 | s.version = '1.4.2' | |
15 | s.date = '2015-12-03' | |
16 | s.rubyforge_project = 'iniparse' | |
14 | s.version = '1.5.0' | |
15 | s.date = '2020-02-27' | |
17 | 16 | |
18 | 17 | s.summary = 'A pure Ruby library for parsing INI documents.' |
19 | 18 | s.authors = ['Anthony Williams'] |
31 | 30 | s.extra_rdoc_files = %w(LICENSE README.rdoc) |
32 | 31 | |
33 | 32 | # Dependencies. |
34 | s.add_development_dependency('rspec', '~> 2.14') | |
33 | s.add_development_dependency('rspec', '~> 3.4') | |
35 | 34 | |
36 | 35 | # = MANIFEST = |
37 | 36 | s.files = %w[ |
37 | CHANGELOG.md | |
38 | 38 | Gemfile |
39 | 39 | LICENSE |
40 | 40 | README.rdoc |
56 | 56 | # include_blank<Boolean>:: Include blank/comment lines? |
57 | 57 | # |
58 | 58 | def each(include_blank = false) |
59 | return enum_for(:each, include_blank) unless block_given? | |
60 | ||
59 | 61 | @lines.each do |line| |
60 | 62 | if include_blank || ! (line.is_a?(Array) ? line.empty? : line.blank?) |
61 | 63 | yield(line) |
110 | 112 | include LineCollection |
111 | 113 | |
112 | 114 | def <<(line) |
113 | if line.kind_of?(IniParse::Lines::Option) | |
115 | if line.kind_of?(IniParse::Lines::Option) || (has_key?('__anonymous__') && (line.blank? || line.kind_of?(IniParse::Lines::Comment))) | |
114 | 116 | option = line |
115 | 117 | line = IniParse::Lines::AnonymousSection.new |
116 | 118 | |
160 | 162 | self |
161 | 163 | end |
162 | 164 | |
165 | def each(*args) | |
166 | return enum_for(:each, *args) unless block_given? | |
167 | ||
168 | super(*args) do |value| | |
169 | if value.is_a?(Array) | |
170 | value.each { |item| yield(item) } | |
171 | else | |
172 | yield value | |
173 | end | |
174 | end | |
175 | end | |
176 | ||
163 | 177 | # Return an array containing the keys for the lines added to this |
164 | 178 | # collection. |
165 | 179 | def keys |
166 | map { |line| line.kind_of?(Array) ? line.first.key : line.key } | |
180 | map(&:key).uniq | |
167 | 181 | end |
168 | 182 | end |
169 | 183 | end |
10 | 10 | @comment_prefix = opts.fetch(:comment_prefix, ' ') |
11 | 11 | @comment_offset = opts.fetch(:comment_offset, 0) |
12 | 12 | @indent = opts.fetch(:indent, '') |
13 | @option_sep = opts.fetch(:option_sep, nil) | |
13 | 14 | end |
14 | 15 | |
15 | 16 | # Returns if this line has an inline comment. |
53 | 54 | comment_sep: @comment_sep, |
54 | 55 | comment_prefix: @comment_prefix, |
55 | 56 | comment_offset: @comment_offset, |
56 | indent: @indent | |
57 | indent: @indent, | |
58 | option_sep: @option_sep | |
57 | 59 | } |
58 | 60 | end |
59 | 61 | end |
247 | 249 | class Option |
248 | 250 | include Line |
249 | 251 | |
250 | @regex = /^\s*([^=]+) # Option | |
251 | = | |
252 | (.*?)$ # Value | |
252 | @regex = /^\s*([^=]+?) # Option, not greedy | |
253 | (\s*=\s*) # Separator, greedy | |
254 | (.*?)$ # Value | |
253 | 255 | /x |
254 | 256 | |
255 | 257 | attr_accessor :key, :value |
262 | 264 | def initialize(key, value, opts = {}) |
263 | 265 | super(opts) |
264 | 266 | @key, @value = key.to_s, value |
267 | @option_sep = opts.fetch(:option_sep, ' = ') | |
265 | 268 | end |
266 | 269 | |
267 | 270 | def self.parse(line, opts) |
268 | 271 | if m = @regex.match(line) |
269 | [:option, m[1].strip, typecast(m[2].strip), opts] | |
272 | opts[:option_sep] = m[2] | |
273 | [:option, m[1].strip, typecast(m[3].strip), opts] | |
270 | 274 | end |
271 | 275 | end |
272 | 276 | |
290 | 294 | # because of options key duplication |
291 | 295 | def line_contents |
292 | 296 | if value.kind_of?(Array) |
293 | value.map { |v, i| "#{key} = #{v}" } | |
297 | value.map { |v, i| "#{key}#{@option_sep}#{v}" } | |
294 | 298 | else |
295 | "#{key} = #{value}" | |
299 | "#{key}#{@option_sep}#{value}" | |
296 | 300 | end |
297 | 301 | end |
298 | 302 | end |
63 | 63 | if parsed.nil? |
64 | 64 | raise IniParse::ParseError, |
65 | 65 | "A line of your INI document could not be parsed to a " \ |
66 | "LineType: '#{line}'." | |
66 | "LineType: #{line.inspect}." | |
67 | 67 | end |
68 | 68 | |
69 | 69 | parsed |
88 | 88 | |
89 | 89 | line = m[1] |
90 | 90 | else |
91 | line.rstrip! | |
91 | line = line.chomp | |
92 | 92 | end |
93 | 93 | |
94 | 94 | [line, opts] |
6 | 6 | require File.join(dir, 'parser') |
7 | 7 | |
8 | 8 | module IniParse |
9 | VERSION = '1.4.2' | |
9 | VERSION = '1.5.0'.freeze | |
10 | 10 | |
11 | 11 | # A base class for IniParse errors. |
12 | 12 | class IniParseError < StandardError; end |
2 | 2 | describe "IniParse::Document" do |
3 | 3 | it 'should have a +lines+ reader' do |
4 | 4 | methods = IniParse::Document.instance_methods.map { |m| m.to_sym } |
5 | methods.should include(:lines) | |
5 | expect(methods).to include(:lines) | |
6 | 6 | end |
7 | 7 | |
8 | 8 | it 'should not have a +lines+ writer' do |
9 | 9 | methods = IniParse::Document.instance_methods.map { |m| m.to_sym } |
10 | methods.should_not include(:lines=) | |
10 | expect(methods).not_to include(:lines=) | |
11 | 11 | end |
12 | 12 | |
13 | 13 | it 'should delegate #[] to +lines+' do |
14 | 14 | doc = IniParse::Document.new |
15 | doc.lines.should_receive(:[]).with('key') | |
15 | expect(doc.lines).to receive(:[]).with('key') | |
16 | 16 | doc['key'] |
17 | 17 | end |
18 | 18 | |
19 | 19 | it 'should call #each to +lines+' do |
20 | 20 | doc = IniParse::Document.new |
21 | doc.lines.should_receive(:each) | |
21 | expect(doc.lines).to receive(:each) | |
22 | 22 | doc.each { |l| } |
23 | 23 | end |
24 | 24 | |
25 | 25 | it 'should be enumerable' do |
26 | IniParse::Document.included_modules.should include(Enumerable) | |
26 | expect(IniParse::Document.included_modules).to include(Enumerable) | |
27 | 27 | |
28 | 28 | sections = [ |
29 | 29 | IniParse::Lines::Section.new('first section'), |
33 | 33 | doc = IniParse::Document.new |
34 | 34 | doc.lines << sections[0] << sections[1] |
35 | 35 | |
36 | doc.map { |line| line }.should == sections | |
36 | expect(doc.map { |line| line }).to eq(sections) | |
37 | 37 | end |
38 | 38 | |
39 | 39 | describe '#has_section?' do |
44 | 44 | end |
45 | 45 | |
46 | 46 | it 'should return true if a section with the given key exists' do |
47 | @doc.should have_section('first section') | |
48 | @doc.should have_section('another section') | |
47 | expect(@doc).to have_section('first section') | |
48 | expect(@doc).to have_section('another section') | |
49 | 49 | end |
50 | 50 | |
51 | 51 | it 'should return true if no section with the given key exists' do |
52 | @doc.should_not have_section('second section') | |
52 | expect(@doc).not_to have_section('second section') | |
53 | 53 | end |
54 | 54 | end |
55 | 55 | |
69 | 69 | end |
70 | 70 | |
71 | 71 | it 'removes the section given a key' do |
72 | lambda { document.delete('first') }. | |
73 | should change { document['first'] }.to(nil) | |
72 | expect { document.delete('first') }. | |
73 | to change { document['first'] }.to(nil) | |
74 | 74 | end |
75 | 75 | |
76 | 76 | it 'removes the section given a Section' do |
77 | lambda { document.delete(document['first']) }. | |
78 | should change { document['first'] }.to(nil) | |
77 | expect { document.delete(document['first']) }. | |
78 | to change { document['first'] }.to(nil) | |
79 | 79 | end |
80 | 80 | |
81 | 81 | it 'removes the lines' do |
82 | lambda { document.delete('first') }. | |
83 | should change { document.to_ini.match(/alpha/) }.to(nil) | |
82 | expect { document.delete('first') }. | |
83 | to change { document.to_ini.match(/alpha/) }.to(nil) | |
84 | 84 | end |
85 | 85 | |
86 | 86 | it 'returns the document' do |
87 | document.delete('first').should eql(document) | |
87 | expect(document.delete('first')).to eql(document) | |
88 | 88 | end |
89 | 89 | end |
90 | 90 | |
121 | 121 | describe 'when no path is given to save' do |
122 | 122 | it 'should save the INI document if a path was given when initialized' do |
123 | 123 | doc = IniParse::Document.new('/a/path/to/a/file.ini') |
124 | File.should_receive(:open).with('/a/path/to/a/file.ini', 'w') | |
124 | expect(File).to receive(:open).with('/a/path/to/a/file.ini', 'w') | |
125 | 125 | doc.save |
126 | 126 | end |
127 | 127 | |
128 | 128 | it 'should raise IniParseError if no path was given when initialized' do |
129 | lambda { IniParse::Document.new.save }.should \ | |
129 | expect { IniParse::Document.new.save }.to \ | |
130 | 130 | raise_error(IniParse::IniParseError) |
131 | 131 | end |
132 | 132 | end |
133 | 133 | |
134 | 134 | describe 'when a path is given to save' do |
135 | 135 | it "should update the document's +path+" do |
136 | File.stub(:open).and_return(true) | |
136 | allow(File).to receive(:open).and_return(true) | |
137 | 137 | doc = IniParse::Document.new('/a/path/to/a/file.ini') |
138 | 138 | doc.save('/a/new/path.ini') |
139 | doc.path.should == '/a/new/path.ini' | |
139 | expect(doc.path).to eq('/a/new/path.ini') | |
140 | 140 | end |
141 | 141 | |
142 | 142 | it 'should save the INI document to the given path' do |
143 | File.should_receive(:open).with('/a/new/path.ini', 'w') | |
143 | expect(File).to receive(:open).with('/a/new/path.ini', 'w') | |
144 | 144 | IniParse::Document.new('/a/path/to/a/file.ini').save('/a/new/path.ini') |
145 | 145 | end |
146 | 146 | |
147 | 147 | it 'should raise IniParseError if no path was given when initialized' do |
148 | lambda { IniParse::Document.new.save }.should \ | |
148 | expect { IniParse::Document.new.save }.to \ | |
149 | 149 | raise_error(IniParse::IniParseError) |
150 | 150 | end |
151 | 151 | end |
6 | 6 | end |
7 | 7 | |
8 | 8 | it 'should parse without any errors' do |
9 | lambda { IniParse.parse(@fixture) }.should_not raise_error | |
10 | end | |
11 | ||
12 | it 'should have the correct sections' do | |
13 | IniParse.parse(fixture('openttd.ini')).lines.keys.should == [ | |
9 | expect { IniParse.parse(@fixture) }.not_to raise_error | |
10 | end | |
11 | ||
12 | it 'should have the correct sections' do | |
13 | expect(IniParse.parse(fixture('openttd.ini')).lines.keys).to eq([ | |
14 | 14 | 'misc', 'music', 'difficulty', 'game_creation', 'vehicle', |
15 | 15 | 'construction', 'station', 'economy', 'pf', 'order', 'gui', 'ai', |
16 | 16 | 'locale', 'network', 'currency', 'servers', 'bans', 'news_display', |
17 | 17 | 'version', 'preset-J', 'newgrf', 'newgrf-static' |
18 | ] | |
18 | ]) | |
19 | 19 | end |
20 | 20 | |
21 | 21 | it 'should have the correct options' do |
23 | 23 | doc = IniParse.parse(@fixture) |
24 | 24 | section = doc['misc'] |
25 | 25 | |
26 | section.lines.keys.should == [ | |
26 | expect(section.lines.keys).to eq([ | |
27 | 27 | 'display_opt', 'news_ticker_sound', 'fullscreen', 'language', |
28 | 28 | 'resolution', 'screenshot_format', 'savegame_format', |
29 | 29 | 'rightclick_emulate', 'small_font', 'medium_font', 'large_font', |
31 | 31 | 'large_aa', 'sprite_cache_size', 'player_face', |
32 | 32 | 'transparency_options', 'transparency_locks', 'invisibility_options', |
33 | 33 | 'keyboard', 'keyboard_caps' |
34 | ] | |
34 | ]) | |
35 | 35 | |
36 | 36 | # Test some of the options. |
37 | section['display_opt'].should == 'SHOW_TOWN_NAMES|SHOW_STATION_NAMES|SHOW_SIGNS|FULL_ANIMATION|FULL_DETAIL|WAYPOINTS' | |
38 | section['news_ticker_sound'].should be_false | |
39 | section['language'].should == 'english_US.lng' | |
40 | section['resolution'].should == '1680,936' | |
41 | section['large_size'].should == 16 | |
37 | expect(section['display_opt']).to eq('SHOW_TOWN_NAMES|SHOW_STATION_NAMES|SHOW_SIGNS|FULL_ANIMATION|FULL_DETAIL|WAYPOINTS') | |
38 | expect(section['news_ticker_sound']).to be_falsey | |
39 | expect(section['language']).to eq('english_US.lng') | |
40 | expect(section['resolution']).to eq('1680,936') | |
41 | expect(section['large_size']).to eq(16) | |
42 | 42 | |
43 | 43 | # Test some other options. |
44 | doc['currency']['suffix'].should == '" credits"' | |
45 | doc['news_display']['production_nobody'].should == 'summarized' | |
46 | doc['version']['version_number'].should == '070039B0' | |
47 | ||
48 | doc['preset-J']['gcf/1_other/BlackCC/mauvetoblackw.grf'].should be_nil | |
49 | doc['preset-J']['gcf/1_other/OpenGFX/OpenGFX_-_newFaces_v0.1.grf'].should be_nil | |
50 | end | |
51 | ||
52 | it 'should be identical to the original when calling #to_ini' do | |
53 | IniParse.parse(@fixture).to_ini.should == @fixture | |
44 | expect(doc['currency']['suffix']).to eq('" credits"') | |
45 | expect(doc['news_display']['production_nobody']).to eq('summarized') | |
46 | expect(doc['version']['version_number']).to eq('070039B0') | |
47 | ||
48 | expect(doc['preset-J']['gcf/1_other/BlackCC/mauvetoblackw.grf']).to be_nil | |
49 | expect(doc['preset-J']['gcf/1_other/OpenGFX/OpenGFX_-_newFaces_v0.1.grf']).to be_nil | |
50 | end | |
51 | ||
52 | it 'should be identical to the original when calling #to_ini' do | |
53 | expect(IniParse.parse(@fixture).to_ini).to eq(@fixture) | |
54 | 54 | end |
55 | 55 | end |
56 | 56 | |
60 | 60 | end |
61 | 61 | |
62 | 62 | it 'should parse without any errors' do |
63 | lambda { IniParse.parse(@fixture) }.should_not raise_error | |
64 | end | |
65 | ||
66 | it 'should have the correct sections' do | |
67 | IniParse.parse(fixture('race07.ini')).lines.keys.should == [ | |
63 | expect { IniParse.parse(@fixture) }.not_to raise_error | |
64 | end | |
65 | ||
66 | it 'should have the correct sections' do | |
67 | expect(IniParse.parse(fixture('race07.ini')).lines.keys).to eq([ | |
68 | 68 | 'Header', 'Race', 'Slot010', 'Slot016', 'Slot013', 'Slot018', |
69 | 69 | 'Slot002', 'END' |
70 | ] | |
70 | ]) | |
71 | 71 | end |
72 | 72 | |
73 | 73 | it 'should have the correct options' do |
75 | 75 | doc = IniParse.parse(@fixture) |
76 | 76 | section = doc['Slot010'] |
77 | 77 | |
78 | section.lines.keys.should == [ | |
78 | expect(section.lines.keys).to eq([ | |
79 | 79 | 'Driver', 'SteamUser', 'SteamId', 'Vehicle', 'Team', 'QualTime', |
80 | 80 | 'Laps', 'Lap', 'LapDistanceTravelled', 'BestLap', 'RaceTime' |
81 | ] | |
81 | ]) | |
82 | 82 | |
83 | 83 | # Test some of the options. |
84 | section['Driver'].should == 'Mark Voss' | |
85 | section['SteamUser'].should == 'mvoss' | |
86 | section['SteamId'].should == 1865369 | |
87 | section['Vehicle'].should == 'Chevrolet Lacetti 2007' | |
88 | section['Team'].should == 'TEMPLATE_TEAM' | |
89 | section['QualTime'].should == '1:37.839' | |
90 | section['Laps'].should == 13 | |
91 | section['LapDistanceTravelled'].should == 3857.750244 | |
92 | section['BestLap'].should == '1:38.031' | |
93 | section['RaceTime'].should == '0:21:38.988' | |
94 | ||
95 | section['Lap'].should == [ | |
84 | expect(section['Driver']).to eq('Mark Voss') | |
85 | expect(section['SteamUser']).to eq('mvoss') | |
86 | expect(section['SteamId']).to eq(1865369) | |
87 | expect(section['Vehicle']).to eq('Chevrolet Lacetti 2007') | |
88 | expect(section['Team']).to eq('TEMPLATE_TEAM') | |
89 | expect(section['QualTime']).to eq('1:37.839') | |
90 | expect(section['Laps']).to eq(13) | |
91 | expect(section['LapDistanceTravelled']).to eq(3857.750244) | |
92 | expect(section['BestLap']).to eq('1:38.031') | |
93 | expect(section['RaceTime']).to eq('0:21:38.988') | |
94 | ||
95 | expect(section['Lap']).to eq([ | |
96 | 96 | '(0, -1.000, 1:48.697)', '(1, 89.397, 1:39.455)', |
97 | 97 | '(2, 198.095, 1:38.060)', '(3, 297.550, 1:38.632)', |
98 | 98 | '(4, 395.610, 1:38.031)', '(5, 494.242, 1:39.562)', |
100 | 100 | '(8, 791.785, 1:39.889)', '(9, 890.151, 1:39.420)', |
101 | 101 | '(10, 990.040, 1:39.401)', '(11, 1089.460, 1:39.506)', |
102 | 102 | '(12, 1188.862, 1:40.017)' |
103 | ] | |
104 | ||
105 | doc['Header']['Version'].should == '1.1.1.14' | |
106 | doc['Header']['TimeString'].should == '2008/09/13 23:26:32' | |
107 | doc['Header']['Aids'].should == '0,0,0,0,0,1,1,0,0' | |
108 | ||
109 | doc['Race']['AIDB'].should == 'GameData\Locations\Anderstorp_2007\2007_ANDERSTORP.AIW' | |
110 | doc['Race']['Race Length'].should == 0.1 | |
111 | end | |
112 | ||
113 | it 'should be identical to the original when calling #to_ini' do | |
114 | pending('awaiting presevation (or lack) of whitespace around =') do | |
115 | IniParse.parse(@fixture).to_ini.should == @fixture | |
116 | end | |
103 | ]) | |
104 | ||
105 | expect(doc['Header']['Version']).to eq('1.1.1.14') | |
106 | expect(doc['Header']['TimeString']).to eq('2008/09/13 23:26:32') | |
107 | expect(doc['Header']['Aids']).to eq('0,0,0,0,0,1,1,0,0') | |
108 | ||
109 | expect(doc['Race']['AIDB']).to eq('GameData\Locations\Anderstorp_2007\2007_ANDERSTORP.AIW') | |
110 | expect(doc['Race']['Race Length']).to eq(0.1) | |
111 | end | |
112 | ||
113 | it 'should be identical to the original when calling #to_ini' do | |
114 | pending('awaiting presevation (or lack) of whitespace around =') | |
115 | expect(IniParse.parse(@fixture).to_ini).to eq(@fixture) | |
117 | 116 | end |
118 | 117 | end |
119 | 118 | |
123 | 122 | end |
124 | 123 | |
125 | 124 | it 'should parse without any errors' do |
126 | lambda { IniParse.parse(@fixture) }.should_not raise_error | |
127 | end | |
128 | ||
129 | it 'should have the correct sections' do | |
130 | IniParse.parse(@fixture).lines.keys.should == [ | |
125 | expect { IniParse.parse(@fixture) }.not_to raise_error | |
126 | end | |
127 | ||
128 | it 'should have the correct sections' do | |
129 | expect(IniParse.parse(@fixture).lines.keys).to eq([ | |
131 | 130 | 'global', 'printers' |
132 | ] | |
131 | ]) | |
133 | 132 | end |
134 | 133 | |
135 | 134 | it 'should have the correct options' do |
137 | 136 | doc = IniParse.parse(@fixture) |
138 | 137 | section = doc['global'] |
139 | 138 | |
140 | section.lines.keys.should == [ | |
139 | expect(section.lines.keys).to eq([ | |
141 | 140 | 'debug pid', 'log level', 'server string', 'printcap name', |
142 | 141 | 'printing', 'encrypt passwords', 'use spnego', 'passdb backend', |
143 | 142 | 'idmap domains', 'idmap config default: default', |
151 | 150 | 'usershare allow full config', 'com.apple:filter shares by access', |
152 | 151 | 'obey pam restrictions', 'acl check permissions', |
153 | 152 | 'name resolve order', 'include' |
154 | ] | |
155 | ||
156 | section['display charset'].should == 'UTF-8-MAC' | |
157 | section['vfs objects'].should == 'darwinacl,darwin_streams' | |
158 | section['usershare path'].should == '/var/samba/shares' | |
159 | end | |
160 | ||
161 | it 'should be identical to the original when calling #to_ini' do | |
162 | IniParse.parse(@fixture).to_ini.should == @fixture | |
153 | ]) | |
154 | ||
155 | expect(section['display charset']).to eq('UTF-8-MAC') | |
156 | expect(section['vfs objects']).to eq('darwinacl,darwin_streams') | |
157 | expect(section['usershare path']).to eq('/var/samba/shares') | |
158 | end | |
159 | ||
160 | it 'should be identical to the original when calling #to_ini' do | |
161 | expect(IniParse.parse(@fixture).to_ini).to eq(@fixture) | |
163 | 162 | end |
164 | 163 | end |
165 | 164 | |
169 | 168 | end |
170 | 169 | |
171 | 170 | it 'should be identical to the original when calling #to_ini' do |
172 | IniParse.parse(@fixture).to_ini.should == @fixture | |
171 | expect(IniParse.parse(@fixture).to_ini).to eq(@fixture) | |
173 | 172 | end |
174 | 173 | end |
175 | 174 | |
179 | 178 | end |
180 | 179 | |
181 | 180 | it 'should be identical to the original when calling #to_ini' do |
182 | IniParse.parse(@fixture).to_ini.should == @fixture | |
181 | expect(IniParse.parse(@fixture).to_ini).to eq(@fixture) | |
182 | end | |
183 | end | |
184 | ||
185 | describe 'DOS line endings' do | |
186 | before(:all) do | |
187 | @fixture = fixture(:dos_endings) | |
188 | end | |
189 | ||
190 | it 'should have the correct sections' do | |
191 | expect(IniParse.parse(@fixture).lines.keys).to eq(%w[database]) | |
192 | end | |
193 | ||
194 | it 'should have the correct options' do | |
195 | # Test the keys from one section. | |
196 | doc = IniParse.parse(@fixture) | |
197 | section = doc['database'] | |
198 | ||
199 | expect(section.lines.keys).to eq(%w[first second]) | |
200 | ||
201 | expect(section['first']).to eq(true) | |
202 | expect(section['second']).to eq(false) | |
203 | end | |
204 | ||
205 | pending 'should be identical to the original when calling #to_ini' do | |
206 | expect(IniParse.parse(@fixture).to_ini).to eq(@fixture) | |
207 | end | |
208 | end | |
209 | ||
210 | describe 'anonymous-order.ini fixture' do | |
211 | # https://github.com/antw/iniparse/issues/17 | |
212 | let(:raw) { fixture(:anon_section_with_comments) } | |
213 | ||
214 | it 'should be identical to the original when calling #to_ini' do | |
215 | expect(IniParse.parse(raw).to_ini).to eq(raw) | |
183 | 216 | end |
184 | 217 | end |
185 | 218 | end |
7 | 7 | screenshot_format = |
8 | 8 | savegame_format = |
9 | 9 | rightclick_emulate = false |
10 | small_font = | |
11 | medium_font = | |
12 | large_font = | |
10 | small_font = | |
11 | medium_font = | |
12 | large_font = | |
13 | 13 | small_size = 6 |
14 | 14 | medium_size = 10 |
15 | 15 | large_size = 16 |
21 | 21 | transparency_options = 3 |
22 | 22 | transparency_locks = 0 |
23 | 23 | invisibility_options = 30 |
24 | keyboard = | |
25 | keyboard_caps = | |
24 | keyboard = | |
25 | keyboard_caps = | |
26 | 26 | |
27 | 27 | [music] |
28 | 28 | playlist = 0 |
22 | 22 | IniParse::Generator.gen do |doc| |
23 | 23 | doc.a_section do |section| |
24 | 24 | %w( option comment blank ).each do |meth| |
25 | section.should respond_to(meth) | |
25 | expect(section).to respond_to(meth) | |
26 | 26 | end |
27 | 27 | end |
28 | 28 | end |
29 | 29 | end |
30 | 30 | |
31 | 31 | it 'should add a Section to the document' do |
32 | IniParse::Generator.gen do |doc| | |
32 | expect(IniParse::Generator.gen do |doc| | |
33 | 33 | doc.a_section { |section| } |
34 | end.should have_section("a_section") | |
34 | end).to have_section("a_section") | |
35 | 35 | end |
36 | 36 | |
37 | 37 | it 'should change the Generator context to the section during the section block' do |
38 | 38 | IniParse::Generator.gen do |doc| |
39 | 39 | doc.a_section do |section| |
40 | section.context.should be_kind_of(IniParse::Lines::Section) | |
41 | section.context.key.should == "a_section" | |
40 | expect(section.context).to be_kind_of(IniParse::Lines::Section) | |
41 | expect(section.context.key).to eq("a_section") | |
42 | 42 | end |
43 | 43 | end |
44 | 44 | end |
46 | 46 | it 'should reset the Generator context to the document after the section block' do |
47 | 47 | IniParse::Generator.gen do |doc| |
48 | 48 | doc.a_section { |section| } |
49 | doc.context.should be_kind_of(IniParse::Document) | |
49 | expect(doc.context).to be_kind_of(IniParse::Document) | |
50 | 50 | end |
51 | 51 | end |
52 | 52 | |
53 | 53 | it 'should append a blank line to the document, after the section' do |
54 | IniParse::Generator.gen do |doc| | |
54 | expect(IniParse::Generator.gen do |doc| | |
55 | 55 | doc.a_section { |section| } |
56 | end.lines.to_a.last.should be_kind_of(IniParse::Lines::Blank) | |
56 | end.lines.to_a.last).to be_kind_of(IniParse::Lines::Blank) | |
57 | 57 | end |
58 | 58 | |
59 | 59 | it 'should raise a LineNotAllowed if you attempt to nest a section' do |
60 | lambda do | |
60 | expect do | |
61 | 61 | IniParse::Generator.gen do |doc| |
62 | 62 | doc.a_section do |section_one| |
63 | 63 | section_one.another_section { |section_two| } |
64 | 64 | end |
65 | 65 | end |
66 | end.should raise_error(IniParse::LineNotAllowed) | |
66 | end.to raise_error(IniParse::LineNotAllowed) | |
67 | 67 | end |
68 | 68 | end |
69 | 69 | |
77 | 77 | describe 'when the context is a Document' do |
78 | 78 | it "adds the option to an __anonymous__ section" do |
79 | 79 | doc = IniParse::Generator.gen { |doc| doc.my_option = "a value" } |
80 | doc['__anonymous__']['my_option'].should eql('a value') | |
80 | expect(doc['__anonymous__']['my_option']).to eql('a value') | |
81 | 81 | end |
82 | 82 | end |
83 | 83 | |
90 | 90 | end |
91 | 91 | |
92 | 92 | section = document["a_section"] |
93 | section.should have_option("my_option") | |
94 | section["my_option"].should == "a value" | |
93 | expect(section).to have_option("my_option") | |
94 | expect(section["my_option"]).to eq("a value") | |
95 | 95 | end |
96 | 96 | end |
97 | 97 | end |
12 | 12 | describe 'When generating a document using Generator with section blocks,' do |
13 | 13 | |
14 | 14 | it 'should be able to compile an empty document' do |
15 | lambda { IniParse::Generator.gen { |doc| } }.should_not raise_error | |
15 | expect { IniParse::Generator.gen { |doc| } }.not_to raise_error | |
16 | 16 | end |
17 | 17 | |
18 | 18 | it 'should raise LocalJumpError if no block is given' do |
19 | lambda { IniParse::Generator.gen }.should raise_error(LocalJumpError) | |
19 | expect { IniParse::Generator.gen }.to raise_error(LocalJumpError) | |
20 | 20 | end |
21 | 21 | |
22 | 22 | it "should yield an object with generator methods" do |
23 | 23 | IniParse::Generator.gen do |doc| |
24 | 24 | %w( section option comment blank ).each do |meth| |
25 | doc.should respond_to(meth) | |
25 | expect(doc).to respond_to(meth) | |
26 | 26 | end |
27 | 27 | end |
28 | 28 | end |
38 | 38 | IniParse::Generator.gen do |doc| |
39 | 39 | doc.section("a section") do |section| |
40 | 40 | %w( option comment blank ).each do |meth| |
41 | section.should respond_to(meth) | |
41 | expect(section).to respond_to(meth) | |
42 | 42 | end |
43 | 43 | end |
44 | 44 | end |
45 | 45 | end |
46 | 46 | |
47 | 47 | it 'should add a Section to the document' do |
48 | IniParse::Generator.gen do |doc| | |
48 | expect(IniParse::Generator.gen do |doc| | |
49 | 49 | doc.section("a section") { |section| } |
50 | end.should have_section("a section") | |
50 | end).to have_section("a section") | |
51 | 51 | end |
52 | 52 | |
53 | 53 | it 'should change the Generator context to the section during the section block' do |
54 | 54 | IniParse::Generator.gen do |doc| |
55 | 55 | doc.section("a section") do |section| |
56 | section.context.should be_kind_of(IniParse::Lines::Section) | |
57 | section.context.key.should == "a section" | |
56 | expect(section.context).to be_kind_of(IniParse::Lines::Section) | |
57 | expect(section.context.key).to eq("a section") | |
58 | 58 | end |
59 | 59 | end |
60 | 60 | end |
62 | 62 | it 'should reset the Generator context to the document after the section block' do |
63 | 63 | IniParse::Generator.gen do |doc| |
64 | 64 | doc.section("a section") { |section| } |
65 | doc.context.should be_kind_of(IniParse::Document) | |
65 | expect(doc.context).to be_kind_of(IniParse::Document) | |
66 | 66 | end |
67 | 67 | end |
68 | 68 | |
71 | 71 | doc.section("a section") { |section| } |
72 | 72 | end |
73 | 73 | |
74 | document["a section"].to_ini.should match(/\A /) | |
74 | expect(document["a section"].to_ini).to match(/\A /) | |
75 | 75 | end |
76 | 76 | |
77 | 77 | it 'should pass extra options to the Section instance' do |
79 | 79 | doc.section("a section", :indent => ' ') { |section| } |
80 | 80 | end |
81 | 81 | |
82 | document["a section"].to_ini.should match(/\A /) | |
82 | expect(document["a section"].to_ini).to match(/\A /) | |
83 | 83 | end |
84 | 84 | |
85 | 85 | it 'should append a blank line to the document, after the section' do |
86 | IniParse::Generator.gen do |doc| | |
86 | expect(IniParse::Generator.gen do |doc| | |
87 | 87 | doc.section("a section") { |section| } |
88 | end.lines.to_a.last.should be_kind_of(IniParse::Lines::Blank) | |
88 | end.lines.to_a.last).to be_kind_of(IniParse::Lines::Blank) | |
89 | 89 | end |
90 | 90 | |
91 | 91 | it 'should raise a LineNotAllowed if you attempt to nest a section' do |
92 | lambda do | |
92 | expect do | |
93 | 93 | IniParse::Generator.gen do |doc| |
94 | 94 | doc.section("a section") do |section_one| |
95 | 95 | section_one.section("another_section") { |section_two| } |
96 | 96 | end |
97 | 97 | end |
98 | end.should raise_error(IniParse::LineNotAllowed) | |
98 | end.to raise_error(IniParse::LineNotAllowed) | |
99 | 99 | end |
100 | 100 | end |
101 | 101 | |
113 | 113 | doc.option("my option", "a value") |
114 | 114 | end |
115 | 115 | |
116 | document['__anonymous__']['my option'].should eql('a value') | |
116 | expect(document['__anonymous__']['my option']).to eql('a value') | |
117 | 117 | end |
118 | 118 | end |
119 | 119 | |
126 | 126 | end |
127 | 127 | |
128 | 128 | section = document["a section"] |
129 | section.should have_option("my option") | |
130 | section["my option"].should == "a value" | |
129 | expect(section).to have_option("my option") | |
130 | expect(section["my option"]).to eq("a value") | |
131 | 131 | end |
132 | 132 | |
133 | 133 | it 'should pass extra options to the Option instance' do |
137 | 137 | end |
138 | 138 | end |
139 | 139 | |
140 | document["a section"].option("my option").to_ini.should match(/^ /) | |
140 | expect(document["a section"].option("my option").to_ini).to match(/^ /) | |
141 | 141 | end |
142 | 142 | |
143 | 143 | it "should use the parent document's options as a base" do |
147 | 147 | end |
148 | 148 | end |
149 | 149 | |
150 | document["a section"].option("my option").to_ini.should match(/^ /) | |
150 | expect(document["a section"].option("my option").to_ini).to match(/^ /) | |
151 | 151 | end |
152 | 152 | |
153 | 153 | it "should use the parent section's options as a base" do |
157 | 157 | end |
158 | 158 | end |
159 | 159 | |
160 | document["a section"].option("my option").to_ini.should match(/^ /) | |
160 | expect(document["a section"].option("my option").to_ini).to match(/^ /) | |
161 | 161 | end |
162 | 162 | |
163 | 163 | it "should allow customisation of the parent's options" do |
170 | 170 | end |
171 | 171 | |
172 | 172 | option_ini = document["a section"].option("my option").to_ini |
173 | option_ini.should match(/^ /) | |
174 | option_ini.should match(/ # a comment/) | |
173 | expect(option_ini).to match(/^ /) | |
174 | expect(option_ini).to match(/ # a comment/) | |
175 | 175 | end |
176 | 176 | |
177 | 177 | it "should not use the parent section's comment when setting line options" do |
181 | 181 | end |
182 | 182 | end |
183 | 183 | |
184 | document["a section"].option("my option").to_ini.should_not match(/My section$/) | |
184 | expect(document["a section"].option("my option").to_ini).not_to match(/My section$/) | |
185 | 185 | end |
186 | 186 | end |
187 | 187 | end |
198 | 198 | doc.comment("My comment", :indent => ' ') |
199 | 199 | end |
200 | 200 | |
201 | document.lines.to_a.first.to_ini.should match(/\A /) | |
201 | expect(document.lines.to_a.first.to_ini).to match(/\A /) | |
202 | 202 | end |
203 | 203 | |
204 | 204 | it 'should ignore any extra :comment option' do |
206 | 206 | doc.comment("My comment", :comment => 'Ignored') |
207 | 207 | end |
208 | 208 | |
209 | document.lines.to_a.first.to_ini.should match(/My comment/) | |
210 | document.lines.to_a.first.to_ini.should_not match(/Ignored/) | |
209 | expect(document.lines.to_a.first.to_ini).to match(/My comment/) | |
210 | expect(document.lines.to_a.first.to_ini).not_to match(/Ignored/) | |
211 | 211 | end |
212 | 212 | |
213 | 213 | describe 'when the context is a Document' do |
217 | 217 | end |
218 | 218 | |
219 | 219 | comment = document.lines.to_a.first |
220 | comment.should be_kind_of(IniParse::Lines::Comment) | |
221 | comment.to_ini.should match(/My comment/) | |
220 | expect(comment).to be_kind_of(IniParse::Lines::Comment) | |
221 | expect(comment.to_ini).to match(/My comment/) | |
222 | 222 | end |
223 | 223 | |
224 | 224 | it 'should use the default line options as a base' do |
229 | 229 | comment_ini = document.lines.to_a.first.to_ini |
230 | 230 | |
231 | 231 | # Match separator (;) and offset (0). |
232 | comment_ini.should == '; My comment' | |
232 | expect(comment_ini).to eq('; My comment') | |
233 | 233 | end |
234 | 234 | end |
235 | 235 | |
242 | 242 | end |
243 | 243 | |
244 | 244 | comment = document['a section'].lines.to_a.first |
245 | comment.should be_kind_of(IniParse::Lines::Comment) | |
246 | comment.to_ini.should match(/My comment/) | |
245 | expect(comment).to be_kind_of(IniParse::Lines::Comment) | |
246 | expect(comment.to_ini).to match(/My comment/) | |
247 | 247 | end |
248 | 248 | |
249 | 249 | it "should use the parent document's line options as a base" do |
253 | 253 | end |
254 | 254 | end |
255 | 255 | |
256 | document['a section'].lines.to_a.first.to_ini.should match(/^ ;/) | |
256 | expect(document['a section'].lines.to_a.first.to_ini).to match(/^ ;/) | |
257 | 257 | end |
258 | 258 | |
259 | 259 | it "should use the parent section's line options as a base" do |
263 | 263 | end |
264 | 264 | end |
265 | 265 | |
266 | document['a section'].lines.to_a.first.to_ini.should match(/^ ;/) | |
266 | expect(document['a section'].lines.to_a.first.to_ini).to match(/^ ;/) | |
267 | 267 | end |
268 | 268 | |
269 | 269 | it "should allow customisation of the parent's options" do |
274 | 274 | end |
275 | 275 | |
276 | 276 | # Match separator (#) and offset (5) |
277 | document['a section'].lines.to_a.first.to_ini.should \ | |
278 | == ' # My comment' | |
277 | expect(document['a section'].lines.to_a.first.to_ini).to \ | |
278 | eq(' # My comment') | |
279 | 279 | end |
280 | 280 | |
281 | 281 | it "should not use the parent section's comment when setting line options" do |
286 | 286 | end |
287 | 287 | |
288 | 288 | comment_ini = document['a section'].lines.to_a.first.to_ini |
289 | comment_ini.should match(/My comment/) | |
290 | comment_ini.should_not match(/My section/) | |
289 | expect(comment_ini).to match(/My comment/) | |
290 | expect(comment_ini).not_to match(/My section/) | |
291 | 291 | end |
292 | 292 | end |
293 | 293 | end |
304 | 304 | doc.blank |
305 | 305 | end |
306 | 306 | |
307 | document.lines.to_a.first.should be_kind_of(IniParse::Lines::Blank) | |
307 | expect(document.lines.to_a.first).to be_kind_of(IniParse::Lines::Blank) | |
308 | 308 | end |
309 | 309 | |
310 | 310 | it 'should add a blank line to the section when it is the context' do |
314 | 314 | end |
315 | 315 | end |
316 | 316 | |
317 | document['a section'].lines.to_a.first.should be_kind_of(IniParse::Lines::Blank) | |
317 | expect(document['a section'].lines.to_a.first).to be_kind_of(IniParse::Lines::Blank) | |
318 | 318 | end |
319 | 319 | end |
320 | 320 |
28 | 28 | describe 'adding a section' do |
29 | 29 | it 'should add a Section to the document' do |
30 | 30 | @gen.section("a section") |
31 | @gen.document.should have_section("a section") | |
31 | expect(@gen.document).to have_section("a section") | |
32 | 32 | end |
33 | 33 | |
34 | 34 | it 'should change the Generator context to the section' do |
35 | 35 | @gen.section("a section") |
36 | @gen.context.should == @gen.document['a section'] | |
36 | expect(@gen.context).to eq(@gen.document['a section']) | |
37 | 37 | end |
38 | 38 | |
39 | 39 | it 'should pass extra options to the Section instance' do |
40 | 40 | @gen.section("a section", :indent => ' ') |
41 | @gen.document["a section"].to_ini.should match(/\A /) | |
41 | expect(@gen.document["a section"].to_ini).to match(/\A /) | |
42 | 42 | end |
43 | 43 | end |
44 | 44 | |
52 | 52 | it 'should pass extra options to the Option instance' do |
53 | 53 | @gen.section("a section") |
54 | 54 | @gen.option("my option", "a value", :indent => ' ') |
55 | @gen.document["a section"].option("my option").to_ini.should match(/^ /) | |
55 | expect(@gen.document["a section"].option("my option").to_ini).to match(/^ /) | |
56 | 56 | end |
57 | 57 | |
58 | 58 | describe 'when the context is a Document' do |
59 | 59 | it "should add the option to an __anonymous__ section" do |
60 | 60 | @gen.option("key", "value") |
61 | @gen.document['__anonymous__']['key'].should eql('value') | |
61 | expect(@gen.document['__anonymous__']['key']).to eql('value') | |
62 | 62 | end |
63 | 63 | end |
64 | 64 | |
66 | 66 | it 'should add the option to the section' do |
67 | 67 | @gen.section("a section") |
68 | 68 | @gen.option("my option", "a value") |
69 | @gen.document["a section"].should have_option("my option") | |
70 | @gen.document["a section"]["my option"].should == "a value" | |
69 | expect(@gen.document["a section"]).to have_option("my option") | |
70 | expect(@gen.document["a section"]["my option"]).to eq("a value") | |
71 | 71 | end |
72 | 72 | end |
73 | 73 | end |
81 | 81 | describe 'adding a comment' do |
82 | 82 | it 'should pass extra options to the Option instance' do |
83 | 83 | @gen.comment("My comment", :indent => ' ') |
84 | @gen.document.lines.to_a.first.to_ini.should match(/^ /) | |
84 | expect(@gen.document.lines.to_a.first.to_ini).to match(/^ /) | |
85 | 85 | end |
86 | 86 | |
87 | 87 | it 'should ignore any extra :comment option' do |
88 | 88 | @gen.comment("My comment", :comment => 'Ignored') |
89 | 89 | comment_ini = @gen.document.lines.to_a.first.to_ini |
90 | comment_ini.should match(/My comment/) | |
91 | comment_ini.should_not match(/Ignored/) | |
90 | expect(comment_ini).to match(/My comment/) | |
91 | expect(comment_ini).not_to match(/Ignored/) | |
92 | 92 | end |
93 | 93 | |
94 | 94 | describe 'when the context is a Document' do |
95 | 95 | it 'should add a comment to the document' do |
96 | 96 | @gen.comment('My comment') |
97 | 97 | comment = @gen.document.lines.to_a.first |
98 | comment.should be_kind_of(IniParse::Lines::Comment) | |
99 | comment.to_ini.should match(/; My comment/) | |
98 | expect(comment).to be_kind_of(IniParse::Lines::Comment) | |
99 | expect(comment.to_ini).to match(/; My comment/) | |
100 | 100 | end |
101 | 101 | end |
102 | 102 | |
105 | 105 | @gen.section('a section') |
106 | 106 | @gen.comment('My comment') |
107 | 107 | comment = @gen.document['a section'].lines.to_a.first |
108 | comment.should be_kind_of(IniParse::Lines::Comment) | |
109 | comment.to_ini.should match(/My comment/) | |
108 | expect(comment).to be_kind_of(IniParse::Lines::Comment) | |
109 | expect(comment.to_ini).to match(/My comment/) | |
110 | 110 | end |
111 | 111 | end |
112 | 112 | end |
121 | 121 | it 'should add a blank line to the document when it is the context' do |
122 | 122 | @gen.blank |
123 | 123 | comment = @gen.document.lines.to_a.first |
124 | comment.should be_kind_of(IniParse::Lines::Blank) | |
124 | expect(comment).to be_kind_of(IniParse::Lines::Blank) | |
125 | 125 | end |
126 | 126 | |
127 | 127 | it 'should add a blank line to the section when it is the context' do |
128 | 128 | @gen.section('a section') |
129 | 129 | @gen.blank |
130 | 130 | comment = @gen.document['a section'].lines.to_a.first |
131 | comment.should be_kind_of(IniParse::Lines::Blank) | |
131 | expect(comment).to be_kind_of(IniParse::Lines::Blank) | |
132 | 132 | end |
133 | 133 | end |
134 | 134 |
44 | 44 | end |
45 | 45 | |
46 | 46 | describe '.open' do |
47 | before(:each) { File.stub(:read).and_return('[section]') } | |
47 | before(:each) { allow(File).to receive(:read).and_return('[section]') } | |
48 | 48 | |
49 | 49 | it 'should return an IniParse::Document' do |
50 | IniParse.open('/my/path.ini').should be_kind_of(IniParse::Document) | |
50 | expect(IniParse.open('/my/path.ini')).to be_kind_of(IniParse::Document) | |
51 | 51 | end |
52 | 52 | |
53 | 53 | it 'should set the path on the returned Document' do |
54 | IniParse.open('/my/path.ini').path.should == '/my/path.ini' | |
54 | expect(IniParse.open('/my/path.ini').path).to eq('/my/path.ini') | |
55 | 55 | end |
56 | 56 | |
57 | 57 | it 'should read the file at the given path' do |
58 | File.should_receive(:read).with('/my/path.ini').and_return('[section]') | |
58 | expect(File).to receive(:read).with('/my/path.ini').and_return('[section]') | |
59 | 59 | IniParse.open('/my/path.ini') |
60 | 60 | end |
61 | 61 | end |
3 | 3 | # Shared specs for all Collection types... |
4 | 4 | # ---------------------------------------------------------------------------- |
5 | 5 | |
6 | share_examples_for "LineCollection" do | |
6 | shared_examples_for "LineCollection" do | |
7 | 7 | before(:each) do |
8 | 8 | @collection << (@c1 = IniParse::Lines::Comment.new) |
9 | 9 | @collection << @i1 |
15 | 15 | |
16 | 16 | describe '#each' do |
17 | 17 | it 'should remove blanks and comments by default' do |
18 | @collection.each { |l| l.should be_kind_of(@i1.class) } | |
18 | @collection.each { |l| expect(l).to be_kind_of(@i1.class) } | |
19 | 19 | end |
20 | 20 | |
21 | 21 | it 'should not remove blanks and comments if true is given' do |
26 | 26 | arr << line |
27 | 27 | end |
28 | 28 | |
29 | arr.should == [@c1, @i1, @i2, @b1, @i3, @b2] | |
29 | expect(arr).to eq([@c1, @i1, @i2, @b1, @i3, @b2]) | |
30 | 30 | end |
31 | 31 | end |
32 | 32 | |
33 | 33 | describe '#[]' do |
34 | 34 | it 'should fetch the correct value' do |
35 | @collection['first'].should == @i1 | |
36 | @collection['second'].should == @i2 | |
37 | @collection['third'].should == @i3 | |
35 | expect(@collection['first']).to eq(@i1) | |
36 | expect(@collection['second']).to eq(@i2) | |
37 | expect(@collection['third']).to eq(@i3) | |
38 | 38 | end |
39 | 39 | |
40 | 40 | it 'should return nil if the given key does not exist' do |
41 | @collection['does not exist'].should be_nil | |
41 | expect(@collection['does not exist']).to be_nil | |
42 | 42 | end |
43 | 43 | end |
44 | 44 | |
45 | 45 | describe '#[]=' do |
46 | 46 | it 'should successfully add a new key' do |
47 | 47 | @collection['fourth'] = @new |
48 | @collection['fourth'].should == @new | |
48 | expect(@collection['fourth']).to eq(@new) | |
49 | 49 | end |
50 | 50 | |
51 | 51 | it 'should successfully update an existing key' do |
52 | 52 | @collection['second'] = @new |
53 | @collection['second'].should == @new | |
53 | expect(@collection['second']).to eq(@new) | |
54 | 54 | |
55 | 55 | # Make sure the old data is gone. |
56 | @collection.detect { |s| s.key == 'second' }.should be_nil | |
56 | expect(@collection.detect { |s| s.key == 'second' }).to be_nil | |
57 | 57 | end |
58 | 58 | |
59 | 59 | it 'should typecast given keys to a string' do |
60 | 60 | @collection[:a_symbol] = @new |
61 | @collection['a_symbol'].should == @new | |
61 | expect(@collection['a_symbol']).to eq(@new) | |
62 | 62 | end |
63 | 63 | end |
64 | 64 | |
65 | 65 | describe '#<<' do |
66 | 66 | it 'should set the key correctly if given a new item' do |
67 | @collection.should_not have_key(@new.key) | |
67 | expect(@collection).not_to have_key(@new.key) | |
68 | 68 | @collection << @new |
69 | @collection.should have_key(@new.key) | |
69 | expect(@collection).to have_key(@new.key) | |
70 | 70 | end |
71 | 71 | |
72 | 72 | it 'should append Blank lines' do |
73 | 73 | @collection << IniParse::Lines::Blank.new |
74 | @collection.instance_variable_get(:@lines).last.should \ | |
74 | expect(@collection.instance_variable_get(:@lines).last).to \ | |
75 | 75 | be_kind_of(IniParse::Lines::Blank) |
76 | 76 | end |
77 | 77 | |
78 | 78 | it 'should append Comment lines' do |
79 | 79 | @collection << IniParse::Lines::Comment.new |
80 | @collection.instance_variable_get(:@lines).last.should \ | |
80 | expect(@collection.instance_variable_get(:@lines).last).to \ | |
81 | 81 | be_kind_of(IniParse::Lines::Comment) |
82 | 82 | end |
83 | 83 | |
84 | 84 | it 'should return self' do |
85 | (@collection << @new).should == @collection | |
85 | expect(@collection << @new).to eq(@collection) | |
86 | 86 | end |
87 | 87 | end |
88 | 88 | |
89 | 89 | describe '#delete' do |
90 | 90 | it 'should remove the given value and adjust the indicies' do |
91 | @collection['second'].should_not be_nil | |
91 | expect(@collection['second']).not_to be_nil | |
92 | 92 | @collection.delete('second') |
93 | @collection['second'].should be_nil | |
94 | @collection['first'].should == @i1 | |
95 | @collection['third'].should == @i3 | |
93 | expect(@collection['second']).to be_nil | |
94 | expect(@collection['first']).to eq(@i1) | |
95 | expect(@collection['third']).to eq(@i3) | |
96 | 96 | end |
97 | 97 | |
98 | 98 | it "should do nothing if the supplied key does not exist" do |
99 | 99 | @collection.delete('does not exist') |
100 | @collection['first'].should == @i1 | |
101 | @collection['third'].should == @i3 | |
100 | expect(@collection['first']).to eq(@i1) | |
101 | expect(@collection['third']).to eq(@i3) | |
102 | 102 | end |
103 | 103 | end |
104 | 104 | |
105 | 105 | describe '#to_a' do |
106 | 106 | it 'should return an array' do |
107 | @collection.to_a.should be_kind_of(Array) | |
107 | expect(@collection.to_a).to be_kind_of(Array) | |
108 | 108 | end |
109 | 109 | |
110 | 110 | it 'should include all lines' do |
111 | @collection.to_a.should == [@c1, @i1, @i2, @b1, @i3, @b2] | |
111 | expect(@collection.to_a).to eq([@c1, @i1, @i2, @b1, @i3, @b2]) | |
112 | 112 | end |
113 | 113 | |
114 | 114 | it 'should include references to the same line objects as the collection' do |
115 | 115 | @collection << @new |
116 | @collection.to_a.last.object_id.should == @new.object_id | |
116 | expect(@collection.to_a.last.object_id).to eq(@new.object_id) | |
117 | 117 | end |
118 | 118 | end |
119 | 119 | |
120 | 120 | describe '#to_hash' do |
121 | 121 | it 'should return a hash' do |
122 | @collection.to_hash.should be_kind_of(Hash) | |
122 | expect(@collection.to_hash).to be_kind_of(Hash) | |
123 | 123 | end |
124 | 124 | |
125 | 125 | it 'should have the correct keys' do |
126 | 126 | hash = @collection.to_hash |
127 | hash.keys.length.should == 3 | |
128 | hash.should have_key('first') | |
129 | hash.should have_key('second') | |
130 | hash.should have_key('third') | |
127 | expect(hash.keys.length).to eq(3) | |
128 | expect(hash).to have_key('first') | |
129 | expect(hash).to have_key('second') | |
130 | expect(hash).to have_key('third') | |
131 | 131 | end |
132 | 132 | |
133 | 133 | it 'should have the correct values' do |
134 | 134 | hash = @collection.to_hash |
135 | hash['first'].should == @i1 | |
136 | hash['second'].should == @i2 | |
137 | hash['third'].should == @i3 | |
135 | expect(hash['first']).to eq(@i1) | |
136 | expect(hash['second']).to eq(@i2) | |
137 | expect(hash['third']).to eq(@i3) | |
138 | 138 | end |
139 | 139 | end |
140 | 140 | |
141 | 141 | describe '#keys' do |
142 | 142 | it 'should return an array of strings' do |
143 | @collection.keys.should == ['first', 'second', 'third'] | |
143 | expect(@collection.keys).to eq(['first', 'second', 'third']) | |
144 | 144 | end |
145 | 145 | end |
146 | 146 | end |
162 | 162 | |
163 | 163 | describe '#<<' do |
164 | 164 | it 'should raise a LineNotAllowed exception if a Section is pushed' do |
165 | lambda { @collection << IniParse::Lines::Section.new('s') }.should \ | |
165 | expect { @collection << IniParse::Lines::Section.new('s') }.to \ | |
166 | 166 | raise_error(IniParse::LineNotAllowed) |
167 | 167 | end |
168 | 168 | |
173 | 173 | @collection << option_one |
174 | 174 | @collection << option_two |
175 | 175 | |
176 | @collection['k'].should == [option_one, option_two] | |
176 | expect(@collection['k']).to eq([option_one, option_two]) | |
177 | 177 | end |
178 | 178 | end |
179 | 179 | |
181 | 181 | it 'should handle duplicates' do |
182 | 182 | @collection << @i1 << @i2 << @i3 |
183 | 183 | @collection << IniParse::Lines::Option.new('first', 'v5') |
184 | @collection.keys.should == ['first', 'second', 'third'] | |
184 | expect(@collection.keys).to eq(['first', 'second', 'third']) | |
185 | end | |
186 | end | |
187 | ||
188 | context 'with duplicate lines' do | |
189 | let(:collection) { IniParse::OptionCollection.new } | |
190 | ||
191 | let(:opt_1) { IniParse::Lines::Option.new('my_key', 'value_1') } | |
192 | let(:opt_2) { IniParse::Lines::Option.new('my_key', 'value_2') } | |
193 | ||
194 | before do | |
195 | collection << opt_1 | |
196 | collection << opt_2 | |
197 | end | |
198 | ||
199 | it 'yields each item in turn' do | |
200 | expect { |b| collection.each(&b) }.to yield_successive_args(opt_1, opt_2) | |
185 | 201 | end |
186 | 202 | end |
187 | 203 | end |
201 | 217 | it 'should add merge Section with the other, if it is a duplicate' do |
202 | 218 | new_section = IniParse::Lines::Section.new('first') |
203 | 219 | @collection << @i1 |
204 | @i1.should_receive(:merge!).with(new_section).once | |
220 | expect(@i1).to receive(:merge!).with(new_section).once | |
205 | 221 | @collection << new_section |
206 | 222 | end |
207 | 223 | end |
12 | 12 | describe "IniParse::Lines::Line module" do |
13 | 13 | describe '#to_ini' do |
14 | 14 | it 'should return +line_contents+' do |
15 | IniParse::Test::FakeLine.new.to_ini.should == 'fake line' | |
15 | expect(IniParse::Test::FakeLine.new.to_ini).to eq('fake line') | |
16 | 16 | end |
17 | 17 | |
18 | 18 | it 'should preserve line indents' do |
19 | IniParse::Test::FakeLine.new( | |
19 | expect(IniParse::Test::FakeLine.new( | |
20 | 20 | :indent => ' ' |
21 | ).to_ini.should == ' fake line' | |
21 | ).to_ini).to eq(' fake line') | |
22 | 22 | end |
23 | 23 | |
24 | 24 | describe 'when a comment is set' do |
25 | 25 | it 'should correctly include the comment' do |
26 | IniParse::Test::FakeLine.new( | |
26 | expect(IniParse::Test::FakeLine.new( | |
27 | 27 | :comment => 'comment', :comment_sep => ';', :comment_offset => 10 |
28 | ).to_ini.should == 'fake line ; comment' | |
28 | ).to_ini).to eq('fake line ; comment') | |
29 | 29 | end |
30 | 30 | |
31 | 31 | it 'should correctly indent the comment' do |
32 | IniParse::Test::FakeLine.new( | |
32 | expect(IniParse::Test::FakeLine.new( | |
33 | 33 | :comment => 'comment', :comment_sep => ';', :comment_offset => 15 |
34 | ).to_ini.should == 'fake line ; comment' | |
34 | ).to_ini).to eq('fake line ; comment') | |
35 | 35 | end |
36 | 36 | |
37 | 37 | it 'should use ";" as a default comment seperator' do |
38 | IniParse::Test::FakeLine.new( | |
38 | expect(IniParse::Test::FakeLine.new( | |
39 | 39 | :comment => 'comment' |
40 | ).to_ini.should == 'fake line ; comment' | |
40 | ).to_ini).to eq('fake line ; comment') | |
41 | 41 | end |
42 | 42 | |
43 | 43 | it 'should use the correct seperator' do |
44 | IniParse::Test::FakeLine.new( | |
44 | expect(IniParse::Test::FakeLine.new( | |
45 | 45 | :comment => 'comment', :comment_sep => '#' |
46 | ).to_ini.should == 'fake line # comment' | |
46 | ).to_ini).to eq('fake line # comment') | |
47 | 47 | end |
48 | 48 | |
49 | 49 | it 'should use the ensure a space is added before the comment seperator' do |
50 | IniParse::Test::FakeLine.new( | |
50 | expect(IniParse::Test::FakeLine.new( | |
51 | 51 | :comment => 'comment', :comment_sep => ';', :comment_offset => 0 |
52 | ).to_ini.should == 'fake line ; comment' | |
52 | ).to_ini).to eq('fake line ; comment') | |
53 | 53 | end |
54 | 54 | |
55 | 55 | it 'should not add an extra space if the line is blank' do |
57 | 57 | :comment => 'comment', :comment_sep => ';', :comment_offset => 0 |
58 | 58 | ) |
59 | 59 | |
60 | line.stub(:line_contents).and_return('') | |
61 | line.to_ini.should == '; comment' | |
60 | allow(line).to receive(:line_contents).and_return('') | |
61 | expect(line.to_ini).to eq('; comment') | |
62 | 62 | end |
63 | 63 | end |
64 | 64 | |
65 | 65 | describe 'when no comment is set' do |
66 | 66 | it 'should not add trailing space if :comment_offset has a value' do |
67 | IniParse::Test::FakeLine.new(:comment_offset => 10).to_ini.should == 'fake line' | |
67 | expect(IniParse::Test::FakeLine.new(:comment_offset => 10).to_ini).to eq('fake line') | |
68 | 68 | end |
69 | 69 | |
70 | 70 | it 'should not add a comment seperator :comment_sep has a value' do |
71 | IniParse::Test::FakeLine.new(:comment_sep => ';').to_ini.should == 'fake line' | |
71 | expect(IniParse::Test::FakeLine.new(:comment_sep => ';').to_ini).to eq('fake line') | |
72 | 72 | end |
73 | 73 | end |
74 | 74 | end |
75 | 75 | |
76 | 76 | describe '#has_comment?' do |
77 | 77 | it 'should return true if :comment has a non-blank value' do |
78 | IniParse::Test::FakeLine.new(:comment => 'comment').should have_comment | |
78 | expect(IniParse::Test::FakeLine.new(:comment => 'comment')).to have_comment | |
79 | 79 | end |
80 | 80 | |
81 | 81 | it 'should return true if :comment has a blank value' do |
82 | IniParse::Test::FakeLine.new(:comment => '').should have_comment | |
82 | expect(IniParse::Test::FakeLine.new(:comment => '')).to have_comment | |
83 | 83 | end |
84 | 84 | |
85 | 85 | it 'should return false if :comment has a nil value' do |
86 | IniParse::Test::FakeLine.new.should_not have_comment | |
87 | IniParse::Test::FakeLine.new(:comment => nil).should_not have_comment | |
86 | expect(IniParse::Test::FakeLine.new).not_to have_comment | |
87 | expect(IniParse::Test::FakeLine.new(:comment => nil)).not_to have_comment | |
88 | 88 | end |
89 | 89 | end |
90 | 90 | end |
97 | 97 | before(:each) { @section = IniParse::Lines::Section.new('a section') } |
98 | 98 | |
99 | 99 | it 'should respond_to +lines+' do |
100 | @section.should respond_to(:lines) | |
100 | expect(@section).to respond_to(:lines) | |
101 | 101 | end |
102 | 102 | |
103 | 103 | it 'should not respond_to +lines=+' do |
104 | @section.should_not respond_to(:lines=) | |
104 | expect(@section).not_to respond_to(:lines=) | |
105 | 105 | end |
106 | 106 | |
107 | 107 | it 'should include Enumerable' do |
108 | IniParse::Lines::Section.included_modules.should include(Enumerable) | |
108 | expect(IniParse::Lines::Section.included_modules).to include(Enumerable) | |
109 | 109 | end |
110 | 110 | |
111 | 111 | describe '#initialize' do |
112 | 112 | it 'should typecast the given key to a string' do |
113 | IniParse::Lines::Section.new(:symbol).key.should == 'symbol' | |
113 | expect(IniParse::Lines::Section.new(:symbol).key).to eq('symbol') | |
114 | 114 | end |
115 | 115 | end |
116 | 116 | |
118 | 118 | it 'should retrieve the line identified by the given key' do |
119 | 119 | option = IniParse::Lines::Option.new('k', 'value one') |
120 | 120 | @section.lines << option |
121 | @section.option('k').should == option | |
121 | expect(@section.option('k')).to eq(option) | |
122 | 122 | end |
123 | 123 | |
124 | 124 | it 'should return nil if the given key does not exist' do |
125 | @section.option('does_not_exist').should be_nil | |
125 | expect(@section.option('does_not_exist')).to be_nil | |
126 | 126 | end |
127 | 127 | end |
128 | 128 | |
129 | 129 | describe '#each' do |
130 | 130 | it 'should call #each on +lines+' do |
131 | @section.lines.should_receive(:each) | |
131 | expect(@section.lines).to receive(:each) | |
132 | 132 | @section.each { |l| } |
133 | 133 | end |
134 | 134 | end |
135 | 135 | |
136 | 136 | describe '#[]' do |
137 | 137 | it 'should return nil if the given key does not exist' do |
138 | @section['k'].should be_nil | |
138 | expect(@section['k']).to be_nil | |
139 | 139 | end |
140 | 140 | |
141 | 141 | it 'should return a value if the given key exists' do |
142 | 142 | @section.lines << IniParse::Lines::Option.new('k', 'v') |
143 | @section['k'].should == 'v' | |
143 | expect(@section['k']).to eq('v') | |
144 | 144 | end |
145 | 145 | |
146 | 146 | it 'should return an array of values if the key is a duplicate' do |
147 | 147 | @section.lines << IniParse::Lines::Option.new('k', 'v1') |
148 | 148 | @section.lines << IniParse::Lines::Option.new('k', 'v2') |
149 | 149 | @section.lines << IniParse::Lines::Option.new('k', 'v3') |
150 | @section['k'].should == ['v1', 'v2', 'v3'] | |
150 | expect(@section['k']).to eq(['v1', 'v2', 'v3']) | |
151 | 151 | end |
152 | 152 | |
153 | 153 | it 'should typecast the key to a string' do |
154 | 154 | @section.lines << IniParse::Lines::Option.new('k', 'v') |
155 | @section[:k].should == 'v' | |
155 | expect(@section[:k]).to eq('v') | |
156 | 156 | end |
157 | 157 | end |
158 | 158 | |
159 | 159 | describe '#[]=' do |
160 | 160 | it 'should add a new Option with the given key and value' do |
161 | 161 | @section['k'] = 'a value' |
162 | @section.option('k').should be_kind_of(IniParse::Lines::Option) | |
163 | @section['k'].should == 'a value' | |
162 | expect(@section.option('k')).to be_kind_of(IniParse::Lines::Option) | |
163 | expect(@section['k']).to eq('a value') | |
164 | 164 | end |
165 | 165 | |
166 | 166 | it 'should update the Option if one already exists' do |
167 | 167 | @section.lines << IniParse::Lines::Option.new('k', 'orig value') |
168 | 168 | @section['k'] = 'new value' |
169 | @section['k'].should == 'new value' | |
169 | expect(@section['k']).to eq('new value') | |
170 | 170 | end |
171 | 171 | |
172 | 172 | it 'should replace the existing Option if it is an array' do |
173 | 173 | @section.lines << IniParse::Lines::Option.new('k', 'v1') |
174 | 174 | @section.lines << IniParse::Lines::Option.new('k', 'v2') |
175 | 175 | @section['k'] = 'new value' |
176 | @section.option('k').should be_kind_of(IniParse::Lines::Option) | |
177 | @section['k'].should == 'new value' | |
176 | expect(@section.option('k')).to be_kind_of(IniParse::Lines::Option) | |
177 | expect(@section['k']).to eq('new value') | |
178 | 178 | end |
179 | 179 | |
180 | 180 | it 'should typecast the key to a string' do |
181 | 181 | @section[:k] = 'a value' |
182 | @section['k'].should == 'a value' | |
182 | expect(@section['k']).to eq('a value') | |
183 | 183 | end |
184 | 184 | end |
185 | 185 | |
198 | 198 | @new_section.lines << IniParse::Lines::Option.new('d', 'val4') |
199 | 199 | |
200 | 200 | @section.merge!(@new_section) |
201 | @section['a'].should == 'val1' | |
202 | @section['b'].should == 'val2' | |
203 | @section['c'].should == 'val3' | |
204 | @section['d'].should == 'val4' | |
201 | expect(@section['a']).to eq('val1') | |
202 | expect(@section['b']).to eq('val2') | |
203 | expect(@section['c']).to eq('val3') | |
204 | expect(@section['d']).to eq('val4') | |
205 | 205 | end |
206 | 206 | |
207 | 207 | it 'should handle duplicates' do |
208 | 208 | @new_section.lines << IniParse::Lines::Option.new('a', 'val2') |
209 | 209 | @section.merge!(@new_section) |
210 | @section['a'].should == ['val1', 'val2'] | |
210 | expect(@section['a']).to eq(['val1', 'val2']) | |
211 | 211 | end |
212 | 212 | |
213 | 213 | it 'should handle duplicates on both sides' do |
216 | 216 | @new_section.lines << IniParse::Lines::Option.new('a', 'val4') |
217 | 217 | |
218 | 218 | @section.merge!(@new_section) |
219 | @section['a'].should == ['val1', 'val2', 'val3', 'val4'] | |
219 | expect(@section['a']).to eq(['val1', 'val2', 'val3', 'val4']) | |
220 | 220 | end |
221 | 221 | |
222 | 222 | it 'should copy blank lines' do |
224 | 224 | @section.merge!(@new_section) |
225 | 225 | line = nil |
226 | 226 | @section.each(true) { |l| line = l } |
227 | line.should be_kind_of(IniParse::Lines::Blank) | |
227 | expect(line).to be_kind_of(IniParse::Lines::Blank) | |
228 | 228 | end |
229 | 229 | |
230 | 230 | it 'should copy comments' do |
232 | 232 | @section.merge!(@new_section) |
233 | 233 | line = nil |
234 | 234 | @section.each(true) { |l| line = l } |
235 | line.should be_kind_of(IniParse::Lines::Comment) | |
235 | expect(line).to be_kind_of(IniParse::Lines::Comment) | |
236 | 236 | end |
237 | 237 | end |
238 | 238 | |
246 | 246 | end |
247 | 247 | |
248 | 248 | it 'removes the option given a key' do |
249 | lambda { @section.delete('a') }. | |
250 | should change { @section['a'] }.to(nil) | |
249 | expect { @section.delete('a') }. | |
250 | to change { @section['a'] }.to(nil) | |
251 | 251 | end |
252 | 252 | |
253 | 253 | it 'removes the option given an Option' do |
254 | lambda { @section.delete(opt_one) }. | |
255 | should change { @section['a'] }.to(nil) | |
254 | expect { @section.delete(opt_one) }. | |
255 | to change { @section['a'] }.to(nil) | |
256 | 256 | end |
257 | 257 | |
258 | 258 | it 'should not remove non-matching lines' do |
259 | lambda { @section.delete('a') }.should_not change { @section['c'] } | |
259 | expect { @section.delete('a') }.not_to change { @section['c'] } | |
260 | 260 | end |
261 | 261 | |
262 | 262 | it 'returns the section' do |
263 | @section.delete('a').should eql(@section) | |
263 | expect(@section.delete('a')).to eql(@section) | |
264 | 264 | end |
265 | 265 | end |
266 | 266 | |
267 | 267 | describe '#to_ini' do |
268 | 268 | it 'should include the section key' do |
269 | IniParse::Lines::Section.new('a section').to_ini.should == '[a section]' | |
269 | expect(IniParse::Lines::Section.new('a section').to_ini).to eq('[a section]') | |
270 | 270 | end |
271 | 271 | |
272 | 272 | it 'should include lines belonging to the section' do |
277 | 277 | ) |
278 | 278 | @section.lines << IniParse::Lines::Option.new('b', 'val2') |
279 | 279 | |
280 | @section.to_ini.should == | |
280 | expect(@section.to_ini).to eq( | |
281 | 281 | "[a section]\n" \ |
282 | 282 | "a = val1\n" \ |
283 | 283 | "\n" \ |
284 | 284 | "; my comment\n" \ |
285 | 285 | "b = val2" |
286 | ) | |
286 | 287 | end |
287 | 288 | |
288 | 289 | it 'should include duplicate lines' do |
289 | 290 | @section.lines << IniParse::Lines::Option.new('a', 'val1') |
290 | 291 | @section.lines << IniParse::Lines::Option.new('a', 'val2') |
291 | 292 | |
292 | @section.to_ini.should == | |
293 | expect(@section.to_ini).to eq( | |
293 | 294 | "[a section]\n" \ |
294 | 295 | "a = val1\n" \ |
295 | 296 | "a = val2" |
297 | ) | |
296 | 298 | end |
297 | 299 | end |
298 | 300 | |
302 | 304 | end |
303 | 305 | |
304 | 306 | it 'should return true if an option with the given key exists' do |
305 | @section.should have_option('first') | |
307 | expect(@section).to have_option('first') | |
306 | 308 | end |
307 | 309 | |
308 | 310 | it 'should return true if no option with the given key exists' do |
309 | @section.should_not have_option('second') | |
311 | expect(@section).not_to have_option('second') | |
310 | 312 | end |
311 | 313 | end |
312 | 314 | end |
318 | 320 | describe 'Iniparse::Lines::Option' do |
319 | 321 | describe '#initialize' do |
320 | 322 | it 'should typecast the given key to a string' do |
321 | IniParse::Lines::Option.new(:symbol, '').key.should == 'symbol' | |
323 | expect(IniParse::Lines::Option.new(:symbol, '').key).to eq('symbol') | |
322 | 324 | end |
323 | 325 | end |
324 | 326 | |
325 | 327 | describe '#to_ini' do |
326 | 328 | it 'should include the key and value' do |
327 | IniParse::Lines::Option.new('key', 'value').to_ini.should == 'key = value' | |
329 | expect(IniParse::Lines::Option.new('key', 'value').to_ini).to eq('key = value') | |
328 | 330 | end |
329 | 331 | end |
330 | 332 | |
334 | 336 | end |
335 | 337 | |
336 | 338 | it 'should typecast empty values to nil' do |
337 | parse('key =').should be_option_tuple('key', nil) | |
338 | parse('key = ').should be_option_tuple('key', nil) | |
339 | parse('key = ').should be_option_tuple('key', nil) | |
339 | expect(parse('key =')).to be_option_tuple('key', nil) | |
340 | expect(parse('key = ')).to be_option_tuple('key', nil) | |
341 | expect(parse('key = ')).to be_option_tuple('key', nil) | |
340 | 342 | end |
341 | 343 | |
342 | 344 | it 'should not typecast "true" if true is part of a word' do |
343 | parse('key = TestTrueTest').should be_option_tuple('key', 'TestTrueTest') | |
344 | parse('key = TrueTest').should be_option_tuple('key', 'TrueTest') | |
345 | parse('key = TestTrue').should be_option_tuple('key', 'TestTrue') | |
345 | expect(parse('key = TestTrueTest')).to be_option_tuple('key', 'TestTrueTest') | |
346 | expect(parse('key = TrueTest')).to be_option_tuple('key', 'TrueTest') | |
347 | expect(parse('key = TestTrue')).to be_option_tuple('key', 'TestTrue') | |
346 | 348 | end |
347 | 349 | |
348 | 350 | it 'should not typecast "false" if false is part of a word' do |
349 | parse('key = TestFalseTest').should be_option_tuple('key', 'TestFalseTest') | |
350 | parse('key = FalseTest').should be_option_tuple('key', 'FalseTest') | |
351 | parse('key = TestFalse').should be_option_tuple('key', 'TestFalse') | |
351 | expect(parse('key = TestFalseTest')).to be_option_tuple('key', 'TestFalseTest') | |
352 | expect(parse('key = FalseTest')).to be_option_tuple('key', 'FalseTest') | |
353 | expect(parse('key = TestFalse')).to be_option_tuple('key', 'TestFalse') | |
352 | 354 | end |
353 | 355 | |
354 | 356 | it 'should typecast "true" to TrueClass' do |
355 | parse('key = true').should be_option_tuple('key', true) | |
356 | parse('key = TRUE').should be_option_tuple('key', true) | |
357 | expect(parse('key = true')).to be_option_tuple('key', true) | |
358 | expect(parse('key = TRUE')).to be_option_tuple('key', true) | |
357 | 359 | end |
358 | 360 | |
359 | 361 | it 'should typecast "false" to FalseClass' do |
360 | parse('key = false').should be_option_tuple('key', false) | |
361 | parse('key = FALSE').should be_option_tuple('key', false) | |
362 | expect(parse('key = false')).to be_option_tuple('key', false) | |
363 | expect(parse('key = FALSE')).to be_option_tuple('key', false) | |
362 | 364 | end |
363 | 365 | |
364 | 366 | it 'should typecast integer values to Integer' do |
365 | parse('key = 1').should be_option_tuple('key', 1) | |
366 | parse('key = 10').should be_option_tuple('key', 10) | |
367 | expect(parse('key = 1')).to be_option_tuple('key', 1) | |
368 | expect(parse('key = 10')).to be_option_tuple('key', 10) | |
367 | 369 | end |
368 | 370 | |
369 | 371 | it 'should not typecast integers with a leading 0 to Integer' do |
370 | parse('key = 0700').should be_option_tuple('key', '0700') | |
372 | expect(parse('key = 0700')).to be_option_tuple('key', '0700') | |
371 | 373 | end |
372 | 374 | |
373 | 375 | it 'should typecast negative integer values to Integer' do |
374 | parse('key = -1').should be_option_tuple('key', -1) | |
376 | expect(parse('key = -1')).to be_option_tuple('key', -1) | |
375 | 377 | end |
376 | 378 | |
377 | 379 | it 'should typecast float values to Float' do |
378 | parse('key = 3.14159265').should be_option_tuple('key', 3.14159265) | |
380 | expect(parse('key = 3.14159265')).to be_option_tuple('key', 3.14159265) | |
379 | 381 | end |
380 | 382 | |
381 | 383 | it 'should typecast negative float values to Float' do |
382 | parse('key = -3.14159265').should be_option_tuple('key', -3.14159265) | |
384 | expect(parse('key = -3.14159265')).to be_option_tuple('key', -3.14159265) | |
383 | 385 | end |
384 | 386 | |
385 | 387 | it 'should typecast scientific notation numbers to Float' do |
386 | parse('key = 10e5').should be_option_tuple('key', 10e5) | |
387 | parse('key = 10e+5').should be_option_tuple('key', 10e5) | |
388 | parse('key = 10e-5').should be_option_tuple('key', 10e-5) | |
389 | ||
390 | parse('key = -10e5').should be_option_tuple('key', -10e5) | |
391 | parse('key = -10e+5').should be_option_tuple('key', -10e5) | |
392 | parse('key = -10e-5').should be_option_tuple('key', -10e-5) | |
393 | ||
394 | parse('key = 3.14159265e5').should be_option_tuple('key', 3.14159265e5) | |
395 | parse('key = 3.14159265e+5').should be_option_tuple('key', 3.14159265e5) | |
396 | parse('key = 3.14159265e-5').should be_option_tuple('key', 3.14159265e-5) | |
397 | ||
398 | parse('key = -3.14159265e5').should be_option_tuple('key', -3.14159265e5) | |
399 | parse('key = -3.14159265e+5').should be_option_tuple('key', -3.14159265e5) | |
400 | parse('key = -3.14159265e-5').should be_option_tuple('key', -3.14159265e-5) | |
388 | expect(parse('key = 10e5')).to be_option_tuple('key', 10e5) | |
389 | expect(parse('key = 10e+5')).to be_option_tuple('key', 10e5) | |
390 | expect(parse('key = 10e-5')).to be_option_tuple('key', 10e-5) | |
391 | ||
392 | expect(parse('key = -10e5')).to be_option_tuple('key', -10e5) | |
393 | expect(parse('key = -10e+5')).to be_option_tuple('key', -10e5) | |
394 | expect(parse('key = -10e-5')).to be_option_tuple('key', -10e-5) | |
395 | ||
396 | expect(parse('key = 3.14159265e5')).to be_option_tuple('key', 3.14159265e5) | |
397 | expect(parse('key = 3.14159265e+5')).to be_option_tuple('key', 3.14159265e5) | |
398 | expect(parse('key = 3.14159265e-5')).to be_option_tuple('key', 3.14159265e-5) | |
399 | ||
400 | expect(parse('key = -3.14159265e5')).to be_option_tuple('key', -3.14159265e5) | |
401 | expect(parse('key = -3.14159265e+5')).to be_option_tuple('key', -3.14159265e5) | |
402 | expect(parse('key = -3.14159265e-5')).to be_option_tuple('key', -3.14159265e-5) | |
401 | 403 | end |
402 | 404 | end |
403 | 405 | end |
413 | 415 | describe 'IniParse::Lines::Comment' do |
414 | 416 | describe '#has_comment?' do |
415 | 417 | it 'should return true if :comment has a non-blank value' do |
416 | IniParse::Lines::Comment.new(:comment => 'comment').should have_comment | |
418 | expect(IniParse::Lines::Comment.new(:comment => 'comment')).to have_comment | |
417 | 419 | end |
418 | 420 | |
419 | 421 | it 'should return true if :comment has a blank value' do |
420 | IniParse::Lines::Comment.new(:comment => '').should have_comment | |
422 | expect(IniParse::Lines::Comment.new(:comment => '')).to have_comment | |
421 | 423 | end |
422 | 424 | |
423 | 425 | it 'should return true if :comment has a nil value' do |
424 | IniParse::Lines::Comment.new.should have_comment | |
425 | IniParse::Lines::Comment.new(:comment => nil).should have_comment | |
426 | expect(IniParse::Lines::Comment.new).to have_comment | |
427 | expect(IniParse::Lines::Comment.new(:comment => nil)).to have_comment | |
426 | 428 | end |
427 | 429 | end |
428 | 430 | |
429 | 431 | describe '#to_ini' do |
430 | 432 | it 'should return the comment' do |
431 | IniParse::Lines::Comment.new( | |
433 | expect(IniParse::Lines::Comment.new( | |
432 | 434 | :comment => 'a comment' |
433 | ).to_ini.should == '; a comment' | |
435 | ).to_ini).to eq('; a comment') | |
434 | 436 | end |
435 | 437 | |
436 | 438 | it 'should preserve comment offset' do |
437 | IniParse::Lines::Comment.new( | |
439 | expect(IniParse::Lines::Comment.new( | |
438 | 440 | :comment => 'a comment', :comment_offset => 10 |
439 | ).to_ini.should == ' ; a comment' | |
441 | ).to_ini).to eq(' ; a comment') | |
440 | 442 | end |
441 | 443 | |
442 | 444 | it 'should return just the comment_sep if the comment is blank' do |
443 | IniParse::Lines::Comment.new.to_ini.should == ';' | |
445 | expect(IniParse::Lines::Comment.new.to_ini).to eq(';') | |
444 | 446 | end |
445 | 447 | end |
446 | 448 | end |
8 | 8 | end |
9 | 9 | |
10 | 10 | it 'should have a comment as the first line' do |
11 | @doc.lines.to_a.first.should be_kind_of(IniParse::Lines::Comment) | |
11 | expect(@doc.lines.to_a.first).to be_kind_of(IniParse::Lines::Comment) | |
12 | 12 | end |
13 | 13 | |
14 | 14 | it 'should have one section' do |
15 | @doc.lines.keys.should == ['first_section'] | |
15 | expect(@doc.lines.keys).to eq(['first_section']) | |
16 | 16 | end |
17 | 17 | |
18 | 18 | it 'should have one option belonging to `first_section`' do |
19 | @doc['first_section']['key'].should == 'value' | |
19 | expect(@doc['first_section']['key']).to eq('value') | |
20 | 20 | end |
21 | 21 | end |
22 | 22 | |
23 | 23 | it 'should allow blank lines to preceed the first section' do |
24 | lambda { | |
24 | expect { | |
25 | 25 | @doc = IniParse::Parser.new(fixture(:blank_before_section)).parse |
26 | }.should_not raise_error | |
26 | }.not_to raise_error | |
27 | 27 | |
28 | @doc.lines.to_a.first.should be_kind_of(IniParse::Lines::Blank) | |
28 | expect(@doc.lines.to_a.first).to be_kind_of(IniParse::Lines::Blank) | |
29 | 29 | end |
30 | 30 | |
31 | 31 | it 'should allow a blank line to belong to a section' do |
32 | lambda { | |
32 | expect { | |
33 | 33 | @doc = IniParse::Parser.new(fixture(:blank_in_section)).parse |
34 | }.should_not raise_error | |
34 | }.not_to raise_error | |
35 | 35 | |
36 | @doc['first_section'].lines.to_a.first.should be_kind_of(IniParse::Lines::Blank) | |
36 | expect(@doc['first_section'].lines.to_a.first).to be_kind_of(IniParse::Lines::Blank) | |
37 | 37 | end |
38 | 38 | |
39 | 39 | it 'should permit comments on their own line' do |
40 | lambda { | |
40 | expect { | |
41 | 41 | @doc = IniParse::Parser.new(fixture(:comment_line)).parse |
42 | }.should_not raise_error | |
42 | }.not_to raise_error | |
43 | 43 | |
44 | 44 | line = @doc['first_section'].lines.to_a.first |
45 | line.comment.should eql('; block comment ;') | |
45 | expect(line.comment).to eql('; block comment ;') | |
46 | 46 | end |
47 | 47 | |
48 | 48 | it 'should permit options before the first section' do |
49 | 49 | doc = IniParse::Parser.new(fixture(:option_before_section)).parse |
50 | 50 | |
51 | doc.lines.should have_key('__anonymous__') | |
52 | doc['__anonymous__']['foo'].should eql('bar') | |
53 | doc['foo']['another'].should eql('thing') | |
51 | expect(doc.lines).to have_key('__anonymous__') | |
52 | expect(doc['__anonymous__']['foo']).to eql('bar') | |
53 | expect(doc['foo']['another']).to eql('thing') | |
54 | 54 | end |
55 | 55 | |
56 | 56 | it 'should raise ParseError if a line could not be parsed' do |
57 | lambda { IniParse::Parser.new(fixture(:invalid_line)).parse }.should \ | |
57 | expect { IniParse::Parser.new(fixture(:invalid_line)).parse }.to \ | |
58 | 58 | raise_error(IniParse::ParseError) |
59 | 59 | end |
60 | 60 | |
64 | 64 | end |
65 | 65 | |
66 | 66 | it 'should have two sections' do |
67 | @doc.lines.to_a.length.should == 2 | |
67 | expect(@doc.lines.to_a.length).to eq(2) | |
68 | 68 | end |
69 | 69 | |
70 | 70 | it 'should have one section' do |
71 | @doc.lines.keys.should == ['first_section = name', | |
72 | 'another_section = a name'] | |
71 | expect(@doc.lines.keys).to eq(['first_section = name', | |
72 | 'another_section = a name']) | |
73 | 73 | end |
74 | 74 | |
75 | 75 | it 'should have one option belonging to `first_section = name`' do |
76 | @doc['first_section = name']['key'].should == 'value' | |
76 | expect(@doc['first_section = name']['key']).to eq('value') | |
77 | 77 | end |
78 | 78 | |
79 | 79 | it 'should have one option belonging to `another_section = a name`' do |
80 | @doc['another_section = a name']['another'].should == 'thing' | |
80 | expect(@doc['another_section = a name']['another']).to eq('thing') | |
81 | 81 | end |
82 | 82 | end |
83 | 83 | |
88 | 88 | |
89 | 89 | it 'should only add the section once' do |
90 | 90 | # "first_section" and "second_section". |
91 | @doc.lines.to_a.length.should == 2 | |
91 | expect(@doc.lines.to_a.length).to eq(2) | |
92 | 92 | end |
93 | 93 | |
94 | 94 | it 'should retain values from the first time' do |
95 | @doc['first_section']['key'].should == 'value' | |
95 | expect(@doc['first_section']['key']).to eq('value') | |
96 | 96 | end |
97 | 97 | |
98 | 98 | it 'should add new keys' do |
99 | @doc['first_section']['third'].should == 'fourth' | |
99 | expect(@doc['first_section']['third']).to eq('fourth') | |
100 | 100 | end |
101 | 101 | |
102 | 102 | it 'should merge in duplicate keys' do |
103 | @doc['first_section']['another'].should == %w( thing again ) | |
103 | expect(@doc['first_section']['another']).to eq(%w( thing again )) | |
104 | 104 | end |
105 | 105 | end |
106 | 106 | end |
3 | 3 | |
4 | 4 | describe 'Parsing a line' do |
5 | 5 | it 'should strip leading whitespace and set the :indent option' do |
6 | IniParse::Parser.parse_line(' [section]').should \ | |
6 | expect(IniParse::Parser.parse_line(' [section]')).to \ | |
7 | 7 | be_section_tuple(:any, {:indent => ' '}) |
8 | 8 | end |
9 | 9 | |
10 | 10 | it 'should raise an error if the line could not be matched' do |
11 | lambda { IniParse::Parser.parse_line('invalid line') }.should \ | |
11 | expect { IniParse::Parser.parse_line('invalid line') }.to \ | |
12 | 12 | raise_error(IniParse::ParseError) |
13 | 13 | end |
14 | 14 | |
16 | 16 | begin |
17 | 17 | # Remove last type. |
18 | 18 | type = IniParse::Parser.parse_types.pop |
19 | type.should_not_receive(:parse) | |
19 | expect(type).not_to receive(:parse) | |
20 | 20 | IniParse::Parser.parse_line('[section]') |
21 | 21 | ensure |
22 | 22 | IniParse::Parser.parse_types << type |
35 | 35 | end |
36 | 36 | |
37 | 37 | it 'should return an option tuple' do |
38 | @tuple.should be_option_tuple('k', 'v') | |
38 | expect(@tuple).to be_option_tuple('k', 'v') | |
39 | 39 | end |
40 | 40 | |
41 | 41 | it 'should set no indent, comment, offset or separator' do |
42 | @tuple.last[:indent].should be_nil | |
43 | @tuple.last[:comment].should be_nil | |
44 | @tuple.last[:comment_offset].should be_nil | |
45 | @tuple.last[:comment_sep].should be_nil | |
42 | expect(@tuple.last[:indent]).to be_nil | |
43 | expect(@tuple.last[:comment]).to be_nil | |
44 | expect(@tuple.last[:comment_offset]).to be_nil | |
45 | expect(@tuple.last[:comment_sep]).to be_nil | |
46 | 46 | end |
47 | 47 | end |
48 | 48 | |
49 | 49 | describe 'with "k = a value with spaces"' do |
50 | 50 | it 'should return an option tuple' do |
51 | IniParse::Parser.parse_line('k = a value with spaces').should \ | |
51 | expect(IniParse::Parser.parse_line('k = a value with spaces')).to \ | |
52 | 52 | be_option_tuple('k', 'a value with spaces') |
53 | 53 | end |
54 | 54 | end |
59 | 59 | end |
60 | 60 | |
61 | 61 | it 'should return an option tuple' do |
62 | @tuple.should be_option_tuple('k', 'v') | |
62 | expect(@tuple).to be_option_tuple('k', 'v') | |
63 | 63 | end |
64 | 64 | |
65 | 65 | it 'should set the comment to "a comment"' do |
66 | @tuple.should be_option_tuple(:any, :any, :comment => 'a comment') | |
67 | end | |
68 | ||
69 | it 'should set the comment separator to ";"' do | |
70 | @tuple.should be_option_tuple(:any, :any, :comment_sep => ';') | |
66 | expect(@tuple).to be_option_tuple(:any, :any, :comment => 'a comment') | |
67 | end | |
68 | ||
69 | it 'should set the comment separator to ";"' do | |
70 | expect(@tuple).to be_option_tuple(:any, :any, :comment_sep => ';') | |
71 | 71 | end |
72 | 72 | |
73 | 73 | it 'should set the comment offset to 6' do |
74 | @tuple.should be_option_tuple(:any, :any, :comment_offset => 6) | |
74 | expect(@tuple).to be_option_tuple(:any, :any, :comment_offset => 6) | |
75 | 75 | end |
76 | 76 | end |
77 | 77 | |
81 | 81 | end |
82 | 82 | |
83 | 83 | it 'should return an option tuple with the correct value' do |
84 | @tuple.should be_option_tuple(:any, 'v;w;x y;z') | |
84 | expect(@tuple).to be_option_tuple(:any, 'v;w;x y;z') | |
85 | 85 | end |
86 | 86 | |
87 | 87 | it 'should not set a comment' do |
88 | @tuple.last[:comment].should be_nil | |
89 | @tuple.last[:comment_offset].should be_nil | |
90 | @tuple.last[:comment_sep].should be_nil | |
88 | expect(@tuple.last[:comment]).to be_nil | |
89 | expect(@tuple.last[:comment_offset]).to be_nil | |
90 | expect(@tuple.last[:comment_sep]).to be_nil | |
91 | 91 | end |
92 | 92 | end |
93 | 93 | |
97 | 97 | end |
98 | 98 | |
99 | 99 | it 'should return an option tuple with the correct value' do |
100 | @tuple.should be_option_tuple(:any, 'v;w') | |
100 | expect(@tuple).to be_option_tuple(:any, 'v;w') | |
101 | 101 | end |
102 | 102 | |
103 | 103 | it 'should set the comment to "a comment"' do |
104 | @tuple.should be_option_tuple(:any, :any, :comment => 'a comment') | |
105 | end | |
106 | ||
107 | it 'should set the comment separator to ";"' do | |
108 | @tuple.should be_option_tuple(:any, :any, :comment_sep => ';') | |
104 | expect(@tuple).to be_option_tuple(:any, :any, :comment => 'a comment') | |
105 | end | |
106 | ||
107 | it 'should set the comment separator to ";"' do | |
108 | expect(@tuple).to be_option_tuple(:any, :any, :comment_sep => ';') | |
109 | 109 | end |
110 | 110 | |
111 | 111 | it 'should set the comment offset to 8' do |
112 | @tuple.should be_option_tuple(:any, :any, :comment_offset => 8) | |
112 | expect(@tuple).to be_option_tuple(:any, :any, :comment_offset => 8) | |
113 | 113 | end |
114 | 114 | end |
115 | 115 | |
116 | 116 | describe 'with "key=value"' do |
117 | 117 | it 'should return an option tuple with the correct key and value' do |
118 | IniParse::Parser.parse_line('key=value').should \ | |
118 | expect(IniParse::Parser.parse_line('key=value')).to \ | |
119 | 119 | be_option_tuple('key', 'value') |
120 | 120 | end |
121 | 121 | end |
122 | 122 | |
123 | 123 | describe 'with "key= value"' do |
124 | 124 | it 'should return an option tuple with the correct key and value' do |
125 | IniParse::Parser.parse_line('key= value').should \ | |
125 | expect(IniParse::Parser.parse_line('key= value')).to \ | |
126 | 126 | be_option_tuple('key', 'value') |
127 | 127 | end |
128 | 128 | end |
129 | 129 | |
130 | 130 | describe 'with "key =value"' do |
131 | 131 | it 'should return an option tuple with the correct key and value' do |
132 | IniParse::Parser.parse_line('key =value').should \ | |
132 | expect(IniParse::Parser.parse_line('key =value')).to \ | |
133 | 133 | be_option_tuple('key', 'value') |
134 | 134 | end |
135 | 135 | end |
136 | 136 | |
137 | 137 | describe 'with "key = value"' do |
138 | 138 | it 'should return an option tuple with the correct key and value' do |
139 | IniParse::Parser.parse_line('key = value').should \ | |
139 | expect(IniParse::Parser.parse_line('key = value')).to \ | |
140 | 140 | be_option_tuple('key', 'value') |
141 | 141 | end |
142 | 142 | end |
143 | 143 | |
144 | 144 | describe 'with "key ="' do |
145 | 145 | it 'should return an option tuple with the correct key' do |
146 | IniParse::Parser.parse_line('key =').should \ | |
146 | expect(IniParse::Parser.parse_line('key =')).to \ | |
147 | 147 | be_option_tuple('key') |
148 | 148 | end |
149 | 149 | |
150 | 150 | it 'should set the option value to nil' do |
151 | IniParse::Parser.parse_line('key =').should \ | |
151 | expect(IniParse::Parser.parse_line('key =')).to \ | |
152 | 152 | be_option_tuple(:any, nil) |
153 | 153 | end |
154 | 154 | end |
156 | 156 | |
157 | 157 | describe 'with "key = EEjDDJJjDJDJD233232=="' do |
158 | 158 | it 'should include the "equals" in the option value' do |
159 | IniParse::Parser.parse_line('key = EEjDDJJjDJDJD233232==').should \ | |
159 | expect(IniParse::Parser.parse_line('key = EEjDDJJjDJDJD233232==')).to \ | |
160 | 160 | be_option_tuple('key', 'EEjDDJJjDJDJD233232==') |
161 | 161 | end |
162 | 162 | end |
163 | 163 | |
164 | 164 | describe 'with "key = ==EEjDDJJjDJDJD233232"' do |
165 | 165 | it 'should include the "equals" in the option value' do |
166 | IniParse::Parser.parse_line('key = ==EEjDDJJjDJDJD233232').should \ | |
166 | expect(IniParse::Parser.parse_line('key = ==EEjDDJJjDJDJD233232')).to \ | |
167 | 167 | be_option_tuple('key', '==EEjDDJJjDJDJD233232') |
168 | 168 | end |
169 | 169 | end |
170 | 170 | |
171 | 171 | describe 'with "key.two = value"' do |
172 | 172 | it 'should return an option tuple with the correct key' do |
173 | IniParse::Parser.parse_line('key.two = value').should \ | |
173 | expect(IniParse::Parser.parse_line('key.two = value')).to \ | |
174 | 174 | be_option_tuple('key.two') |
175 | 175 | end |
176 | 176 | end |
177 | 177 | |
178 | 178 | describe 'with "key/with/slashes = value"' do |
179 | 179 | it 'should return an option tuple with the correct key' do |
180 | IniParse::Parser.parse_line('key/with/slashes = value').should \ | |
180 | expect(IniParse::Parser.parse_line('key/with/slashes = value')).to \ | |
181 | 181 | be_option_tuple('key/with/slashes', 'value') |
182 | 182 | end |
183 | 183 | end |
184 | 184 | |
185 | 185 | describe 'with "key_with_underscores = value"' do |
186 | 186 | it 'should return an option tuple with the correct key' do |
187 | IniParse::Parser.parse_line('key_with_underscores = value').should \ | |
187 | expect(IniParse::Parser.parse_line('key_with_underscores = value')).to \ | |
188 | 188 | be_option_tuple('key_with_underscores', 'value') |
189 | 189 | end |
190 | 190 | end |
191 | 191 | |
192 | 192 | describe 'with "key-with-dashes = value"' do |
193 | 193 | it 'should return an option tuple with the correct key' do |
194 | IniParse::Parser.parse_line('key-with-dashes = value').should \ | |
194 | expect(IniParse::Parser.parse_line('key-with-dashes = value')).to \ | |
195 | 195 | be_option_tuple('key-with-dashes', 'value') |
196 | 196 | end |
197 | 197 | end |
198 | 198 | |
199 | 199 | describe 'with "key with spaces = value"' do |
200 | 200 | it 'should return an option tuple with the correct key' do |
201 | IniParse::Parser.parse_line('key with spaces = value').should \ | |
201 | expect(IniParse::Parser.parse_line('key with spaces = value')).to \ | |
202 | 202 | be_option_tuple('key with spaces', 'value') |
203 | 203 | end |
204 | 204 | end |
215 | 215 | end |
216 | 216 | |
217 | 217 | it 'should return a section tuple' do |
218 | @tuple.should be_section_tuple('section') | |
218 | expect(@tuple).to be_section_tuple('section') | |
219 | 219 | end |
220 | 220 | |
221 | 221 | it 'should set no indent, comment, offset or separator' do |
222 | @tuple.last[:indent].should be_nil | |
223 | @tuple.last[:comment].should be_nil | |
224 | @tuple.last[:comment_offset].should be_nil | |
225 | @tuple.last[:comment_sep].should be_nil | |
222 | expect(@tuple.last[:indent]).to be_nil | |
223 | expect(@tuple.last[:comment]).to be_nil | |
224 | expect(@tuple.last[:comment_offset]).to be_nil | |
225 | expect(@tuple.last[:comment_sep]).to be_nil | |
226 | 226 | end |
227 | 227 | end |
228 | 228 | |
229 | 229 | describe 'with "[section with whitespace]"' do |
230 | 230 | it 'should return a section tuple with the correct key' do |
231 | IniParse::Parser.parse_line('[section with whitespace]').should \ | |
231 | expect(IniParse::Parser.parse_line('[section with whitespace]')).to \ | |
232 | 232 | be_section_tuple('section with whitespace') |
233 | 233 | end |
234 | 234 | end |
235 | 235 | |
236 | 236 | describe 'with "[ section with surounding whitespace ]"' do |
237 | 237 | it 'should return a section tuple with the correct key' do |
238 | IniParse::Parser.parse_line('[ section with surounding whitespace ]').should \ | |
238 | expect(IniParse::Parser.parse_line('[ section with surounding whitespace ]')).to \ | |
239 | 239 | be_section_tuple(' section with surounding whitespace ') |
240 | 240 | end |
241 | 241 | end |
246 | 246 | end |
247 | 247 | |
248 | 248 | it 'should return a section tuple' do |
249 | @tuple.should be_section_tuple('section') | |
249 | expect(@tuple).to be_section_tuple('section') | |
250 | 250 | end |
251 | 251 | |
252 | 252 | it 'should set the comment to "a comment"' do |
253 | @tuple.should be_section_tuple(:any, :comment => 'a comment') | |
254 | end | |
255 | ||
256 | it 'should set the comment separator to ";"' do | |
257 | @tuple.should be_section_tuple(:any, :comment_sep => ';') | |
253 | expect(@tuple).to be_section_tuple(:any, :comment => 'a comment') | |
254 | end | |
255 | ||
256 | it 'should set the comment separator to ";"' do | |
257 | expect(@tuple).to be_section_tuple(:any, :comment_sep => ';') | |
258 | 258 | end |
259 | 259 | |
260 | 260 | it 'should set the comment offset to 10' do |
261 | @tuple.should be_section_tuple(:any, :comment_offset => 10) | |
261 | expect(@tuple).to be_section_tuple(:any, :comment_offset => 10) | |
262 | 262 | end |
263 | 263 | end |
264 | 264 | |
268 | 268 | end |
269 | 269 | |
270 | 270 | it 'should return a section tuple with the correct key' do |
271 | @tuple.should be_section_tuple('section;with#comment;chars') | |
271 | expect(@tuple).to be_section_tuple('section;with#comment;chars') | |
272 | 272 | end |
273 | 273 | |
274 | 274 | it 'should not set a comment' do |
275 | @tuple.last[:indent].should be_nil | |
276 | @tuple.last[:comment].should be_nil | |
277 | @tuple.last[:comment_offset].should be_nil | |
278 | @tuple.last[:comment_sep].should be_nil | |
275 | expect(@tuple.last[:indent]).to be_nil | |
276 | expect(@tuple.last[:comment]).to be_nil | |
277 | expect(@tuple.last[:comment_offset]).to be_nil | |
278 | expect(@tuple.last[:comment_sep]).to be_nil | |
279 | 279 | end |
280 | 280 | end |
281 | 281 | |
285 | 285 | end |
286 | 286 | |
287 | 287 | it 'should return a section tuple with the correct key' do |
288 | @tuple.should be_section_tuple('section;with#comment;chars') | |
288 | expect(@tuple).to be_section_tuple('section;with#comment;chars') | |
289 | 289 | end |
290 | 290 | |
291 | 291 | it 'should set the comment to "a comment"' do |
292 | @tuple.should be_section_tuple(:any, :comment => 'a comment') | |
293 | end | |
294 | ||
295 | it 'should set the comment separator to ";"' do | |
296 | @tuple.should be_section_tuple(:any, :comment_sep => ';') | |
292 | expect(@tuple).to be_section_tuple(:any, :comment => 'a comment') | |
293 | end | |
294 | ||
295 | it 'should set the comment separator to ";"' do | |
296 | expect(@tuple).to be_section_tuple(:any, :comment_sep => ';') | |
297 | 297 | end |
298 | 298 | |
299 | 299 | it 'should set the comment offset to 29' do |
300 | @tuple.should be_section_tuple(:any, :comment_offset => 29) | |
300 | expect(@tuple).to be_section_tuple(:any, :comment_offset => 29) | |
301 | 301 | end |
302 | 302 | end |
303 | 303 | |
313 | 313 | end |
314 | 314 | |
315 | 315 | it 'should return a comment tuple with the correct comment' do |
316 | @tuple.should be_comment_tuple('a comment') | |
317 | end | |
318 | ||
319 | it 'should set the comment separator to ";"' do | |
320 | @tuple.should be_comment_tuple(:any, :comment_sep => ';') | |
316 | expect(@tuple).to be_comment_tuple('a comment') | |
317 | end | |
318 | ||
319 | it 'should set the comment separator to ";"' do | |
320 | expect(@tuple).to be_comment_tuple(:any, :comment_sep => ';') | |
321 | 321 | end |
322 | 322 | |
323 | 323 | it 'should set the comment offset to 0' do |
324 | @tuple.should be_comment_tuple(:any, :comment_offset => 0) | |
324 | expect(@tuple).to be_comment_tuple(:any, :comment_offset => 0) | |
325 | 325 | end |
326 | 326 | end |
327 | 327 | |
331 | 331 | end |
332 | 332 | |
333 | 333 | it 'should return a comment tuple with the correct comment' do |
334 | @tuple.should be_comment_tuple('a comment') | |
335 | end | |
336 | ||
337 | it 'should set the comment separator to ";"' do | |
338 | @tuple.should be_comment_tuple(:any, :comment_sep => ';') | |
334 | expect(@tuple).to be_comment_tuple('a comment') | |
335 | end | |
336 | ||
337 | it 'should set the comment separator to ";"' do | |
338 | expect(@tuple).to be_comment_tuple(:any, :comment_sep => ';') | |
339 | 339 | end |
340 | 340 | |
341 | 341 | it 'should set the comment offset to 1' do |
342 | @tuple.should be_comment_tuple(:any, :comment_offset => 1) | |
342 | expect(@tuple).to be_comment_tuple(:any, :comment_offset => 1) | |
343 | 343 | end |
344 | 344 | end |
345 | 345 | |
349 | 349 | end |
350 | 350 | |
351 | 351 | it 'should return a comment tuple with an empty value' do |
352 | @tuple.should be_comment_tuple('') | |
353 | end | |
354 | ||
355 | it 'should set the comment separator to ";"' do | |
356 | @tuple.should be_comment_tuple(:any, :comment_sep => ';') | |
352 | expect(@tuple).to be_comment_tuple('') | |
353 | end | |
354 | ||
355 | it 'should set the comment separator to ";"' do | |
356 | expect(@tuple).to be_comment_tuple(:any, :comment_sep => ';') | |
357 | 357 | end |
358 | 358 | |
359 | 359 | it 'should set the comment offset to 0' do |
360 | @tuple.should be_comment_tuple(:any, :comment_offset => 0) | |
360 | expect(@tuple).to be_comment_tuple(:any, :comment_offset => 0) | |
361 | 361 | end |
362 | 362 | end |
363 | 363 | |
369 | 369 | |
370 | 370 | describe 'with ""' do |
371 | 371 | it 'should return a blank tuple' do |
372 | IniParse::Parser.parse_line('').should be_blank_tuple | |
372 | expect(IniParse::Parser.parse_line('')).to be_blank_tuple | |
373 | 373 | end |
374 | 374 | end |
375 | 375 | |
376 | 376 | describe 'with " "' do |
377 | 377 | it 'should return a blank tuple' do |
378 | IniParse::Parser.parse_line(' ').should be_blank_tuple | |
378 | expect(IniParse::Parser.parse_line(' ')).to be_blank_tuple | |
379 | 379 | end |
380 | 380 | end |
381 | 381 | end |
73 | 73 | third = fourth |
74 | 74 | another = again |
75 | 75 | FIX |
76 | ||
77 | IniParse::Test::Fixtures[:dos_endings] = | |
78 | "[database]\r\n" \ | |
79 | "first = true\r\n" \ | |
80 | "second = false\r\n" | |
81 | ||
82 | # https://github.com/antw/iniparse/issues/17 | |
83 | IniParse::Test::Fixtures[:anon_section_with_comments] = <<-FIX.gsub(/^ /, '') | |
84 | ##################### | |
85 | # A lot of comments # | |
86 | ##################### | |
87 | ||
88 | # optiona comment | |
89 | optiona = A | |
90 | ||
91 | # optionb comment | |
92 | optionb = B | |
93 | ||
94 | # optionc comment | |
95 | optionc = C | |
96 | FIX |
25 | 25 | "expected #{@expected} but got #{@target.class}" |
26 | 26 | end |
27 | 27 | |
28 | def negative_failure_message | |
28 | def failure_message_when_negated | |
29 | 29 | "expected #{@expected} to not be #{@target.class}" |
30 | 30 | end |
31 | 31 | |
67 | 67 | "expected #{@expected_type} tuple #{@failure_message}" |
68 | 68 | end |
69 | 69 | |
70 | def negative_failure_message | |
70 | def failure_message_when_negated | |
71 | 71 | "expected #{@tuple.inspect} to not be #{@expected_type} tuple" |
72 | 72 | end |
73 | 73 |
7 | 7 | |
8 | 8 | describe 'An empty array' do |
9 | 9 | it 'should not pass be_section_tuple' do |
10 | [].should_not be_section_tuple | |
10 | expect([]).not_to be_section_tuple | |
11 | 11 | end |
12 | 12 | |
13 | 13 | it 'should not pass be_option_tuple' do |
14 | [].should_not be_option_tuple | |
14 | expect([]).not_to be_option_tuple | |
15 | 15 | end |
16 | 16 | |
17 | 17 | it 'should not pass be_blank_tuple' do |
18 | [].should_not be_blank_tuple | |
18 | expect([]).not_to be_blank_tuple | |
19 | 19 | end |
20 | 20 | |
21 | 21 | it 'should not pass be_comment_tuple' do |
22 | [].should_not be_comment_tuple | |
22 | expect([]).not_to be_comment_tuple | |
23 | 23 | end |
24 | 24 | end |
25 | 25 | |
33 | 33 | before(:all) { @tuple = [:section, "key", {:opt => "val"}] } |
34 | 34 | |
35 | 35 | it 'should pass be_section_tuple' do |
36 | @tuple.should be_section_tuple | |
36 | expect(@tuple).to be_section_tuple | |
37 | 37 | end |
38 | 38 | |
39 | 39 | it 'should pass be_section_tuple("key")' do |
40 | @tuple.should be_section_tuple("key") | |
40 | expect(@tuple).to be_section_tuple("key") | |
41 | 41 | end |
42 | 42 | |
43 | 43 | it 'should fail be_section_tuple("invalid")' do |
44 | @tuple.should_not be_section_tuple("invalid") | |
44 | expect(@tuple).not_to be_section_tuple("invalid") | |
45 | 45 | end |
46 | 46 | |
47 | 47 | it 'should pass be_section_tuple("key", {:opt => "val"})' do |
48 | @tuple.should be_section_tuple("key", {:opt => "val"}) | |
48 | expect(@tuple).to be_section_tuple("key", {:opt => "val"}) | |
49 | 49 | end |
50 | 50 | |
51 | 51 | it 'should not pass be_section_tuple("key", {:invalid => "val"})' do |
52 | @tuple.should_not be_section_tuple("key", {:invalid => "val"}) | |
52 | expect(@tuple).not_to be_section_tuple("key", {:invalid => "val"}) | |
53 | 53 | end |
54 | 54 | |
55 | 55 | it 'should not pass be_section_tuple("key", {:opt => "invalid"})' do |
56 | @tuple.should_not be_section_tuple("key", {:opt => "invalid"}) | |
56 | expect(@tuple).not_to be_section_tuple("key", {:opt => "invalid"}) | |
57 | 57 | end |
58 | 58 | |
59 | 59 | it 'should fail be_option_tuple' do |
60 | @tuple.should_not be_option_tuple | |
60 | expect(@tuple).not_to be_option_tuple | |
61 | 61 | end |
62 | 62 | |
63 | 63 | it 'should fail be_blank_tuple' do |
64 | @tuple.should_not be_blank_tuple | |
64 | expect(@tuple).not_to be_blank_tuple | |
65 | 65 | end |
66 | 66 | |
67 | 67 | it 'should fail be_comment_tuple' do |
68 | @tuple.should_not be_comment_tuple | |
68 | expect(@tuple).not_to be_comment_tuple | |
69 | 69 | end |
70 | 70 | end |
71 | 71 | |
79 | 79 | before(:all) { @tuple = [:option, "key", "val", {:opt => "val"}] } |
80 | 80 | |
81 | 81 | it 'should fail be_section_tuple' do |
82 | @tuple.should_not be_section_tuple | |
82 | expect(@tuple).not_to be_section_tuple | |
83 | 83 | end |
84 | 84 | |
85 | 85 | it 'should pass be_option_tuple' do |
86 | @tuple.should be_option_tuple | |
86 | expect(@tuple).to be_option_tuple | |
87 | 87 | end |
88 | 88 | |
89 | 89 | it 'should pass be_option_tuple("key")' do |
90 | @tuple.should be_option_tuple("key") | |
90 | expect(@tuple).to be_option_tuple("key") | |
91 | 91 | end |
92 | 92 | |
93 | 93 | it 'should fail be_option_tuple("invalid")' do |
94 | @tuple.should_not be_option_tuple("invalid") | |
94 | expect(@tuple).not_to be_option_tuple("invalid") | |
95 | 95 | end |
96 | 96 | |
97 | 97 | it 'should pass be_option_tuple("key", "val")' do |
98 | @tuple.should be_option_tuple("key", "val") | |
98 | expect(@tuple).to be_option_tuple("key", "val") | |
99 | 99 | end |
100 | 100 | |
101 | 101 | it 'should pass be_option_tuple(:any, "val")' do |
102 | @tuple.should be_option_tuple(:any, "val") | |
102 | expect(@tuple).to be_option_tuple(:any, "val") | |
103 | 103 | end |
104 | 104 | |
105 | 105 | it 'should fail be_option_tuple("key", "invalid")' do |
106 | @tuple.should_not be_option_tuple("key", "invalid") | |
106 | expect(@tuple).not_to be_option_tuple("key", "invalid") | |
107 | 107 | end |
108 | 108 | |
109 | 109 | it 'should pass be_option_tuple("key", "val", { :opt => "val" })' do |
110 | @tuple.should be_option_tuple("key", "val", { :opt => "val" }) | |
110 | expect(@tuple).to be_option_tuple("key", "val", { :opt => "val" }) | |
111 | 111 | end |
112 | 112 | |
113 | 113 | it 'should fail be_option_tuple("key", "val", { :invalid => "val" })' do |
114 | @tuple.should_not be_option_tuple("key", "val", { :invalid => "val" }) | |
114 | expect(@tuple).not_to be_option_tuple("key", "val", { :invalid => "val" }) | |
115 | 115 | end |
116 | 116 | |
117 | 117 | it 'should fail be_option_tuple("key", "val", { :opt => "invalid" })' do |
118 | @tuple.should_not be_option_tuple("key", "val", { :opt => "invalid" }) | |
118 | expect(@tuple).not_to be_option_tuple("key", "val", { :opt => "invalid" }) | |
119 | 119 | end |
120 | 120 | |
121 | 121 | it 'should fail be_blank_tuple' do |
122 | @tuple.should_not be_blank_tuple | |
122 | expect(@tuple).not_to be_blank_tuple | |
123 | 123 | end |
124 | 124 | |
125 | 125 | it 'should fail be_comment_tuple' do |
126 | @tuple.should_not be_comment_tuple | |
126 | expect(@tuple).not_to be_comment_tuple | |
127 | 127 | end |
128 | 128 | end |
129 | 129 | |
137 | 137 | before(:all) { @tuple = [:blank] } |
138 | 138 | |
139 | 139 | it 'should fail be_section_tuple' do |
140 | @tuple.should_not be_section_tuple | |
140 | expect(@tuple).not_to be_section_tuple | |
141 | 141 | end |
142 | 142 | |
143 | 143 | it 'should fail be_option_tuple' do |
144 | @tuple.should_not be_option_tuple | |
144 | expect(@tuple).not_to be_option_tuple | |
145 | 145 | end |
146 | 146 | |
147 | 147 | it 'should pass be_blank_tuple' do |
148 | @tuple.should be_blank_tuple | |
148 | expect(@tuple).to be_blank_tuple | |
149 | 149 | end |
150 | 150 | |
151 | 151 | it 'should fail be_comment_tuple' do |
152 | @tuple.should_not be_comment_tuple | |
152 | expect(@tuple).not_to be_comment_tuple | |
153 | 153 | end |
154 | 154 | end |
155 | 155 | |
163 | 163 | before(:all) { @tuple = [:comment, "A comment", {:opt => "val"}] } |
164 | 164 | |
165 | 165 | it 'should fail be_section_tuple' do |
166 | @tuple.should_not be_section_tuple | |
166 | expect(@tuple).not_to be_section_tuple | |
167 | 167 | end |
168 | 168 | |
169 | 169 | it 'should fail be_option_tuple' do |
170 | @tuple.should_not be_option_tuple | |
170 | expect(@tuple).not_to be_option_tuple | |
171 | 171 | end |
172 | 172 | |
173 | 173 | it 'should fail be_blank_tuple' do |
174 | @tuple.should_not be_blank_tuple | |
174 | expect(@tuple).not_to be_blank_tuple | |
175 | 175 | end |
176 | 176 | |
177 | 177 | it 'should pass be_comment_tuple' do |
178 | @tuple.should be_comment_tuple | |
178 | expect(@tuple).to be_comment_tuple | |
179 | 179 | end |
180 | 180 | |
181 | 181 | it 'should pass be_comment_tuple("A comment")' do |
182 | @tuple.should be_comment_tuple("A comment") | |
182 | expect(@tuple).to be_comment_tuple("A comment") | |
183 | 183 | end |
184 | 184 | |
185 | 185 | it 'should fail be_comment_tuple("Invalid")' do |
186 | @tuple.should_not be_comment_tuple("Invalid") | |
186 | expect(@tuple).not_to be_comment_tuple("Invalid") | |
187 | 187 | end |
188 | 188 | |
189 | 189 | it 'should pass be_comment_tuple("A comment", {:opt => "val"})' do |
190 | @tuple.should be_comment_tuple("A comment", {:opt => "val"}) | |
190 | expect(@tuple).to be_comment_tuple("A comment", {:opt => "val"}) | |
191 | 191 | end |
192 | 192 | |
193 | 193 | it 'should fail be_comment_tuple("A comment", {:invalid => "val"})' do |
194 | @tuple.should_not be_comment_tuple("A comment", {:invalid => "val"}) | |
194 | expect(@tuple).not_to be_comment_tuple("A comment", {:invalid => "val"}) | |
195 | 195 | end |
196 | 196 | |
197 | 197 | it 'should fail be_comment_tuple("A comment", {:opt => "invalid"})' do |
198 | @tuple.should_not be_comment_tuple("A comment", {:opt => "invalid"}) | |
199 | end | |
200 | end | |
198 | expect(@tuple).not_to be_comment_tuple("A comment", {:opt => "invalid"}) | |
199 | end | |
200 | end |