Skip to content

Commit

Permalink
Add tests for StringHelper (#1603)
Browse files Browse the repository at this point in the history
  • Loading branch information
jawira authored May 18, 2021
1 parent 16ae1f0 commit 173977c
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions tests/Phing/Util/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,178 @@ public function isBooleanProvider()
[null, false],
];
}

/**
* @dataProvider startsWithProvider
* @covers \Phing\Util\StringHelper::startsWith
*/
public function testStartsWith($needle, $haystack, $expected)
{
$result = StringHelper::startsWith($needle, $haystack);
$this->assertSame($expected, $result);
}

public function startsWithProvider()
{
return [
// True
['F', 'FooBarBaz', true],
['Foo', 'FooBarBaz', true],
['FooBarBaz', 'FooBarBaz', true],
['', 'FooBarBaz', true],
['', "\x00", true],
["\x00", "\x00", true],
["\x00", "\x00a", true],
["a\x00b", "a\x00bc", true],
// False
['Foo', 'BarBaz', false],
['foo', 'FooBarBaz', false],
['Foo', 'fooBarBaz', false],
['Foo', '', false],
];
}

/**
* @dataProvider endsWithProvider
* @covers \Phing\Util\StringHelper::endsWith
*/
public function testEndsWith($needle = 'o', $haystack = 'foo', $expected = true)
{
$result = StringHelper::endsWith($needle, $haystack);
$this->assertSame($expected, $result);
}


public function endsWithProvider()
{
return [
// True
['z', 'FooBarBaz', true],
['Baz', 'FooBarBaz', true],
['FooBarBaz', 'FooBarBaz', true],
['', 'FooBarBaz', true],
['', "\x00", true],
["\x00", "\x00", true],
["\x00", "a\x00", true],
["b\x00c", "ab\x00c", true],
// False
['Baz', 'FooBar', false],
['baz', 'FooBarBaz', false],
['Baz', 'foobarbaz', false],
['Baz', '', false],
];
}

/**
* @covers \Phing\Util\StringHelper::substring
*/
public function testSubstringSimple()
{
$result = StringHelper::substring('FooBarBaz', 3);
$this->assertSame('BarBaz', $result);
}

/**
* @covers \Phing\Util\StringHelper::substring
* @dataProvider substringProvider
*/
public function testSubstring($string, $start, $end, $expected)
{
$result = StringHelper::substring($string, $start, $end);
$this->assertSame($expected, $result);
}

public function substringProvider()
{
return [
['FooBarBaz', 0, 0, 'F'],
['FooBarBaz', 0, 1, 'Fo'],
['FooBarBaz', 2, 4, 'oBa'],
['FooBarBaz', 0, 0, 'F'],
['FooBarBaz', 3, 3, 'B'],
['FooBarBaz', 0, 8, 'FooBarBaz'],
['FooBarBaz', 0, -1, 'FooBarBaz'],
['FooBarBaz', 5, 8, 'rBaz'],
['FooBarBaz', 5, -1, 'rBaz'],
['FooBarBaz', 8, 8, 'z'],
];
}

/**
* @covers \Phing\Util\StringHelper::substring
* @dataProvider substringErrorProvider
*/
public function testSubstringError($string, $start, $end, $message)
{
$this->expectError();
$this->expectErrorMessage($message);
StringHelper::substring($string, $start, $end);
}

public function substringErrorProvider()
{
return [
['FooBarBaz', -1, 1, 'substring(), Startindex out of bounds must be 0<n<9'],
['FooBarBaz', -10, 100, 'substring(), Startindex out of bounds must be 0<n<9'],
['FooBarBaz', 100, 1, 'substring(), Startindex out of bounds must be 0<n<9'],
['FooBarBaz', 0, 100, 'substring(), Endindex out of bounds must be 0<n<8'],
['FooBarBaz', 3, 1, 'substring(), Endindex out of bounds must be 3<n<8'],
];
}

/**
* @covers \Phing\Util\StringHelper::isSlotVar
* @dataProvider isSlotVarProvider
*/
public function testIsSlotVar($value, $expected)
{
$result = StringHelper::isSlotVar($value);
$this->assertSame($expected, $result);
}

public function isSlotVarProvider()
{
return [
// 1
['%{x}', 1],
['%{dummy}', 1],
['%{my.var}', 1],
['%{Foo.Bar.Baz}', 1],
['%{user.first-name}', 1],
['%{user.first_name}', 1],
[' %{slot.var} ', 1],
// 0
['slot.var', 0],
['%{&é@}', 0],
['%{slot§var}', 0],
['%{}', 0],
['%{slotèvar}', 0],
['%{slot%var}', 0],
['%{ slot.var }', 0],
['}%slot.var{', 0],
];
}

/**
* @covers \Phing\Util\StringHelper::slotVar
* @dataProvider slotVarProvider
*/
public function testSlotVar($var, $expected)
{
$result = StringHelper::slotVar($var);
$this->assertSame($expected, $result);
}

public function slotVarProvider()
{
return [
['%{slot.var}', 'slot.var'],
['%{&é@}', '&é@'],
['', ''],
['%{}', ''],
['%{ }', ''],
[' %{ slot.var } ', 'slot.var'],
['FooBarBaz', 'FooBarBaz'],
];
}
}

0 comments on commit 173977c

Please sign in to comment.