This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Don't expose TestRunner nodes through TestSession * Remove unused returns * Add function comments. * EOF newline * Add Context object. * Remove unused variables * Add a comment about what a Context does * Add missing test case
- Loading branch information
1 parent
6fde1cf
commit 7a4f7b8
Showing
2 changed files
with
82 additions
and
0 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,26 @@ | ||
--[[ | ||
The Context object implements a write-once key-value store. It also allows | ||
for a new Context object to inherit the entries from an existing one. | ||
]] | ||
local Context = {} | ||
|
||
function Context.new(parent) | ||
local meta = {} | ||
local index = {} | ||
meta.__index = index | ||
|
||
if parent then | ||
for key, value in pairs(getmetatable(parent).__index) do | ||
index[key] = value | ||
end | ||
end | ||
|
||
function meta.__newindex(_obj, key, value) | ||
assert(index[key] == nil, string.format("Cannot reassign %s in context", tostring(key))) | ||
index[key] = value | ||
end | ||
|
||
return setmetatable({}, meta) | ||
end | ||
|
||
return Context |
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,56 @@ | ||
local TestEZ = script.Parent.Parent.TestEZ | ||
local Context = require(TestEZ.Context) | ||
|
||
return { | ||
["Context.new returns a new context"] = function() | ||
assert(Context.new(), "Context.new() returned nil") | ||
end, | ||
["context.foo returns nil if it wasn't set"] = function() | ||
local context = Context.new() | ||
assert(context.foo == nil, string.format("Got %s, expected nil", tostring(context.foo))) | ||
end, | ||
["context.foo returns the value from setting context.foo"] = function() | ||
local context = Context.new() | ||
context.foo = "BAR" | ||
assert(context.foo == "BAR", string.format("Got %s, expected BAR", tostring(context.foo))) | ||
end, | ||
["context.foo can't be set twice"] = function() | ||
local context = Context.new() | ||
context.foo = "foo" | ||
local success, _ = pcall(function() | ||
context.foo = "bar" | ||
end) | ||
assert(not success, "Expected second context.foo to error") | ||
end, | ||
["Context.new accepts a parent"] = function() | ||
local parent = Context.new() | ||
assert(Context.new(parent), "Context.new(parent) returned nil") | ||
end, | ||
["A child context can still read its parent values"] = function() | ||
local parent = Context.new() | ||
parent.foo = "BAR" | ||
local child = Context.new(parent) | ||
assert(child.foo == "BAR", string.format("Got %s, expected BAR", tostring(child.foo))) | ||
end, | ||
["A parent context can't read its child values"] = function() | ||
local parent = Context.new() | ||
local child = Context.new(parent) | ||
child.foo = "BAR" | ||
assert(parent.foo == nil, string.format("Got %s, expected nil", tostring(parent.foo))) | ||
end, | ||
["A child can't overwrite parent values"] = function() | ||
local parent = Context.new() | ||
parent.foo = "foo" | ||
local child = Context.new(parent) | ||
local success, _ = pcall(function() | ||
child.foo = "bar" | ||
end) | ||
assert(not success, "Expected setting child.foo to error") | ||
end, | ||
["A child won't see changes to the parent"] = function() | ||
local parent = Context.new() | ||
local child = Context.new(parent) | ||
parent.foo = "foo" | ||
assert(child.foo == nil, string.format("Got %s, expected nil", tostring(parent.foo))) | ||
end, | ||
} |