[前][次][番号順一覧][スレッド一覧]

ruby-changes:21281

From: ayumin <ko1@a...>
Date: Sun, 25 Sep 2011 21:05:15 +0900 (JST)
Subject: [ruby-changes:21281] ayumin:r33330 (trunk): * test/syck/test/yamldbm.rb: add test for Syck::DBM.

ayumin	2011-09-25 21:05:06 +0900 (Sun, 25 Sep 2011)

  New Revision: 33330

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33330

  Log:
    * test/syck/test/yamldbm.rb: add test for Syck::DBM.
    * test/psych/test_yamldbm.rb: add test for Psych::DBM.
    * test/psych/test_yamlstore.rb: add test for Psych::PStore.

  Added files:
    trunk/test/psych/test_yamldbm.rb
    trunk/test/psych/test_yamlstore.rb
    trunk/test/syck/test_yamldbm.rb
  Modified files:
    trunk/ChangeLog

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 33329)
+++ ChangeLog	(revision 33330)
@@ -1,3 +1,9 @@
+Sun Sep 25 20:57:18 2011  Ayumu AIZAWA  <ayumu.aizawa@g...>
+
+	* test/syck/test/yamldbm.rb: add test for Syck::DBM.
+	* test/psych/test_yamldbm.rb: add test for Psych::DBM.
+	* test/psych/test_yamlstore.rb: add test for Psych::PStore.
+
 Sun Sep 25 20:54:10 2011  Ayumu AIZAWA  <ayumu.aizawa@g...>
 
 	* lib/yaml/dbm/dbm.rb: fix #update, add #key for using istead #index.
Index: test/psych/test_yamlstore.rb
===================================================================
--- test/psych/test_yamlstore.rb	(revision 0)
+++ test/psych/test_yamlstore.rb	(revision 33330)
@@ -0,0 +1,79 @@
+require 'test/unit'
+require 'yaml/store'
+Psych::Store = YAML::Store unless defined?(Psych::Store)
+
+module Psych
+  class YAMLStoreTest < Test::Unit::TestCase
+    def setup
+      @engine, YAML::ENGINE.yamler = YAML::ENGINE.yamler, 'psych'
+      @yamlstore_file = "yamlstore.tmp.#{Process.pid}"
+      @yamlstore = YAML::Store.new(@yamlstore_file)
+    end
+
+    def teardown
+      YAML::ENGINE.yamler = @engine
+      File.unlink(@yamlstore_file) rescue nil
+    end
+
+    def test_opening_new_file_in_readonly_mode_should_result_in_empty_values
+      @yamlstore.transaction(true) do
+        assert_nil @yamlstore[:foo]
+        assert_nil @yamlstore[:bar]
+      end
+    end
+
+    def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
+      @yamlstore.transaction do
+        assert_nil @yamlstore[:foo]
+        assert_nil @yamlstore[:bar]
+      end
+    end
+
+    def test_data_should_be_loaded_correctly_when_in_readonly_mode
+      @yamlstore.transaction do
+        @yamlstore[:foo] = "bar"
+      end
+      @yamlstore.transaction(true) do
+        assert_equal "bar", @yamlstore[:foo]
+      end
+    end
+
+    def test_data_should_be_loaded_correctly_when_in_readwrite_mode
+      @yamlstore.transaction do
+        @yamlstore[:foo] = "bar"
+      end
+      @yamlstore.transaction do
+        assert_equal "bar", @yamlstore[:foo]
+      end
+    end
+
+    def test_changes_after_commit_are_discarded
+      @yamlstore.transaction do
+        @yamlstore[:foo] = "bar"
+        @yamlstore.commit
+        @yamlstore[:foo] = "baz"
+      end
+      @yamlstore.transaction(true) do
+        assert_equal "bar", @yamlstore[:foo]
+      end
+    end
+
+    def test_changes_are_not_written_on_abort
+      @yamlstore.transaction do
+        @yamlstore[:foo] = "bar"
+        @yamlstore.abort
+      end
+      @yamlstore.transaction(true) do
+        assert_nil @yamlstore[:foo]
+      end
+    end
+
+    def test_writing_inside_readonly_transaction_raises_error
+      assert_raise(PStore::Error) do
+        @yamlstore.transaction(true) do
+          @yamlstore[:foo] = "bar"
+        end
+      end
+    end
+  end
+end
Index: test/psych/test_yamldbm.rb
===================================================================
--- test/psych/test_yamldbm.rb	(revision 0)
+++ test/psych/test_yamldbm.rb	(revision 33330)
@@ -0,0 +1,152 @@
+# -*- coding: UTF-8 -*-
+require 'test/unit'
+require 'yaml/dbm'
+Psych::DBM = YAML::DBM unless defined?(Psych::DBM)
+
+module Psych
+  class YAMLDBMTest < Test::Unit::TestCase
+    def setup
+      @engine, YAML::ENGINE.yamler = YAML::ENGINE.yamler, 'psych'
+      @yamldbm_file = "yamldbm.tmp.#{Process.pid}"
+      @yamldbm = YAML::DBM.new(@yamldbm_file)
+    end
+
+    def teardown
+      YAML::ENGINE.yamler = @engine
+      File.unlink(@yamldbm_file + '.db') rescue nil
+    end
+
+    def test_store
+      @yamldbm.store('a','b')
+      @yamldbm.store('c','d')
+      assert_equal 'b', @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_nil @yamldbm['e']
+    end
+
+    def test_store_using_carret
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal 'b', @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_nil @yamldbm['e']
+    end
+
+    def test_to_a
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal([['a','b'],['c','d']], @yamldbm.to_a)
+    end
+
+    def test_to_hash
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal({'a'=>'b','c'=>'d'}, @yamldbm.to_hash)
+    end
+
+    def test_has_value?
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal true, @yamldbm.has_value?('b')
+      assert_equal true, @yamldbm.has_value?('d')
+      assert_equal false, @yamldbm.has_value?('f')
+    end
+
+    # Note:
+    # YAML::DBM#index makes warning from internal of ::DBM#index.
+    # It says 'DBM#index is deprecated; use DBM#key', but DBM#key
+    # behaves not same as DBM#index.
+    #
+    # def test_index
+    #  @yamldbm['a'] = 'b'
+    #  @yamldbm['c'] = 'd'
+    #  assert_equal 'a', @yamldbm.index('b')
+    #  assert_equal 'c', @yamldbm.index('d')
+    #  assert_nil @yamldbm.index('f')
+    # end
+
+    def test_key
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal 'a', @yamldbm.key('b')
+      assert_equal 'c', @yamldbm.key('d')
+      assert_nil @yamldbm.key('f')
+    end
+
+    def test_fetch
+      assert_equal('bar', @yamldbm['foo']='bar')
+      assert_equal('bar', @yamldbm.fetch('foo'))
+      assert_nil @yamldbm.fetch('bar')
+      assert_equal('baz', @yamldbm.fetch('bar', 'baz'))
+      assert_equal('foobar', @yamldbm.fetch('bar') {|key| 'foo' + key })
+    end
+
+    def test_shift
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal(['a','b'], @yamldbm.shift)
+      assert_equal(['c','d'], @yamldbm.shift)
+      assert_nil @yamldbm.shift
+    end
+
+    def test_invert
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal({'b'=>'a','d'=>'c'}, @yamldbm.invert)
+    end
+
+    def test_update
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm.update({'c'=>'d','e'=>'f'})
+      assert_equal 'b', @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+    end
+
+    def test_replace
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm.replace({'c'=>'d','e'=>'f'})
+      assert_nil @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+    end
+
+    def test_delete
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal 'b', @yamldbm.delete('a')
+      assert_nil @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_nil @yamldbm.delete('e')
+    end
+
+    def test_delete_if
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm['e'] = 'f'
+
+      @yamldbm.delete_if {|k,v| k == 'a'}
+      assert_nil @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+
+      @yamldbm.delete_if {|k,v| v == 'd'}
+      assert_nil @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+
+      @yamldbm.delete_if {|k,v| false }
+      assert_equal 'f', @yamldbm['e']
+    end
+
+    def test_reject
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm['e'] = 'f'
+      assert_equal({'c'=>'d','e'=>'f'}, @yamldbm.reject {|k,v| k == 'a'})
+      assert_equal({'a'=>'b','e'=>'f'}, @yamldbm.reject {|k,v| v == 'd'})
+      assert_equal({'a'=>'b','c'=>'d','e'=>'f'}, @yamldbm.reject {false})
+    end
+  end
+end
Index: test/syck/test_yamldbm.rb
===================================================================
--- test/syck/test_yamldbm.rb	(revision 0)
+++ test/syck/test_yamldbm.rb	(revision 33330)
@@ -0,0 +1,152 @@
+# -*- coding: UTF-8 -*-
+require 'test/unit'
+require 'yaml/dbm'
+Syck::DBM = YAML::DBM unless defined?(Syck::DBM)
+
+module Syck
+  class YAMLDBMTest < Test::Unit::TestCase
+    def setup
+      @engine, YAML::ENGINE.yamler = YAML::ENGINE.yamler, 'syck'
+      @yamldbm_file = "yamldbm.tmp.#{Process.pid}"
+      @yamldbm = YAML::DBM.new(@yamldbm_file)
+    end
+
+    def teardown
+      YAML::ENGINE.yamler = @engine
+      File.unlink(@yamldbm_file + '.db') rescue nil
+    end
+
+    def test_store
+      @yamldbm.store('a','b')
+      @yamldbm.store('c','d')
+      assert_equal 'b', @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_nil @yamldbm['e']
+    end
+
+    def test_store_using_carret
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal 'b', @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_nil @yamldbm['e']
+    end
+
+    def test_to_a
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal([['a','b'],['c','d']], @yamldbm.to_a)
+    end
+
+    def test_to_hash
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal({'a'=>'b','c'=>'d'}, @yamldbm.to_hash)
+    end
+
+    def test_has_value?
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal true, @yamldbm.has_value?('b')
+      assert_equal true, @yamldbm.has_value?('d')
+      assert_equal false, @yamldbm.has_value?('f')
+    end
+
+    # Note:
+    # YAML::DBM#index makes warning from internal of ::DBM#index.
+    # It says 'DBM#index is deprecated; use DBM#key', but DBM#key
+    # behaves not same as DBM#index.
+    #
+    # def test_index
+    #  @yamldbm['a'] = 'b'
+    #  @yamldbm['c'] = 'd'
+    #  assert_equal 'a', @yamldbm.index('b')
+    #  assert_equal 'c', @yamldbm.index('d')
+    #  assert_nil @yamldbm.index('f')
+    # end
+
+    def test_key
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal 'a', @yamldbm.key('b')
+      assert_equal 'c', @yamldbm.key('d')
+      assert_nil @yamldbm.key('f')
+    end
+
+    def test_fetch
+      assert_equal('bar', @yamldbm['foo']='bar')
+      assert_equal('bar', @yamldbm.fetch('foo'))
+      assert_nil @yamldbm.fetch('bar')
+      assert_equal('baz', @yamldbm.fetch('bar', 'baz'))
+      assert_equal('foobar', @yamldbm.fetch('bar') {|key| 'foo' + key })
+    end
+
+    def test_shift
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal(['a','b'], @yamldbm.shift)
+      assert_equal(['c','d'], @yamldbm.shift)
+      assert_nil @yamldbm.shift
+    end
+
+    def test_invert
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal({'b'=>'a','d'=>'c'}, @yamldbm.invert)
+    end
+
+    def test_update
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm.update({'c'=>'d','e'=>'f'})
+      assert_equal 'b', @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+    end
+
+    def test_replace
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm.replace({'c'=>'d','e'=>'f'})
+      assert_nil @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+    end
+
+    def test_delete
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      assert_equal 'b', @yamldbm.delete('a')
+      assert_nil @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_nil @yamldbm.delete('e')
+    end
+
+    def test_delete_if
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm['e'] = 'f'
+
+      @yamldbm.delete_if {|k,v| k == 'a'}
+      assert_nil @yamldbm['a']
+      assert_equal 'd', @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+
+      @yamldbm.delete_if {|k,v| v == 'd'}
+      assert_nil @yamldbm['c']
+      assert_equal 'f', @yamldbm['e']
+
+      @yamldbm.delete_if {|k,v| false }
+      assert_equal 'f', @yamldbm['e']
+    end
+
+    def test_reject
+      @yamldbm['a'] = 'b'
+      @yamldbm['c'] = 'd'
+      @yamldbm['e'] = 'f'
+      assert_equal({'c'=>'d','e'=>'f'}, @yamldbm.reject {|k,v| k == 'a'})
+      assert_equal({'a'=>'b','e'=>'f'}, @yamldbm.reject {|k,v| v == 'd'})
+      assert_equal({'a'=>'b','c'=>'d','e'=>'f'}, @yamldbm.reject {false})
+    end
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]