#!/usr/bin/env ruby require 'test/unit' currentPath = File.dirname(__FILE__) require File.join( currentPath, '../../lib/masterview/io' ) class TestMTimeStringHashMIO < Test::Unit::TestCase include MasterView TestStringMIOTemplatePathname = Pathname.new('../tmp/template') ErbPathname = Pathname.new('../tmp/erb') HelloWorldFullPath = '../tmp/template/hello_world.txt' HelloWorldPath = 'hello_world.txt' TestWritePath = 'foo.txt' TestWritePathname = Pathname.new(TestWritePath) IOMgr = MIOTrees.new def setup IOMgr.template = MTimeStringHashMIOTree.new('.html') IOMgr.erb = MTimeStringHashMIOTree.new('.rhtml' ) IOMgr.file = MTimeStringHashMIOTree.new() @template_hash = IOMgr.template.string_hash @erb_hash = IOMgr.erb.string_hash @file_hash = IOMgr.file.string_hash end def test_create_foo f = IOMgr.template.path( 'foo') assert_equal MTimeStringMIO, f.class assert_equal 'foo', f.pathname.to_s assert_equal 'foo', f.full_pathname.to_s assert_nil f.mtime end def test_create_foo_with_useless_dots f = IOMgr.template.path( 'foo/bar/../cat') assert_equal MTimeStringMIO, f.class assert_equal 'foo/cat', f.pathname.to_s assert_equal 'foo/cat', f.full_pathname.to_s f.write('hello') assert @template_hash.has_key?('foo/cat') end def test_create_foo_with_useless_dots2 f = IOMgr.template.path( 'foo/bar/../cat/../dog') assert_equal MTimeStringMIO, f.class assert_equal 'foo/dog', f.pathname.to_s assert_equal 'foo/dog', f.full_pathname.to_s f.write('hello') assert @template_hash.has_key?('foo/dog') end def test_create_useless_dots f = IOMgr.template.path( '../../foo') assert_equal MTimeStringMIO, f.class assert_equal '../../foo', f.pathname.to_s assert_equal '../../foo', f.full_pathname.to_s f.write('hello') assert @template_hash['../../foo'] end def test_create_bar f = IOMgr.erb.path( 'bar') assert_equal MTimeStringMIO, f.class assert_equal 'bar', f.pathname.to_s assert_equal 'bar', f.full_pathname.to_s end def test_create_cat f = IOMgr.file.path('cat') assert_equal MTimeStringMIO, f.class assert_equal 'cat', f.pathname.to_s assert_equal 'cat', f.full_pathname.to_s end def test_create_foo_w_ext f = IOMgr.template.path( 'foo.txt') assert_equal MTimeStringMIO, f.class assert_equal 'foo.txt', f.pathname.to_s assert_equal 'foo.txt', f.full_pathname.to_s end def test_create_parts f = IOMgr.template.path( 'sub/one/two/three/foo.txt') assert_equal MTimeStringMIO, f.class assert_equal 'sub/one/two/three/foo.txt', f.pathname.to_s assert_equal 'sub/one/two/three/foo.txt', f.full_pathname.to_s end def test_read f = IOMgr.template.path( HelloWorldPath) assert_nil f.read assert_nil f.mtime @template_hash[HelloWorldPath] = "hello\nworld" assert_equal "hello\nworld", f.read assert_kind_of Time, f.mtime end def test_read_disable_cache @template_hash[HelloWorldPath] = "hello\nworld" f = IOMgr.template.path( HelloWorldPath) assert_equal "hello\nworld", f.read @template_hash[HelloWorldPath] = "foo\nbar" #assert_equal "hello\nworld", f.read #still reading from cache, #todo enable when caching module is ready for testing assert_equal "foo\nbar", f.read(:disable_cache => true) end def test_write_string f = IOMgr.template.path( TestWritePath) assert_nil f.mtime f.write("foo\nbar") assert_equal "foo\nbar", @template_hash[TestWritePath] assert_equal "foo\nbar", IOMgr.template.path( TestWritePath).read assert_kind_of Time, f.mtime m = f.mtime assert_equal m, f.mtime sleep 1 # wait noticeable time f.write("updated") assert_kind_of Time, f.mtime assert f.mtime > m end def test_write_block @template_hash.delete(TestWritePath) IOMgr.template.path( TestWritePath).write { |f| f.write('hello') } assert_equal "hello", IOMgr.template.path( TestWritePath).read end def test_write_multi_block @template_hash.delete(TestWritePath) IOMgr.template.path( TestWritePath).write do |f| f.write('hello') f.write('world') end assert_equal "helloworld", IOMgr.template.path( TestWritePath).read end def test_write_mkpath @template_hash.clear IOMgr.template.path( TestWritePath).write("foo\nbar") assert_equal "foo\nbar", IOMgr.template.path( TestWritePath).read end def test_exist @template_hash.clear assert_equal false, IOMgr.template.path( TestWritePath ).exist? assert @template_hash.empty? IOMgr.template.path( TestWritePath).write("foo") assert_equal true, IOMgr.template.path( TestWritePath).exist? end def test_identical @template_hash.clear assert_equal false, IOMgr.template.path( TestWritePath).identical?("hello\nworld") @template_hash[TestWritePath] = "hello\nworld" assert_equal true, IOMgr.template.path( TestWritePath).identical?("hello\nworld") assert_equal false, IOMgr.template.path( TestWritePath).identical?("foo") end def test_findfiles_single_dir @template_hash.clear IOMgr.template.path( 'apple.txt').write('foo') IOMgr.template.path( 'bar.txt').write('foo') @template_hash[('baz.txt').to_s] = 'foo' found = IOMgr.template.find assert_equal 3, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal MTimeStringMIO, found[2].class assert_equal 'apple.txt', found[0].pathname.to_s assert_equal 'bar.txt', found[1].pathname.to_s assert_equal ('baz.txt').to_s, found[2].pathname.to_s assert_equal ('baz.txt').to_s, found[2].full_pathname.to_s end def test_findfiles_single_sub_dir @template_hash.clear IOMgr.template.path( 'sub/apple.txt').write('foo') IOMgr.template.path( 'sub/bar.txt').write('foo') IOMgr.template.path( 'sub/baz.txt').write('foo') found = IOMgr.template.find(:path => 'sub') assert_equal 3, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal MTimeStringMIO, found[2].class assert_equal 'sub/apple.txt', found[0].pathname.to_s assert_equal 'sub/bar.txt', found[1].pathname.to_s assert_equal 'sub/baz.txt', found[2].pathname.to_s end def test_findfiles_single_sub2_dir_multi_arg @template_hash.clear IOMgr.template.path( 'sub/two/apple.txt').write('foo') IOMgr.template.path( 'sub/two/bar.txt').write('foo') IOMgr.template.path( 'sub/two/baz.txt').write { |f| f.write('foo') } found = IOMgr.template.find(:path => 'sub/two') assert_equal 3, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal MTimeStringMIO, found[2].class assert_equal 'sub/two/apple.txt', found[0].full_pathname.to_s assert_equal 'sub/two/apple.txt', found[0].pathname.to_s assert_equal 'sub/two/bar.txt', found[1].pathname.to_s assert_equal 'sub/two/baz.txt', found[2].pathname.to_s end def test_findfiles_single_dir_pattern @template_hash.clear IOMgr.template.path( 'sub/two/apple.txt').write('foo') IOMgr.template.path( 'sub/two/bar.log').write('foo') @template_hash['sub/two/baz.txt'] = 'foo' found = IOMgr.template.find(:path => 'sub/two', :pattern => '*.txt') assert_equal 2, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal 'sub/two/apple.txt', found[0].pathname.to_s assert_equal 'sub/two/baz.txt', found[1].pathname.to_s end def test_findfiles_multi_dir_root @template_hash.clear IOMgr.template.path( 'one/two/three.txt').write('foo') IOMgr.template.path( 'sub/two/apple.txt').write('foo') IOMgr.template.path( 'sub/two/bar.log').write('foo') IOMgr.template.path( 'sub/two/baz.txt').write { |f| f.write('foo') } found = IOMgr.template.find assert_equal 4, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal MTimeStringMIO, found[2].class assert_equal MTimeStringMIO, found[3].class assert_equal 'one/two/three.txt', found[0].pathname.to_s assert_equal 'sub/two/apple.txt', found[1].pathname.to_s assert_equal 'sub/two/bar.log', found[2].pathname.to_s assert_equal 'sub/two/baz.txt', found[3].pathname.to_s end def test_findfiles_multi_dir_pattern @template_hash.clear IOMgr.template.path( 'one/two/three.txt').write('foo') IOMgr.template.path( 'sub/two/apple.txt').write('foo') IOMgr.template.path( 'sub/two/bar.log').write('foo') IOMgr.template.path( 'sub/two/baz.txt').write('foo') found = IOMgr.template.find(:pattern => '*.txt') assert_equal 3, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal MTimeStringMIO, found[2].class assert_equal 'one/two/three.txt', found[0].pathname.to_s assert_equal 'sub/two/apple.txt', found[1].pathname.to_s assert_equal 'sub/two/baz.txt', found[2].pathname.to_s end def test_findfiles_multi_dir_pattern_deep @template_hash.clear IOMgr.template.path( 'one/two/three.txt').write('foo') IOMgr.template.path( 'sub/two/apple.txt').write('foo') IOMgr.template.path( 'sub/two/bar.log').write('foo') IOMgr.template.path( 'sub/two/baz.txt').write('foo') found = IOMgr.template.find(:path => 'sub/two', :pattern => '*.txt') assert_equal 2, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal 'sub/two/apple.txt', found[0].pathname.to_s assert_equal 'sub/two/baz.txt', found[1].pathname.to_s end def test_findfiles_multi_dir_pattern_deep_with_block @template_hash.clear IOMgr.template.path( 'one/two/three.txt').write('foo') IOMgr.template.path( 'sub/two/apple.txt').write('foo') IOMgr.template.path( 'sub/two/bar.log').write('foo') IOMgr.template.path( 'sub/two/baz.txt').write('foo') array_of_mio = [] found = IOMgr.template.find(:path => 'sub/two', :pattern => '*.txt'){ |mio| array_of_mio << mio } assert_equal 2, found.length assert_equal MTimeStringMIO, found[0].class assert_equal MTimeStringMIO, found[1].class assert_equal 'sub/two/apple.txt', found[0].pathname.to_s assert_equal 'sub/two/baz.txt', found[1].pathname.to_s assert_equal 2, array_of_mio.length assert_kind_of MTimeStringMIO, array_of_mio[0] assert_kind_of MTimeStringMIO, array_of_mio[1] assert_equal 'sub/two/apple.txt', array_of_mio[0].pathname.to_s assert_equal 'sub/two/baz.txt', array_of_mio[1].pathname.to_s end end