<?php

/*
 * Copyright (c) 2015, Leif Lindbäck
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Id1354fw\Util;

/**
 * @author Leif Lindback, leifl@kth.se
 */
class StringsTest extends \PHPUnit_Framework_TestCase {

    /**
     * @covers Id1354fw\Util\Strings::removeTrailingString
     */
    public function testRemoveTrailingString() {
        $origString = 'abcdefgh';
        $this->assertEquals($origString, Strings::removeTrailingString($origString, 'defg'));
        $this->assertEquals($origString, Strings::removeTrailingString($origString, 'g'));
        $this->assertEquals($origString, Strings::removeTrailingString($origString, ''));
        $this->assertEquals($origString, Strings::removeTrailingString($origString, NULL));
        $this->assertEquals('abcd', Strings::removeTrailingString($origString, 'efgh'));
        $this->assertEquals('abcdefg', Strings::removeTrailingString($origString, 'h'));
        $this->assertEquals('a', Strings::removeTrailingString($origString, 'bcdefgh'));
        $this->assertEquals('', Strings::removeTrailingString($origString, 'abcdefgh'));
    }

    /**
     * @covers Id1354fw\Util\Strings::removeLeadingString
     */
    public function testRemoveLeadingString() {
        $origString = 'abcdefgh';
        $this->assertEquals($origString, Strings::removeLeadingString($origString, 'bcde'));
        $this->assertEquals($origString, Strings::removeLeadingString($origString, 'b'));
        $this->assertEquals($origString, Strings::removeLeadingString($origString, ''));
        $this->assertEquals($origString, Strings::removeLeadingString($origString, NULL));
        $this->assertEquals('efgh', Strings::removeLeadingString($origString, 'abcd'));
        $this->assertEquals('h', Strings::removeLeadingString($origString, 'abcdefg'));
        $this->assertEquals('bcdefgh', Strings::removeLeadingString($origString, 'a'));
        $this->assertEquals('', Strings::removeLeadingString($origString, 'abcdefgh'));
    }

    /**
     * @covers Id1354fw\Util\Strings::countOccurences
     */
    public function testCountOccurences() {
        $this->assertEquals(3, Strings::countOccurrences('ddd', 'd'));
        $this->assertEquals(3, Strings::countOccurrences('adadada', 'd'));
        $this->assertEquals(3, Strings::countOccurrences('jhhgjghjddd', 'd'));
        $this->assertEquals(3, Strings::countOccurrences('dddjhgjghjghj', 'd'));
        $this->assertEquals(3, Strings::countOccurrences('ddjghjgjghjgd', 'd'));
        $this->assertEquals(0, Strings::countOccurrences('ddd', 's'));
        $this->assertEquals(0, Strings::countOccurrences('', 'd'));
        $this->assertEquals(0, Strings::countOccurrences('ddd', ''));
        $this->assertEquals(0, Strings::countOccurrences(NULL, 'd'));
        $this->assertEquals(0, Strings::countOccurrences('ddd', NULL));
    }

    /**
     * @covers Id1354fw\Util\Strings::startsWith
     */
    public function testStartsWith() {
        $this->assertTrue(Strings::startsWith('abcd', 'a'));
        $this->assertTrue(Strings::startsWith('abcd', 'abc'));
        $this->assertTrue(Strings::startsWith('a', 'a'));
        $this->assertFalse(Strings::startsWith('abc', 'b'));
        $this->assertFalse(Strings::startsWith('abc', 'bc'));
        $this->assertFalse(Strings::startsWith('', 'a'));
        $this->assertFalse(Strings::startsWith('a', ''));
        $this->assertFalse(Strings::startsWith(NULL, 'a'));
        $this->assertFalse(Strings::startsWith('a', NULL));
    }

    /**
     * @covers Id1354fw\Util\Strings::endsWith
     */
    public function testEndsWith() {
        $this->assertTrue(Strings::endsWith('abcd', 'd'));
        $this->assertTrue(Strings::endsWith('abcd', 'bcd'));
        $this->assertTrue(Strings::endsWith('a', 'a'));
        $this->assertFalse(Strings::endsWith('abc', 'b'));
        $this->assertFalse(Strings::endsWith('abc', 'ab'));
        $this->assertFalse(Strings::endsWith('', 'a'));
        $this->assertFalse(Strings::endsWith('a', ''));
        $this->assertFalse(Strings::endsWith(NULL, 'a'));
        $this->assertFalse(Strings::endsWith('a', NULL));
    }

}
