-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use an unsafe, uninitialized value instead of initializing a `let` expression one extra time.
- Loading branch information
1 parent
eb5cb4d
commit 2c9c366
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,67 @@ | ||
require "../../spec_helper" | ||
|
||
private class HelperObject | ||
class_getter instances = [] of self | ||
|
||
def initialize | ||
@@instances << self | ||
end | ||
end | ||
|
||
private def count_executed_examples(group) | ||
count = 0 | ||
group.each_example do |example| | ||
count += 1 if example.run? | ||
end | ||
count | ||
end | ||
|
||
Spectator.describe Spectator::Core::Helpers do | ||
describe "#let" do | ||
let x = 42 | ||
|
||
it "sets the value" do | ||
expect(x).to eq(42) | ||
end | ||
|
||
let instance = HelperObject.new | ||
|
||
it "sets the value before each example" do |example| | ||
# There should be an instance created for each *executed* example in the group. | ||
example_count = count_executed_examples(example.group) | ||
expect(HelperObject.instances.size).to eq(example_count) | ||
end | ||
|
||
it "does not reuse the instance" do | ||
expect(HelperObject.instances).to all_be_unique | ||
end | ||
|
||
it "retains the type and isn't nullable" do | ||
# TODO: Use `have_type` matcher. | ||
expect(typeof(x)).to eq(Int32) | ||
# The type becomes a union due to the different type from the nested context. | ||
expect(typeof(instance)).to eq(HelperObject | String) | ||
end | ||
|
||
let y = x + 1 | ||
|
||
it "can depend on other values" do | ||
expect(y).to eq(43) | ||
end | ||
|
||
context "can override the value in a nested context" do | ||
let x = 99 | ||
|
||
it "sets the value" do | ||
expect(x).to eq(99) | ||
end | ||
|
||
let instance = "foo" | ||
|
||
it "changes the type" do | ||
# TODO: Use `have_type` matcher. | ||
expect(typeof(instance)).to eq(String | HelperObject) | ||
end | ||
end | ||
end | ||
end |
This file contains 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