package-system-tests.lisp 6.29 KB
Newer Older
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
;;; Copyright 2007 Red Daly
;;; Copyright 2007 Vladimir Sedach

;;; SPDX-License-Identifier: BSD-3-Clause

;;; Redistribution and use in source and binary forms, with or
;;; without modification, are permitted provided that the following
;;; conditions are met:

;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.

;;; 2. Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials provided
;;; with the distribution.

;;; 3. Neither the name of the copyright holder nor the names of its
;;; contributors may be used to endorse or promote products derived
;;; from this software without specific prior written permission.

;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
;;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
;;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
;;; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
;;; ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;;; POSSIBILITY OF SUCH DAMAGE.
35

36
(in-package #:parenscript.tests)
37

38
(fiveam:in-suite package-system-tests)
39

40
41
(test-ps-js operator-packages1
  (#:new)
42
  "new();")
43

44
(defpackage #:parenscript.tests.my-library
45
  (:use #:parenscript))
46
47
(setf (ps-package-prefix '#:parenscript.tests.my-library)
      "my_library_")
48
49

(test-ps-js lib-function1
50
  (defun parenscript.tests.my-library::library-function (x y)
51
    (+ x y))
52
53
  "function my_library_libraryFunction(x, y) {
        return x + y;
54
     };")
55

Vladimir Sedach's avatar
Vladimir Sedach committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(test-ps-js lib-function2
  (defun parenscript.tests.my-library::library-function
      (parenscript.tests.my-library::x
       &key ((:y parenscript.tests.my-library::z) 1))
    (+ parenscript.tests.my-library::x parenscript.tests.my-library::z))
  "function my_library_libraryFunction(my_library_x) {
    var _js2 = arguments.length;
    for (var n1 = 1; n1 < _js2; n1 += 2) {
        switch (arguments[n1]) {
        case 'y':
            my_library_z = arguments[n1 + 1];
        };
    };
    var my_library_z = 'undefined' === typeof my_library_z ? 1 : my_library_z;

    return my_library_x + my_library_z;
};")

74
(test-ps-js uniform-symbol-handling1
75
76
  (progn (create parenscript.tests.my-library::foo 1)
         (getprop foo 'parenscript.tests.my-library::foo))
77
  "{ my_library_foo : 1 };
78
79
foo.my_library_foo;")

80
81
82
83
84
85
86
(let ((map (make-hash-table)))
  (defun symbol-obfuscator (symbol)
    (or #1=(gethash symbol map)
        (setf #1# (make-symbol (map 'string (lambda (x)
                                              (code-char (1+ (char-code x))))
                                    (symbol-name symbol)))))))

87
(defpackage #:parenscript.tests.obfuscate-me)
88
89
(obfuscate-package '#:parenscript.tests.obfuscate-me
                   #'symbol-obfuscator)
90
91

(test-ps-js obfuscation1
92
93
  (defun parenscript.tests.obfuscate-me::libfun2 (a b parenscript.tests.obfuscate-me::foo)
    (+ a (parenscript.tests.my-library::library-function b parenscript.tests.obfuscate-me::foo)))
94
  "function mjcgvo3(a, b, gpp) {
95
    __PS_MV_REG = [];
96
    return a + my_library_libraryFunction(b, gpp);
97
};")
98

99
100
101
(defpackage #:parenscript.tests.obfuscate-and-prefix)
(obfuscate-package '#:parenscript.tests.obfuscate-and-prefix #'symbol-obfuscator)
(setf (ps-package-prefix '#:parenscript.tests.obfuscate-and-prefix) "__FOO___")
102
103

(test-ps-js obfuscate-and-prefix
104
  (defun parenscript.tests.obfuscate-and-prefix::xfun (a parenscript.tests.obfuscate-and-prefix::b parenscript.tests.my-library::d)
105
    (* a
106
107
       (parenscript.tests.obfuscate-me::libfun2 parenscript.tests.obfuscate-and-prefix::b a)
       (parenscript.tests.my-library::library-function parenscript.tests.my-library::d parenscript.tests.obfuscate-and-prefix::b)))
108
  "function __FOO___ygvo(a, __FOO___c, my_library_d) {
109
    __PS_MV_REG = [];
110
    return a * mjcgvo3(__FOO___c, a) * my_library_libraryFunction(my_library_d, __FOO___c);
111
};")
112

113
(defpackage #:parenscript.tests.pststpkg
114
  (:use #:parenscript))
115

116
(setf (ps-package-prefix '#:parenscript.tests.pststpkg) "prefix_")
117

118
119
120
121
(fiveam:test namespace1 ()
  (fiveam:is (string=
              (ps* 'parenscript.tests.pststpkg::foo)
              "prefix_foo;")))
122

123
(cl:in-package #:parenscript.tests.pststpkg)
124

125
(parenscript.tests::test-ps-js namespace-and-special-forms
126
127
128
129
  (defun foo ()
    (let ((foo (create bar 1 not-a-keyword something)))
      (return-from foo (and (not foo) (+ (getprop foo 'bar) some-other-var)))))
"function prefix_foo() {
130
131
var foo1 = { prefix_bar : 1, prefix_notAKeyword : prefix_something };
return !foo1 && foo1.prefix_bar + prefix_someOtherVar;
132
};")
133

134
135
136
(parenscript.tests::test-ps-js exported-interface
  (defun parenscript.tests:interface-function (baz)
    (+ baz parenscript.tests.obfuscate-me::foo))
137
"function interfaceFunction(prefix_baz) {
138
    return prefix_baz + gpp;
139
};")
140

141
142
143
(parenscript.tests::test-ps-js prefixed-symbol-macro-obj1
  (symbol-macrolet ((x (+ 1 2)))
    (ps:create x x))
144
  "{ prefix_x : 1 + 2 };")
145

146
(cl:in-package #:parenscript.tests)
147

148
149
150
151
(fiveam:test compile-stream-in-package
  (fiveam:is
   (string=
    (with-input-from-string (s "
152
(defun parenscript.tests.obfuscate-and-prefix::xfun (a parenscript.tests.obfuscate-and-prefix::b parenscript.tests.my-library::d)
153
    (* a
154
155
       (parenscript.tests.obfuscate-me::libfun2 parenscript.tests.obfuscate-and-prefix::b a)
       (parenscript.tests.my-library::library-function parenscript.tests.my-library::d parenscript.tests.obfuscate-and-prefix::b)))
156

157
(in-package #:parenscript.tests.pststpkg)
158

159
160
(defun parenscript.tests:interface-function (baz)
    (+ baz parenscript.tests.obfuscate-me::foo))
161
")
162
163
         (ps-compile-stream s))
       "function __FOO___ygvo(a, __FOO___c, my_library_d) {
164
    __PS_MV_REG = [];
165
166
167
168
169
170
    return a * mjcgvo3(__FOO___c, a) * my_library_libraryFunction(my_library_d, __FOO___c);
};
function interfaceFunction(prefix_baz) {
    return prefix_baz + gpp;
};
")))