Class: AlFuncTest

Inherits:
MiniTest::Unit::TestCase
  • Object
show all
Includes:
AlControllerSession
Defined in:
lib/al_functest.rb

Overview

ファンクショナルテストクラス

Defined Under Namespace

Classes: EmulateStdin, OutputTrap

Constant Summary

@@current_dir =
Dir.pwd()

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from AlControllerSession

#session

Instance Attribute Details

- (String) response (readonly)

Returns 出力のバッファ

Returns:

  • (String)

    出力のバッファ



52
53
54
# File 'lib/al_functest.rb', line 52

def response
  @response
end

Class Method Details

+ (Object) ctrl(controller_name)

コントローラの指定



57
58
59
60
61
62
# File 'lib/al_functest.rb', line 57

def self.ctrl( controller_name )
  Alone.ctrl.clear << controller_name.to_s
  Dir.chdir( File.join( AL_CTRL_DIR, AlControllerBase::CTRL ) )
  require './main.rb'
  Dir.chdir( @@current_dir )
end

+ (Object) save_response(dir_name)

Note:

ファイル名は、テストメソッド名になる。

Aloneの出力をファイルへ保存する事を指示

Parameters:

  • dir_name (String)

    保存ディレクトリ名



71
72
73
# File 'lib/al_functest.rb', line 71

def self.save_response( dir_name )
  OutputTrap::save_response( dir_name )
end

Instance Method Details

- (Object) _init_alone(action)

Alone初期化



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/al_functest.rb', line 93

def _init_alone( action )
  # CAUTION: Alone本体の構造と密結合しているので、変更がある場合は双方同時に変更すること。
  Alone.class_variable_set( :@@headers, [ "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" ] )
  Alone.class_variable_set( :@@cookies, nil )
  Alone.class_variable_set( :@@flag_redirect, false )
  Alone.class_variable_set( :@@flag_send_http_headers, false )

  AlForm.class_variable_set( :@@request_get, {} )
  AlForm.class_variable_set( :@@request_post, {} )

  @response = ""
  $stdout = OutputTrap.new( @response, __name__ )

  Alone::action.clear << action
end

- (Object) assert_redirected_to(options, message = "")

アサーション:リダイレクト先が意図したものか確認

Parameters:

  • options (String, Hash)

    完全なURI または :action=>“”,… の形式で、アクション等を指示

  • message (String) (defaults to: "")

    表示するメッセージ



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/al_functest.rb', line 225

def assert_redirected_to( options, message = "" )
  location = redirect_to_url()
  if ! location
    assert( false, "No redirected." )
  end

  if options.class == String
    assert_equal( options, location, message )
    return
  end

  flag_success = true
  options.each do |k,v|
    text = "#{k}=#{Alone::encode_uri_component(v)}"
    flag_success &&= location.include?( text )
  end
  assert( flag_success, %Q(#{message}\n<"#{options}"> expected. but was\n<"#{location}">) )
end

- (Object) assert_response(expect_code, message = "")

アサーション:HTTPレスポンス(ステータス)コードの確認

Parameters:

  • expect_code (Integer, Symbol)

    予期しているレスポンスコード または、レスポンスコードの特定範囲を表すシンボル

  • message (String) (defaults to: "")

    表示するメッセージ



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/al_functest.rb', line 193

def assert_response( expect_code, message = "" )
  status_code = 200
  Alone.class_variable_get( :@@headers ).each do |line|
    if /^Status: (\d+)/ =~ line
      status_code = $1.to_i
    end
  end

  case expect_code
  when Integer
    assert_equal( status_code, expect_code, message )
  when :success
    assert_equal( status_code, 200, message )
  when :redirect
    assert( 300 <= status_code && status_code <= 399, message )
  when :missing
    assert_equal( status_code, 404, message )
  when :error
    assert( 500 <= status_code && status_code <= 599, message )
  else
    assert( false, "assert_response(): Parameter error." )
  end
end

- (Object) assigns(key)

コントローラによって割り当てられたインスタンス変数を返す

Parameters:

  • key (String, Symbol)

    インスタンス変数名

Returns:

  • (Object)

    変数の値



251
252
253
# File 'lib/al_functest.rb', line 251

def assigns( key )
  $AlController.instance_variable_get( "@#{key}" )
end

- (Hash) cookies

ユーザーに対して送信されるクッキーを返す

Returns:

  • (Hash)

    クッキーのハッシュ



261
262
263
264
265
266
267
268
269
# File 'lib/al_functest.rb', line 261

def cookies()
  ret = {}
  Alone::class_variable_get( :@@headers ).each do |line|
    if /^Set-Cookie: (\w+)=(\w+)/ =~ line
      ret[$1.to_sym] = Alone::decode_uri_component( $2 )
    end
  end
  return ret
end

- (Object) get(action = nil, parameters = {})

GETリクエストエミュレート

Parameters:

  • action (String) (defaults to: nil)

    アクション名

  • parameters (Hash) (defaults to: {})

    GETパラメータ



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
# File 'lib/al_functest.rb', line 116

def get( action = nil, parameters = {} )
  _init_alone( action.to_s )

  # set parameters
  ENV["REQUEST_METHOD"] = "GET"
  s = ""
  s << "ctrl=#{Alone::ctrl}&"  if ! Alone::ctrl.empty?
  s << "action=#{Alone::action}&"  if ! Alone::action.empty?
  parameters.each do |k,v|
    case v
    when NilClass
      # nothing to do.
    when Array
      v.each { |v1| s << "#{k}=#{Alone::encode_uri_component(v1)}&" }
    else
      s << "#{k}=#{Alone::encode_uri_component(v)}&"
    end
  end
  s.chop!
  ENV["QUERY_STRING"] = s
  ENV["SCRIPT_NAME"] = "/index.rb"
  ENV["REQUEST_URI"] = "/index.rb?#{s}"

  Dir.chdir( File.join( AL_CTRL_DIR, AlControllerBase::CTRL ) )
  $AlController._exec()
  Dir.chdir( @@current_dir )
end

- (Object) p(*arg)

デバッグ用表示 p



312
313
314
# File 'lib/al_functest.rb', line 312

def p( *arg )
  arg.each { |a| STDOUT.puts a.inspect }
end

- (Object) post(action = nil, parameters = {})

POSTリクエストエミュレート

Parameters:

  • action (String) (defaults to: nil)

    アクション名

  • parameters (Hash) (defaults to: {})

    POSTパラメータ



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/al_functest.rb', line 151

def post( action = nil, parameters = {} )
  _init_alone( action )

  # set parameters
  ENV["REQUEST_METHOD"] = "POST"
  s = ""
  s << "ctrl=#{Alone::ctrl}&"  if ! Alone::ctrl.empty?
  s << "action=#{Alone::action}&"  if ! Alone::action.empty?
  ENV["QUERY_STRING"] = s
  ENV["SCRIPT_NAME"] = "/index.rb"
  ENV["REQUEST_URI"] = "/index.rb?#{s}"

  # make request
  s = ""
  parameters.each do |k,v|
    case v
    when NilClass
      # nothing to do.
    when Array
      v.each { |v1| s << "#{k}=#{Alone::encode_uri_component(v1)}&" }
    else
      s << "#{k}=#{Alone::encode_uri_component(v)}&"
    end
  end
  s.chop!

  $stdin = EmulateStdin.new( s )
  ENV["CONTENT_LENGTH"] = $stdin.content_length

  Dir.chdir( File.join( AL_CTRL_DIR, AlControllerBase::CTRL ) )
  $AlController._exec()
  Dir.chdir( @@current_dir )
end

デバッグ用表示 print



328
329
330
# File 'lib/al_functest.rb', line 328

def print( *arg )
  STDOUT.print *arg
end

- (Object) puts(*arg)

デバッグ用表示 puts



320
321
322
# File 'lib/al_functest.rb', line 320

def puts( *arg )
  STDOUT.puts *arg
end

- (String, NilClass) redirect_to_url Also known as: redirect_to_uri

リダイレクト先を示すURL

Returns:

  • (String)

    リダイレクト先

  • (NilClass)

    リダイレクトが指示されていない場合



278
279
280
281
282
283
284
285
# File 'lib/al_functest.rb', line 278

def redirect_to_url()
  Alone.class_variable_get( :@@headers ).each do |line|
    if /^Location: (.+)$/ =~ line
      return $1
    end
  end
  return nil
end

- (Object) set_state(state)

ステートをセット

Parameters:

  • state (String)

    ステート文字列



294
295
296
# File 'lib/al_functest.rb', line 294

def set_state( state )
  $AlController.set_state( state )
end

- (Object) setup

テストセットアップ(初期化)



79
80
81
82
83
84
85
86
87
# File 'lib/al_functest.rb', line 79

def setup()
  # (note)
  #  al_controller.rb のオブジェクト生成部と合わせること。
  #
  $AlController = AlController.allocate
  $AlController.instance_variable_set( :@state, AlSession["AL_STATE_#{AlController::CTRL}"] )
  $AlController.instance_variable_set( :@flag_raise_state_error, false )
  $AlController.__send__( :initialize )
end

- (String) state

ステートを返す

Returns:

  • (String)

    ステート



304
305
306
# File 'lib/al_functest.rb', line 304

def state()
  return $AlController.state
end