Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for StringHelper #1603

Merged
merged 30 commits into from
May 18, 2021
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fe38a90
Merge remote-tracking branch 'upstream/master'
jawira Feb 11, 2019
974a2f1
Merge remote-tracking branch 'upstream/master'
jawira Jul 1, 2019
c3b1e48
Merge remote-tracking branch 'upstream/master'
jawira Jul 12, 2019
32e4914
Merge remote-tracking branch 'upstream/master'
jawira Jul 26, 2019
275cc96
Merge remote-tracking branch 'upstream/master'
jawira Oct 23, 2019
4de09d3
ComposerTask: update code & doc #1163
jawira Oct 23, 2019
64aefbf
Merge branch 'master' of github.com:jawira/fork-phing
jawira Nov 19, 2019
d769c90
Merge remote-tracking branch 'upstream/master'
jawira Nov 19, 2019
9d8da67
Merge remote-tracking branch 'upstream/master'
jawira Aug 8, 2020
2549e00
Merge remote-tracking branch 'upstream/master'
jawira Sep 5, 2020
b57b6da
Merge remote-tracking branch 'upstream/master'
jawira Sep 29, 2020
ec4ab93
Merge remote-tracking branch 'upstream/master'
jawira Oct 17, 2020
8f5343c
Merge remote-tracking branch 'upstream/master'
jawira Oct 29, 2020
d098a0f
Merge remote-tracking branch 'upstream/master'
jawira Dec 14, 2020
c7bcabf
Merge remote-tracking branch 'upstream/master'
jawira Dec 19, 2020
919e771
Merge remote-tracking branch 'upstream/main' into main
jawira Feb 2, 2021
424a754
Merge remote-tracking branch 'upstream/main'
jawira Mar 9, 2021
f79dc57
Merge remote-tracking branch 'upstream/main'
jawira Mar 16, 2021
12c46e9
Merge remote-tracking branch 'upstream/main' into main
jawira Apr 19, 2021
605d358
Merge remote-tracking branch 'upstream/main' into main
jawira Apr 26, 2021
342c9ac
Merge remote-tracking branch 'upstream/main' into main
jawira May 4, 2021
8451ae1
Merge remote-tracking branch 'upstream/main' into main
jawira May 16, 2021
85dc263
Add tests for startsWith
jawira May 16, 2021
d7f2886
Add tests for endsWith
jawira May 16, 2021
7b2e196
Add tests for substring
jawira May 16, 2021
c6a05d4
Add simple test for substring
jawira May 16, 2021
15fac50
Add edge cases for substring
jawira May 16, 2021
d160094
Add tests for isSlotVar
jawira May 16, 2021
f8e5622
Add tests for SlotVar
jawira May 16, 2021
d1e176e
Add tests for SlotVar
jawira May 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
];
}
}