Skip to content

Commit

Permalink
Use SyntaxError instead of NameError. Closes #144
Browse files Browse the repository at this point in the history
  • Loading branch information
dmendel committed Oct 30, 2022
1 parent 7cbd2d3 commit 3a0b75f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions ChangeLog.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Use Comparable#clamp instead of manual calculations.
* Update tests to new minitest requirements.
* Adjust tests due to changes in ruby 3.1

== Version 2.4.13 (2022-10-16)

Expand Down
6 changes: 3 additions & 3 deletions lib/bindata/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,19 @@ def validate_field(name)
def ensure_valid_name(name)
if name && !option?(:fieldnames_are_values)
if malformed_name?(name)
raise NameError.new("", name), "field '#{name}' is an illegal fieldname"
raise SyntaxError, "field '#{name}' is an illegal fieldname"
end

if duplicate_name?(name)
raise SyntaxError, "duplicate field '#{name}'"
end

if name_shadows_method?(name)
raise NameError.new("", name), "field '#{name}' shadows an existing method"
raise SyntaxError, "field '#{name}' shadows an existing method"
end

if name_is_reserved?(name)
raise NameError.new("", name), "field '#{name}' is a reserved name"
raise SyntaxError, "field '#{name}' is a reserved name"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/primitive_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DuplicateNamePrimitive < BinData::Primitive
class ExistingNamePrimitive < BinData::Primitive
int8 :object_id
end
}.must_raise_on_line NameError, 2, "field 'object_id' shadows an existing method in ExistingNamePrimitive"
}.must_raise_on_line SyntaxError, 2, "field 'object_id' shadows an existing method in ExistingNamePrimitive"
end

it "fails on unknown endian" do
Expand Down
6 changes: 3 additions & 3 deletions test/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MalformedNameRecord < BinData::Record
int8 :a
int8 "45"
end
}.must_raise_on_line NameError, 3, "field '45' is an illegal fieldname in MalformedNameRecord"
}.must_raise_on_line SyntaxError, 3, "field '45' is an illegal fieldname in MalformedNameRecord"
end

it "fails on duplicate names" do
Expand All @@ -73,15 +73,15 @@ class ReservedNameRecord < BinData::Record
int8 :a
int8 :invert # from Hash.instance_methods
end
}.must_raise_on_line NameError, 3, "field 'invert' is a reserved name in ReservedNameRecord"
}.must_raise_on_line SyntaxError, 3, "field 'invert' is a reserved name in ReservedNameRecord"
end

it "fails when field name shadows an existing method" do
_ {
class ExistingNameRecord < BinData::Record
int8 :object_id
end
}.must_raise_on_line NameError, 2, "field 'object_id' shadows an existing method in ExistingNameRecord"
}.must_raise_on_line SyntaxError, 2, "field 'object_id' shadows an existing method in ExistingNameRecord"
end

it "fails on unknown endian" do
Expand Down

0 comments on commit 3a0b75f

Please sign in to comment.