jbundle 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,4 +3,5 @@ pkg/*
3
3
  .bundle
4
4
  spec/dist/*
5
5
  .rvmrc
6
- .DS_Store
6
+ Gemfile.lock
7
+
data/README.md CHANGED
@@ -12,34 +12,36 @@ JBundle is a Ruby gem.
12
12
 
13
13
  Define a set of javascript files to bundle and minify
14
14
 
15
- JBundle.config do
16
- version '1.6.1'
15
+ ```ruby
16
+ JBundle.config do
17
+ version '1.6.1'
17
18
 
18
- src_dir File.dirname(__FILE__) + '/src'
19
+ src_dir File.dirname(__FILE__) + '/src'
19
20
 
20
- bundle 'foo.js' do
21
- file 'file1.js'
22
- file 'file2.js'
23
- end
21
+ bundle 'foo.js' do
22
+ file 'file1.js'
23
+ file 'file2.js'
24
+ end
24
25
 
25
- bundle 'foo2.js' do
26
- file 'file3.js'
27
- file 'file4.js'
28
- end
26
+ bundle 'foo2.js' do
27
+ file 'file3.js'
28
+ file 'file4.js'
29
+ end
29
30
 
30
- file 'file4.js'
31
+ file 'file4.js'
31
32
 
32
- file 'text.txt'
33
-
34
- # Filters can be use for string substitution
35
- filter do |src, config|
36
- src.gsub(/<VERSION>/, config.version)
37
- end
38
-
39
- target_dir 'dist'
33
+ file 'text.txt'
34
+
35
+ # Filters can be use for string substitution
36
+ filter do |src, config|
37
+ src.gsub(/<VERSION>/, config.version)
38
+ end
39
+
40
+ target_dir 'dist'
41
+
42
+ end
43
+ ```
40
44
 
41
- end
42
-
43
45
  Then write them to the configured target directory
44
46
 
45
47
  JBundle.write!
@@ -196,8 +198,13 @@ JBundle command-line comes with a built-in Rack server that makes it easy to tes
196
198
 
197
199
  Starting test server on http://localhost:5555. Available bundles:
198
200
  - /foo.js
201
+ Run tests on ./tests/index.html
199
202
 
200
- That serves bundles defined in your JFile in port 5555. Pass the -p option for a different port.
203
+ That serves JavaScript bundles defined in your JFile in port 5555. Pass the -p option for a different port. ./tests/index.html runs your tests (Qunit by default) in the ./tests directory
204
+
205
+ You can chose what testing framework to use when initialising the project. Options are qunit and jasmine.
206
+
207
+ jbundle init foo.js --tests=jasmine
201
208
 
202
209
  Learn more about the JBundle command-line with
203
210
 
@@ -30,6 +30,7 @@ module JBundle
30
30
  JBundle.config.bundles_and_files.each do |f|
31
31
  say "- /#{f.original_name} ==> /#{f.name}", :green
32
32
  end
33
+ say "Run tests on ./tests/index.html"
33
34
 
34
35
  handler = Rack::Handler.default
35
36
  downward = false
@@ -66,9 +67,10 @@ Then package your work
66
67
  jsbundle
67
68
  )
68
69
 
69
- desc 'init', 'Create example JFile and test stubs'
70
+ desc 'init', 'Create example JFile and test stubs. Usage: jbundle init foo.js'
70
71
  method_option :tests, :default => 'qunit', :aliases => '-t'
71
- def init(name)
72
+ def init(name = nil)
73
+ name = ask("Name of your library (ie. foo.js, awesome.js, etc.):") unless name
72
74
  @name = name
73
75
  @klass_name = name.sub('.js', '').split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
74
76
 
@@ -76,16 +78,39 @@ Then package your work
76
78
  empty_directory 'src'
77
79
  template('templates/license.tt', "src/license.txt")
78
80
  template('templates/lib.tt', "src/#{name}")
79
- if options[:tests] == 'qunit'
80
- empty_directory 'test'
81
- template('templates/index.tt', "test/index.html")
82
- template('templates/tests.tt', "test/tests.js")
83
- copy_file 'templates/qunit.tt', 'test/qunit.js'
84
- copy_file 'templates/qunit_css.tt', 'test/qunit.css'
81
+ case options[:tests]
82
+ when 'qunit' then init_qunit
83
+ when 'jasmine' then init_jasmine
84
+ else puts "Don't know how to initalize tests for #{options[:tests].inspect}"
85
85
  end
86
86
  empty_directory 'dist'
87
87
  say AFTER_INIT_MESSAGE, :yellow
88
88
  end
89
89
 
90
+ private
91
+
92
+ def init_qunit
93
+ empty_directory 'test'
94
+ template 'templates/qunit/index.html.tt', 'test/index.html'
95
+ template 'templates/qunit/tests.js.tt', 'test/tests.js'
96
+ copy_file 'templates/qunit/qunit.js', 'test/qunit.js'
97
+ copy_file 'templates/qunit/qunit.css', 'test/qunit.css'
98
+ end
99
+
100
+ def init_jasmine
101
+ empty_directory 'test'
102
+ template 'templates/jasmine/index.html.tt', 'test/index.html'
103
+ template 'templates/jasmine/tests.js.tt', 'test/tests.js'
104
+ [
105
+ 'spec_helper.js',
106
+ 'jasmine_favicon.png',
107
+ 'jasmine.js',
108
+ 'jasmine.css',
109
+ 'jasmine-html.js'
110
+ ].each do |file|
111
+ template "templates/jasmine/#{file}", "test/#{file}"
112
+ end
113
+ end
114
+
90
115
  end
91
116
  end
@@ -11,7 +11,7 @@ module JBundle
11
11
  # Configure JBundle on every request.
12
12
  # Expensive but allows for reloading changes to JFile
13
13
  def call(env)
14
- bundle_name = env['PATH_INFO'].sub('/', '')
14
+ bundle_name = env['PATH_INFO'].split('/').last
15
15
  begin
16
16
  JBundle.config_from_file(@jfile)
17
17
  compiler = JBundle.build(bundle_name)
@@ -0,0 +1,53 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2
+ "http://www.w3.org/TR/html4/loose.dtd">
3
+ <html>
4
+ <head>
5
+ <title>Jasmine Spec Runner</title>
6
+
7
+ <link rel="shortcut icon" type="image/png" href="jasmine_favicon.png">
8
+
9
+ <link rel="stylesheet" type="text/css" href="jasmine.css">
10
+ <script type="text/javascript" src="jasmine.js"></script>
11
+ <script type="text/javascript" src="jasmine-html.js"></script>
12
+
13
+ <!-- include spec files here... -->
14
+ <script type="text/javascript" src="spec_helper.js"></script>
15
+ <script type="text/javascript" src="tests.js"></script>
16
+
17
+ <!-- include source files here... -->
18
+ <script type="text/javascript" src="http://localhost:5555/<%= @name %>"></script>
19
+
20
+ <script type="text/javascript">
21
+ (function() {
22
+ var jasmineEnv = jasmine.getEnv();
23
+ jasmineEnv.updateInterval = 1000;
24
+
25
+ var trivialReporter = new jasmine.TrivialReporter();
26
+
27
+ jasmineEnv.addReporter(trivialReporter);
28
+
29
+ jasmineEnv.specFilter = function(spec) {
30
+ return trivialReporter.specFilter(spec);
31
+ };
32
+
33
+ var currentWindowOnload = window.onload;
34
+
35
+ window.onload = function() {
36
+ if (currentWindowOnload) {
37
+ currentWindowOnload();
38
+ }
39
+ execJasmine();
40
+ };
41
+
42
+ function execJasmine() {
43
+ jasmineEnv.execute();
44
+ }
45
+
46
+ })();
47
+ </script>
48
+
49
+ </head>
50
+
51
+ <body>
52
+ </body>
53
+ </html>
@@ -0,0 +1,190 @@
1
+ jasmine.TrivialReporter = function(doc) {
2
+ this.document = doc || document;
3
+ this.suiteDivs = {};
4
+ this.logRunningSpecs = false;
5
+ };
6
+
7
+ jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) {
8
+ var el = document.createElement(type);
9
+
10
+ for (var i = 2; i < arguments.length; i++) {
11
+ var child = arguments[i];
12
+
13
+ if (typeof child === 'string') {
14
+ el.appendChild(document.createTextNode(child));
15
+ } else {
16
+ if (child) { el.appendChild(child); }
17
+ }
18
+ }
19
+
20
+ for (var attr in attrs) {
21
+ if (attr == "className") {
22
+ el[attr] = attrs[attr];
23
+ } else {
24
+ el.setAttribute(attr, attrs[attr]);
25
+ }
26
+ }
27
+
28
+ return el;
29
+ };
30
+
31
+ jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
32
+ var showPassed, showSkipped;
33
+
34
+ this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' },
35
+ this.createDom('div', { className: 'banner' },
36
+ this.createDom('div', { className: 'logo' },
37
+ this.createDom('span', { className: 'title' }, "Jasmine"),
38
+ this.createDom('span', { className: 'version' }, runner.env.versionString())),
39
+ this.createDom('div', { className: 'options' },
40
+ "Show ",
41
+ showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }),
42
+ this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "),
43
+ showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }),
44
+ this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped")
45
+ )
46
+ ),
47
+
48
+ this.runnerDiv = this.createDom('div', { className: 'runner running' },
49
+ this.createDom('a', { className: 'run_spec', href: '?' }, "run all"),
50
+ this.runnerMessageSpan = this.createDom('span', {}, "Running..."),
51
+ this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, ""))
52
+ );
53
+
54
+ this.document.body.appendChild(this.outerDiv);
55
+
56
+ var suites = runner.suites();
57
+ for (var i = 0; i < suites.length; i++) {
58
+ var suite = suites[i];
59
+ var suiteDiv = this.createDom('div', { className: 'suite' },
60
+ this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
61
+ this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description));
62
+ this.suiteDivs[suite.id] = suiteDiv;
63
+ var parentDiv = this.outerDiv;
64
+ if (suite.parentSuite) {
65
+ parentDiv = this.suiteDivs[suite.parentSuite.id];
66
+ }
67
+ parentDiv.appendChild(suiteDiv);
68
+ }
69
+
70
+ this.startedAt = new Date();
71
+
72
+ var self = this;
73
+ showPassed.onclick = function(evt) {
74
+ if (showPassed.checked) {
75
+ self.outerDiv.className += ' show-passed';
76
+ } else {
77
+ self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, '');
78
+ }
79
+ };
80
+
81
+ showSkipped.onclick = function(evt) {
82
+ if (showSkipped.checked) {
83
+ self.outerDiv.className += ' show-skipped';
84
+ } else {
85
+ self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, '');
86
+ }
87
+ };
88
+ };
89
+
90
+ jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
91
+ var results = runner.results();
92
+ var className = (results.failedCount > 0) ? "runner failed" : "runner passed";
93
+ this.runnerDiv.setAttribute("class", className);
94
+ //do it twice for IE
95
+ this.runnerDiv.setAttribute("className", className);
96
+ var specs = runner.specs();
97
+ var specCount = 0;
98
+ for (var i = 0; i < specs.length; i++) {
99
+ if (this.specFilter(specs[i])) {
100
+ specCount++;
101
+ }
102
+ }
103
+ var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s");
104
+ message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s";
105
+ this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild);
106
+
107
+ this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString()));
108
+ };
109
+
110
+ jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
111
+ var results = suite.results();
112
+ var status = results.passed() ? 'passed' : 'failed';
113
+ if (results.totalCount === 0) { // todo: change this to check results.skipped
114
+ status = 'skipped';
115
+ }
116
+ this.suiteDivs[suite.id].className += " " + status;
117
+ };
118
+
119
+ jasmine.TrivialReporter.prototype.reportSpecStarting = function(spec) {
120
+ if (this.logRunningSpecs) {
121
+ this.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...');
122
+ }
123
+ };
124
+
125
+ jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
126
+ var results = spec.results();
127
+ var status = results.passed() ? 'passed' : 'failed';
128
+ if (results.skipped) {
129
+ status = 'skipped';
130
+ }
131
+ var specDiv = this.createDom('div', { className: 'spec ' + status },
132
+ this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
133
+ this.createDom('a', {
134
+ className: 'description',
135
+ href: '?spec=' + encodeURIComponent(spec.getFullName()),
136
+ title: spec.getFullName()
137
+ }, spec.description));
138
+
139
+
140
+ var resultItems = results.getItems();
141
+ var messagesDiv = this.createDom('div', { className: 'messages' });
142
+ for (var i = 0; i < resultItems.length; i++) {
143
+ var result = resultItems[i];
144
+
145
+ if (result.type == 'log') {
146
+ messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString()));
147
+ } else if (result.type == 'expect' && result.passed && !result.passed()) {
148
+ messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));
149
+
150
+ if (result.trace.stack) {
151
+ messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
152
+ }
153
+ }
154
+ }
155
+
156
+ if (messagesDiv.childNodes.length > 0) {
157
+ specDiv.appendChild(messagesDiv);
158
+ }
159
+
160
+ this.suiteDivs[spec.suite.id].appendChild(specDiv);
161
+ };
162
+
163
+ jasmine.TrivialReporter.prototype.log = function() {
164
+ var console = jasmine.getGlobal().console;
165
+ if (console && console.log) {
166
+ if (console.log.apply) {
167
+ console.log.apply(console, arguments);
168
+ } else {
169
+ console.log(arguments); // ie fix: console.log.apply doesn't exist on ie
170
+ }
171
+ }
172
+ };
173
+
174
+ jasmine.TrivialReporter.prototype.getLocation = function() {
175
+ return this.document.location;
176
+ };
177
+
178
+ jasmine.TrivialReporter.prototype.specFilter = function(spec) {
179
+ var paramMap = {};
180
+ var params = this.getLocation().search.substring(1).split('&');
181
+ for (var i = 0; i < params.length; i++) {
182
+ var p = params[i].split('=');
183
+ paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
184
+ }
185
+
186
+ if (!paramMap.spec) {
187
+ return true;
188
+ }
189
+ return spec.getFullName().indexOf(paramMap.spec) === 0;
190
+ };
@@ -0,0 +1,166 @@
1
+ body {
2
+ font-family: "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif;
3
+ }
4
+
5
+
6
+ .jasmine_reporter a:visited, .jasmine_reporter a {
7
+ color: #303;
8
+ }
9
+
10
+ .jasmine_reporter a:hover, .jasmine_reporter a:active {
11
+ color: blue;
12
+ }
13
+
14
+ .run_spec {
15
+ float:right;
16
+ padding-right: 5px;
17
+ font-size: .8em;
18
+ text-decoration: none;
19
+ }
20
+
21
+ .jasmine_reporter {
22
+ margin: 0 5px;
23
+ }
24
+
25
+ .banner {
26
+ color: #303;
27
+ background-color: #fef;
28
+ padding: 5px;
29
+ }
30
+
31
+ .logo {
32
+ float: left;
33
+ font-size: 1.1em;
34
+ padding-left: 5px;
35
+ }
36
+
37
+ .logo .version {
38
+ font-size: .6em;
39
+ padding-left: 1em;
40
+ }
41
+
42
+ .runner.running {
43
+ background-color: yellow;
44
+ }
45
+
46
+
47
+ .options {
48
+ text-align: right;
49
+ font-size: .8em;
50
+ }
51
+
52
+
53
+
54
+
55
+ .suite {
56
+ border: 1px outset gray;
57
+ margin: 5px 0;
58
+ padding-left: 1em;
59
+ }
60
+
61
+ .suite .suite {
62
+ margin: 5px;
63
+ }
64
+
65
+ .suite.passed {
66
+ background-color: #dfd;
67
+ }
68
+
69
+ .suite.failed {
70
+ background-color: #fdd;
71
+ }
72
+
73
+ .spec {
74
+ margin: 5px;
75
+ padding-left: 1em;
76
+ clear: both;
77
+ }
78
+
79
+ .spec.failed, .spec.passed, .spec.skipped {
80
+ padding-bottom: 5px;
81
+ border: 1px solid gray;
82
+ }
83
+
84
+ .spec.failed {
85
+ background-color: #fbb;
86
+ border-color: red;
87
+ }
88
+
89
+ .spec.passed {
90
+ background-color: #bfb;
91
+ border-color: green;
92
+ }
93
+
94
+ .spec.skipped {
95
+ background-color: #bbb;
96
+ }
97
+
98
+ .messages {
99
+ border-left: 1px dashed gray;
100
+ padding-left: 1em;
101
+ padding-right: 1em;
102
+ }
103
+
104
+ .passed {
105
+ background-color: #cfc;
106
+ display: none;
107
+ }
108
+
109
+ .failed {
110
+ background-color: #fbb;
111
+ }
112
+
113
+ .skipped {
114
+ color: #777;
115
+ background-color: #eee;
116
+ display: none;
117
+ }
118
+
119
+
120
+ /*.resultMessage {*/
121
+ /*white-space: pre;*/
122
+ /*}*/
123
+
124
+ .resultMessage span.result {
125
+ display: block;
126
+ line-height: 2em;
127
+ color: black;
128
+ }
129
+
130
+ .resultMessage .mismatch {
131
+ color: black;
132
+ }
133
+
134
+ .stackTrace {
135
+ white-space: pre;
136
+ font-size: .8em;
137
+ margin-left: 10px;
138
+ max-height: 5em;
139
+ overflow: auto;
140
+ border: 1px inset red;
141
+ padding: 1em;
142
+ background: #eef;
143
+ }
144
+
145
+ .finished-at {
146
+ padding-left: 1em;
147
+ font-size: .6em;
148
+ }
149
+
150
+ .show-passed .passed,
151
+ .show-skipped .skipped {
152
+ display: block;
153
+ }
154
+
155
+
156
+ #jasmine_content {
157
+ position:fixed;
158
+ right: 100%;
159
+ }
160
+
161
+ .runner {
162
+ border: 1px solid gray;
163
+ display: block;
164
+ margin: 5px 0;
165
+ padding: 2px 0 2px 10px;
166
+ }