Skip to content

Commit

Permalink
Tests pass on latest elixir.
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Apr 18, 2012
1 parent 274ab37 commit 4e4851f
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -12,7 +12,7 @@ setup:
cd deps/cowboy && make

ebin: lib/*.ex lib/*/*.ex lib/*/*/*.ex
@ rm -f ebin/::*.beam
@ rm -rf ebin
@ echo Compiling ...
@ mkdir -p $(EBIN_DIR)
@ touch $(EBIN_DIR)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.exs
Expand Up @@ -3,12 +3,12 @@
# elixir -pa ebin --no-halt examples/simple.exs

defmodule MyApp do
use Dynamo::Router
use Dynamo.Router

get "/foo/bar" do
response.reply(200, [], "Hello World!")
end
end

Code.prepend_path("deps/cowboy/ebin")
Dynamo::Cowboy.run MyApp
Dynamo.Cowboy.run MyApp
6 changes: 3 additions & 3 deletions lib/dynamo/cowboy.ex
@@ -1,4 +1,4 @@
defmodule Dynamo::Cowboy do
defmodule Dynamo.Cowboy do
@moduledoc """
Provides a runner using Cowboy webserver.
Check run/2 for more information.
Expand All @@ -14,15 +14,15 @@ defmodule Dynamo::Cowboy do
## Example
Dynamo::Cowboy.run MyApp, port: 80
Dynamo.Cowboy.run MyApp, port: 80
"""
def run(app, options // []) do
:application.start(:cowboy)

port = Keyword.get options, :port, 3000
acceptors = Keyword.get options, :acceptors, 100
handler = Keyword.get options, :handler, Dynamo::Cowboy::Handler
handler = Keyword.get options, :handler, Dynamo.Cowboy.Handler
dispatch = Keyword.get options, :dispatch, dispatch_for(app, handler)
verbose = Keyword.get options, :verbose, true

Expand Down
8 changes: 4 additions & 4 deletions lib/dynamo/cowboy/handler.ex
@@ -1,4 +1,4 @@
defmodule Dynamo::Cowboy::Handler do
defmodule Dynamo.Cowboy.Handler do
@moduledoc """
This is the default Cowboy handler that is able
to respond to http and websockets requests.
Expand All @@ -11,12 +11,12 @@ defmodule Dynamo::Cowboy::Handler do
end

def handle(req, app) do
res = app.service(Dynamo::Cowboy::Request.new(req), Dynamo::Cowboy::Response.new(req))
res = app.service(Dynamo.Cowboy.Request.new(req), Dynamo.Cowboy.Response.new(req))

if is_record(res, Dynamo::Cowboy::Response) do
if is_record(res, Dynamo.Cowboy.Response) do
{ :ok, res.cowboy_request, app }
else:
raise "Expected service to return a Dynamo::Cowboy::Response, got #{inspect res}"
raise "Expected service to return a Dynamo.Cowboy.Response, got #{inspect res}"
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/dynamo/cowboy/request.ex
@@ -1,8 +1,8 @@
defmodule Dynamo::Cowboy::Request do
defmodule Dynamo.Cowboy.Request do
require :cowboy_http_req, as: R

@doc """
Builds a new Dynamo::Cowboy::Request based on
Builds a new Dynamo.Cowboy.Request based on
the original Cowboy request object.
"""
def new(req) do
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamo/cowboy/response.ex
@@ -1,8 +1,8 @@
defmodule Dynamo::Cowboy::Response do
defmodule Dynamo.Cowboy.Response do
require :cowboy_http_req, as: R

@doc """
Builds a new Dynamo::Cowboy::Response based on
Builds a new Dynamo.Cowboy.Response based on
the original Cowboy request object.
"""
def new(req) do
Expand Down
8 changes: 4 additions & 4 deletions lib/dynamo/router.ex
@@ -1,23 +1,23 @@
# Dynamo::Routes brings routing semantics to your module.
# Dynamo.Routes brings routing semantics to your module.
#
# ## Examples
#
# defmodule MyApp do
# use Dynamo::Router
# use Dynamo.Router
#
# get "users/:id" do
# response.write_head 200, [{ "Content-Type", "application/json" }]
# response.end JSON.encode(User.find(id))
# end
# end
#
defmodule Dynamo::Router do
defmodule Dynamo.Router do
defmacro __using__(module, _) do
Module.add_compile_callback module, __MODULE__

quote do
@dynamo_router true
import Dynamo::Router::DSL
import Dynamo.Router.DSL

@overridable true
def service(req, res) do
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamo/router/dsl.ex
@@ -1,4 +1,4 @@
defmodule Dynamo::Router::DSL do
defmodule Dynamo.Router.DSL do
# Main API to define routes. It accepts an expression representing
# the path and many options allowing the match to be configured.
#
Expand Down Expand Up @@ -140,7 +140,7 @@ defmodule Dynamo::Router::DSL do
# Finally, we also include both _verb and _path vars. Although they
# are quoted so their contents are not available inside the function.
quote do
match = apply Dynamo::Router::Utils, unquote(generator), [unquote(path)]
match = apply Dynamo.Router.Utils, unquote(generator), [unquote(path)]
args = [
quote(do: _verb),
match,
Expand Down
6 changes: 3 additions & 3 deletions lib/dynamo/router/utils.ex
@@ -1,6 +1,6 @@
defexception Dynamo::Router::InvalidSpec, message: "invalid route specification"
defexception Dynamo.Router.InvalidSpec, message: "invalid route specification"

defmodule Dynamo::Router::Utils do
defmodule Dynamo.Router.Utils do
# Generates a representation that will only match routes
# according to the given `spec`.
#
Expand Down Expand Up @@ -81,7 +81,7 @@ defmodule Dynamo::Router::Utils do

def handle_segment_match({ :glob, _identifier, expr }, t, acc) do
if t != [] do
raise(InvalidSpec, message: "cannot have a *glob followed by other segments")
raise(Dynamo.Router.InvalidSpec, message: "cannot have a *glob followed by other segments")
end

case acc do
Expand Down
8 changes: 4 additions & 4 deletions test/dynamo/cowboy/request_test.exs
@@ -1,14 +1,14 @@
Code.require_file "../../../test_helper", __FILE__

defmodule Dynamo::Cowboy::RequestTest do
use ExUnit::Case
defmodule Dynamo.Cowboy.RequestTest do
use ExUnit.Case

def setup_all do
Dynamo::Cowboy.run __MODULE__, port: 8011, verbose: false
Dynamo.Cowboy.run __MODULE__, port: 8011, verbose: false
end

def teardown_all do
Dynamo::Cowboy.shutdown __MODULE__
Dynamo.Cowboy.shutdown __MODULE__
end

def service(req, res) do
Expand Down
28 changes: 14 additions & 14 deletions test/dynamo/cowboy/router_test.exs
@@ -1,28 +1,28 @@
Code.require_file "../../../test_helper", __FILE__

defmodule Dynamo::Cowboy::RouterApp do
use Dynamo::Router
defmodule Dynamo.Cowboy.RouterTest do
use ExUnit.Case

get "/foo/bar" do
response.reply(200, [], "Hello World!")
end
defmodule RouterApp do
use Dynamo.Router

get "/mounted" do
response.reply(200, [], request.path)
end
get "/foo/bar" do
response.reply(200, [], "Hello World!")
end

mount __MODULE__, at: "/baz"
end
get "/mounted" do
response.reply(200, [], request.path)
end

defmodule Dynamo::Cowboy::RouterTest do
use ExUnit::Case
mount __MODULE__, at: "/baz"
end

def setup_all do
Dynamo::Cowboy.run RouterApp, port: 8012, verbose: false
Dynamo.Cowboy.run RouterApp, port: 8012, verbose: false
end

def teardown_all do
Dynamo::Cowboy.shutdown RouterApp
Dynamo.Cowboy.shutdown RouterApp
end

test "basic request on a router app" do
Expand Down
12 changes: 6 additions & 6 deletions test/dynamo/router/utils_test.exs
@@ -1,18 +1,18 @@
Code.require_file "../../../test_helper", __FILE__

defmodule Dynamo::Router::UtilsTest::Macros do
defmodule Dynamo.Router.UtilsTest.Macros do
defmacro assert_quoted(left, right) do
quote do
assert_equal quote(hygiene: false, do: unquote(left)), unquote(right)
end
end
end

defmodule Dynamo::Router::UtilsTest do
import Macros
require Dynamo::Router::Utils, as: R
defmodule Dynamo.Router.UtilsTest do
import Dynamo.Router.UtilsTest.Macros
require Dynamo.Router.Utils, as: R

use ExUnit::Case
use ExUnit.Case

def test_split_single_segment do
assert_equal ["foo"], R.split("/foo")
Expand Down Expand Up @@ -65,7 +65,7 @@ defmodule Dynamo::Router::UtilsTest do
def test_generate_invalid_match_with_segments_after_glob do
R.generate_match("/foo/*bar/baz")
flunk "generate_match should have failed"
rescue: x in [Dynamo::Router::InvalidSpec]
rescue: x in [Dynamo.Router.InvalidSpec]
"cannot have a *glob followed by other segments" = x.message
end
end
94 changes: 47 additions & 47 deletions test/dynamo/router_test.exs
@@ -1,70 +1,70 @@
Code.require_file "../../test_helper", __FILE__

defrecord Dynamo::RouterTest::RequestMock, mount: nil
defmodule Dynamo.RouterTest do
use ExUnit.Case

defmodule Dynamo::RouterTest::Sample0 do
use Dynamo::Router
defrecord RequestMock, mount: nil

def service(_req, _res) do
:from_sample_0
end
defmodule Sample0 do
use Dynamo.Router

get "/nested/:arg" do
arg
end
def service(_req, _res) do
:from_sample_0
end

get "/nested/:arg" do
arg
end

get "/with_request" do
request
get "/with_request" do
request
end
end
end

defmodule Dynamo::RouterTest::Sample1 do
use Dynamo::Router
defmodule Sample1 do
use Dynamo.Router

get "/1/bar" do
1
end
get "/1/bar" do
1
end

get "/2/:bar" do
bar
end
get "/2/:bar" do
bar
end

get "/3/bar-:bar" do
bar
end
get "/3/bar-:bar" do
bar
end

get "/4/*bar" do
bar
end
get "/4/*bar" do
bar
end

get "/5/bar-*bar" do
bar
end
get "/5/bar-*bar" do
bar
end

get ["6", "foo"] do
200
end
get ["6", "foo"] do
200
end

get "/7/:foo" when size(foo) <= 3 do
foo
end
get "/7/:foo" when size(foo) <= 3 do
foo
end

match "/8/foo" do
8
end
match "/8/foo" do
8
end

put "/9/foo", to: Sample0
put "/9/foo", to: Sample0

mount Sample0, at: "/10"
mount Sample0, at: ["11", "deep"]
mount Sample0, at: "/10"
mount Sample0, at: ["11", "deep"]

def not_found(_request, _response) do
404
def not_found(_request, _response) do
404
end
end
end

defmodule Dynamo::RouterTest do
use ExUnit::Case

def test_dispatch_single_segment do
assert_equal 1, Sample1.dispatch(:GET, ["1","bar"], {}, {})
Expand Down

0 comments on commit 4e4851f

Please sign in to comment.