This repository was archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Add expect.extend #142
Merged
benbrimeyer
merged 15 commits into
Roblox:master
from
benbrimeyer:bbrimeyer/feature/extend-expectation
Oct 1, 2020
Merged
Add expect.extend #142
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0c81fc8
Set up ExpectationContext
benbrimeyer 5bd0356
Matchers works as intended
benbrimeyer 5838189
Make expect.extend throw in describe blocks
benbrimeyer 3060601
Add luacheck lint globals
benbrimeyer 06be60a
Prevent from overwriting protected Expectation api
benbrimeyer 1be79b0
prevent matchers from overwriting defaults
benbrimeyer 6edf17b
Address lint
benbrimeyer 77c6878
shorten line length for lint
benbrimeyer 795e3cf
Add changelog
benbrimeyer 062c427
Remove uneeded parameter
benbrimeyer 36c8ad1
Make Expectation extend checks static, can be caught earlier
benbrimeyer bcd405a
Update changelog again
benbrimeyer 091ef70
shorten line length for lint
benbrimeyer e218e60
String format change
benbrimeyer 1ee20ce
typo in comment lass => class
benbrimeyer 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
local Expectation = require(script.Parent.Expectation) | ||
local checkMatcherNameCollisions = Expectation.checkMatcherNameCollisions | ||
|
||
local function copy(t) | ||
local result = {} | ||
|
||
for key, value in pairs(t) do | ||
result[key] = value | ||
end | ||
|
||
return result | ||
end | ||
|
||
local ExpectationContext = {} | ||
ExpectationContext.__index = ExpectationContext | ||
|
||
function ExpectationContext.new(parent) | ||
local self = { | ||
_extensions = parent and copy(parent._extensions) or {}, | ||
} | ||
|
||
return setmetatable(self, ExpectationContext) | ||
end | ||
|
||
function ExpectationContext:startExpectationChain(...) | ||
return Expectation.new(...):extend(self._extensions) | ||
end | ||
|
||
function ExpectationContext:extend(config) | ||
for key, value in pairs(config) do | ||
assert(self._extensions[key] == nil, string.format("Cannot reassign %q in expect.extend", key)) | ||
assert(checkMatcherNameCollisions(key), string.format("Cannot overwrite matcher %q; it already exists", key)) | ||
|
||
self._extensions[key] = value | ||
end | ||
end | ||
|
||
return ExpectationContext |
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,25 @@ | ||
-- luacheck: globals describe beforeAll expect | ||
|
||
local noOptMatcher = function(_received, _expected) | ||
return { | ||
message = "", | ||
pass = true, | ||
} | ||
end | ||
|
||
return function() | ||
beforeAll(function() | ||
expect.extend({ | ||
customMatcher = noOptMatcher, | ||
}) | ||
end) | ||
|
||
describe("redefine matcher", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
-- This should throw since we are redefining the same matcher | ||
customMatcher = noOptMatcher, | ||
}) | ||
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,16 @@ | ||
-- luacheck: globals describe expect | ||
|
||
local noOptMatcher = function(_received, _expected) | ||
return { | ||
message = "", | ||
pass = true, | ||
} | ||
end | ||
|
||
return function() | ||
describe("SHOULD NOT work in a describe block", function() | ||
expect.extend({ | ||
test = noOptMatcher, | ||
}) | ||
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,64 @@ | ||
-- luacheck: globals describe beforeAll expect it | ||
|
||
local noOptMatcher = function(_received, _expected) | ||
return { | ||
message = "", | ||
pass = true, | ||
} | ||
end | ||
|
||
return function() | ||
beforeAll(function() | ||
expect.extend({ | ||
scope_0 = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD inherit from previous beforeAll", function() | ||
assert(expect().scope_0, "should have scope_0") | ||
end) | ||
|
||
describe("scope 1", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
scope_1 = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD inherit from previous beforeAll", function() | ||
assert(expect().scope_1, "should have scope_1") | ||
end) | ||
|
||
it("SHOULD inherit from previous root level beforeAll", function() | ||
assert(expect().scope_0, "should have scope_0") | ||
end) | ||
|
||
it("SHOULD NOT inherit scope 2", function() | ||
assert(expect().scope_2 == nil, "should not have scope_0") | ||
end) | ||
|
||
describe("scope 2", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
scope_2 = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD inherit from previous beforeAll in scope 2", function() | ||
assert(expect().scope_2, "should have scope_2") | ||
end) | ||
|
||
it("SHOULD inherit from previous beforeAll in scope 1", function() | ||
assert(expect().scope_1, "should have scope_1") | ||
end) | ||
|
||
it("SHOULD inherit from previous beforeAll in scope 0", function() | ||
assert(expect().scope_0, "should have scope_0") | ||
end) | ||
end) | ||
end) | ||
|
||
it("SHOULD NOT inherit from scope 1", function() | ||
assert(expect().scope_1 == nil, "should not have scope_1") | ||
end) | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
matchers
used? I don't see it anywhere else.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use it once when we need to negate our expectation
https://github.com/Roblox/testez/pull/142/files#diff-c51d01b32afee27efe59f26103c1e704R129
We pass in the non-bound versions of the extend config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yep. There it is.