Skip to content

Commit

Permalink
Have assertions should be distinct.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 17, 2024
1 parent 2c81fee commit 0ff3839
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/sus/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def initialize(identity: nil, target: nil, output: Output.buffered, inverted: fa
# The absolute orientation of this set of assertions, i.e. whether the assertions are expected to pass or fail regardless of the parent. Used for correctly formatting the output.
attr :orientation

# Whether this set of assertions is isolated from the parent. This is used to ensure taht any deferred assertions are competed before the parent is completed. This is used by `receive` assertions which are deferred until the user code of the test has completed.
# Whether this set of assertions is isolated from the parent. This is used to ensure that any deferred assertions are competed before the parent is completed. This is used by `receive` assertions which are deferred until the user code of the test has completed.
attr :isolated

# Distinct is used to identify a set of assertions as a single statement for the purpose of user feedback. It's used by top level ensure statements to ensure that error messages are captured and reported on those statements.
Expand Down
7 changes: 4 additions & 3 deletions lib/sus/have.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def print(output)
end

def call(assertions, subject)
assertions.nested(self) do |assertions|
# We want to group all the assertions in to a distinct group:
assertions.nested(self, distinct: true) do |assertions|
assertions.assert(subject.key?(@name), "has key")
if @predicate
Expect.new(assertions, subject[@name]).to(@predicate)
Expand All @@ -39,7 +40,7 @@ def print(output)
end

def call(assertions, subject)
assertions.nested(self) do |assertions|
assertions.nested(self, distinct: true) do |assertions|
assertions.assert(subject.respond_to?(@name), "has attribute")
if @predicate
Expect.new(assertions, subject.public_send(@name)).to(@predicate)
Expand All @@ -61,7 +62,7 @@ def call(assertions, subject)
index = 0

subject.each do |value|
assertions.nested("[#{index}] = #{value.inspect}") do |assertions|
assertions.nested("[#{index}] = #{value.inspect}", distinct: true) do |assertions|
@predicate&.call(assertions, value)
end

Expand Down
13 changes: 13 additions & 0 deletions test/sus/have.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,17 @@ def after
expect(array).not.to have_value(be < 1)
end
end

describe Hash do
let(:values) {[{a: 1}, {b: 2}, {c: 3}]}

it "can contain keys" do
expect(values).to have_value(have_keys(a: be == 1))
end

it "doesn't contain keys" do
expect(values).not.to have_value(have_keys(a: be == 2))
expect(values).not.to have_value(have_keys(d: be == 4))
end
end
end

0 comments on commit 0ff3839

Please sign in to comment.