sinatra-mvc 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -40,7 +40,7 @@ operating systems might be added later.
40
40
  Installing
41
41
  ----------
42
42
 
43
- Installing Sinatra MVC is reasonably simple. All you need is Mercurial,
43
+ Installing Sinatra MVC is reasonably simple. All you need is Ruby Gems,
44
44
  some development headers (distributed by your operating system) and
45
45
  a terminal.
46
46
 
@@ -128,7 +128,8 @@ For a Rubygems installation simply run:
128
128
 
129
129
  # gem update
130
130
 
131
- To get the latest updates from the repository, just pull (and merge if needed).
131
+ To get the latest updates from the repository, just pull (and merge if
132
+ needed).
132
133
 
133
134
  $ cd $HOME/src/sinatra-mvc
134
135
  $ hg pull
@@ -184,6 +185,11 @@ the name of the file in the `translations` directory, without the `.yml`
184
185
  file extension. Just like `views_root`, `translations` is a subdirectory
185
186
  of your project.
186
187
 
188
+ The development server socket can be configured using the `port` and `bind`
189
+ options. These determine the TCP port and the listen address of the
190
+ development server. These will be ignored when you're using the rackup file
191
+ to run your server (i.e. any other method than running `sinatra-mvc`).
192
+
187
193
  The database connection is defined by `database_connection`. The value is
188
194
  a string, following the syntax:
189
195
 
@@ -357,12 +363,12 @@ Some sidemarks with this selection of templating solutions:
357
363
  for now.
358
364
 
359
365
  Normally, you have to do weird stuff in Sinatra like using
360
- `:'directory/my_view.erubis'` for rendering views in sub directories. Sinatra
361
- MVC has added automatic view prefixes. The former method of using hardcoded
362
- prefixes still works, but now there's URI-based mapping as well. In short,
363
- it uses the views from the directory path in the view directory if that
364
- path matches the URI prefix. For example, if you have a controller like
365
- this:
366
+ `:'directory/my_view.erubis'` for rendering views in sub directories.
367
+ Sinatra MVC has added automatic view prefixes. The former method of using
368
+ hardcoded prefixes still works, but now there's URI-based mapping as well.
369
+ In short, it uses the views from the directory path in the view directory
370
+ if that path matches the URI prefix. For example, if you have a controller
371
+ like this:
366
372
 
367
373
  get '/my/little/pony/pink'
368
374
  erubis :pink
@@ -434,8 +440,8 @@ defined in your `models` directory. If you just want to migrate your models
434
440
 
435
441
  This will only update the tables in such a way it can't modify any of the
436
442
  data already present. To do that, you'll have to write migrations. This
437
- functionality is lacking at the moment. Datamapper is able to run migrations,
438
- but nobody bothered documenting how they work.
443
+ functionality is lacking at the moment. Datamapper is able to run
444
+ migrations, but nobody bothered documenting how they work.
439
445
 
440
446
  Internationalisation
441
447
  --------------------
@@ -459,6 +465,55 @@ To run a script, simply call:
459
465
  $ cd my_project
460
466
  $ sinatra-mvc <scriptname without .rb>
461
467
 
468
+ Tests
469
+ -----
470
+
471
+ Since version 0.0.4 tests are intergrated into Sinatra MVC. If you
472
+ value stability in an application, tests are an awesome way to meet that
473
+ goal. [`Rack::Test`][18] is used to define and run tests on your
474
+ application.
475
+
476
+ The Sinatra DSL is augmented by a test function. This fuction works as a
477
+ skeleton to house your tests. This function also tracks your test coverage.
478
+ When testing, the output will tell you if you've covered all your code
479
+ with tests. The method it uses is reading the defined controller paths, and
480
+ matching that with the defined tests. It's recommended to define the tests
481
+ in the same file as the actual code. If you don't like this approach for
482
+ what ever reason, a separate `app/tests` directory will do as well.
483
+
484
+ Here's an example:
485
+
486
+ get '/horse/:name' do |name|
487
+ "Hello horsey! Hello #{h name}!"
488
+ end
489
+
490
+ test '/horse/:name' do
491
+ def test_horse_name
492
+ get '/horse/pwny'
493
+ assert_equal last_response.body, 'Hello horsey! Hello pwny!'
494
+ end
495
+ end
496
+
497
+ Within the `test '/path' do ... end` body you can use all of the Rack::Test
498
+ functionality you normally use in standard tests. The same gotchas apply
499
+ here. All of the test functions have to have the `test_` prefix. Test
500
+ functions should be unique. All of the Sinatra MVC test function bodies
501
+ share a single scope.
502
+
503
+ At the moment automatic test coverage reporting does not understand the
504
+ difference between HTTP methods. You'll have to make sure to test all of
505
+ the methods that use the same path yourself. In the future Sinatra MVC
506
+ will track your `Rack::Test` usage as well, to provide a complete test
507
+ coverage report.
508
+
509
+ Running the tests is easy. Just run:
510
+
511
+ $ cd ~/src/my_project
512
+ $ sinatra-mvc test
513
+
514
+ The `--verbose` flag shows more information about the running tests. All
515
+ the `Rack::Test` command line flags are also supported and used.
516
+
462
517
  Single Character Reserved Variables
463
518
  -----------------------------------
464
519
 
@@ -469,12 +524,12 @@ Just don't use these as variables within controllers and views, mkay?
469
524
  * `c - ` Conditional form field.
470
525
  * `n - ` Just meaning "n" of something.
471
526
 
472
- [1]: http://rubydoc.info/gems/sinatra/1.1.0/file/README.rdoc#Views___Templates
527
+ [1]: http://www.sinatrarb.com/intro#Views%20/%20Templates
473
528
  [2]: http://rtomayko.github.com/shotgun/
474
529
  [3]: http://www.modrails.com/
475
530
  [4]: http://heroku.com/
476
- [5]: http://www.rubydoc.info/gems/sinatra/1.1.0/file/README.rdoc#Application_Class_Scope
477
- [6]: http://www.rubydoc.info/gems/sinatra/1.1.0/file/README.rdoc
531
+ [5]: http://www.sinatrarb.com/intro#Application/Class%20Scope
532
+ [6]: http://www.sinatrarb.com/intro
478
533
  [7]: http://rubydoc.info/gems/dm-core/1.0.2/frames
479
534
  [8]: https://github.com/jorrizza/sinatra-mvc
480
535
  [9]: https://bitbucket.org/jorrizza/sinatra-mvc
@@ -484,4 +539,5 @@ Just don't use these as variables within controllers and views, mkay?
484
539
  [14]: http://www.rubydoc.info/gems/dm-validations/1.0.2/frames
485
540
  [15]: http://www.sinatrarb.com/configuration.html
486
541
  [16]: http://r18n.rubyforge.org/sinatra.html
487
- [17]: http://gembundler.com/man/gemfile.5.html
542
+ [17]: http://gembundler.com/man/gemfile.5.html
543
+ [18]: https://github.com/brynary/rack-test
@@ -3,6 +3,10 @@
3
3
  # Determine the location of our project
4
4
  PROJECT = ENV['PROJECT'] ? ENV['PROJECT'] : '.'
5
5
 
6
+ # We have to make sure the environment variable RACK_ENV is set
7
+ # to "test" when we're actually testing.
8
+ ENV['RACK_ENV'] = 'test' if ARGV[0] == 'test'
9
+
6
10
  Dir.chdir PROJECT do
7
11
  require 'sinatra-mvc'
8
12
 
@@ -13,8 +13,7 @@ Encoding.default_external = 'UTF-8'
13
13
  # Project include path.
14
14
  $:.push PROJECT
15
15
 
16
- # Guess what. We need these.
17
- require 'rubygems'
16
+ # Guess what. We need this.
18
17
  require 'sinatra/base'
19
18
 
20
19
  # Load all of the core modules, in order.
@@ -29,6 +28,7 @@ require 'sinatra-mvc/flash_messages'
29
28
  require 'sinatra-mvc/post_handler'
30
29
  require 'sinatra-mvc/conditional_form_field'
31
30
  require 'sinatra-mvc/escaping'
31
+ require 'sinatra-mvc/testing'
32
32
 
33
33
  # We use Bundler to manage the rest of the deps.
34
34
  require 'bundler/setup'
@@ -37,4 +37,5 @@ require 'bundler/setup'
37
37
  require 'conf/environment'
38
38
  require 'sinatra-mvc/database_connection'
39
39
  require 'sinatra-mvc/load_app'
40
+ require 'sinatra-mvc/do_tests'
40
41
  require 'sinatra-mvc/load_utils'
@@ -0,0 +1,48 @@
1
+ # Run tests if the user calls sinatra-mvc test.
2
+
3
+ if ENV['RACK_ENV'] == 'test' && !defined? RACKUP
4
+ puts '>> Running unit tests using Rack::Test'
5
+ require 'test/unit'
6
+ require 'rack/test'
7
+
8
+ class SinatraMVC
9
+ # A general test case class for SinatraMVC.
10
+ class UnitTest < Test::Unit::TestCase
11
+ include Rack::Test::Methods
12
+
13
+ def app
14
+ ::SinatraMVC
15
+ end
16
+ end
17
+ end
18
+
19
+ # Add every test defined in our project to the unit test.
20
+ SinatraMVC.tests.each do |t|
21
+ SinatraMVC::UnitTest.class_exec &t
22
+ end
23
+
24
+ # Because test/unit is just a MiniTest shell, we can cheat a bit
25
+ # here to start the tests.
26
+ MiniTest::Unit.new.run(ARGV)
27
+
28
+ # Print out the test coverage.
29
+ verbose = ARGV.include? '--verbose'
30
+ number_of_calls = 0
31
+ number_of_tests = 0
32
+ puts "\n>> Verbose test coverage:" if verbose
33
+ SinatraMVC.test_coverage.each do |method, calls|
34
+ puts " #{method.to_s.upcase}" if verbose && calls.count > 0
35
+ calls.keys.sort.each do |call|
36
+ test_written = calls[call]
37
+ number_of_calls += 1
38
+ number_of_tests += 1 if test_written
39
+
40
+ puts " #{call}: #{test_written ? 'Yes' : 'No'}" if verbose
41
+ end
42
+ end
43
+ puts ">> Test coverage: #{number_of_tests}/#{number_of_calls}"
44
+
45
+ # We don't want to start the server, now do we?
46
+ puts '>> Done testing'
47
+ exit
48
+ end
@@ -0,0 +1,73 @@
1
+ class SinatraMVC
2
+ class << self
3
+ @@tests = []
4
+ @@test_paths = []
5
+
6
+ # Override get, put, post and delete to track test coverage.
7
+ @@controller_calls = {}
8
+ [:get, :put, :post, :delete].each do |method|
9
+ @@controller_calls[method] = []
10
+ end
11
+
12
+ # Wrapper for tracking Sinatra::Base#get calls.
13
+ def get(path, opts={}, &block)
14
+ @@controller_calls[:get] << path
15
+
16
+ super path, opts, &block
17
+ end
18
+
19
+ # Wrapper for tracking Sinatra::Base#put calls.
20
+ def put(path, opts={}, &block)
21
+ @@controller_calls[:put] << path
22
+
23
+ super path, opts, &block
24
+ end
25
+
26
+ # Wrapper for tracking Sinatra::Base#post calls.
27
+ def post(path, opts={}, &block)
28
+ @@controller_calls[:post] << path
29
+
30
+ super path, opts, &block
31
+ end
32
+
33
+ # Wrapper for tracking Sinatra::Base#delete calls.
34
+ def delete(path, opts={}, &block)
35
+ @@controller_calls[:delete] << path
36
+
37
+ super path, opts, &block
38
+ end
39
+
40
+ # A test block within SinatraMVC will do nothing.
41
+ # It only stores the block for use when we're actually testing.
42
+ def test(path = nil, &block)
43
+ if test?
44
+ @@tests << block
45
+ @@test_paths << path
46
+ end
47
+ end
48
+
49
+ # Returns an array of test blocks if in testing mode.
50
+ def tests
51
+ if test?
52
+ @@tests
53
+ end
54
+ end
55
+
56
+ # Returns a test coverage report if in testing mode.
57
+ # TODO: track individual tests for HTTP methods in Rack::Test.
58
+ def test_coverage
59
+ if test?
60
+ coverage = {}
61
+
62
+ @@controller_calls.each do |method, calls|
63
+ tests_done = calls.map do |call|
64
+ @@test_paths.include?(call)
65
+ end
66
+ coverage[method] = Hash[*calls.zip(tests_done).flatten]
67
+ end
68
+
69
+ coverage
70
+ end
71
+ end
72
+ end
73
+ end
@@ -5,7 +5,7 @@ class SinatraMVC
5
5
  # mode, in which set() could be called in the request scope.
6
6
  before do |obj|
7
7
  obj.class.class_exec request do |request|
8
- first_dir = request.env['REQUEST_URI'].split('/')[1]
8
+ first_dir = request.path_info.split('/')[1]
9
9
 
10
10
  if first_dir
11
11
  if File.directory? File.join(settings.views_root, first_dir)
@@ -21,3 +21,6 @@ gem 'erubis'
21
21
  # gem 'RedCloth'
22
22
  # gem 'liquid'
23
23
  # gem 'less'
24
+
25
+ # When testing, uncomment this.
26
+ # gem 'rack-test'
@@ -7,5 +7,6 @@ require 'dm-migrations'
7
7
 
8
8
  # Templating Engines
9
9
  require 'erubis'
10
+ #require 'rdiscount'
10
11
  #require 'redcloth'
11
12
  #require 'liquid'
@@ -14,5 +14,9 @@ public: public
14
14
  default_locale: en
15
15
  translations: i18n
16
16
 
17
+ # Default server config. Not used when using the Rackup file.
18
+ port: 4567
19
+ bind: 0.0.0.0
20
+
17
21
  # Database
18
22
  database_connection: mysql://sinatra:sinatra@localhost/sinatra_mvc
@@ -40,7 +40,7 @@ operating systems might be added later.
40
40
  Installing
41
41
  ----------
42
42
 
43
- Installing Sinatra MVC is reasonably simple. All you need is Mercurial,
43
+ Installing Sinatra MVC is reasonably simple. All you need is Ruby Gems,
44
44
  some development headers (distributed by your operating system) and
45
45
  a terminal.
46
46
 
@@ -128,7 +128,8 @@ For a Rubygems installation simply run:
128
128
 
129
129
  # gem update
130
130
 
131
- To get the latest updates from the repository, just pull (and merge if needed).
131
+ To get the latest updates from the repository, just pull (and merge if
132
+ needed).
132
133
 
133
134
  $ cd $HOME/src/sinatra-mvc
134
135
  $ hg pull
@@ -184,6 +185,11 @@ the name of the file in the `translations` directory, without the `.yml`
184
185
  file extension. Just like `views_root`, `translations` is a subdirectory
185
186
  of your project.
186
187
 
188
+ The development server socket can be configured using the `port` and `bind`
189
+ options. These determine the TCP port and the listen address of the
190
+ development server. These will be ignored when you're using the rackup file
191
+ to run your server (i.e. any other method than running `sinatra-mvc`).
192
+
187
193
  The database connection is defined by `database_connection`. The value is
188
194
  a string, following the syntax:
189
195
 
@@ -357,12 +363,12 @@ Some sidemarks with this selection of templating solutions:
357
363
  for now.
358
364
 
359
365
  Normally, you have to do weird stuff in Sinatra like using
360
- `:'directory/my_view.erubis'` for rendering views in sub directories. Sinatra
361
- MVC has added automatic view prefixes. The former method of using hardcoded
362
- prefixes still works, but now there's URI-based mapping as well. In short,
363
- it uses the views from the directory path in the view directory if that
364
- path matches the URI prefix. For example, if you have a controller like
365
- this:
366
+ `:'directory/my_view.erubis'` for rendering views in sub directories.
367
+ Sinatra MVC has added automatic view prefixes. The former method of using
368
+ hardcoded prefixes still works, but now there's URI-based mapping as well.
369
+ In short, it uses the views from the directory path in the view directory
370
+ if that path matches the URI prefix. For example, if you have a controller
371
+ like this:
366
372
 
367
373
  get '/my/little/pony/pink'
368
374
  erubis :pink
@@ -434,8 +440,8 @@ defined in your `models` directory. If you just want to migrate your models
434
440
 
435
441
  This will only update the tables in such a way it can't modify any of the
436
442
  data already present. To do that, you'll have to write migrations. This
437
- functionality is lacking at the moment. Datamapper is able to run migrations,
438
- but nobody bothered documenting how they work.
443
+ functionality is lacking at the moment. Datamapper is able to run
444
+ migrations, but nobody bothered documenting how they work.
439
445
 
440
446
  Internationalisation
441
447
  --------------------
@@ -459,6 +465,55 @@ To run a script, simply call:
459
465
  $ cd my_project
460
466
  $ sinatra-mvc <scriptname without .rb>
461
467
 
468
+ Tests
469
+ -----
470
+
471
+ Since version 0.0.4 tests are intergrated into Sinatra MVC. If you
472
+ value stability in an application, tests are an awesome way to meet that
473
+ goal. [`Rack::Test`][18] is used to define and run tests on your
474
+ application.
475
+
476
+ The Sinatra DSL is augmented by a test function. This fuction works as a
477
+ skeleton to house your tests. This function also tracks your test coverage.
478
+ When testing, the output will tell you if you've covered all your code
479
+ with tests. The method it uses is reading the defined controller paths, and
480
+ matching that with the defined tests. It's recommended to define the tests
481
+ in the same file as the actual code. If you don't like this approach for
482
+ what ever reason, a separate `app/tests` directory will do as well.
483
+
484
+ Here's an example:
485
+
486
+ get '/horse/:name' do |name|
487
+ "Hello horsey! Hello #{h name}!"
488
+ end
489
+
490
+ test '/horse/:name' do
491
+ def test_horse_name
492
+ get '/horse/pwny'
493
+ assert_equal last_response.body, 'Hello horsey! Hello pwny!'
494
+ end
495
+ end
496
+
497
+ Within the `test '/path' do ... end` body you can use all of the Rack::Test
498
+ functionality you normally use in standard tests. The same gotchas apply
499
+ here. All of the test functions have to have the `test_` prefix. Test
500
+ functions should be unique. All of the Sinatra MVC test function bodies
501
+ share a single scope.
502
+
503
+ At the moment automatic test coverage reporting does not understand the
504
+ difference between HTTP methods. You'll have to make sure to test all of
505
+ the methods that use the same path yourself. In the future Sinatra MVC
506
+ will track your `Rack::Test` usage as well, to provide a complete test
507
+ coverage report.
508
+
509
+ Running the tests is easy. Just run:
510
+
511
+ $ cd ~/src/my_project
512
+ $ sinatra-mvc test
513
+
514
+ The `--verbose` flag shows more information about the running tests. All
515
+ the `Rack::Test` command line flags are also supported and used.
516
+
462
517
  Single Character Reserved Variables
463
518
  -----------------------------------
464
519
 
@@ -469,12 +524,12 @@ Just don't use these as variables within controllers and views, mkay?
469
524
  * `c - ` Conditional form field.
470
525
  * `n - ` Just meaning "n" of something.
471
526
 
472
- [1]: http://rubydoc.info/gems/sinatra/1.1.0/file/README.rdoc#Views___Templates
527
+ [1]: http://www.sinatrarb.com/intro#Views%20/%20Templates
473
528
  [2]: http://rtomayko.github.com/shotgun/
474
529
  [3]: http://www.modrails.com/
475
530
  [4]: http://heroku.com/
476
- [5]: http://www.rubydoc.info/gems/sinatra/1.1.0/file/README.rdoc#Application_Class_Scope
477
- [6]: http://www.rubydoc.info/gems/sinatra/1.1.0/file/README.rdoc
531
+ [5]: http://www.sinatrarb.com/intro#Application/Class%20Scope
532
+ [6]: http://www.sinatrarb.com/intro
478
533
  [7]: http://rubydoc.info/gems/dm-core/1.0.2/frames
479
534
  [8]: https://github.com/jorrizza/sinatra-mvc
480
535
  [9]: https://bitbucket.org/jorrizza/sinatra-mvc
@@ -484,4 +539,5 @@ Just don't use these as variables within controllers and views, mkay?
484
539
  [14]: http://www.rubydoc.info/gems/dm-validations/1.0.2/frames
485
540
  [15]: http://www.sinatrarb.com/configuration.html
486
541
  [16]: http://r18n.rubyforge.org/sinatra.html
487
- [17]: http://gembundler.com/man/gemfile.5.html
542
+ [17]: http://gembundler.com/man/gemfile.5.html
543
+ [18]: https://github.com/brynary/rack-test
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joris van Rooij
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-12 00:00:00 +01:00
17
+ date: 2011-02-23 00:00:00 +01:00
18
18
  default_executable: sinatra-mvc
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -187,6 +187,8 @@ files:
187
187
  - lib/sinatra-mvc/view_prefix.rb
188
188
  - lib/sinatra-mvc/settings.rb
189
189
  - lib/sinatra-mvc/conditional_form_field.rb
190
+ - lib/sinatra-mvc/testing.rb
191
+ - lib/sinatra-mvc/do_tests.rb
190
192
  - lib/sinatra-mvc.rb
191
193
  - skel/.hgignore
192
194
  - skel/.gitignore