1 | 'use strict';
|
2 |
|
3 | var assert = require('assert');
|
4 | var fs = require('fs');
|
5 | var path = require('path');
|
6 |
|
7 | var localeval = require('localeval');
|
8 |
|
9 | var umdify = require('../');
|
10 |
|
11 |
|
12 | module.exports = function() {
|
13 | triggered({});
|
14 | triggered();
|
15 |
|
16 | okTemplateName();
|
17 | invalidTemplateName();
|
18 |
|
19 | okTemplatePath();
|
20 | invalidTemplatePath();
|
21 |
|
22 | indentAsNumber();
|
23 | indentAsString();
|
24 |
|
25 | noCode();
|
26 | };
|
27 |
|
28 | function triggered(options) {
|
29 | read(function(data) {
|
30 | var code = umdify(data, options);
|
31 |
|
32 | var triggered;
|
33 | localeval(code, {
|
34 | trigger: function() {
|
35 | triggered = true;
|
36 | }
|
37 | });
|
38 |
|
39 | assert(triggered);
|
40 | });
|
41 | }
|
42 |
|
43 | function okTemplateName() {
|
44 | read(function(data) {
|
45 | var code = umdify(data, {
|
46 | template: 'returnExportsGlobal'
|
47 | });
|
48 |
|
49 | var triggered;
|
50 | localeval(code, {
|
51 | trigger: function() {
|
52 | triggered = true;
|
53 | }
|
54 | });
|
55 |
|
56 | assert(triggered);
|
57 | });
|
58 | }
|
59 |
|
60 | function invalidTemplateName() {
|
61 | read(function(data) {
|
62 | assert.throws(function() {
|
63 | var code = umdify(data, {
|
64 | template: 'foobar'
|
65 | });
|
66 | },
|
67 | Error);
|
68 | });
|
69 | }
|
70 |
|
71 | function okTemplatePath() {
|
72 | read(function(data) {
|
73 | var p = path.join(__dirname, '..', 'templates', 'umd.hbs');
|
74 |
|
75 | var code = umdify(data, {
|
76 | template: p
|
77 | });
|
78 |
|
79 | var triggered;
|
80 | localeval(code, {
|
81 | trigger: function() {
|
82 | triggered = true;
|
83 | }
|
84 | });
|
85 |
|
86 | assert(triggered);
|
87 | });
|
88 | }
|
89 |
|
90 | function invalidTemplatePath() {
|
91 | read(function(data) {
|
92 | var p = path.join(__dirname, '..', 'templates', 'foo');
|
93 |
|
94 | assert.throws(function() {
|
95 | var code = umdify(data, {
|
96 | template: p
|
97 | });
|
98 | },
|
99 | Error);
|
100 | });
|
101 | }
|
102 |
|
103 | function indentAsNumber() {
|
104 | read(function(data) {
|
105 | var code = umdify(data, {
|
106 | indent: 4
|
107 | });
|
108 |
|
109 | var triggered;
|
110 | localeval(code, {
|
111 | trigger: function() {
|
112 | triggered = true;
|
113 | }
|
114 | });
|
115 |
|
116 | assert(triggered);
|
117 | });
|
118 | }
|
119 |
|
120 | function indentAsString() {
|
121 | read(function(data) {
|
122 | var code = umdify(data, {
|
123 | indent: ' '
|
124 | });
|
125 | var code2 = umdify(data, {
|
126 | indent: 4
|
127 | });
|
128 |
|
129 | assert(code === code2);
|
130 | });
|
131 | }
|
132 |
|
133 | function noCode() {
|
134 | assert.throws(function() {
|
135 | var umd = umdify();
|
136 | }, function(err) {
|
137 | if(err instanceof Error) {
|
138 | return true;
|
139 | }
|
140 | });
|
141 | }
|
142 |
|
143 | function read(cb) {
|
144 | var p = path.join(__dirname, 'data', 'demo.js');
|
145 |
|
146 | fs.readFile(p, {
|
147 | encoding: 'utf-8'
|
148 | }, function(err, data) {
|
149 | if(err) {
|
150 | return console.error(err);
|
151 | }
|
152 |
|
153 | cb(data);
|
154 | });
|
155 | }
|