@@ -8,15 +8,37 @@ describe "moon", ->
88 with_dev ->
99 moon = require " moon"
1010
11- it " should determine correct type" , ->
12- class Test
13-
14- things = {
15- Test , Test !, 1 , true , nil , " hello"
16- }
17-
18- types = [ moon. type t for t in * things]
19- assert . same types, { Test , Test , " number" , " boolean" , " nil" , " string" }
11+ describe " type" , ->
12+ it " returns the class for a class" , ->
13+ class Test
14+ assert . equal Test , moon. type Test
15+
16+ it " returns the class for an instance" , ->
17+ class Test
18+ assert . equal Test , moon. type Test !
19+
20+ it " returns 'table' for __base" , ->
21+ class Test
22+ assert . equal " table" , moon. type Test . __base
23+
24+ it " returns primitive type for non-tables" , ->
25+ assert . equal " number" , moon. type 1
26+ assert . equal " boolean" , moon. type true
27+ assert . equal " nil" , moon. type nil
28+ assert . equal " string" , moon. type " hello"
29+ assert . equal " function" , moon. type ->
30+
31+ it " returns 'table' for plain tables" , ->
32+ assert . equal " table" , moon. type {}
33+ assert . equal " table" , moon. type { hello : " world" }
34+
35+ it " works with inheritance" , ->
36+ class Parent
37+ class Child extends Parent
38+ assert . equal Child , moon. type Child !
39+ assert . equal Parent , moon. type Parent !
40+ assert . equal " table" , moon. type Child . __base
41+ assert . equal " table" , moon. type Parent . __base
2042
2143 it " should get upvalue" , ->
2244 fn = do
@@ -114,6 +136,61 @@ describe "moon", ->
114136
115137 assert . same a, { hello : " world" , cat : " mouse" , foo : " bar" }
116138
139+ describe " is_class" , ->
140+ it " returns true for a class" , ->
141+ class Hello
142+ assert . truthy moon. is_class Hello
143+
144+ it " returns false for an instance" , ->
145+ class Hello
146+ assert . falsy moon. is_class Hello !
147+
148+ it " returns false for __base" , ->
149+ class Hello
150+ assert . falsy moon. is_class Hello . __base
151+
152+ it " returns false for plain tables and non-tables" , ->
153+ assert . falsy moon. is_class {}
154+ assert . falsy moon. is_class 123
155+ assert . falsy moon. is_class " hello"
156+ assert . falsy moon. is_class nil
157+ assert . falsy moon. is_class true
158+
159+ it " works with inheritance" , ->
160+ class Parent
161+ class Child extends Parent
162+ assert . truthy moon. is_class Parent
163+ assert . truthy moon. is_class Child
164+ assert . falsy moon. is_class Child !
165+
166+ describe " is_instance" , ->
167+ it " returns true for an instance" , ->
168+ class Hello
169+ assert . truthy moon. is_instance Hello !
170+
171+ it " returns false for a class" , ->
172+ class Hello
173+ assert . falsy moon. is_instance Hello
174+
175+ it " returns false for __base" , ->
176+ class Hello
177+ assert . falsy moon. is_instance Hello . __base
178+
179+ it " returns false for plain tables and non-tables" , ->
180+ assert . falsy moon. is_instance {}
181+ assert . falsy moon. is_instance 123
182+ assert . falsy moon. is_instance " hello"
183+ assert . falsy moon. is_instance nil
184+ assert . falsy moon. is_instance true
185+
186+ it " works with inheritance" , ->
187+ class Parent
188+ class Child extends Parent
189+ assert . truthy moon. is_instance Parent !
190+ assert . truthy moon. is_instance Child !
191+ assert . falsy moon. is_instance Parent
192+ assert . falsy moon. is_instance Child
193+
117194 it " should fold" , ->
118195 numbers = { 4 , 3 , 5 , 6 , 7 , 2 , 3 }
119196 sum = moon. fold numbers, ( a, b) -> a + b
0 commit comments