Skip to content

Commit

Permalink
Add enum reserved checks, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Apr 19, 2024
1 parent dc8658a commit 4565e24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/protoboeuf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,16 @@ def self.parse_enum(input, pos)
numbers_taken.add(constant.number)
end

# Check that reserved constant numbers are not used
constants.each do |constant|
# For each reserved range
reserved.each do |r|
if (r.respond_to?(:include?) && r.include?(constant.number)) || r == constant.number
raise ParseError.new("constant #{constant.name} uses reserved constant number #{constant.number}", constant.pos)
end
end
end

Enum.new(enum_name, constants, options, pos)
end

Expand Down
7 changes: 7 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ def test_hex_enum_const
assert_equal (-0xBA), unit.enums[0].constants[1].number
end

def test_enum_reserved
ProtoBoeuf.parse_string('enum Foo { C0 = 0; C1 = 1; reserved 5; }')
assert_raises { ProtoBoeuf.parse_string('enum Foo { C0 = 0; C1 = 1; reserved 1; }') }
assert_raises { ProtoBoeuf.parse_string('enum Foo { C0 = 0; C1 = 5; reserved 1 to 10; }') }
assert_raises { ProtoBoeuf.parse_string('enum Foo { C0 = 0; C1 = 5; reserved 2 to max; }') }
end

def test_package_name
unit = ProtoBoeuf.parse_string('package foo;')
assert_equal 'foo', unit.package
Expand Down

0 comments on commit 4565e24

Please sign in to comment.