Class: TestRoleBuilder

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
Rack::Test::Methods
Defined in:
lib/belphanior/servant/test/tc_role_builder.rb

Instance Method Summary collapse

Instance Method Details

#appObject



41
42
43
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 41

def app
  Sinatra::Application
end

#setupObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 45

def setup
  app.set :roles, []
  @default_description = {
    "name" => "test description",
    "description" => "An example of a role description.",
    "commands" => [
      {
        "name" => "test command",
        "description" => "An example command.",
        "arguments" => [
          {
            "name" => "test arg",
            "description" => "An example argument."
          }
        ]
      }
    ]
  }
  @default_usage = [
    "GET",
    "/path",
    "data"
  ]
end

#teardownObject



70
71
72
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 70

def teardown
  app.clear_handlers
end

#test_add_handler_adds_handlerObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 81

def test_add_handler_adds_handler
  app.add_role_description @default_description
  app.add_handler("test command", ["argument 1", "argument 2"],
    "POST", "/test/$(argument 1)", "$(argument 2)") { |arg1|
    "arg1 is "+arg1+" arg2 is "+(request.body.read)
  }
  post '/test/foo', 'bar'
  assert last_response.ok?
  assert_equal("arg1 is foo arg2 is bar", last_response.body)
end

#test_add_handler_updates_protocolObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 92

def test_add_handler_updates_protocol
  app.add_role_description @default_description
  app.set_role_url("/test")
  app.add_handler("test command", ["argument 1"], "GET", "/test/$(argument 1)", "") {|arg1|}
  get '/protocol'
  assert_equal(200, last_response.status)
  assert_equivalent_json_objects(
    {
      "roles" => [{
        "role_url" => "/test",
        "handlers" => [{
          "name" => "test command",
          "method" => "GET",
          "path" => "/test/$(argument 1)"}]
    }]},
    JSON.parse(last_response.body))
end

#test_identifier_case_insensitivityObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 116

def test_identifier_case_insensitivity
  app.add_role_description @default_description
  app.set_role_url "/test"
  app.add_handler("My command", ["Cap"], "GET", "path", "data") do end
  get '/protocol'
  assert_equal(200, last_response.status)
  assert_equivalent_json_objects(
    {
      "roles" => [{
        "role_url" => "/test",
        "handlers" => [{
          "name" => "my command",
          "method" => "GET",
          "path" => "path",
          "data" => "data"}]
    }]},
    JSON.parse(last_response.body))
end

#test_multi_roleObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 134

def test_multi_role
  app.add_role_description @default_description
  app.add_role_description({
      "name" => "second role",
      "description" => "An example of a role description.",
      "commands" => [
        {
          "name" => "test command 2",
          "description" => "An example command.",
          "arguments" => [
            {
              "name" => "test arg b",
              "description" => "An example argument."
            }
          ]
        }
      ]
    }
    )
  app.add_handler("test command", ["test arg"],
    "POST", "/test2/$(test arg)", "", 0) { |arg1|
    "arg1 is " + arg1
  }
  app.add_handler("test command 2", ["test arg b"],
    "GET", "/test3/$(test arg b)", "", 1) { |arg1|
    "arg1b is " + arg1
  }

  post '/test2/foo', ""
  assert last_response.ok?
  assert_equal("arg1 is foo", last_response.body)

  get '/test3/bar'
  assert last_response.ok?
  assert_equal("arg1b is bar", last_response.body)
end

#test_role_builder_utils_usage_string_to_sinatra_pathObject



110
111
112
113
114
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 110

def test_role_builder_utils_usage_string_to_sinatra_path
  assert_equal "/test/:value/test/:value2",
  RoleBuilderUtils.usage_string_to_sinatra_path(
    "/test/$(value)/test/$(value2)")
end

#test_role_describer_accepts_role_descriptionObject



74
75
76
77
78
79
# File 'lib/belphanior/servant/test/tc_role_builder.rb', line 74

def test_role_describer_accepts_role_description
  app.add_role_description @default_description
  get '/role_descriptions/test_description'
  assert last_response.ok?
  assert_equal JSON.generate(@default_description), last_response.body
end