test/image_test.rb in ray-0.1.0.pre1 vs test/image_test.rb in ray-0.1.0

- old
+ new

@@ -1,82 +1,102 @@ require File.expand_path(File.dirname(__FILE__)) + '/helpers.rb' context Ray::Image do setup { Ray::Image } - context "created with a hash" do - setup { topic.new(:w => 50, :h => 70) } + asserts(:new, path_of("pop.wav")).raises_kind_of RuntimeError + asserts(:new, path_of("thing.png")).raises_kind_of RuntimeError - asserts(:w).equals(50) - asserts(:h).equals(70) - end + asserts(:new, [0, 0]).raises_kind_of Exception - denies("creating from a file") { - topic.new(path_of("aqua.bmp")) - }.raises(RuntimeError) + asserts(:new, StringIO.new(File.read(path_of("pop.wav")))). + raises_kind_of RuntimeError +end - asserts("creating from a non-existing file") { - topic.new(path_of("doesnt_exist.bmp")) - }.raises(RuntimeError) +context "an image loaded from a file" do + setup { Ray::Image.new path_of("sprite.png") } - denies("creating from an IO object") { - open(path_of("aqua.bmp")) { |io| topic.new(io) } - }.raises(RuntimeError) + asserts(:width).equals 128 + asserts(:height).equals 192 + asserts(:size).equals Ray::Vector2[128, 192] - asserts("creating from an invalid IO object") { - open(path_of("VeraMono.ttf")) { |io| topic.new(io) } - }.raises(RuntimeError) + asserts(:[], 128, 100).nil + asserts(:[], 100, 192).nil - denies "two different images are equal, despite their content" do - a = topic.new(:w => 10, :h => 10).fill(Ray::Color.red).update - b = topic.new(:w => 10, :h => 10).fill(Ray::Color.red).update + asserts(:[]=, 128, 100, Ray::Color.green).raises_kind_of RangeError + asserts(:[]=, 100, 192, Ray::Color.green).raises_kind_of RangeError - a == b + context "after changing a pixel" do + hookup { topic[0, 10] = Ray::Color.green } + asserts(:[], 0, 10).equals Ray::Color.green + denies(:[], 1, 10).equals Ray::Color.green end - - asserts "two images representing the same one are equal" do - Ray.screen == Ray.screen - end end -context "an image" do - setup { Ray::Image.new(:w => 10, :h => 10).fill(Ray::Color.green).update } +context "an image loaded from an IO" do + setup { open(path_of("sprite.png"), "rb") { |io| Ray::Image.new(io) } } - asserts(:size).equals Ray::Vector2[10, 10] + asserts(:width).equals 128 + asserts(:height).equals 192 + asserts(:size).equals Ray::Vector2[128, 192] +end - asserts("#dup returns a copy of the receiver") { topic.dup != topic } +context "an image created from a size" do + setup { Ray::Image.new [64, 128] } - asserts("a pixel outside the image") { topic[10, 10] }.nil - asserts("a pixel inside the image is a Ray::Color") { topic[5, 5].is_a? Ray::Color } + asserts(:width).equals 64 + asserts(:height).equals 128 + asserts(:size).equals Ray::Vector2[64, 128] - asserts("setting a pixel outside the image") { - topic.lock { topic[10, 10] = Ray::Color.red } - }.raises(ArgumentError) + asserts(:tex_rect, [0, 0, 64, 128]).equals Ray::Rect[0, 0, 1, 1] + asserts(:tex_rect, [0, 0, 32, 128]).equals Ray::Rect[0, 0, 0.5, 1] + asserts(:tex_rect, [0, 0, 64, 64]).equals Ray::Rect[0, 0, 1, 0.5] + asserts(:tex_rect, [32, 32, 32, 32]).equals Ray::Rect[0.5, 0.25, 0.5, 0.25] - denies("setting a pixel outside the image") { - topic { topic[5, 5] = Ray::Color.red } - }.raises(RuntimeError) + denies :smooth? - asserts("setting a pixel in a non-locked image") { - topic[5, 5] = Ray::Color.red - }.raises(RuntimeError) - - context "after having changed a pixel" do - hookup { topic.lock { topic[3, 2] = Ray::Color.green } } - asserts("the changed pixel") { topic[3, 2] }.equals(Ray::Color.green) + context "after enabling smoothing" do + hookup { topic.smooth = true } + asserts :smooth? end +end - asserts(:clip).equals(Ray::Rect.new(0, 0, 10, 10)) +context "an image copy" do + setup do + img = Ray::Image.new [2, 2] + img[0, 0] = Ray::Color.red + img[0, 1] = Ray::Color.green + img[1, 0] = Ray::Color.blue + img[1, 1] = Ray::Color.white - context "after having changed the clipping rect" do - hookup { topic.clip([5, 5, 3, 3]) } - asserts(:clip).equals(Ray::Rect.new(5, 5, 3, 3)) + img.dup end - asserts("clip inside a block and otside the block") { - res = [] - topic.clip([5, 5, 3, 3]) { res << topic.clip } - res << topic.clip - }.equals([Ray::Rect.new(5, 5, 3, 3), Ray::Rect.new(0, 0, 10, 10)]) + asserts(:size).equals Ray::Vector2[2, 2] + + asserts(:[], 0, 0).equals Ray::Color.red + asserts(:[], 0, 1).equals Ray::Color.green + asserts(:[], 1, 0).equals Ray::Color.blue + asserts(:[], 1, 1).equals Ray::Color.white + + context "saved and loaded again" do + path = path_of("test_save.png") + + setup do + topic.write path + Ray::Image.new path + end + + asserts(:size).equals Ray::Vector2[2, 2] + + asserts(:[], 0, 0).equals Ray::Color.red + asserts(:[], 0, 1).equals Ray::Color.green + asserts(:[], 1, 0).equals Ray::Color.blue + asserts(:[], 1, 1).equals Ray::Color.white + + teardown do + File.delete path if File.exist? path + end + end end run_tests if __FILE__ == $0