Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,41 @@ end
See [eight_bit_color.rb](lib/super_diff/csi/eight_bit_color.rb)
for the list of available colors.

### Disabling the key

You can disable the key by changing the following config (default: true):

``` ruby
SuperDiff.configure do |config|
config.key_enabled = false
end
```

### Hiding unimportant lines

When looking at a large diff for which many of the lines do not change,
it can be difficult to locate the lines which do. Text-oriented
diffs such as those you get from a conventional version control system
solve this problem by removing those unchanged lines from the diff
entirely. The same can be done in SuperDiff.

``` ruby
SuperDiff.configure do |config|
config.diff_elision_enabled = false
config.diff_elision_maximum = 3
end
```

* `diff_elision_enabled` — The elision logic is disabled by default so
as not to surprise people, so setting this to `true` will turn it on.
* `diff_elision_maximum` — This number controls what happens to
unchanged lines (i.e. lines that are neither "insert" lines nor
"delete" lines) that are in between changed lines. If a section of
unchanged lines is beyond this number, the gem will elide (a fancy
word for remove) the data structures within that section as much as
possible until the limit is reached or it cannot go further. Elided
lines are replaced with a `# ...` marker.

### Diffing custom objects

If you are comparing two data structures
Expand Down
7 changes: 7 additions & 0 deletions lib/super_diff/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Configuration
:elision_marker_color,
:expected_color,
:header_color,
:key_enabled,
)

def initialize(options = {})
Expand All @@ -31,6 +32,7 @@ def initialize(options = {})
@extra_operation_tree_builder_classes = [].freeze
@extra_operation_tree_classes = [].freeze
@header_color = :white
@key_enabled = true

merge!(options)
end
Expand All @@ -54,6 +56,10 @@ def diff_elision_enabled?
@diff_elision_enabled
end

def key_enabled?
@key_enabled
end

def merge!(configuration_or_options)
options =
if configuration_or_options.is_a?(self.class)
Expand Down Expand Up @@ -131,6 +137,7 @@ def to_h
extra_operation_tree_builder_classes.dup,
extra_operation_tree_classes: extra_operation_tree_classes.dup,
header_color: header_color,
key_enabled: key_enabled?,
}
end

Expand Down
62 changes: 32 additions & 30 deletions lib/super_diff/rspec/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,36 +286,38 @@ class ExpectedsForMultipleDiffs
def from(expected)
return expected if self === expected

text =
colorizer.wrap("Diff:", SuperDiff.configuration.header_color) +
"\n\n" +
colorizer.wrap(
"┌ (Key) ──────────────────────────┐",
SuperDiff.configuration.border_color
) +
"\n" +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
colorizer.wrap(
"‹-› in expected, not in actual",
SuperDiff.configuration.expected_color
) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
colorizer.wrap(
"‹+› in actual, not in expected",
SuperDiff.configuration.actual_color
) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
"‹ › in both expected and actual" +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap(
"└─────────────────────────────────┘",
SuperDiff.configuration.border_color
)
text = colorizer.wrap("Diff:", SuperDiff.configuration.header_color)

if SuperDiff.configuration.key_enabled?
text += "\n\n" +
colorizer.wrap(
"┌ (Key) ──────────────────────────┐",
SuperDiff.configuration.border_color
) +
"\n" +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
colorizer.wrap(
"‹-› in expected, not in actual",
SuperDiff.configuration.expected_color
) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
colorizer.wrap(
"‹+› in actual, not in expected",
SuperDiff.configuration.actual_color
) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
"‹ › in both expected and actual" +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap(
"└─────────────────────────────────┘",
SuperDiff.configuration.border_color
)
end

new([[expected, text]])
end
Expand Down
4 changes: 4 additions & 0 deletions spec/integration/rspec/eq_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -953,4 +953,8 @@
it_behaves_like "a matcher that supports elided diffs" do
let(:matcher) { :eq }
end

it_behaves_like "a matcher that supports a toggleable key" do
let(:matcher) { :eq }
end
end
55 changes: 29 additions & 26 deletions spec/support/integration/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def build_expected_output(
color_enabled:,
snippet:,
expectation:,
key_enabled: true,
newline_before_expectation: false,
indentation: 7,
diff: nil
Expand All @@ -64,32 +65,34 @@ def build_expected_output(

white_line "Diff:"

newline

line do
blue "┌ (Key) ──────────────────────────┐"
end

line do
blue "│ "
magenta "‹-› in expected, not in actual"
blue " │"
end

line do
blue "│ "
yellow "‹+› in actual, not in expected"
blue " │"
end

line do
blue "│ "
text "‹ › in both expected and actual"
blue " │"
end

line do
blue "└─────────────────────────────────┘"
if key_enabled
newline

line do
blue "┌ (Key) ──────────────────────────┐"
end

line do
blue "│ "
magenta "‹-› in expected, not in actual"
blue " │"
end

line do
blue "│ "
yellow "‹+› in actual, not in expected"
blue " │"
end

line do
blue "│ "
text "‹ › in both expected and actual"
blue " │"
end

line do
blue "└─────────────────────────────────┘"
end
end

newline
Expand Down
115 changes: 115 additions & 0 deletions spec/support/shared_examples/key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
shared_examples_for "a matcher that supports a toggleable key" do
context "if key_enabled is set to true" do
it "produces the key" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
expected = [
"Afghanistan",
"Aland Islands",
"Albania"
]
actual = [
"Afghanistan",
"Aland Islands",
"Australia"
]
expect(actual).to #{matcher}(expected)
TEST
program = make_plain_test_program(
snippet,
color_enabled: color_enabled,
configuration: {
key_enabled: true,
},
)

expected_output = build_expected_output(
color_enabled: color_enabled,
snippet: %|expect(actual).to #{matcher}(expected)|,
newline_before_expectation: true,
expectation: proc {
line do
plain "Expected "
actual %|["Afghanistan", "Aland Islands", "Australia"]|
end

line do
plain " to eq "
expected %|["Afghanistan", "Aland Islands", "Albania"]|
end
},
diff: proc {
plain_line %| [|
plain_line %| "Afghanistan",|
plain_line %| "Aland Islands",|
expected_line %|- "Albania"|
actual_line %|+ "Australia"|
plain_line %| ]|
},
key_enabled: true,
)

expect(program).
to produce_output_when_run(expected_output).
in_color(color_enabled)
end
end
end

context "if key_enabled is set to false" do
it "does not produce the key" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
expected = [
"Afghanistan",
"Aland Islands",
"Albania"
]
actual = [
"Afghanistan",
"Aland Islands",
"Australia"
]
expect(actual).to #{matcher}(expected)
TEST
program = make_plain_test_program(
snippet,
color_enabled: color_enabled,
configuration: {
key_enabled: false,
},
)

expected_output = build_expected_output(
key_enabled: false,
color_enabled: color_enabled,
snippet: %|expect(actual).to #{matcher}(expected)|,
newline_before_expectation: true,
expectation: proc {
line do
plain "Expected "
actual %|["Afghanistan", "Aland Islands", "Australia"]|
end

line do
plain " to eq "
expected %|["Afghanistan", "Aland Islands", "Albania"]|
end
},
diff: proc {
plain_line %| [|
plain_line %| "Afghanistan",|
plain_line %| "Aland Islands",|
expected_line %|- "Albania"|
actual_line %|+ "Australia"|
plain_line %| ]|
},
)

expect(program).
to produce_output_when_run(expected_output).
in_color(color_enabled)
end
end
end
end