0

It asks to implement the class Tester, which receives a class and and runs all its methods that start with the word test.

class AssertionFailed < Exception
end
class TestCase
  def setUp
  end
  def tierDown
  end
  def assertTrue(expresion)
    if not expresion then
      raise AssertionFailed.new(expresion.to_s + " is not true")
    end
  end
  def assertEquals(result, expected)
    if result != expected
      raise AssertionFailed.new(result.to_s + " is not equal to " + expected.to_s)
    end
  end
end
class IntegerTest < TestCase
  def setUp
    @number = 1
  end
  def testSum
    assertTrue(1 + @number == 2)
    @number += 1
  end
  def testSub
    assertTrue(2 - @number == @number)
  end
  def testMulByZero
    assertEquals(@number*0, 1)
  end
  def testAddByZero
    assertEquals(@number + 0, @number + 1)
  end
end

Tester.test(IntegerTest)

Example:

Tester.test(IntegerTest)
[*] testMulByZero failed: 0 is not equals to 1
[*] testAddByZero failed: 1 is not equals to 2

Help: The grep method of the Iterable module receives a regular expression, and returns all The elements that match that expression. For the exercise, use grep (\ test * ) on The collection of methods to obtain the methods sought.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • JFYI, method names like `testAddByZero` don't conform to accepted naming conventions. Idiomatic way to name that would be `test_add_by_zero`. In general, `camelCase` is just not used in ruby. – Sergio Tulentsev Jun 11 '17 at 14:22

1 Answers1

0

I finally got an answer by this source and this one What I have done is starting the test that is given, then i create an array by asking to de class testig his instace which start with test, finaly it's an iteration on the array asking to execute eachone of the methods and if they fail the assertion then show them

class Tester

def self.test(testing)
    tested=testing.new
    tested.setUp
    method_arr=testing.instance_methods.grep(/test*/)
    method_arr.each do |x|
        begin
        tested.send(:"#{x}")
        rescue AssertionFailed =>assert_fail
            puts "[*]" + "#{assert_fail}"
        end
    end
end 

end