-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
358 additions
and
10 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
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,38 @@ | ||
local Expectation = require(script.Parent.Expectation) | ||
|
||
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 | ||
if self._extensions[key] then | ||
error(string.format("Cannot reassign %q in expect.extend", key)) | ||
end | ||
|
||
self._extensions[key] = value | ||
end | ||
end | ||
|
||
return ExpectationContext |
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
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
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
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,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 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 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("test").scope_1 == nil, "should not have scope_1") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
-- luacheck: globals describe beforeAll expect it | ||
|
||
local noOptMatcher = function(_received, _expected) | ||
return { | ||
message = "", | ||
pass = true, | ||
} | ||
end | ||
|
||
local ERROR_CANNOT_OVERWRITE = "Cannot overwrite matcher" | ||
local ERROR_CANNOT_START_WITH = "Matchers cannot start with" | ||
|
||
local runTest = function(expectedError) | ||
return function() | ||
local success, message = pcall(function() | ||
expect() | ||
end) | ||
|
||
assert(success == false, "should have been thrown") | ||
assert(message:match(expectedError), string.format("\nUnexpected error:\n%s", message)) | ||
end | ||
end | ||
|
||
return function() | ||
describe("attempt to overwrite default", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
-- This should throw since `ok` is a default matcher | ||
ok = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD fail with expected error message", runTest(ERROR_CANNOT_OVERWRITE)) | ||
end) | ||
|
||
describe("attempt to overwrite never", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
-- This should throw since `never` is protected | ||
never = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD fail with expected error message", runTest(ERROR_CANNOT_OVERWRITE)) | ||
end) | ||
|
||
describe("attempt to overwrite self", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
-- This should throw since `a` is protected | ||
a = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD fail with expected error message", runTest(ERROR_CANNOT_OVERWRITE)) | ||
end) | ||
|
||
describe("attempt to start with _", function() | ||
beforeAll(function() | ||
expect.extend({ | ||
-- This should throw since this starts with _ | ||
_fooBar = noOptMatcher, | ||
}) | ||
end) | ||
|
||
it("SHOULD fail with expected error message", runTest(ERROR_CANNOT_START_WITH)) | ||
end) | ||
end |
Oops, something went wrong.