Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

Commit 7a4f7b8

Browse files
authored
Add a Context class (#110)
* 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
1 parent 6fde1cf commit 7a4f7b8

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Context.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--[[
2+
The Context object implements a write-once key-value store. It also allows
3+
for a new Context object to inherit the entries from an existing one.
4+
]]
5+
local Context = {}
6+
7+
function Context.new(parent)
8+
local meta = {}
9+
local index = {}
10+
meta.__index = index
11+
12+
if parent then
13+
for key, value in pairs(getmetatable(parent).__index) do
14+
index[key] = value
15+
end
16+
end
17+
18+
function meta.__newindex(_obj, key, value)
19+
assert(index[key] == nil, string.format("Cannot reassign %s in context", tostring(key)))
20+
index[key] = value
21+
end
22+
23+
return setmetatable({}, meta)
24+
end
25+
26+
return Context

tests/Context.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
local TestEZ = script.Parent.Parent.TestEZ
2+
local Context = require(TestEZ.Context)
3+
4+
return {
5+
["Context.new returns a new context"] = function()
6+
assert(Context.new(), "Context.new() returned nil")
7+
end,
8+
["context.foo returns nil if it wasn't set"] = function()
9+
local context = Context.new()
10+
assert(context.foo == nil, string.format("Got %s, expected nil", tostring(context.foo)))
11+
end,
12+
["context.foo returns the value from setting context.foo"] = function()
13+
local context = Context.new()
14+
context.foo = "BAR"
15+
assert(context.foo == "BAR", string.format("Got %s, expected BAR", tostring(context.foo)))
16+
end,
17+
["context.foo can't be set twice"] = function()
18+
local context = Context.new()
19+
context.foo = "foo"
20+
local success, _ = pcall(function()
21+
context.foo = "bar"
22+
end)
23+
assert(not success, "Expected second context.foo to error")
24+
end,
25+
["Context.new accepts a parent"] = function()
26+
local parent = Context.new()
27+
assert(Context.new(parent), "Context.new(parent) returned nil")
28+
end,
29+
["A child context can still read its parent values"] = function()
30+
local parent = Context.new()
31+
parent.foo = "BAR"
32+
local child = Context.new(parent)
33+
assert(child.foo == "BAR", string.format("Got %s, expected BAR", tostring(child.foo)))
34+
end,
35+
["A parent context can't read its child values"] = function()
36+
local parent = Context.new()
37+
local child = Context.new(parent)
38+
child.foo = "BAR"
39+
assert(parent.foo == nil, string.format("Got %s, expected nil", tostring(parent.foo)))
40+
end,
41+
["A child can't overwrite parent values"] = function()
42+
local parent = Context.new()
43+
parent.foo = "foo"
44+
local child = Context.new(parent)
45+
local success, _ = pcall(function()
46+
child.foo = "bar"
47+
end)
48+
assert(not success, "Expected setting child.foo to error")
49+
end,
50+
["A child won't see changes to the parent"] = function()
51+
local parent = Context.new()
52+
local child = Context.new(parent)
53+
parent.foo = "foo"
54+
assert(child.foo == nil, string.format("Got %s, expected nil", tostring(parent.foo)))
55+
end,
56+
}

0 commit comments

Comments
 (0)