@@ -21,6 +21,12 @@ describe "moon", ->
2121 class Test
2222 assert . equal " table" , moon. type Test . __base
2323
24+ it " returns 'table' for __base with inheritance" , ->
25+ class Parent
26+ class Child extends Parent
27+ assert . equal " table" , moon. type Child . __base
28+ assert . equal " table" , moon. type Parent . __base
29+
2430 it " returns primitive type for non-tables" , ->
2531 assert . equal " number" , moon. type 1
2632 assert . equal " boolean" , moon. type true
@@ -149,6 +155,12 @@ describe "moon", ->
149155 class Hello
150156 assert . falsy moon. is_class Hello . __base
151157
158+ it " returns false for __base with inheritance" , ->
159+ class Parent
160+ class Child extends Parent
161+ assert . falsy moon. is_class Child . __base
162+ assert . falsy moon. is_class Parent . __base
163+
152164 it " returns false for plain tables and non-tables" , ->
153165 assert . falsy moon. is_class {}
154166 assert . falsy moon. is_class 123
@@ -163,6 +175,42 @@ describe "moon", ->
163175 assert . truthy moon. is_class Child
164176 assert . falsy moon. is_class Child !
165177
178+ describe " is_instance and is_class with imposter tables" , ->
179+ it " rejects table with only __base set" , ->
180+ fake = { __base : {} }
181+ assert . falsy moon. is_class fake
182+ assert . falsy moon. is_instance fake
183+
184+ it " rejects table with __base and non-callable metatable" , ->
185+ fake = setmetatable { __base : {} } , { __index : {} }
186+ assert . falsy moon. is_class fake
187+ assert . falsy moon. is_instance fake
188+
189+ it " rejects table with self-referencing __index but no metatable" , ->
190+ fake = {}
191+ fake. __index = fake
192+ assert . falsy moon. is_class fake
193+ assert . falsy moon. is_instance fake
194+
195+ it " rejects table with __class set directly" , ->
196+ fake = { __class : {} }
197+ assert . falsy moon. is_class fake
198+ assert . falsy moon. is_instance fake
199+
200+ it " rejects table whose metatable has __class but not self-referencing __index" , ->
201+ mt = { __class : {} }
202+ fake = setmetatable {} , mt
203+ assert . falsy moon. is_class fake
204+ assert . falsy moon. is_instance fake
205+
206+ it " rejects table with self-referencing __index used as its own metatable" , ->
207+ -- looks like a __base used as a metatable for itself
208+ fake = {}
209+ fake. __index = fake
210+ setmetatable fake, fake
211+ assert . falsy moon. is_class fake
212+ assert . falsy moon. is_instance fake
213+
166214 describe " is_instance" , ->
167215 it " returns true for an instance" , ->
168216 class Hello
@@ -176,6 +224,12 @@ describe "moon", ->
176224 class Hello
177225 assert . falsy moon. is_instance Hello . __base
178226
227+ it " returns false for __base with inheritance" , ->
228+ class Parent
229+ class Child extends Parent
230+ assert . falsy moon. is_instance Child . __base
231+ assert . falsy moon. is_instance Parent . __base
232+
179233 it " returns false for plain tables and non-tables" , ->
180234 assert . falsy moon. is_instance {}
181235 assert . falsy moon. is_instance 123
0 commit comments