|
1 | 1 | (ns cljs.module-processing-tests |
2 | 2 | (:require [clojure.java.io :as io] |
3 | 3 | [cljs.closure :as closure] |
4 | | - [clojure.test :refer :all] |
| 4 | + [clojure.string :as string] |
| 5 | + [clojure.test :refer [deftest is]] |
5 | 6 | [cljs.env :as env] |
6 | 7 | [cljs.analyzer :as ana] |
7 | 8 | [cljs.compiler :as comp] |
8 | 9 | [cljs.js-deps :as deps] |
9 | 10 | [cljs.util :as util] |
10 | | - [cljs.test-util :as test])) |
| 11 | + [cljs.test-util :as test]) |
| 12 | + (:import [java.io File])) |
11 | 13 |
|
12 | 14 | ;; Hard coded JSX transform for the test case |
13 | 15 | (defn preprocess-jsx [ijs _] |
|
23 | 25 | "React.createElement(\"circle\", {cx:\"100px\", cy:\"100px\", r:\"100px\", fill:this.props.color})" |
24 | 26 | ")")))) |
25 | 27 |
|
| 28 | +(defn absolute-module-path |
| 29 | + ([relpath] |
| 30 | + (absolute-module-path relpath false)) |
| 31 | + ([relpath code?] |
| 32 | + (let [filename (as-> (subs relpath (inc (.lastIndexOf relpath "/"))) $ |
| 33 | + (string/replace $ "_" "-") |
| 34 | + (subs $ 0 (.lastIndexOf $ "."))) |
| 35 | + dirname (as-> (io/file relpath) $ |
| 36 | + (.getAbsolutePath $) |
| 37 | + (subs $ 0 (.lastIndexOf $ (str File/separator))) |
| 38 | + (string/replace $ "/" "$") |
| 39 | + ;; Windows |
| 40 | + (string/replace $ "\\" "$") |
| 41 | + (if code? |
| 42 | + (string/replace $ ":" "_") |
| 43 | + (string/replace $ ":" "-")))] |
| 44 | + (str "module" (when-not (.startsWith dirname "$") "$") dirname "$" filename)))) |
| 45 | + |
26 | 46 | (defmethod closure/js-transforms :jsx [ijs opts] |
27 | 47 | (preprocess-jsx ijs opts)) |
28 | 48 |
|
|
47 | 67 | :preprocess :jsx}] |
48 | 68 | :closure-warnings {:non-standard-jsdoc :off}}))) |
49 | 69 | "processed modules are added to :libs")) |
50 | | - |
51 | | - (is (= {"React" {:name "module$src$test$cljs$reactJS" |
| 70 | + (is (= {"React" {:name (absolute-module-path "src/test/cljs/reactJS.js") |
52 | 71 | :module-type :commonjs} |
53 | | - "Circle" {:name "module$src$test$cljs$Circle" |
| 72 | + "Circle" {:name (absolute-module-path "src/test/cljs/Circle.js") |
54 | 73 | :module-type :commonjs}} |
55 | 74 | (:js-module-index @cenv)) |
56 | 75 | "Processed modules are added to :js-module-index"))) |
|
74 | 93 | :closure-warnings {:non-standard-jsdoc :off}}))) |
75 | 94 | "processed modules are added to :libs") |
76 | 95 |
|
77 | | - (is (= {"es6-hello" {:name "module$src$test$cljs$es6-hello" |
| 96 | + (is (= {"es6-hello" {:name (absolute-module-path "src/test/cljs/es6_hello.js") |
78 | 97 | :module-type :es6}} |
79 | 98 | (:js-module-index @cenv)) |
80 | 99 | "Processed modules are added to :js-module-index") |
81 | 100 |
|
82 | | - (is (= "goog.provide(\"module$src$test$cljs$es6_hello\");var sayHello$$module$src$test$cljs$es6_hello=function(){console.log(\"Hello, world!\")};module$src$test$cljs$es6_hello.sayHello=sayHello$$module$src$test$cljs$es6_hello" |
83 | | - (slurp "out/src/test/cljs/es6_hello.js")))))) |
| 101 | + (is (re-find |
| 102 | + #"goog.provide\(\"module\$[a-zA-Z0-9$_]+?src\$test\$cljs\$es6_hello\"\);" |
| 103 | + (slurp "out/src/test/cljs/es6_hello.js")))))) |
84 | 104 |
|
85 | 105 | (deftest test-module-name-substitution |
86 | 106 | (test/delete-out-files) |
|
93 | 113 | (with-out-str |
94 | 114 | (comp/emit (ana/analyze (ana/empty-env) form)))) |
95 | 115 | crlf (if util/windows? "\r\n" "\n") |
96 | | - output (str "module$src$test$cljs$calculator.add((3),(4));" crlf)] |
| 116 | + output (str (absolute-module-path "src/test/cljs/calculator.js" true) ".add((3),(4));" crlf)] |
97 | 117 | (swap! cenv |
98 | 118 | #(assoc % :js-dependency-index (deps/js-dependency-index opts))) |
99 | 119 | (binding [ana/*cljs-ns* 'cljs.user] |
100 | 120 | (is (= (compile '(ns my-calculator.core (:require [calculator :as calc :refer [subtract add] :rename {subtract sub}]))) |
101 | 121 | (str "goog.provide('my_calculator.core');" crlf |
102 | 122 | "goog.require('cljs.core');" crlf |
103 | | - "goog.require('module$src$test$cljs$calculator');" crlf))) |
| 123 | + "goog.require('" (absolute-module-path "src/test/cljs/calculator.js" true) "');" |
| 124 | + crlf))) |
104 | 125 | (is (= (compile '(calc/add 3 4)) output)) |
105 | 126 | (is (= (compile '(calculator/add 3 4)) output)) |
106 | 127 | (is (= (compile '(add 3 4)) output)) |
107 | 128 | (is (= (compile '(sub 5 4)) |
108 | | - (str "module$src$test$cljs$calculator.subtract((5),(4));" crlf)))))))) |
| 129 | + (str (absolute-module-path "src/test/cljs/calculator.js" true) |
| 130 | + ".subtract((5),(4));" crlf)))))))) |
109 | 131 |
|
110 | 132 | (deftest test-cljs-1822 |
111 | 133 | (test/delete-out-files) |
|
132 | 154 | :preprocess :jsx}] |
133 | 155 | :closure-warnings {:non-standard-jsdoc :off}}))) |
134 | 156 | "processed modules are added to :libs")) |
135 | | - (is (= {"React" {:name "module$src$test$cljs$react-min" |
| 157 | + (is (= {"React" {:name (absolute-module-path "src/test/cljs/react-min.js") |
136 | 158 | :module-type :commonjs} |
137 | | - "Circle" {:name "module$src$test$cljs$Circle-min" |
| 159 | + "Circle" {:name (absolute-module-path "src/test/cljs/Circle-min.js") |
138 | 160 | :module-type :commonjs}} |
139 | 161 | (:js-module-index @cenv)) |
140 | 162 | "Processed modules are added to :js-module-index"))) |
|
161 | 183 | :closure-warnings {:non-standard-jsdoc :off}}))) |
162 | 184 | "processed modules are added to :libs")) |
163 | 185 |
|
164 | | - (is (= {"React" {:name "module$src$test$cljs$reactJS" |
| 186 | + (is (= {"React" {:name (absolute-module-path "src/test/cljs/reactJS.js") |
165 | 187 | :module-type :commonjs} |
166 | | - "Circle" {:name "module$src$test$cljs$Circle" |
| 188 | + "Circle" {:name (absolute-module-path "src/test/cljs/Circle.js") |
167 | 189 | :module-type :commonjs}} |
168 | 190 | (:js-module-index @cenv)) |
169 | 191 | "Processed modules are added to :js-module-index"))) |
0 commit comments