File: parameter.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (145 lines) | stat: -rw-r--r-- 3,138 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require File.expand_path('../../helper', __FILE__)

class SpecParameter
  include Innate::Node
  map '/'

  def index
    "index"
  end

  def no_params
    "no params"
  end

  def single_param param
    "single param (#{param})"
  end

  def double_param param1, param2
    "double param (#{param1}, #{param2})"
  end

  def all_params *params
    "all params (#{params.join(', ')})"
  end

  def at_least_one param, *params
    "at least one (#{param}, #{params.join(', ')})"
  end

  def one_default param = 'default'
    "one_default (#{param})"
  end

  def cat1__cat11
    'cat1: cat11'
  end

  def cat1__cat11__cat111
    'cat1: cat11: cat111'
  end
end

class SpecParameter2
  include Innate::Node
  map '/jo'

  def add(one, two = nil, three = nil)
    "#{one}:#{two}:#{three}"
  end

  def keys
    request.params.keys.inspect
  end
end

class SpecParameter3
  include Innate::Node
  map '/ma'

  def index(*args)
    request.params['foo'].to_s.dump
  end
end

describe "Simple Parameters" do
  def handle(*url)
    response = Innate::Mock.get(*url)
    response.status.should == 200
    response.body
  end

  it "Should respond to no parameters given" do
    handle('/no_params').should == "no params"
  end

  it "Should respond to only / with the index" do
    handle('/').should == "index"
  end

  it "call /bar though index doesn't take params" do
    lambda{ handle('/bar') }.should.raise
  end

  it "action that takes a single param" do
    handle('/single_param/foo').should == "single param (foo)"
  end

  it "action that takes two params" do
    handle('/double_param/foo/bar').should == "double param (foo, bar)"
  end

  it "action that takes two params but we give only one" do
    lambda{ handle('/double_param/foo') }.should.raise
  end

  it "action that takes all params" do
    handle('/all_params/foo/bar/foobar').should == "all params (foo, bar, foobar)"
  end

  it "action that takes all params but needs at least one" do
    handle('/at_least_one/foo/bar/foobar').should == "at least one (foo, bar, foobar)"
  end

  it "action that takes all params but needs at least one (not given here)" do
    lambda{ handle('/at_least_one') }.
      should.raise(ArgumentError)
  end

  it "one default" do
    handle('/one_default').should == "one_default (default)"
  end

  it "one default" do
    handle('/one_default/my_default').should == "one_default (my_default)"
  end

  it "jo/add should raise with 0 parameters" do
    lambda{ handle('/jo/add') }.
      should.raise(ArgumentError)
  end

  it "add should raise with 4 parameters" do
    lambda{ handle('/jo/add/1/2/3/4') }.
      should.raise(ArgumentError)
  end

  it "add should not raise with 1-3 parameters" do
    handle('/jo/add/1').should == '1::'
    handle('/jo/add/1/2').should == '1:2:'
    handle('/jo/add/1/2/3').should == '1:2:3'
  end

  it 'params should have no content without params' do
    handle('/ma').should == '""'
  end

  it 'should have a parameter via QUERY_PARAMS' do
    handle('/ma?foo=bar').should == '"bar"'
  end

  it 'should handle valueless params' do
    handle('/jo/keys?foo').should == '["foo"]'
  end
end