-
Notifications
You must be signed in to change notification settings - Fork 82
/
package.zy
42 lines (30 loc) · 1.78 KB
/
package.zy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// one can define a package directly, like this:
(def hi (package "hello"
{ World := "earth"; (defn Myfun [x] (concat World x)) }
))
// but now preferred is to use the (import) functionality, tested in import.zy
(assert (== hi.World "earth"))
(assert (== (hi.Myfun "yes") "earthyes"))
// or one can define a package by sourcing another file:
{p = (package "doggerel" (source "tests/pkghelp"))}
(assert (== p.A.B 42))
(assert (== p.A.Dog "bella"))
(assert (== (p.A.F) 54)) // we can call functions
(assert (== (p.Assistor 9) (+ 9 42 3 7))) // functions can refer to package local values and functions
// and test importing pre-made packages:
{k := (source "tests/prepackage")}
(assert (== (k.Funky "yipee") "yipee roverDog chases cat"))
(assert (== k.Kit "cat"))
// package visibility rules like in Go: lowercase => private, Uppercase => Public.
(expectError "Error calling '+': Cannot access private member 'privetLane' of package 'helloKit'" (+ k.privetLane 1))
(expectError "Error calling 'infix': Cannot access private member 'privetLane' of package 'helloKit'" {k.privetLane = 1 })
// check that visibility rules apply to imports of imports, and that we don't infinite loop:
(def outer (package "outerSpace" (def inner (source "tests/prepackage"))))
(assert (== outer.inner.Kit "cat"))
(expectError "Error calling 'concat': Cannot access private member 'privetLane' of package 'helloKit'"
(concat "" outer.inner.privetLane))
(expectError "Error calling '+': Cannot access private member 'privetLane' of package 'helloKit'" (printf "the number is %v" (+ 0 outer.inner.privetLane)))
// but that we can inspect package inside package:
(expectError "" outer.inner)
// verify that functions inside the package can access their own private data
(assert (== (outer.inner.UsePriv) "my number is 42"))