diff --git a/ext/opencv/rect.cpp b/ext/opencv/rect.cpp index 9cc1686..e07abb9 100644 --- a/ext/opencv/rect.cpp +++ b/ext/opencv/rect.cpp @@ -47,12 +47,14 @@ namespace rubyopencv { /* * Create a rectangle * - * @overload new(x = 0, y = 0, width = 0, height = 0) - * @param x [Integer] x coordinate - * @param y [Integer] y coordinate - * @param width [Integer] Width - * @param height [Integer] Height - * @return [Rect] new rectangle + * @overload new() + * @return [Rect] new rectangle + * @overload new(x, y, width, height) + * @param x [Integer] x coordinate + * @param y [Integer] y coordinate + * @param width [Integer] Width + * @param height [Integer] Height + * @return [Rect] new rectangle */ VALUE rb_initialize(int argc, VALUE *argv, VALUE self) { const int SIZE = 4; @@ -73,6 +75,9 @@ namespace rubyopencv { selfptr->width = NUM2INT(values[2]); selfptr->height = NUM2INT(values[3]); break; + default: + rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 4)", argc); + break; } return self; diff --git a/test/test_rect.rb b/test/test_rect.rb new file mode 100755 index 0000000..f04e17b --- /dev/null +++ b/test/test_rect.rb @@ -0,0 +1,86 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8 -*- +require 'test/unit' +require 'opencv' +require File.expand_path(File.dirname(__FILE__)) + '/helper' + +include OpenCV + +# Tests for OpenCV::Rect +class TestRect < OpenCVTestCase + def test_x + rect = Rect.new + rect.x = 100 + assert_equal(100, rect.x) + rect.x = 200 + assert_equal(200, rect.x) + end + + def test_y + rect = Rect.new + rect.y = 100 + assert_equal(100, rect.y) + rect.y = 200 + assert_equal(200, rect.y) + end + + def test_width + rect = Rect.new + rect.width = 100 + assert_equal(100, rect.width) + rect.width = 200 + assert_equal(200, rect.width) + end + + def test_height + rect = Rect.new + rect.height = 100 + assert_equal(100, rect.height) + rect.height = 200 + assert_equal(200, rect.height) + end + + def test_initialize + rect = Rect.new + assert_equal(0, rect.x) + assert_equal(0, rect.y) + assert_equal(0, rect.width) + assert_equal(0, rect.height) + + rect = Rect.new(10, 20, 30, 40) + assert_equal(10, rect.x) + assert_equal(20, rect.y) + assert_equal(30, rect.width) + assert_equal(40, rect.height) + + assert_raise(TypeError) { + Rect.new(DUMMY_OBJ, 1, 1, 1) + } + assert_raise(TypeError) { + Rect.new(1, DUMMY_OBJ, 1, 1) + } + assert_raise(TypeError) { + Rect.new(1, 1, DUMMY_OBJ, 1) + } + assert_raise(TypeError) { + Rect.new(1, 1, 1, DUMMY_OBJ) + } + assert_raise(ArgumentError) { + Rect.new(1) + } + assert_raise(ArgumentError) { + Rect.new(1, 2) + } + assert_raise(ArgumentError) { + Rect.new(1, 2, 3) + } + assert_raise(ArgumentError) { + Rect.new(1, 2, 3, 4, 5) + } + end + + def test_to_s + rect = Rect.new(1, 2, 3, 4) + assert_equal('#', rect.to_s) + end +end