[source_span] Preserve symmetric SourceSpan equality and add FileSpan equality tests
diff --git a/pkgs/source_span/lib/src/file.dart b/pkgs/source_span/lib/src/file.dart index 6de8484..8aa5814 100644 --- a/pkgs/source_span/lib/src/file.dart +++ b/pkgs/source_span/lib/src/file.dart
@@ -388,14 +388,12 @@ if (other is _FileSpan) { return _start == other._start && _end == other._end && - sourceUrl == other.sourceUrl && - (file == other.file || text == other.text); + sourceUrl == other.sourceUrl; } if (other is SourceSpan) { return _start == other.start.offset && _end == other.end.offset && - sourceUrl == other.sourceUrl && - text == other.text; + sourceUrl == other.sourceUrl; } return false; }
diff --git a/pkgs/source_span/test/file_test.dart b/pkgs/source_span/test/file_test.dart index e51d61b..5b957e1 100644 --- a/pkgs/source_span/test/file_test.dart +++ b/pkgs/source_span/test/file_test.dart
@@ -533,5 +533,31 @@ }); }); }); + + group('equality', () { + test('two FileSpans with the same offsets and URL are equal', () { + final span1 = file.span(5, 12); + final span2 = file.span(5, 12); + expect(span1, equals(span2)); + expect(span1.hashCode, equals(span2.hashCode)); + }); + + test('FileSpan and SourceSpan equality is symmetric', () { + final fileSpan = file.span(5, 12); + final sourceSpan = SourceSpan(SourceLocation(5, sourceUrl: 'foo.dart'), + SourceLocation(12, sourceUrl: 'foo.dart'), 'other!!'); + expect(fileSpan == sourceSpan, isTrue); + expect(sourceSpan == fileSpan, isTrue); + }); + + test('FileSpans with different offsets or URLs are not equal', () { + final span = file.span(5, 12); + expect(span, isNot(equals(file.span(6, 12)))); + expect(span, isNot(equals(file.span(5, 11)))); + final otherFile = + SourceFile.fromString('foo bar baz\nwhiz bang boom', url: 'b.dart'); + expect(span, isNot(equals(otherFile.span(5, 12)))); + }); + }); }); }