/storyteller/test/story_teller_test.rb
Ruby | 661 lines | 441 code | 147 blank | 73 comment | 15 complexity | a219326d8f3c5db187458f3b35ca34fa MD5 | raw file
Possible License(s): LGPL-2.1
- require 'test/unit'
- require 'test/test_case'
- require 'story_teller'
- class StoryTellerTest < Test::Unit::TestCase
- def setup
- FileUtils.mkdir("data")
- FileUtils.mkdir("data/mytrip")
- FileUtils.mkdir("data/yourtrip")
- FileUtils.mkdir("data/ourtrip")
- FileUtils.mkdir("data2")
- FileUtils.mkdir("data2/mytrip2")
-
- FileUtils.cp("test/art.jpg","data/mytrip/1.jpg")
- FileUtils.cp("test/dog.jpg","data/mytrip/2.jpg")
- FileUtils.cp("test/art.jpg","data2/mytrip2/Picture 001.jpg")
- FileUtils.cp("test/dog.jpg","data2/mytrip2/Picture 002.jpg")
-
- FileUtils.mkdir("root")
- FileUtils.mkdir("root/one")
- FileUtils.mkdir("root/two")
- FileUtils.mkdir("root/three")
-
- FileUtils.mkdir("tmp_dir")
-
- File.open("root/one/a.txt","a") { |file| file.puts "1" }
- File.open("root/one/b.txt","a") { |file| file.puts "12" }
- File.open("root/one/c.txt","a") { |file| file.puts "123" }
- File.open("root/two/d.txt","a") { |file| file.puts "1234" }
- File.open("root/two/e.txt","a") { |file| file.puts "12345" }
- File.open("root/three/f.txt","a") { |file| file.puts "123456" }
- File.open("root/three/g.txt","a") { |file| file.puts "1234567" }
- File.open("root/three/h.txt","a") { |file| file.puts "12345678" }
- File.open("myflash.grin","a") { |file| file.puts "" }
- Help.display_level = Help::QUIET_LEVEL
- @story = StoryTeller.new("tmp_dir")
- @story.assign_home
- end
-
- def teardown
- FileUtils.rm_rf("data")
- FileUtils.rm_rf("data2")
- FileUtils.rm_rf("root")
- FileUtils.rm_rf("tmp_dir") if File.exist? "tmp_dir"
- FileUtils.rm_rf("blah") if File.exist? "blah"
- FileUtils.rm(".properties") if File.exist? ".properties"
- FileUtils.rm_rf("empty_dir") if File.exist? "empty_dir"
- FileUtils.rm("myflash.grin") if File.exist? "myflash.grin"
- FileUtils.rm("myflash.swf") if File.exist? "myflash.swf"
- FileUtils.rm("myflash.smil") if File.exist? "myflash.smil"
- FileUtils.rm("myflash.blah") if File.exist? "myflash.blah"
- FileUtils.rm("movie.swf") if File.exist? "movie.swf"
- FileUtils.rm("movie.grin") if File.exist? "movie.grin"
- FileUtils.rm("movie.smil") if File.exist? "movie.smil"
- FileUtils.rm("blah.blah.swf") if File.exist? "blah.blah.swf"
- FileUtils.rm("blah.blah.smil") if File.exist? "blah.blah.smil"
- FileUtils.rm("blah.swf") if File.exist? "blah.swf"
- FileUtils.rm("blah.smil") if File.exist? "blah.smil"
- end
- def test_constructor_no_properties
- story = StoryTeller.new("blah")
- assert_equal false, story.valid?
- end
- def test_constructor_default
- File.open(".properties","a") { |file| file.puts "" }
- story = StoryTeller.new
- assert_equal true, story.valid?
- end
- def test_constructor_non_default
- story = StoryTeller.new("tmp_dir")
- assert_equal true, story.valid?
- end
- def test_assign_home
- story = StoryTeller.new("blah")
- assert_equal false, story.valid?
- story.assign_home
- assert_equal true, story.valid?
- assert_equal true, File.exist?("blah/.properties")
- end
- def test_assign_home_sets_flickr_data
- story = StoryTeller.new("blah")
- story.assign_home
-
- fa = FileAnalyser.new
- fa.load_config("blah/.properties")
- assert_equal 'f664bcb86468af50ff33c228b385eb59', fa.property_of("flickr_api_key")
- assert_equal 'dc74f2f7693d245a', fa.property_of("flickr_shared_secret")
- end
- def test_assign_home_do_not_overwrite
- FileUtils.rm("tmp_dir/.properties")
- File.open("tmp_dir/.properties","a") { |infile| infile.puts "aha" }
- st = StoryTeller.new("tmp_dir")
- assert true, File.exists?("tmp_dir/.properties")
- File.open("tmp_dir/.properties","r") do |infile|
- assert_equal "aha\n", infile.gets
- assert_nil infile.gets
- end
- end
- def test_add_media_saves_to_config
- @story.add_media "", "data"
- assert_equal ["data"], @story.media_directories
-
- fa = FileAnalyser.new
- fa.load_config("tmp_dir/.properties")
- assert_equal "data", fa.property_of("media_directory")
- end
-
- def test_add_media_has_name
-
- @story.add_media "Disk1", "root"
- assert_equal true, File.exist?("tmp_dir/Disk1/one")
- assert_equal true, File.exist?("tmp_dir/Disk1/two")
- assert_equal true, File.exist?("tmp_dir/Disk1/.dir")
- assert_equal true, File.exist?("tmp_dir/Disk1/one/.dir")
- assert_equal true, File.exist?("tmp_dir/Disk1/two/.dir")
-
- assert_equal true, File.exist?("tmp_dir/Disk1/one/a.txt")
- assert_equal true, File.exist?("tmp_dir/Disk1/one/b.txt")
- assert_equal true, File.exist?("tmp_dir/Disk1/one/c.txt")
- assert_equal true, File.exist?("tmp_dir/Disk1/two/d.txt")
- assert_equal true, File.exist?("tmp_dir/Disk1/two/e.txt")
-
- end
-
- def test_add_media_no_name
-
- @story.add_media "", "root"
-
- assert_equal true, File.exist?("tmp_dir/one")
- assert_equal true, File.exist?("tmp_dir/two")
- assert_equal true, File.exist?("tmp_dir/.dir")
- assert_equal true, File.exist?("tmp_dir/one/.dir")
- assert_equal true, File.exist?("tmp_dir/two/.dir")
-
- assert_equal true, File.exist?("tmp_dir/one/a.txt")
- assert_equal true, File.exist?("tmp_dir/one/b.txt")
- assert_equal true, File.exist?("tmp_dir/one/c.txt")
- assert_equal true, File.exist?("tmp_dir/two/d.txt")
- assert_equal true, File.exist?("tmp_dir/two/e.txt")
-
- end
-
- def test_add_media_check_properties
-
- @story.add_media "", "root"
-
- assert_equal true, File.exist?("tmp_dir/.dir")
- assert_equal true, File.exist?("tmp_dir/one")
- assert_equal true, File.exist?("tmp_dir/one/.dir")
- assert_equal true, File.exist?("tmp_dir/one/a.txt")
-
- fa = FileAnalyser.new
- fa.load_config "tmp_dir/.dir"
- assert_equal "root", fa.property_of("path")
- fa.load_config "tmp_dir/one/.dir"
- assert_equal "root\\one", fa.property_of("path")
- fa.load_config "tmp_dir/one/a.txt"
- assert_equal "root\\one/a.txt", fa.property_of("path")
-
- end
-
- def test_path_of
-
- @story.add_media "", "root"
-
- assert_equal true, File.exist?("tmp_dir/.dir")
- assert_equal true, File.exist?("tmp_dir/one")
- assert_equal true, File.exist?("tmp_dir/one/.dir")
- assert_equal true, File.exist?("tmp_dir/one/a.txt")
-
- assert_equal "root", @story.path_of("tmp_dir")
- assert_equal "root\\one", @story.path_of("tmp_dir/one")
- assert_equal "root\\one", @story.path_of("tmp_dir/one/")
- assert_equal "root\\one/a.txt", @story.path_of("tmp_dir/one/a.txt")
- fa = FileAnalyser.new
- assert_equal "root", FileAnalyser.path_of("tmp_dir")
- assert_equal "root\\one", FileAnalyser.path_of("tmp_dir/one")
- assert_equal "root\\one", FileAnalyser.path_of("tmp_dir/one/")
- assert_equal "root\\one/a.txt", FileAnalyser.path_of("tmp_dir/one/a.txt")
-
- end
-
-
- def test_add_media_ignore_as_required
- @story.ignore "mytrip"
- @story.ignore "yourtrip"
-
- backup = @story.add_media "", "data"
- assert_equal_one_of "/ourtrip", "\\ourtrip", backup.directories[0]
- end
- def test_ignore_default_dotDir
- assert_equal [".dir"], @story.ignoring
- end
- def test_ignore
- @story.ignore "abc"
- assert_equal ["abc",".dir"], @story.ignoring
- assert_equal Help::IGNORE, @story.all_help[0].id
-
- fa = FileAnalyser.new
- fa.load_config("tmp_dir/.properties")
- assert_equal ["abc"], fa.properties_of("ignore")
- end
- def test_parse_nothing
- @story.parse nil
- @story.parse []
- end
-
- def test_parse_add_media
- @story.parse "add data".split
- assert_equal ["data"], @story.media_directories
- assert_equal Help::BACKUP, @story.all_help[0].id
- end
- def test_parse_add_media_stores_data
- @story.parse "add data".split
- assert_equal true, File.exist?("tmp_dir/mytrip")
- assert_equal true, File.exist?("tmp_dir/yourtrip")
- assert_equal true, File.exist?("tmp_dir/ourtrip")
- end
- def test_parse_add_media_include_root_dir
- @story.parse "add root/one".split
- assert_equal true, File.exist?("tmp_dir/a.txt")
- assert_equal true, File.exist?("tmp_dir/b.txt")
- assert_equal true, File.exist?("tmp_dir/c.txt")
- end
- def test_parse_add_media_specify_directory
- @story.parse "add myone root/one".split
- assert_equal true, File.exist?("tmp_dir/myone/a.txt")
- assert_equal true, File.exist?("tmp_dir/myone/b.txt")
- assert_equal true, File.exist?("tmp_dir/myone/c.txt")
- end
-
- def test_parse_add_unknown_directory
- @story.parse "add /blah".split
- assert_equal Help::UNKNOWN_DIRECTORY, @story.all_help[0].id
- assert_equal Help::ADD_USAGE, @story.all_help[1].id
- end
- def test_parse_add_no_directory
- @story.parse "add".split
- assert_equal Help::NO_DIRECTORY, @story.all_help[0].id
- assert_equal Help::ADD_USAGE, @story.all_help[1].id
- end
- def test_parse_nothing_see_help
- story = StoryTeller.new
- story.parse "".split
- assert_equal Help::NO_REPOSITORY, story.all_help[0].id
- assert_equal Help::REPOSITORY_USAGE, story.all_help[1].id
- end
- def test_parse_ignore
- @story.parse "ignore a b c".split
- assert_equal Help::IGNORE, @story.all_help[0].id
- assert_equal Help::IGNORE, @story.all_help[1].id
- assert_equal Help::IGNORE, @story.all_help[2].id
- end
- def test_parse_no_respository
- @story.parse "ignore a b c".split
- assert_equal Help::IGNORE, @story.all_help[0].id
- assert_equal Help::IGNORE, @story.all_help[1].id
- assert_equal Help::IGNORE, @story.all_help[2].id
- end
- def test_parse_new
- @story.parse "new".split
- assert_equal true, @story.valid?
- assert_equal true, File.exist?("storyteller/.properties")
- assert_equal Help::REPOSITORY_CREATED, @story.all_help[0].id
- end
- def test_parse_new_with_name
- @story.parse "new blah".split
- assert_equal true, @story.valid?
- assert_equal true, File.exist?("blah/.properties")
- assert_equal Help::REPOSITORY_CREATED, @story.all_help[0].id
- end
- def test_parse_tag_root_directory
- @story.parse "add root".split
- @story.parse ["tag",".","Blah blah blah"]
-
- fa = FileAnalyser.new
- fa.load_config "tmp_dir/.dir"
- assert_equal "Blah blah blah", fa.property_of("tag")
- end
- def test_parse_tag_directory
- @story.parse "add root".split
- @story.parse ["tag","one","More stuff"]
-
- fa = FileAnalyser.new
- fa.load_config "tmp_dir/one/.dir"
- assert_equal "More stuff", fa.property_of("tag")
- end
- def test_parse_tag_file
- @story.parse "add root".split
- @story.parse ["tag","one/a.txt","Still More stuff"]
- fa = FileAnalyser.new
- fa.load_config "tmp_dir/one/a.txt"
- assert_equal "Still More stuff", fa.property_of("tag")
- end
-
- def test_parse_tag_file_with_spaces
- @story.parse "add data2".split
- @story.parse ["tag","mytrip2/Picture 001.jpg","Still More stuff"]
- fa = FileAnalyser.new
- fa.load_config "tmp_dir/mytrip2/Picture 001.jpg"
- assert_equal "Still More stuff", fa.property_of("tag")
- end
- def test_parse_flickr_no_file
- @story.parse "add root".split
- @story.parse "flickr".split
- assert_equal Help::FLICKR_USAGE, @story.all_help.last.id
- end
-
- def test_property_of
- @story.parse "add root".split
- @story.parse ["tag",".","Blah blah blah"]
- @story.parse ["tag","one/a.txt","Still More stuff"]
-
- assert_equal "Blah blah blah", @story.property_of("tmp_dir","tag")
- assert_equal "Still More stuff", @story.property_of("tmp_dir/one/a.txt","tag")
- assert_equal "Blah blah blah", FileAnalyser.property_of("tmp_dir","tag")
- assert_equal "Still More stuff", FileAnalyser.property_of("tmp_dir/one/a.txt","tag")
- end
- def test_properties_of
- @story.parse "add root".split
- @story.parse ["tag",".","Blah blah blah"]
- @story.parse ["tag","one/a.txt","Still More stuff"]
-
- assert_equal "Blah blah blah", @story.properties_of("tmp_dir","tag").last
- assert_equal "Still More stuff", @story.properties_of("tmp_dir/one/a.txt","tag").last
- assert_equal "Blah blah blah", FileAnalyser.properties_of("tmp_dir","tag").last
- assert_equal "Still More stuff", FileAnalyser.properties_of("tmp_dir/one/a.txt","tag").last
- end
-
- def test_parse_compile_no_files
- @story.parse "add data".split
- @story.parse "compile".split
- assert_equal Help::COMPILE_USAGE, @story.all_help.last.id
- end
-
- def test_parse_compile_generate_file_smil
- @story.parse "compile myflash.grin myflash.blah smil".split
- assert_equal true, File.exists?("myflash.blah")
- assert_equal false, File.exists?("myflash.swf")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
- def test_parse_compile_generate_file_flash
- @story.parse "compile myflash.grin myflash.blah flash".split
- assert_equal false, File.exists?("myflash.smil")
- assert_equal true, File.exists?("myflash.blah")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
- def test_parse_compile_generate_file_only_input_file
- @story.parse "compile myflash.grin".split
- assert_equal true, File.exists?("myflash.swf")
- assert_equal true, File.exists?("myflash.smil")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
- def test_parse_compile_generate_file_no_type_swf
- @story.parse "compile myflash.grin blah.swf".split
- assert_equal true, File.exists?("blah.swf")
- assert_equal false, File.exists?("blah.smil")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
- def test_parse_compile_generate_file_no_type_smil
- @story.parse "compile myflash.grin blah.smil".split
- assert_equal false, File.exists?("blah.swf")
- assert_equal true, File.exists?("blah.smil")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
- def test_parse_compile_generate_file_no_type_unknown
- @story.parse "compile myflash.grin blah.blah".split
- assert_equal true, File.exists?("blah.blah.swf")
- assert_equal true, File.exists?("blah.blah.smil")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
- def test_parse_compile_generate_file_unknown_extension_and_no_type
- @story.parse "compile myflash.grin blah.blah".split
- assert_equal true, File.exists?("blah.blah.swf")
- assert_equal true, File.exists?("blah.blah.smil")
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- end
-
-
- def test_parse_compile_generate_file_unknown_input_file
- @story.parse "compile unknown.grin myflash.swf".split
- assert_equal Help::UNKNOWN_FILE, @story.all_help.last.id
- end
-
- def test_parse_flash_no_directory
- @story.parse "add data".split
- @story.parse "flash".split
- assert_equal Help::FLASH_USAGE, @story.all_help.last.id
- end
- def test_parse_flash_unknown_directory
- @story.parse "add data".split
- @story.parse "flash movie.swf blah".split
- assert_equal Help::UNKNOWN_FILE, @story.all_help.last.id
- assert_equal false, File.exists?("movie.swf")
- end
- def test_parse_flash_directory
- @story.parse "add data".split
- @story.parse ["tag","mytrip","The title"]
- @story.parse ["tag","mytrip/1.jpg","A picture"]
-
- @story.parse "flash movie.swf tmp_dir/mytrip".split
- assert_equal Help::COMPILE_COMPLETE, @story.all_help.last.id
- assert_equal true, File.exists?("movie.grin")
- File.open("movie.grin") do |infile|
- assert_equal "The title\n", infile.gets
- assert_equal "image \"data\\mytrip/2.jpg\"\n", infile.gets
- assert_equal "image \"data\\mytrip/1.jpg\" A picture\n", infile.gets
- assert_nil infile.gets
- end
- assert_equal true, File.exists?("movie.swf")
-
- end
- def test_freehand_image_with_spaces
- @story.parse "add data2".split
- @story.parse ["tag","mytrip2","The title"]
- @story.parse ["tag","mytrip2/Picture 001.jpg","A picture"]
- freehand = Freehand.new(@story,"movie.swf")
- freehand.parse nil
- freehand.parse "image \"tmp_dir/mytrip2/Picture 001.jpg\""
- freehand.parse "image \"tmp_dir/mytrip2/Picture 002.jpg\""
- freehand.parse "end"
-
- assert_equal true, File.exists?("movie.grin")
- File.open("movie.grin") do |infile|
- assert_equal "image \"data2\\mytrip2/Picture 001.jpg\" A picture\n", infile.gets
- assert_equal "image \"data2\\mytrip2/Picture 002.jpg\"\n", infile.gets
- assert_nil infile.gets
- end
- end
-
- def test_freehand_realizes_images_from_story
- @story.parse "add data".split
- @story.parse ["tag","mytrip","The title"]
- @story.parse ["tag","mytrip/1.jpg","A picture"]
- freehand = Freehand.new(@story,"movie.swf")
- freehand.parse nil
- freehand.parse "image tmp_dir/mytrip/1.jpg"
- freehand.parse "end"
-
- assert_equal true, File.exists?("movie.grin")
- File.open("movie.grin") do |infile|
- assert_equal "image \"data\\mytrip/1.jpg\" A picture\n", infile.gets
- assert_nil infile.gets
- end
- end
-
- def test_freehand_realizes_images_and_labels_from_story
- @story.parse "add data".split
- @story.parse ["tag","mytrip","The title"]
- @story.parse ["tag","mytrip/1.jpg","A picture"]
- freehand = Freehand.new(@story,"movie.swf")
- freehand.parse nil
- freehand.parse "image tmp_dir/mytrip/1.jpg"
- freehand.parse "image tmp_dir/mytrip/1.jpg A better tagged picture"
- freehand.parse "image tmp_dir/mytrip/2.jpg Another great picture"
- freehand.parse "end"
-
- assert_equal true, File.exists?("movie.grin")
- File.open("movie.grin") do |infile|
- assert_equal "image \"data\\mytrip/1.jpg\" A picture\n", infile.gets
- assert_equal "image \"data\\mytrip/1.jpg\" A better tagged picture\n", infile.gets
- assert_equal "image \"data\\mytrip/2.jpg\" Another great picture\n", infile.gets
- assert_nil infile.gets
- end
-
- end
-
- def test_parse_update_media_nothing
- @story.parse "add data".split
- @story.parse "update data".split
- end
- def test_parse_update_media_new_directory
- @story.parse "add data".split
-
- FileUtils.mkdir("data/newtrip")
- FileUtils.cp("test/art.jpg","data/newtrip/1.jpg")
- FileUtils.cp("test/dog.jpg","data/newtrip/2.jpg")
-
- @story.parse "update data".split
- assert_equal true, File.exist?("tmp_dir/newtrip")
- assert_equal true, File.exist?("tmp_dir/newtrip/1.jpg")
- assert_equal true, File.exist?("tmp_dir/newtrip/2.jpg")
-
- end
- def test_size
- @story.parse "add root".split
- assert_equal_one_of 690, 52, @story.size
- end
- def test_extractFileData()
- assert_equal ["",""], StoryTeller.extractFileData("image");
- assert_equal ["1.jpg",""], StoryTeller.extractFileData("blah 1.jpg");
- assert_equal ["1.jpg","some data"], StoryTeller.extractFileData("image 1.jpg some data");
- assert_equal ["Picture 001.jpg",""], StoryTeller.extractFileData("blah \"Picture 001.jpg\"");
- assert_equal ["Picture 001.jpg","more data"], StoryTeller.extractFileData("blah \"Picture 001.jpg\" more data");
- assert_equal ["Picture 001.jpg","more"], StoryTeller.extractFileData("blah \"Picture 001.jpg\" more");
- end
-
- # def test_parse_update_media_removed_directory
- # @story.parse "add data".split
- #
- # FileUtils.rm_rf("data/mytrip")
- #
- # @story.parse "update data".split
- # assert_equal false, File.exist?("tmp_dir/mytrip")
- # end
-
-
- ####################3
- #
- # changing the backup functionality
- #
- # def backup(what,where)
- # return false unless what == "-all"
- #
- # number_of_files = 0
- # media_directories.each do |media_dir|
- # backup_dir = "#{@home}/#{media_dir}"
- # Find.find(backup_dir) do |path|
- # next if File.directory? path
- # next if internal_file? path
- #
- # number_of_files += 1
- # source_file = source_file_for path
- # dest_file = destination_file_for path, where
- # dest_dir = directory_of(dest_file)
- #
- # @all_help << Help.backedup_file(source_file, dest_file)
- # FileUtils.mkdir_p(dest_dir) unless File.exist? dest_dir
- # FileUtils.cp source_file, dest_file
- # end
- # end
- # @all_help << Help.backup_complete(where,number_of_files)
- # end
- #
- # def source_file_for(path)
- # start_index = @home.length + "/Disk1".length
- # "#{data_directory}#{path[start_index..-1]}"
- # end
- #
- # def test_parse_backup_all
- # @story.parse "add root".split
- # @story.parse "backup -all empty_dir".split
- #
- # File.open("empty_dir/Disk1/one/a.txt","r") do |file|
- # assert_equal "1\n", file.gets
- # assert_equal nil, file.gets
- # end
- #
- # File.open("empty_dir/Disk1/three/f.txt","r") do |file|
- # assert_equal "123456\n", file.gets
- # assert_equal nil, file.gets
- # end
- # end
- #
- # def test_parse_backup_all_only_set_directories
- # File.open("tmp_dir/ignore.me","a") {}
- # @story.parse "add root".split
- # @story.parse "backup -all empty_dir".split
- # assert_equal false, File.exist?("empty_dir/ignore.me")
- # end
- #
- # def test_parse_backup_message
- # @story.parse "add root".split
- # @story.parse "backup -all empty_dir".split
- # assert_equal Help::BACKUP_COMPLETE, @story.all_help.last.id
- # end
- #
- # def test_parse_add_store_root_as_property
- # @story.parse "add root".split
- # @story.parse "backup -all empty_dir".split
- # assert_equal Help::BACKUP_COMPLETE, @story.all_help.last.id
- # end
-
- end