Parent

Files

Class/Module Index [+]

Quicksearch

TestIniFile

Public Instance Methods

setup() click to toggle source
# File test/test_inifile.rb, line 17
def setup
  @ini_file = ::IniFile.new 'test/data/good.ini'
  @contents = [
    ['section_one', 'one', '1'],
    ['section_one', 'two', '2'],
    ['section_two', 'three', '3'],
    ['section three', 'four', '4'],
    ['section three', 'five', '5'],
    ['section three', 'six', '6'],
    ['section_five', 'seven and eight', '7 & 8']
  ].sort
end
test_class_load() click to toggle source
# File test/test_inifile.rb, line 30
def test_class_load
  ini_file = ::IniFile.load 'test/data/good.ini'
  assert_instance_of ::IniFile, ini_file

  # see if we can parse different style comments
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/comment.ini'}

  ini_file = ::IniFile.load 'test/data/comment.ini', :comment => '#'
  assert_instance_of ::IniFile, ini_file

  # see if we can parse mixed style comments
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/mixed_comment.ini'}

  ini_file = ::IniFile.load 'test/data/mixed_comment.ini', :comment => ';#'
  assert_instance_of ::IniFile, ini_file

  # see if we can parse different style param separators
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/param.ini'}

  ini_file = ::IniFile.load 'test/data/param.ini', :parameter => ':'
  assert_instance_of ::IniFile, ini_file

  # make sure we error out on files with bad lines
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/bad_1.ini'}
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/bad_2.ini'}
end
test_clone() click to toggle source
# File test/test_inifile.rb, line 57
def test_clone
  clone = @ini_file.clone
  assert_equal @ini_file, clone
  assert !clone.tainted?
  assert !clone.frozen?

  # the clone should be completely independent of the original
  clone['new_section']['one'] = 1
  assert_not_equal @ini_file, clone

  # the tainted state is copied to clones
  @ini_file.taint
  assert @ini_file.tainted?

  clone = @ini_file.clone
  assert clone.tainted?

  # the frozen state is also copied to clones
  @ini_file.freeze
  assert @ini_file.frozen?

  clone = @ini_file.clone
  assert clone.tainted?
  assert clone.frozen?
end
test_delete_section() click to toggle source
# File test/test_inifile.rb, line 83
def test_delete_section
  assert_nil @ini_file.delete_section('section_nil')

  h = {'one' => '1', 'two' => '2'}
  assert_equal true, @ini_file.has_section?('section_one')
  assert_equal h, @ini_file.delete_section('section_one')
  assert_equal false, @ini_file.has_section?('section_one')
  assert_nil @ini_file.delete_section('section_one')
end
test_dup() click to toggle source
# File test/test_inifile.rb, line 93
def test_dup
  dup = @ini_file.dup
  assert_equal @ini_file, dup
  assert !dup.tainted?
  assert !dup.frozen?

  # the duplicate should be completely independent of the original
  dup['new_section']['one'] = 1
  assert_not_equal @ini_file, dup

  # the tainted state is copied to duplicates
  @ini_file.taint
  assert @ini_file.tainted?

  dup = @ini_file.dup
  assert dup.tainted?

  # the frozen state, however, is not
  @ini_file.freeze
  assert @ini_file.frozen?

  dup = @ini_file.dup
  assert dup.tainted?
  assert !dup.frozen?
end
test_each() click to toggle source
# File test/test_inifile.rb, line 119
def test_each
  ary = []
  @ini_file.each {|*args| ary << args}

  assert_equal @contents, ary.sort

  ary = []
  ::IniFile.new('temp.ini').each {|*args| ary << args}
  assert_equal [], ary
end
test_each_section() click to toggle source
# File test/test_inifile.rb, line 130
def test_each_section
  expected = [
    'section_one', 'section_two', 'section three',
    'section_four', 'section_five'
  ].sort

  ary = []
  @ini_file.each_section {|section| ary << section}

  assert_equal expected, ary.sort

  ary = []
  ::IniFile.new('temp.ini').each_section {|section| ary << section}
  assert_equal [], ary
end
test_eql_eh() click to toggle source
# File test/test_inifile.rb, line 146
def test_eql_eh
  assert @ini_file.eql?(@ini_file)
  assert @ini_file.eql?(@ini_file.clone)
  assert !@ini_file.eql?('string')
  assert !@ini_file.eql?(IniFile.new(''))
end
test_freeze() click to toggle source
# File test/test_inifile.rb, line 153
def test_freeze
  assert_equal false, @ini_file.frozen?
  @ini_file.each_section do |s|
    assert_equal false, @ini_file[s].frozen?
  end

  @ini_file.freeze

  assert_equal true, @ini_file.frozen?
  @ini_file.each_section do |s|
    assert_equal true, @ini_file[s].frozen?
  end
end
test_has_section_eh() click to toggle source
# File test/test_inifile.rb, line 167
def test_has_section_eh
  assert_equal true,  @ini_file.has_section?('section_one')
  assert_equal false, @ini_file.has_section?('section_ten')
  assert_equal true,  @ini_file.has_section?(:section_two)
  assert_equal false, @ini_file.has_section?(nil)

  ini_file = ::IniFile.new 'temp.ini'
  assert_equal false, ini_file.has_section?('section_one')
  assert_equal false, ini_file.has_section?('one')
  assert_equal false, ini_file.has_section?('two')
end
test_index() click to toggle source
# File test/test_inifile.rb, line 179
def test_index
  expected = {
    'one' => '1',
    'two' => '2'
  }
  assert_equal expected, @ini_file[:section_one]

  expected = {'three' => '3'}
  assert_equal expected, @ini_file['section_two']

  expected = {
    'four' => '4',
    'five' => '5',
    'six'  => '6',
  }
  assert_equal expected, @ini_file['section three']

  expected = {}
  assert_equal expected, @ini_file['section_four']

  expected = {'seven and eight' => '7 & 8'}
  assert_equal expected, @ini_file['section_five']

  expected = {}
  assert_equal expected, @ini_file['section_six']

  assert_nil @ini_file[nil]

  expected = {}
  ini_file = ::IniFile.new 'temp.ini'
  assert_equal expected, ini_file['section_one']
  assert_equal expected, ini_file['one']
  assert_nil ini_file[nil]
end
test_initialize() click to toggle source
# File test/test_inifile.rb, line 214
def test_initialize
  # see if we can parse different style comments
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/comment.ini'}

  ini_file = ::IniFile.new 'test/data/comment.ini', :comment => '#'
  assert_equal true, ini_file.has_section?('section_one')

  # see if we can parse different style param separators
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/param.ini'}

  ini_file = ::IniFile.new 'test/data/param.ini', :parameter => ':'
  assert_equal true, ini_file.has_section?('section_one')
  assert_equal '1', ini_file['section_one']['one']
  assert_equal '2', ini_file['section_one']['two']

  # make sure we error out on files with bad lines
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/bad_1.ini'}
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/bad_2.ini'}
end
test_sections() click to toggle source
# File test/test_inifile.rb, line 234
def test_sections
  expected = [
    'section_one', 'section_two', 'section three',
    'section_four', 'section_five'
  ].sort

  assert_equal expected, @ini_file.sections.sort

  ini_file = ::IniFile.new 'temp.ini'
  assert_equal [], ini_file.sections
end
test_taint() click to toggle source
# File test/test_inifile.rb, line 246
def test_taint
  assert_equal false, @ini_file.tainted?
  @ini_file.each_section do |s|
    assert_equal false, @ini_file[s].tainted?
  end

  @ini_file.taint

  assert_equal true, @ini_file.tainted?
  @ini_file.each_section do |s|
    assert_equal true, @ini_file[s].tainted?
  end
end
test_write() click to toggle source
# File test/test_inifile.rb, line 260
def test_write
  tmp = 'test/data/temp.ini'
  ::File.delete tmp if ::Kernel.test(ff, tmp)

  @ini_file.save tmp
  assert_equal true, ::Kernel.test(ff, tmp)

  ::File.delete tmp if ::Kernel.test(ff, tmp)

  ini_file = ::IniFile.new tmp
  ini_file.save
  assert_nil ::Kernel.test(ss, tmp)

  ::File.delete tmp if ::Kernel.test(ff, tmp)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.