From b11cda4ef7de798821b609deb762b1215d47084a Mon Sep 17 00:00:00 2001
From: Maikel <maikel@email.org.au>
Date: Wed, 10 Apr 2024 00:12:05 +1000
Subject: [PATCH] Allow comparison with nil (#3)

* Allow comparison with nil

* Update changelog: Allow comparison with nil
---
 CHANGELOG.md                                                  | 4 ++++
 lib/virtual_assembly/semantizer/semantic_object.rb            | 2 ++
 lib/virtual_assembly/semantizer/semantic_property.rb          | 2 +-
 spec/lib/virtual_assembly/semantizer/semantic_object_spec.rb  | 1 +
 .../lib/virtual_assembly/semantizer/semantic_property_spec.rb | 1 +
 5 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 102023a..dc38601 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Objects can be compared to `nil` with `==`.
+
 ## [1.1.0] - 2024-01-25
 
 ### Added
diff --git a/lib/virtual_assembly/semantizer/semantic_object.rb b/lib/virtual_assembly/semantizer/semantic_object.rb
index f37e2a8..19694fe 100644
--- a/lib/virtual_assembly/semantizer/semantic_object.rb
+++ b/lib/virtual_assembly/semantizer/semantic_object.rb
@@ -142,6 +142,8 @@ def serialize(serializer)
       end
 
       def ==(other)
+        return false unless other.respond_to?(:semanticProperties)
+
         semanticProperties == other.semanticProperties
       end
 
diff --git a/lib/virtual_assembly/semantizer/semantic_property.rb b/lib/virtual_assembly/semantizer/semantic_property.rb
index 4b10d32..564d914 100644
--- a/lib/virtual_assembly/semantizer/semantic_property.rb
+++ b/lib/virtual_assembly/semantizer/semantic_property.rb
@@ -74,7 +74,7 @@ def value=(new_value)
       end
 
       def ==(other)
-        name == other.name && value == other.value
+        other && name == other.name && value == other.value
       end
     end
   end
diff --git a/spec/lib/virtual_assembly/semantizer/semantic_object_spec.rb b/spec/lib/virtual_assembly/semantizer/semantic_object_spec.rb
index b3e7213..3ba0e5b 100644
--- a/spec/lib/virtual_assembly/semantizer/semantic_object_spec.rb
+++ b/spec/lib/virtual_assembly/semantizer/semantic_object_spec.rb
@@ -37,6 +37,7 @@
     expect(object).to eq object.clone
     expect(object).to eq other_clazz.new
 
+    expect(object).to_not eq nil
     expect(object).to_not eq other_clazz.new("id5")
     expect(object).to_not eq other_clazz.new(nil, "typeA")
 
diff --git a/spec/lib/virtual_assembly/semantizer/semantic_property_spec.rb b/spec/lib/virtual_assembly/semantizer/semantic_property_spec.rb
index 8e731fe..9bca969 100644
--- a/spec/lib/virtual_assembly/semantizer/semantic_property_spec.rb
+++ b/spec/lib/virtual_assembly/semantizer/semantic_property_spec.rb
@@ -31,5 +31,6 @@
     expect(green).to eq green_colour
     expect(green).to_not eq green_grass
     expect(green).to_not eq purple
+    expect(green).to_not eq nil
   end
 end