Login   Register  
PHP Classes
elePHPant
Icontem

File: tests/Dec2RomanNumConverterTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nikola Posa  >  Dec2RomanNumConverter  >  tests/Dec2RomanNumConverterTest.php  >  Download  
File: tests/Dec2RomanNumConverterTest.php
Role: Unit test script
Content type: text/plain
Description: Dec2RomanNumConverter tests.
Class: Dec2RomanNumConverter
Convert numbers between decimal and roman
Author: By
Last change:
Date: 3 years ago
Size: 3,170 bytes
 

Contents

Class file image Download
<?php
/**
 * Dec2RomanNumConverter
 *
 * Copyright (C) 2009 Nikola Posa (http://www.nikolaposa.in.rs)
 *
 * This file is part of Dec2RomanNumConverter.
 *
 * Dec2RomanNumConverter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dec2RomanNumConverter is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dec2RomanNumConverter. If not, see <http://www.gnu.org/licenses/>.
 */

require_once dirname(__FILE__) . '/TestHelper.php';
require_once 
'Dec2RomanNumConverter.php';

/**
 * Dec2RomanNumConverter tests.
 *
 * @author Nikola Posa <posa.nikola@gmail.com>
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License
 */
class Dec2RomanNumConverterTest extends PHPUnit_Framework_TestCase
{
    public function 
testDec2RomanMethodExceptionShouldBeThrownInCaseNumberLargerThan3999IsPassed()
    {
        try {
            
Dec2RomanNumConverter::dec2roman(7000);

            
$this->fail();
        }
        catch (
Exception $e) {
            
$this->assertType('Dec2RomanNumConverter_Exception'$e);
            
$this->assertRegexp('/larger than 3999/i'$e->getMessage());
        }
    }

    public function 
testDec2RomanMethod()
    {
        
$valuesExpected = array(
            
10 => 'X',
            
23 => 'XXIII',
            
277  => 'CCLXXVII',
            
1987  => 'MCMLXXXVII',
            
2010  => 'MMX',
            
3024  => 'MMMXXIV',
            
1573  => 'MDLXXIII',
            
1234 => 'MCCXXXIV',
            
'732'  => 'DCCXXXII',
            
false  => '',
            
0  => '',
            
true  => 'I',
            
'37foo'  => 'XXXVII'
        
);

        foreach (
$valuesExpected as $input => $output) {
            
$this->assertEquals($outputDec2RomanNumConverter::dec2roman($input));
        }
    }

    public function 
testRoman2DecMethodShouldThrowExceptionForInvalidNumeral()
    {
        try {
            
Dec2RomanNumConverter::roman2dec('XIfoo');

            
$this->fail();
        }
        catch (
Exception $e) {
            
$this->assertType('Dec2RomanNumConverter_Exception'$e);
            
$this->assertRegexp('/is not valid or it is to large/i'$e->getMessage());
        }
    }

    public function 
testRoman2DecMethod()
    {
        
$valuesExpected = array(
            
'MCMLXXXVII' => 1987,
            
'MMX' => 2010,
            
'mmmxxiv' => 3024,
            
'XXIII' => 23,
            
'CCLXXVII' => 277,
            
'MDLXXIII'  => 1573,
            
'IIX' => 8//invalid markup, but it should be handled correctly
            
'DCCXXIX' => 729,
            
'dcccxc' => 890
        
);

        foreach (
$valuesExpected as $input => $output) {
            
$this->assertEquals($outputDec2RomanNumConverter::roman2dec($input));
        }
    }
}