-
Notifications
You must be signed in to change notification settings - Fork 64
Add opt-in binary string diff support #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gschlager
wants to merge
2
commits into
splitwise:main
Choose a base branch
from
gschlager:binary-diff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'super_diff/binary_string/differs' | ||
| require 'super_diff/binary_string/inspection_tree_builders' | ||
| require 'super_diff/binary_string/operation_trees' | ||
| require 'super_diff/binary_string/operation_tree_builders' | ||
| require 'super_diff/binary_string/operation_tree_flatteners' | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| def self.applies_to?(*values) | ||
| values.all? { |value| value.is_a?(::String) && value.encoding == Encoding::ASCII_8BIT } | ||
| end | ||
|
|
||
| SuperDiff.configure do |config| | ||
| config.prepend_extra_differ_classes(Differs::BinaryString) | ||
| config.prepend_extra_inspection_tree_builder_classes( | ||
| InspectionTreeBuilders::BinaryString | ||
| ) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module Differs | ||
| autoload( | ||
| :BinaryString, | ||
| 'super_diff/binary_string/differs/binary_string' | ||
| ) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module Differs | ||
| class BinaryString < Core::AbstractDiffer | ||
| def self.applies_to?(expected, actual) | ||
| SuperDiff::BinaryString.applies_to?(expected, actual) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def operation_tree_builder_class | ||
| OperationTreeBuilders::BinaryString | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module InspectionTreeBuilders | ||
| autoload( | ||
| :BinaryString, | ||
| 'super_diff/binary_string/inspection_tree_builders/binary_string' | ||
| ) | ||
| end | ||
| end | ||
| end |
19 changes: 19 additions & 0 deletions
19
lib/super_diff/binary_string/inspection_tree_builders/binary_string.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module InspectionTreeBuilders | ||
| class BinaryString < Core::AbstractInspectionTreeBuilder | ||
| def self.applies_to?(value) | ||
| SuperDiff::BinaryString.applies_to?(value) | ||
| end | ||
|
|
||
| def call | ||
| Core::InspectionTree.new do |t| | ||
| t.add_text "<binary string (#{object.bytesize} bytes)>" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module OperationTreeBuilders | ||
| autoload( | ||
| :BinaryString, | ||
| 'super_diff/binary_string/operation_tree_builders/binary_string' | ||
| ) | ||
| end | ||
| end | ||
| end |
60 changes: 60 additions & 0 deletions
60
lib/super_diff/binary_string/operation_tree_builders/binary_string.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module OperationTreeBuilders | ||
| class BinaryString < Basic::OperationTreeBuilders::MultilineString | ||
| BYTES_PER_LINE = 16 | ||
| private_constant :BYTES_PER_LINE | ||
|
|
||
| def self.applies_to?(expected, actual) | ||
| SuperDiff::BinaryString.applies_to?(expected, actual) | ||
| end | ||
|
|
||
| def initialize(*args) | ||
| args.first[:expected] = binary_to_hex(args.first[:expected]) | ||
| args.first[:actual] = binary_to_hex(args.first[:actual]) | ||
|
|
||
| super | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def build_operation_tree | ||
| OperationTrees::BinaryString.new([]) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def split_into_lines(string) | ||
| super.map { |line| line.delete_suffix("\n") }.reject(&:empty?) | ||
| end | ||
|
|
||
| def binary_to_hex(data) | ||
| data | ||
| .each_byte | ||
| .each_slice(BYTES_PER_LINE) | ||
| .with_index | ||
| .map { |bytes, index| format_hex_line(index * BYTES_PER_LINE, bytes) } | ||
| .join("\n") | ||
| end | ||
|
|
||
| def format_hex_line(offset, bytes) | ||
| hex_pairs = bytes | ||
| .map { |b| format('%02x', b) } | ||
| .each_slice(2) | ||
| .map(&:join) | ||
| .join(' ') | ||
|
|
||
| ascii = bytes.map { |b| printable_char(b) }.join | ||
|
|
||
| format('%<offset>08x: %<hex>-39s %<ascii>s', offset:, hex: hex_pairs, ascii:) | ||
| end | ||
|
|
||
| def printable_char(byte) | ||
| byte >= 32 && byte < 127 ? byte.chr : '.' | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module OperationTreeFlatteners | ||
| autoload( | ||
| :BinaryString, | ||
| 'super_diff/binary_string/operation_tree_flatteners/binary_string' | ||
| ) | ||
| end | ||
| end | ||
| end |
19 changes: 19 additions & 0 deletions
19
lib/super_diff/binary_string/operation_tree_flatteners/binary_string.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module OperationTreeFlatteners | ||
| class BinaryString < Core::AbstractOperationTreeFlattener | ||
| def build_tiered_lines | ||
| operation_tree.map do |operation| | ||
| Core::Line.new( | ||
| type: operation.name, | ||
| indentation_level: indentation_level, | ||
| value: operation.value | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module OperationTrees | ||
| autoload( | ||
| :BinaryString, | ||
| 'super_diff/binary_string/operation_trees/binary_string' | ||
| ) | ||
| end | ||
| end | ||
| end |
19 changes: 19 additions & 0 deletions
19
lib/super_diff/binary_string/operation_trees/binary_string.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SuperDiff | ||
| module BinaryString | ||
| module OperationTrees | ||
| class BinaryString < Core::AbstractOperationTree | ||
| def self.applies_to?(value) | ||
| SuperDiff::BinaryString.applies_to?(value) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def operation_tree_flattener_class | ||
| OperationTreeFlatteners::BinaryString | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require 'super_diff/binary_string' | ||
|
|
||
| RSpec.describe 'Integration with binary strings', type: :integration do | ||
| context 'when comparing two different binary strings' do | ||
| it 'produces the correct failure message' do | ||
| as_both_colored_and_uncolored do |color_enabled| | ||
| snippet = <<~TEST.strip | ||
| require 'super_diff/binary_string' | ||
| actual = "Hello".b | ||
| expected = "World".b | ||
| expect(actual).to eq(expected) | ||
| TEST | ||
| program = | ||
| make_plain_test_program(snippet, color_enabled: color_enabled) | ||
|
|
||
| expected_output = | ||
| build_expected_output( | ||
| color_enabled: color_enabled, | ||
| snippet: 'expect(actual).to eq(expected)', | ||
| expectation: | ||
| proc do | ||
| line do | ||
| plain 'Expected ' | ||
| actual '<binary string (5 bytes)>' | ||
| plain ' to eq ' | ||
| expected '<binary string (5 bytes)>' | ||
| plain '.' | ||
| end | ||
| end, | ||
| diff: | ||
| proc do | ||
| expected_line '- 00000000: 576f 726c 64 World' | ||
| actual_line '+ 00000000: 4865 6c6c 6f Hello' | ||
| end | ||
| ) | ||
|
|
||
| expect(program).to produce_output_when_run(expected_output).in_color( | ||
| color_enabled | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when comparing binary strings spanning multiple lines' do | ||
| it 'produces a multi-line hex dump diff' do | ||
| as_both_colored_and_uncolored do |color_enabled| | ||
| snippet = <<~TEST.strip | ||
| require 'super_diff/binary_string' | ||
| actual = ("A" * 20).b | ||
| expected = ("A" * 16 + "B" * 4).b | ||
| expect(actual).to eq(expected) | ||
| TEST | ||
| program = | ||
| make_plain_test_program(snippet, color_enabled: color_enabled) | ||
|
|
||
| expected_output = | ||
| build_expected_output( | ||
| color_enabled: color_enabled, | ||
| snippet: 'expect(actual).to eq(expected)', | ||
| expectation: | ||
| proc do | ||
| line do | ||
| plain 'Expected ' | ||
| actual '<binary string (20 bytes)>' | ||
| plain ' to eq ' | ||
| expected '<binary string (20 bytes)>' | ||
| plain '.' | ||
| end | ||
| end, | ||
| diff: | ||
| proc do | ||
| plain_line ' 00000000: 4141 4141 4141 4141 4141 4141 4141 4141 AAAAAAAAAAAAAAAA' | ||
| expected_line '- 00000010: 4242 4242 BBBB' | ||
| actual_line '+ 00000010: 4141 4141 AAAA' | ||
| end | ||
| ) | ||
|
|
||
| expect(program).to produce_output_when_run(expected_output).in_color( | ||
| color_enabled | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.