diff --git a/Hammerspoon Tests/HSinspect.m b/Hammerspoon Tests/HSinspect.m new file mode 100644 index 000000000..a94f6d1d7 --- /dev/null +++ b/Hammerspoon Tests/HSinspect.m @@ -0,0 +1,35 @@ +// +// HSinspect.m +// Hammerspoon +// +// Created by David Peterson on 18-Oct-2018. +// Copyright © 2016 Hammerspoon. All rights reserved. +// + +#import "HSTestCase.h" + +@interface HSinspect : HSTestCase + +@end + +@implementation HSinspect + +- (void)setUp { + [super setUpWithRequire:@"test_inspect"]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testEncode { + RUN_LUA_TEST() +} + +- (void)testDecode { + RUN_LUA_TEST() +} + +@end \ No newline at end of file diff --git a/extensions/inspect/init.lua b/extensions/inspect/init.lua index d653e2fb7..5d0b9eee8 100644 --- a/extensions/inspect/init.lua +++ b/extensions/inspect/init.lua @@ -236,7 +236,8 @@ function Inspector:putTable(t) elseif self.level >= self.depth then self:puts('{...}') else - if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end + local appearances = self.tableAppearances[t] or 0 + if appearances > 1 then self:puts('<', self:getId(t), '>') end local nonSequentialKeys = getNonSequentialKeys(t) local length = #t diff --git a/extensions/inspect/test_inspect.lua b/extensions/inspect/test_inspect.lua new file mode 100644 index 000000000..70aa23f62 --- /dev/null +++ b/extensions/inspect/test_inspect.lua @@ -0,0 +1,24 @@ +-- hs.inspect tests +hs.inspect = require("hs.inspect") + +-- tests the case where a custom __init always returns a new table instance as a key/value +function testInspectAlwaysNewTableKeyValue() + local t = setmetatable({}, { + __init = function(_, _) + return {} + end, + __pairs = function(self) + local function stateless_iter(_, k) + if k ~= "a" then + return "a", "b" + end + end + -- Return an iterator function, the table, starting point + return stateless_iter, self, nil + end, + }) + + assertIsEqual('{a = "b"}', hs.inspect(t)) + + return success() +end