Skip to content

Commit

Permalink
Test parsing routes for much better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
austvik committed Feb 17, 2011
1 parent 55397b1 commit 930a5bf
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/rake/wadlgen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
desc "Generates WADL descriptors from routes"
task :wadlgen => :environment do
require 'wadlgen'
Wadlgen::Wadl.generate("http://example.com/")
puts Wadlgen::Wadl.generate Rails.application, "http://example.com/"
end
10 changes: 5 additions & 5 deletions lib/wadlgen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ module Wadlgen

class Wadl

def self.generate(base)
application = parse_route(base)
puts generate_wadl application
def self.generate(application, base)
application = parse_route(application, base)
generate_wadl application
end

def self.parse_xml(xml_doc)
parser = Wadlgen::XMLParser.new xml_doc
parser.parse
end

def self.parse_route(base)
parser = Wadlgen::RouteParser.new base
def self.parse_route(application, base)
parser = Wadlgen::RouteParser.new application, base
parser.parse
end

Expand Down
31 changes: 13 additions & 18 deletions lib/wadlgen/route_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module Wadlgen

class RouteParser

attr_accessor :base
attr_accessor :application, :base

def initialize(base)
def initialize(application, base)
self.application = application
self.base = base
end

Expand All @@ -15,11 +16,11 @@ def parse
private

def get_route_structure(base)
Rails.application.reload_routes!

app = Wadlgen::Application.new
ress = app.add_resources(base)
Rails.application.routes.named_routes.each do |name, route|

application.routes.routes.each do |route|

defaults = route.defaults

controller = defaults[:controller]
Expand Down Expand Up @@ -48,18 +49,14 @@ def get_route_structure(base)
end

def get_representations(controller, action)
if action == 'edit'
{:html => 'html'}
else
res = {}
controller_name = "#{controller}_controller".camelcase
cont_obj = Object::const_get(controller_name).new()
cont_obj.mimes_for_respond_to.each_pair do |format, type|
res[format] = format
# TODO: Only and Except
end
res
res = {}
controller_name = "#{controller}_controller".camelcase
cont_obj = Object::const_get(controller_name).new()
cont_obj.mimes_for_respond_to.each_pair do |format, type|
res[format] = format
# TODO: Only and Except
end
res
end

def get_params(action)
Expand All @@ -76,8 +73,6 @@ def get_params(action)
{:id => 'query'}
when 'index'
{}
when 'edit'
{}
else
{}
end
Expand Down
168 changes: 168 additions & 0 deletions test/test_route_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
require 'test/unit'
require 'rails'
require 'active_support/core_ext/hash'
require "action_controller/railtie"
require 'wadlgen'

#
# Test Rails application
#
class WadlgenTestApp < Rails::Application
routes.draw do
resources :foos
match "/bar" => "foos#bar"
end
end

class FoosController < ActionController::Base
respond_to :json, :xml, :html
def bar
self.response_body = "Hello World"
end
end

class TestRouteParser < Test::Unit::TestCase

def test_parse_routes

app = Wadlgen::Wadl.parse_route(WadlgenTestApp, 'https://example.com/app')

ress = app.resources
assert_equal 'https://example.com/app', ress.base
assert_equal 4, ress.resources.count
res = ress.resources.first
meth = res.methods.first
assert_equal "GET", meth.name
assert_equal "index", meth.id
end

def test_generate
expected = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://wadl.dev.java.net/2009/02 wadl.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<resources base="https://example.com/app">
<resource path="/foos(.:format)">
<method id="index" name="GET">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
<method id="create" name="POST">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
<param name="id" style="query">
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
</resource>
<resource path="/foos/new(.:format)">
<method id="new" name="GET">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
<param name="id" style="query">
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
</resource>
<resource path="/foos/:id(.:format)">
<method id="show" name="GET">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
<param name="id" style="query">
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
<method id="update" name="PUT">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
<param name="id" style="query">
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
<method id="destroy" name="DELETE">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
<param name="id" style="query">
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
</resource>
<resource path="/bar(.:format)">
<method id="bar">
<request>
<param name="format" style="query">
<option mediaType="application/json" value="json"/>
<option mediaType="application/xml" value="xml"/>
<option mediaType="application/html" value="html"/>
</param>
</request>
<response status="200">
<representation element="json" mediaType="application/json"/>
<representation element="xml" mediaType="application/xml"/>
<representation element="html" mediaType="application/html"/>
</response>
</method>
</resource>
</resources>
</application>
HERE

res = Wadlgen::Wadl.generate(WadlgenTestApp, 'https://example.com/app')
assert_equal expected, res
end

end
2 changes: 1 addition & 1 deletion test/test_parser.rb → test/test_xml_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test/unit'
require 'wadlgen'

class Testparser < Test::Unit::TestCase
class TestParser < Test::Unit::TestCase

def test_parse_simple_wadl
document = <<HERE
Expand Down
6 changes: 3 additions & 3 deletions wadlgen.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Gem::Specification.new do |s|
s.date = %q{2011-02-16}
s.description = %q{Generate WADL from rails routes}
s.email = %q{[email protected]}
s.extra_rdoc_files = ["README", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb"]
s.files = ["Gemfile", "Gemfile.lock", "MIT-LICENSE", "README", "Rakefile", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_parser.rb", "wadlgen.gemspec", "Manifest"]
s.extra_rdoc_files = ["README", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "lib/wadlgen/route_parser.rb", "lib/wadlgen/xml_generator.rb", "lib/wadlgen/xml_parser.rb"]
s.files = ["Gemfile", "Gemfile.lock", "MIT-LICENSE", "README", "Rakefile", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "lib/wadlgen/route_parser.rb", "lib/wadlgen/xml_generator.rb", "lib/wadlgen/xml_parser.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_route_parser.rb", "test/test_xml_parser.rb", "wadlgen.gemspec", "Manifest"]
s.homepage = %q{https://github.com/austvik/wadlgen}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Wadlgen", "--main", "README"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{wadlgen}
s.rubygems_version = %q{1.5.0}
s.summary = %q{Generate WADL from rails routes}
s.test_files = ["test/test_classes.rb", "test/test_parser.rb", "test/test_generate.rb"]
s.test_files = ["test/test_route_parser.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_xml_parser.rb"]

if s.respond_to? :specification_version then
s.specification_version = 3
Expand Down

0 comments on commit 930a5bf

Please sign in to comment.