Skip to content

Commit 646e98f

Browse files
authored
Merge pull request ruby#889 from nobu/attr-args
Allow boolean arguments to `rb_attr` and `rb_define_attr`
2 parents fec1ab2 + 169dc02 commit 646e98f

File tree

2 files changed

+32
-78
lines changed

2 files changed

+32
-78
lines changed

lib/rdoc/parser/c.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ class RDoc::Parser::C < RDoc::Parser
122122

123123
include RDoc::Text
124124

125+
#--
126+
BOOL_ARG_PATTERN = /\s*+\b([01]|Q?(?:true|false)|TRUE|FALSE)\b\s*/
127+
TRUE_VALUES = ['1', 'TRUE', 'true', 'Qtrue'].freeze
128+
#++
129+
125130
##
126131
# Maps C variable names to names of Ruby classes or modules
127132

@@ -259,18 +264,18 @@ def do_attrs
259264
@content.scan(/rb_attr\s*\(
260265
\s*(\w+),
261266
\s*([\w"()]+),
262-
\s*([01]),
263-
\s*([01]),
264-
\s*\w+\);/xm) do |var_name, attr_name, read, write|
267+
#{BOOL_ARG_PATTERN},
268+
#{BOOL_ARG_PATTERN},
269+
\s*\w+\);/xmo) do |var_name, attr_name, read, write|
265270
handle_attr var_name, attr_name, read, write
266271
end
267272

268273
@content.scan(%r%rb_define_attr\(
269274
\s*([\w\.]+),
270275
\s*"([^"]+)",
271-
\s*(\d+),
272-
\s*(\d+)\s*\);
273-
%xm) do |var_name, attr_name, read, write|
276+
#{BOOL_ARG_PATTERN},
277+
#{BOOL_ARG_PATTERN}\);
278+
%xmo) do |var_name, attr_name, read, write|
274279
handle_attr var_name, attr_name, read, write
275280
end
276281
end
@@ -815,8 +820,8 @@ def find_override_comment class_name, meth_obj
815820

816821
def handle_attr(var_name, attr_name, read, write)
817822
rw = ''
818-
rw += 'R' if '1' == read
819-
rw += 'W' if '1' == write
823+
rw += 'R' if TRUE_VALUES.include?(read)
824+
rw += 'W' if TRUE_VALUES.include?(write)
820825

821826
class_name = @known_classes[var_name]
822827

test/rdoc/test_rdoc_parser_c.rb

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,25 @@ def test_initialize
132132
assert_equal expected, known_classes
133133
end
134134

135-
def test_do_attr_rb_attr
135+
def assert_do_attr(flags)
136136
content = <<-EOF
137137
void Init_Blah(void) {
138138
cBlah = rb_define_class("Blah", rb_cObject);
139139
140140
/*
141141
* This is an accessor
142142
*/
143-
rb_attr(cBlah, rb_intern("accessor"), 1, 1, Qfalse);
143+
#{yield "cBlah", "accessor", flags[1], flags[1]};
144144
145145
/*
146146
* This is a reader
147147
*/
148-
rb_attr(cBlah, rb_intern("reader"), 1, 0, Qfalse);
148+
#{yield "cBlah", "reader", flags[1], flags[0]};
149149
150150
/*
151151
* This is a writer
152152
*/
153-
rb_attr(cBlah, rb_intern("writer"), 0, 1, Qfalse);
153+
#{yield "cBlah", "writer", flags[0], flags[1]};
154154
}
155155
EOF
156156

@@ -176,72 +176,21 @@ def test_do_attr_rb_attr
176176
assert_equal 'This is a writer', writer.comment.text
177177
end
178178

179-
def test_do_attr_rb_attr_2
180-
content = <<-EOF
181-
void Init_Blah(void) {
182-
cBlah = rb_define_class("Blah", rb_cObject);
183-
184-
/*
185-
* This is an accessor
186-
*/
187-
rb_attr(cBlah, rb_intern_const("accessor"), 1, 1, Qfalse);
188-
189-
/*
190-
* This is a reader
191-
*/
192-
rb_attr(cBlah, rb_intern_const("reader"), 1, 0, Qfalse);
193-
194-
/*
195-
* This is a writer
196-
*/
197-
rb_attr(cBlah, rb_intern_const("writer"), 0, 1, Qfalse);
198-
}
199-
EOF
200-
201-
klass = util_get_class content, 'cBlah'
202-
203-
attrs = klass.attributes
204-
assert_equal 3, attrs.length, attrs.inspect
205-
206-
accessor = attrs.shift
207-
assert_equal 'accessor', accessor.name
208-
assert_equal 'RW', accessor.rw
209-
assert_equal 'This is an accessor', accessor.comment.text
210-
assert_equal @top_level, accessor.file
211-
212-
reader = attrs.shift
213-
assert_equal 'reader', reader.name
214-
assert_equal 'R', reader.rw
215-
assert_equal 'This is a reader', reader.comment.text
216-
217-
writer = attrs.shift
218-
assert_equal 'writer', writer.name
219-
assert_equal 'W', writer.rw
220-
assert_equal 'This is a writer', writer.comment.text
221-
end
222-
223-
def test_do_attr_rb_define_attr
224-
content = <<-EOF
225-
void Init_Blah(void) {
226-
cBlah = rb_define_class("Blah", rb_cObject);
227-
228-
/*
229-
* This is an accessor
230-
*/
231-
rb_define_attr(cBlah, "accessor", 1, 1);
232-
}
233-
EOF
234-
235-
klass = util_get_class content, 'cBlah'
236-
237-
attrs = klass.attributes
238-
assert_equal 1, attrs.length, attrs.inspect
239-
240-
accessor = attrs.shift
241-
assert_equal 'accessor', accessor.name
242-
assert_equal 'RW', accessor.rw
243-
assert_equal 'This is an accessor', accessor.comment.text
244-
assert_equal @top_level, accessor.file
179+
{
180+
num: %w[0 1],
181+
macro: %w[FALSE TRUE],
182+
ruby: %w[Qfalse Qtrue],
183+
bool: %w[false true],
184+
}.each_pair do |name, values|
185+
define_method("test_do_attr:rb_attr:intern:#{name}") do
186+
assert_do_attr(values) {|c, name, r, w| %[rb_attr(#{c}, rb_intern("#{name}"), #{r}, #{w}, Qfalse)]}
187+
end
188+
define_method("test_do_attr:rb_attr:intern_const:#{name}") do
189+
assert_do_attr(values) {|c, name, r, w| %[rb_attr(#{c}, rb_intern_const("#{name}"), #{r}, #{w}, Qfalse)]}
190+
end
191+
define_method("test_do_attr:rb_define_attr:#{name}") do
192+
assert_do_attr(values) {|c, name, r, w| %[rb_define_attr(#{c}, "#{name}", #{r}, #{w})]}
193+
end
245194
end
246195

247196
def test_do_aliases

0 commit comments

Comments
 (0)