iniparse 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'iniparse'
3
+ require File.join(File.dirname(__FILE__), 'spec_fixtures')
4
+
5
+ module IniParse
6
+ module Test
7
+ module Helpers
8
+ # Taken from Merb Core's spec helper.
9
+ # Merb is licenced using the MIT License and is copyright
10
+ # Engine Yard Inc.
11
+ class BeKindOf
12
+ def initialize(expected) # + args
13
+ @expected = expected
14
+ end
15
+
16
+ def matches?(target)
17
+ @target = target
18
+ @target.kind_of?(@expected)
19
+ end
20
+
21
+ def failure_message
22
+ "expected #{@expected} but got #{@target.class}"
23
+ end
24
+
25
+ def negative_failure_message
26
+ "expected #{@expected} to not be #{@target.class}"
27
+ end
28
+
29
+ def description
30
+ "be_kind_of #{@target}"
31
+ end
32
+ end
33
+
34
+ # Used to match line tuples returned by Parser.parse_line.
35
+ class BeLineTuple
36
+ def initialize(type, value_keys = [], *expected)
37
+ @expected_type = type
38
+ @value_keys = value_keys
39
+
40
+ if expected.nil?
41
+ @expected_opts = {}
42
+ @expected_values = []
43
+ else
44
+ @expected_opts = expected.pop
45
+ @expected_values = expected
46
+ end
47
+ end
48
+
49
+ def matches?(tuple)
50
+ @tuple = tuple
51
+
52
+ @failure_message = catch(:fail) do
53
+ tuple?
54
+ correct_type?
55
+ correct_values?
56
+ correct_opts?
57
+ true
58
+ end
59
+
60
+ @failure_message == true
61
+ end
62
+
63
+ def failure_message
64
+ "expected #{@expected_type} tuple #{@failure_message}"
65
+ end
66
+
67
+ def negative_failure_message
68
+ "expected #{@tuple.inspect} to not be #{@expected_type} tuple"
69
+ end
70
+
71
+ def description
72
+ "be_#{@expected_type}_tuple #{@tuple}"
73
+ end
74
+
75
+ #######
76
+ private
77
+ #######
78
+
79
+ # Matchers.
80
+
81
+ def tuple?
82
+ throw :fail, "but was #{@tuple.class}" unless @tuple.kind_of?(Array)
83
+ end
84
+
85
+ def correct_type?
86
+ throw :fail, "but was #{type} tuple" unless type == @expected_type
87
+ end
88
+
89
+ def correct_values?
90
+ # Make sure the values match.
91
+ @value_keys.each_with_index do |key, i|
92
+ if @expected_values[i] != :any && values[i] != @expected_values[i]
93
+ throw :fail, 'with %s value of "%s" but was "%s"' % [
94
+ key, values[i], @expected_values[i]
95
+ ]
96
+ end
97
+ end
98
+ end
99
+
100
+ def correct_opts?
101
+ if(! @expected_opts.nil?)
102
+ if (! @expected_opts.empty?) && opts.empty?
103
+ throw :fail, 'with options but there were none'
104
+ end
105
+
106
+ @expected_opts.each do |key, value|
107
+ unless opts.has_key?(key)
108
+ throw :fail, 'with option "%s", but key was not present' % key
109
+ end
110
+
111
+ unless opts[key] == value
112
+ throw :fail, 'with option "%s" => "%s" but was "%s"' % [
113
+ key, value, opts[key]
114
+ ]
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ # Tuple values, etc.
121
+
122
+ def type
123
+ @type ||= @tuple.first
124
+ end
125
+
126
+ def values
127
+ @values ||= @tuple.length < 3 ? [] : @tuple[1..-2]
128
+ end
129
+
130
+ def opts
131
+ @opts ||= @tuple.last
132
+ end
133
+ end
134
+
135
+ def be_kind_of(expected) # + args
136
+ BeKindOf.new(expected)
137
+ end
138
+
139
+ def be_section_tuple(key = :any, opts = {})
140
+ BeLineTuple.new(:section, [:key], key, opts)
141
+ end
142
+
143
+ def be_option_tuple(key = :any, value = :any, opts = {})
144
+ BeLineTuple.new(:option, [:key, :value], key, value, opts)
145
+ end
146
+
147
+ def be_blank_tuple
148
+ BeLineTuple.new(:blank)
149
+ end
150
+
151
+ def be_comment_tuple(comment = :any, opts = {})
152
+ BeLineTuple.new(:comment, [:comment], comment, opts)
153
+ end
154
+
155
+ def fixture(fix)
156
+ IniParse::Test::Fixtures[fix]
157
+ end
158
+ end
159
+ end
160
+ end
161
+
162
+ Spec::Runner.configure do |config|
163
+ config.include(IniParse::Test::Helpers)
164
+ end
@@ -0,0 +1,201 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ # --
4
+ # ============================================================================
5
+ # Empty array.
6
+ # ============================================================================
7
+ # ++
8
+
9
+ describe 'An empty array' do
10
+ it 'should not pass be_section_tuple' do
11
+ [].should_not be_section_tuple
12
+ end
13
+
14
+ it 'should not pass be_option_tuple' do
15
+ [].should_not be_option_tuple
16
+ end
17
+
18
+ it 'should not pass be_blank_tuple' do
19
+ [].should_not be_blank_tuple
20
+ end
21
+
22
+ it 'should not pass be_comment_tuple' do
23
+ [].should_not be_comment_tuple
24
+ end
25
+ end
26
+
27
+ # --
28
+ # ============================================================================
29
+ # Section tuple.
30
+ # ============================================================================
31
+ # ++
32
+
33
+ describe 'Line tuple [:section, "key", {:opt => "val"}]' do
34
+ before(:all) { @tuple = [:section, "key", {:opt => "val"}] }
35
+
36
+ it 'should pass be_section_tuple' do
37
+ @tuple.should be_section_tuple
38
+ end
39
+
40
+ it 'should pass be_section_tuple("key")' do
41
+ @tuple.should be_section_tuple("key")
42
+ end
43
+
44
+ it 'should fail be_section_tuple("invalid")' do
45
+ @tuple.should_not be_section_tuple("invalid")
46
+ end
47
+
48
+ it 'should pass be_section_tuple("key", {:opt => "val"})' do
49
+ @tuple.should be_section_tuple("key", {:opt => "val"})
50
+ end
51
+
52
+ it 'should not pass be_section_tuple("key", {:invalid => "val"})' do
53
+ @tuple.should_not be_section_tuple("key", {:invalid => "val"})
54
+ end
55
+
56
+ it 'should not pass be_section_tuple("key", {:opt => "invalid"})' do
57
+ @tuple.should_not be_section_tuple("key", {:opt => "invalid"})
58
+ end
59
+
60
+ it 'should fail be_option_tuple' do
61
+ @tuple.should_not be_option_tuple
62
+ end
63
+
64
+ it 'should fail be_blank_tuple' do
65
+ @tuple.should_not be_blank_tuple
66
+ end
67
+
68
+ it 'should fail be_comment_tuple' do
69
+ @tuple.should_not be_comment_tuple
70
+ end
71
+ end
72
+
73
+ # --
74
+ # ============================================================================
75
+ # Option tuple.
76
+ # ============================================================================
77
+ # ++
78
+
79
+ describe 'Line tuple [:option, "key", "val", {:opt => "val"}]' do
80
+ before(:all) { @tuple = [:option, "key", "val", {:opt => "val"}] }
81
+
82
+ it 'should fail be_section_tuple' do
83
+ @tuple.should_not be_section_tuple
84
+ end
85
+
86
+ it 'should pass be_option_tuple' do
87
+ @tuple.should be_option_tuple
88
+ end
89
+
90
+ it 'should pass be_option_tuple("key")' do
91
+ @tuple.should be_option_tuple("key")
92
+ end
93
+
94
+ it 'should fail be_option_tuple("invalid")' do
95
+ @tuple.should_not be_option_tuple("invalid")
96
+ end
97
+
98
+ it 'should pass be_option_tuple("key", "val")' do
99
+ @tuple.should be_option_tuple("key", "val")
100
+ end
101
+
102
+ it 'should pass be_option_tuple(:any, "val")' do
103
+ @tuple.should be_option_tuple(:any, "val")
104
+ end
105
+
106
+ it 'should fail be_option_tuple("key", "invalid")' do
107
+ @tuple.should_not be_option_tuple("key", "invalid")
108
+ end
109
+
110
+ it 'should pass be_option_tuple("key", "val", { :opt => "val" })' do
111
+ @tuple.should be_option_tuple("key", "val", { :opt => "val" })
112
+ end
113
+
114
+ it 'should fail be_option_tuple("key", "val", { :invalid => "val" })' do
115
+ @tuple.should_not be_option_tuple("key", "val", { :invalid => "val" })
116
+ end
117
+
118
+ it 'should fail be_option_tuple("key", "val", { :opt => "invalid" })' do
119
+ @tuple.should_not be_option_tuple("key", "val", { :opt => "invalid" })
120
+ end
121
+
122
+ it 'should fail be_blank_tuple' do
123
+ @tuple.should_not be_blank_tuple
124
+ end
125
+
126
+ it 'should fail be_comment_tuple' do
127
+ @tuple.should_not be_comment_tuple
128
+ end
129
+ end
130
+
131
+ # --
132
+ # ============================================================================
133
+ # Blank tuple.
134
+ # ============================================================================
135
+ # ++
136
+
137
+ describe 'Line tuple [:blank]' do
138
+ before(:all) { @tuple = [:blank] }
139
+
140
+ it 'should fail be_section_tuple' do
141
+ @tuple.should_not be_section_tuple
142
+ end
143
+
144
+ it 'should fail be_option_tuple' do
145
+ @tuple.should_not be_option_tuple
146
+ end
147
+
148
+ it 'should pass be_blank_tuple' do
149
+ @tuple.should be_blank_tuple
150
+ end
151
+
152
+ it 'should fail be_comment_tuple' do
153
+ @tuple.should_not be_comment_tuple
154
+ end
155
+ end
156
+
157
+ # --
158
+ # ============================================================================
159
+ # Coment tuple.
160
+ # ============================================================================
161
+ # ++
162
+
163
+ describe 'Line tuple [:comment, "A comment", {:opt => "val"}]' do
164
+ before(:all) { @tuple = [:comment, "A comment", {:opt => "val"}] }
165
+
166
+ it 'should fail be_section_tuple' do
167
+ @tuple.should_not be_section_tuple
168
+ end
169
+
170
+ it 'should fail be_option_tuple' do
171
+ @tuple.should_not be_option_tuple
172
+ end
173
+
174
+ it 'should fail be_blank_tuple' do
175
+ @tuple.should_not be_blank_tuple
176
+ end
177
+
178
+ it 'should pass be_comment_tuple' do
179
+ @tuple.should be_comment_tuple
180
+ end
181
+
182
+ it 'should pass be_comment_tuple("A comment")' do
183
+ @tuple.should be_comment_tuple("A comment")
184
+ end
185
+
186
+ it 'should fail be_comment_tuple("Invalid")' do
187
+ @tuple.should_not be_comment_tuple("Invalid")
188
+ end
189
+
190
+ it 'should pass be_comment_tuple("A comment", {:opt => "val"})' do
191
+ @tuple.should be_comment_tuple("A comment", {:opt => "val"})
192
+ end
193
+
194
+ it 'should fail be_comment_tuple("A comment", {:invalid => "val"})' do
195
+ @tuple.should_not be_comment_tuple("A comment", {:invalid => "val"})
196
+ end
197
+
198
+ it 'should fail be_comment_tuple("A comment", {:opt => "invalid"})' do
199
+ @tuple.should_not be_comment_tuple("A comment", {:opt => "invalid"})
200
+ end
201
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iniparse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-13 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: extlib
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.9
24
+ version:
25
+ description: A pure Ruby library for parsing INI documents.
26
+ email: anthony@ninecraft.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - LICENSE
34
+ files:
35
+ - LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - lib/iniparse
39
+ - lib/iniparse/document.rb
40
+ - lib/iniparse/generator.rb
41
+ - lib/iniparse/line_collection.rb
42
+ - lib/iniparse/lines.rb
43
+ - lib/iniparse/parser.rb
44
+ - lib/iniparse/version.rb
45
+ - lib/iniparse.rb
46
+ - spec/document_spec.rb
47
+ - spec/fixture_spec.rb
48
+ - spec/fixtures
49
+ - spec/fixtures/openttd.ini
50
+ - spec/fixtures/race07.ini
51
+ - spec/fixtures/smb.ini
52
+ - spec/generator
53
+ - spec/generator/method_missing_spec.rb
54
+ - spec/generator/with_section_blocks_spec.rb
55
+ - spec/generator/without_section_blocks_spec.rb
56
+ - spec/iniparse_spec.rb
57
+ - spec/line_collection_spec.rb
58
+ - spec/lines_spec.rb
59
+ - spec/parser
60
+ - spec/parser/document_parsing_spec.rb
61
+ - spec/parser/line_parsing_spec.rb
62
+ - spec/spec_fixtures.rb
63
+ - spec/spec_helper.rb
64
+ - spec/spec_helper_spec.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/anthonyw/iniparse
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project: iniparse
87
+ rubygems_version: 1.3.0
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: A pure Ruby library for parsing INI documents.
91
+ test_files: []
92
+