|
| 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