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

Commit

Permalink
Add a Context class (#110)
Browse files Browse the repository at this point in the history
* 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
MagiMaster authored Jun 18, 2020
1 parent 6fde1cf commit 7a4f7b8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Context.lua
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
56 changes: 56 additions & 0 deletions tests/Context.lua
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,
}

0 comments on commit 7a4f7b8

Please sign in to comment.