Sha256: 1ce837f6cb08603af7a4b3f67608289cfe66ef8b264d34f742916484b0d90316

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), "helper"))

class TestFormHash < Test::Unit::TestCase
  def setup
    @agent = WWW::Mechanize.new
    @page  = @agent.get('http://localhost/form_multival.html')
  end

  def test_form_hash
    form = @page.forms.name('post_form').first

    assert_not_nil(form)
    field_length = form.fields.length
    assert_nil(form['intarweb'])
    form['intarweb'] = 'Aaron'

    assert_not_nil(form['intarweb'])
    assert_equal(field_length + 1, form.fields.length)
  end

  def test_add_field_via_hash
    form = @page.forms.name('post_form').first

    assert_not_nil(form)
    field_length = form.fields.length
    assert_nil(form['intarweb'])
    form['intarweb'] = 'Aaron'

    assert_not_nil(form['intarweb'])
    assert_equal(field_length + 1, form.fields.length)
  end

  def test_fields_as_hash
    form = @page.forms.name('post_form').first

    assert_not_nil(form)
    assert_equal(2, form.fields.name('first').length)

    form['first'] = 'Aaron'
    assert_equal('Aaron', form['first'])
    assert_equal('Aaron', form.fields.name('first').first.value)
  end

  def test_keys
    @page = @agent.get('http://localhost/empty_form.html')
    form = @page.forms.first

    assert_not_nil(form)
    assert_equal(false, form.has_field?('name'))
    assert_equal(false, form.has_value?('Aaron'))
    assert_equal(0, form.keys.length)
    assert_equal(0, form.values.length)
    form['name'] = 'Aaron'
    assert_equal(true, form.has_field?('name'))
    assert_equal(true, form.has_value?('Aaron'))
    assert_equal(1, form.keys.length)
    assert_equal(['name'], form.keys)
    assert_equal(1, form.values.length)
    assert_equal(['Aaron'], form.values)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mechanize-0.8.0 test/test_form_as_hash.rb
mechanize-0.8.2 test/test_form_as_hash.rb
mechanize-0.8.1 test/test_form_as_hash.rb
mechanize-0.8.3 test/test_form_as_hash.rb
mechanize-0.8.4 test/test_form_as_hash.rb