#!/usr/bin/env ruby require 'test/unit' currentPath = File.dirname(__FILE__) require File.join( currentPath, '../../lib/masterview/io' ) class TestStringHashMIO < Test::Unit::TestCase include MasterView MVTestDir = File.expand_path( File.join(File.dirname(__FILE__), '..' ) ) TemplateDir = "#{MVTestDir}/tmp/template" TemplateErbDir = "#{MVTestDir}/tmp/erb" HelloWorldPath = 'hello_world.txt' #HelloWorldFullPath = "#{TemplateDir}/hello_world.txt" TestWritePath = 'foo.txt' #TestWriteFullPath = "#{TemplateDir}/foo.txt" #TestWritePathname = Pathname.new(TestWriteFullPath) IOMgr = MIOTrees.new def setup @template_hash = {} @erb_hash = {} @file_hash = {} IOMgr.template = StringHashMIOTree.new(@template_hash, '.html') IOMgr.erb = StringHashMIOTree.new(@erb_hash, '.rhtml' ) IOMgr.file = StringHashMIOTree.new(@file_hash) end #------------------------------------------------------------------------------ # Test Cases #------------------------------------------------------------------------------ def test_create_foo verify_mio_handler_paths( 'foo', IOMgr.template ) end def test_create_foo_with_useless_dots f = IOMgr.template.path( 'foo/bar/../cat') assert_equal StringMIO, 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 StringMIO, 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 StringMIO, 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 verify_mio_handler_paths( 'bar', IOMgr.erb ) end def test_create_cat verify_mio_handler_paths( 'cat', IOMgr.file ) end def test_create_foo_w_ext verify_mio_handler_paths( 'foo.txt', IOMgr.template ) end def test_create_parts verify_mio_handler_paths( 'sub/one/two/three/foo.txt', IOMgr.template ) end def test_read file_content = "hello\nworld" @template_hash[HelloWorldPath] = file_content # stuff into IOMgr.template verify_mio_handler_read( HelloWorldPath, file_content, IOMgr.template, @template_hash ) end def test_read_disable_cache file_content = "hello\nworld" @template_hash[HelloWorldPath] = file_content # stuff into IOMgr.template verify_mio_handler_read( HelloWorldPath, file_content, IOMgr.template, @template_hash ) file_content_changed = "foo\nbar" @template_hash[HelloWorldPath] = file_content_changed # stuff into IOMgr.template #verify_mio_handler_read( HelloWorldPath, file_content, IOMgr.template ) #still reading from cache, #todo enable when caching module is ready for testing verify_mio_handler_read_no_cache( HelloWorldPath, file_content_changed, IOMgr.template, @template_hash ) end def test_write_string @template_hash.delete(TestWritePath) file_content = "foo\nbar" IOMgr.template.path( TestWritePath).write(file_content) verify_mio_handler_read( TestWritePath, file_content, IOMgr.template, @template_hash ) end def test_write_block @template_hash.delete(TestWritePath) file_content = "hello" IOMgr.template.path( TestWritePath).write { |f| f.write(file_content) } verify_mio_handler_read( TestWritePath, file_content, IOMgr.template, @template_hash ) end def test_write_multi_block @template_hash.delete(TestWritePath) file_content_strings = [ 'hello', 'world'] file_content = file_content_strings.join('') IOMgr.template.path( TestWritePath).write do |f| file_content_strings.each { |str| f.write(str) } end verify_mio_handler_read( TestWritePath, file_content, IOMgr.template, @template_hash ) end def test_write_mkpath @template_hash.clear file_content = "foo\nbar" IOMgr.template.path( TestWritePath).write(file_content) verify_mio_handler_read( TestWritePath, file_content, IOMgr.template, @template_hash ) 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 StringMIO, found[0].class assert_equal StringMIO, found[1].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, found[1].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, found[1].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, found[1].class assert_equal StringMIO, found[2].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, found[1].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, 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 StringMIO, found[0].class assert_equal StringMIO, 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 StringMIO, array_of_mio[0] assert_kind_of StringMIO, 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 #------------------------------------------------------------------------------ # Test helpers #------------------------------------------------------------------------------ # verify that a string-hash mio_handler produces the expected file reference paths def verify_mio_handler_paths( rel_path, mio_handler ) #ISSUE: should consider supporting symmetric protocol with FileMIO (pathname) #assert_equal '', mio_handler.to_s # this is just std object impl right now assert_equal true, mio_handler.respond_to?(:to_s) assert_equal false, mio_handler.respond_to?(:to_str) file_path = mio_handler.path( rel_path ) assert_equal StringMIO, file_path.class assert_equal rel_path, file_path.pathname.to_s assert_equal rel_path, file_path.full_pathname.to_s end # verify that the string-hash mio_handler reads the expected content def verify_mio_handler_read( rel_path, file_content, mio_handler, hash_storage ) # verify the actual hash contents are as expected assert_equal file_content, hash_storage[rel_path] # verify that the MIO handler obtains the expected content file_path = mio_handler.path( rel_path ) #n/a: assert_equal rel_path, file_path.to_str assert_equal file_content, file_path.read assert_equal file_content, hash_storage[rel_path] end # verify that the string-hash mio_handler reads the expected content def verify_mio_handler_read_no_cache( rel_path, file_content, mio_handler, hash_storage ) # verify the actual hash contents are as expected assert_equal file_content, hash_storage[rel_path] # verify that the MIO handler obtains the expected content file_path = mio_handler.path( rel_path ) #n/a: assert_equal rel_path, file_path.to_str assert_equal file_content, file_path.read(:disable_cache => true) end end