samedi 10 juin 2017

How to make Ruby Tester?

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.

Aucun commentaire:

Enregistrer un commentaire