@@ -245,6 +245,68 @@ describe "moon", ->
245245 assert . falsy moon. is_instance Parent
246246 assert . falsy moon. is_instance Child
247247
248+ describe " is_instance_of" , ->
249+ it " returns true for direct instance" , ->
250+ class Hello
251+ assert . truthy moon. is_instance_of Hello !, Hello
252+
253+ it " returns true for instance of parent class" , ->
254+ class Parent
255+ class Child extends Parent
256+ assert . truthy moon. is_instance_of Child !, Parent
257+ assert . truthy moon. is_instance_of Child !, Child
258+
259+ it " returns false for instance of unrelated class" , ->
260+ class A
261+ class B
262+ assert . falsy moon. is_instance_of A !, B
263+ assert . falsy moon. is_instance_of B !, A
264+
265+ it " returns false for parent instance checked against child class" , ->
266+ class Parent
267+ class Child extends Parent
268+ assert . falsy moon. is_instance_of Parent !, Child
269+
270+ it " errors when value is not an instance" , ->
271+ class Hello
272+ assert . has_error ( -> moon. is_instance_of Hello , Hello ) , " is_instance_of: expected instance, got table"
273+ assert . has_error ( -> moon. is_instance_of Hello . __base, Hello ) , " is_instance_of: expected instance, got table"
274+ assert . has_error ( -> moon. is_instance_of {} , Hello ) , " is_instance_of: expected instance, got table"
275+ assert . has_error ( -> moon. is_instance_of nil , Hello ) , " is_instance_of: expected instance, got nil"
276+ assert . has_error ( -> moon. is_instance_of 123 , Hello ) , " is_instance_of: expected instance, got number"
277+
278+ it " errors when __base is passed as the value" , ->
279+ class Parent
280+ class Child extends Parent
281+ assert . has_error ( -> moon. is_instance_of Parent . __base, Parent ) , " is_instance_of: expected instance, got table"
282+ assert . has_error ( -> moon. is_instance_of Child . __base, Child ) , " is_instance_of: expected instance, got table"
283+ assert . has_error ( -> moon. is_instance_of Child . __base, Parent ) , " is_instance_of: expected instance, got table"
284+
285+ it " returns false when __base is passed as the class" , ->
286+ class Parent
287+ class Child extends Parent
288+ assert . falsy moon. is_instance_of Parent !, Parent . __base
289+ assert . falsy moon. is_instance_of Child !, Child . __base
290+ assert . falsy moon. is_instance_of Child !, Parent . __base
291+
292+ it " errors when __base is on both sides" , ->
293+ class Parent
294+ class Child extends Parent
295+ assert . has_error ( -> moon. is_instance_of Parent . __base, Parent . __base) , " is_instance_of: expected instance, got table"
296+ assert . has_error ( -> moon. is_instance_of Child . __base, Child . __base) , " is_instance_of: expected instance, got table"
297+ assert . has_error ( -> moon. is_instance_of Child . __base, Parent . __base) , " is_instance_of: expected instance, got table"
298+ assert . has_error ( -> moon. is_instance_of Parent . __base, Child . __base) , " is_instance_of: expected instance, got table"
299+
300+ it " works with deep inheritance chain" , ->
301+ class A
302+ class B extends A
303+ class C extends B
304+ assert . truthy moon. is_instance_of C !, A
305+ assert . truthy moon. is_instance_of C !, B
306+ assert . truthy moon. is_instance_of C !, C
307+ assert . falsy moon. is_instance_of A !, B
308+ assert . falsy moon. is_instance_of A !, C
309+
248310 it " should fold" , ->
249311 numbers = { 4 , 3 , 5 , 6 , 7 , 2 , 3 }
250312 sum = moon. fold numbers, ( a, b) -> a + b
0 commit comments