Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a0572e3

Browse filesBrowse files
fabpottidal
authored andcommitted
[CssSelector] refactored some tests to use @dataProvider
1 parent af27f93 commit a0572e3
Copy full SHA for a0572e3

File tree

Expand file treeCollapse file tree

2 files changed

+59
-40
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+59
-40
lines changed

‎tests/Symfony/Tests/Components/CssSelector/ParserTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Components/CssSelector/ParserTest.php
+28-20Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,19 @@ public function testCssToXpath()
2424
$this->assertEquals('descendant-or-self::foo:h1', Parser::cssToXpath('foo|h1'));
2525
}
2626

27-
public function testParse()
27+
/**
28+
* @dataProvider getCssSelectors
29+
*/
30+
public function testParse($css, $xpath)
2831
{
2932
$parser = new Parser();
3033

31-
$tests = array(
32-
'h1' => "h1",
33-
'foo|h1' => "foo:h1",
34-
'h1, h2, h3' => "h1 | h2 | h3",
35-
'h1:nth-child(3n+1)' => "*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]",
36-
'h1 > p' => "h1/p",
37-
'h1#foo' => "h1[@id = 'foo']",
38-
'h1.foo' => "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
39-
'h1[class*="foo bar"]' => "h1[contains(@class, 'foo bar')]",
40-
'h1[foo|class*="foo bar"]' => "h1[contains(@foo:class, 'foo bar')]",
41-
'h1[class]' => "h1[@class]",
42-
'h1 .foo' => "h1/descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
43-
'h1 #foo' => "h1/descendant::*[@id = 'foo']",
44-
'h1 [class*=foo]' => "h1/descendant::*[contains(@class, 'foo')]",
45-
);
34+
$this->assertEquals($xpath, (string) $parser->parse($css)->toXpath(), '->parse() parses an input string and returns a node');
35+
}
4636

47-
foreach ($tests as $selector => $xpath)
48-
{
49-
$this->assertEquals($xpath, (string) $parser->parse($selector)->toXpath(), '->parse() parses an input string and returns a node');
50-
}
37+
public function testParseExceptions()
38+
{
39+
$parser = new Parser();
5140

5241
try
5342
{
@@ -59,4 +48,23 @@ public function testParse()
5948
$this->assertEquals("Expected symbol, got '' at h1: -> ", $e->getMessage(), '->parse() throws an Exception if the css selector is not valid');
6049
}
6150
}
51+
52+
public function getCssSelectors()
53+
{
54+
return array(
55+
array('h1', "h1"),
56+
array('foo|h1', "foo:h1"),
57+
array('h1, h2, h3', "h1 | h2 | h3"),
58+
array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]"),
59+
array('h1 > p', "h1/p"),
60+
array('h1#foo', "h1[@id = 'foo']"),
61+
array('h1.foo', "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
62+
array('h1[class*="foo bar"]', "h1[contains(@class, 'foo bar')]"),
63+
array('h1[foo|class*="foo bar"]', "h1[contains(@foo:class, 'foo bar')]"),
64+
array('h1[class]', "h1[@class]"),
65+
array('h1 .foo', "h1/descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
66+
array('h1 #foo', "h1/descendant::*[@id = 'foo']"),
67+
array('h1 [class*=foo]', "h1/descendant::*[contains(@class, 'foo')]"),
68+
);
69+
}
6270
}

‎tests/Symfony/Tests/Components/CssSelector/TokenizerTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Components/CssSelector/TokenizerTest.php
+31-20Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,40 @@
1515

1616
class TokenizerTest extends \PHPUnit_Framework_TestCase
1717
{
18-
public function testTokenize()
18+
protected $tokenizer;
19+
20+
public function setUp()
1921
{
20-
$tokenizer = new Tokenizer();
21-
22-
$tests = array(
23-
'h1',
24-
'h1:nth-child(3n+1)',
25-
'h1 > p',
26-
'h1#foo',
27-
'h1.foo',
28-
'h1[class*=foo]',
29-
'h1 .foo',
30-
'h1 #foo',
31-
'h1 [class*=foo]',
32-
);
22+
$this->tokenizer = new Tokenizer();
23+
}
3324

34-
foreach ($tests as $test)
35-
{
36-
$this->assertEquals($test, $this->tokensToString($tokenizer->tokenize($test)), '->tokenize() lexes an input string and returns an array of tokens');
37-
}
25+
/**
26+
* @dataProvider getCssSelectors
27+
*/
28+
public function testTokenize($css)
29+
{
30+
$this->assertEquals($css, $this->tokensToString($this->tokenizer->tokenize($css)), '->tokenize() lexes an input string and returns an array of tokens');
31+
}
32+
33+
public function testTokenizeWithQuotedStrings()
34+
{
35+
$this->assertEquals('foo[class=foo bar ]', $this->tokensToString($this->tokenizer->tokenize('foo[class="foo bar"]')), '->tokenize() lexes an input string and returns an array of tokens');
36+
$this->assertEquals("foo[class=foo Abar ]", $this->tokensToString($this->tokenizer->tokenize('foo[class="foo \\65 bar"]')), '->tokenize() lexes an input string and returns an array of tokens');
37+
}
3838

39-
$this->assertEquals('foo[class=foo bar ]', $this->tokensToString($tokenizer->tokenize('foo[class="foo bar"]')), '->tokenize() lexes an input string and returns an array of tokens');
40-
$this->assertEquals("foo[class=foo Abar ]", $this->tokensToString($tokenizer->tokenize('foo[class="foo \\65 bar"]')), '->tokenize() lexes an input string and returns an array of tokens');
39+
public function getCssSelectors()
40+
{
41+
return array(
42+
array('h1'),
43+
array('h1:nth-child(3n+1)'),
44+
array('h1 > p'),
45+
array('h1#foo'),
46+
array('h1.foo'),
47+
array('h1[class*=foo]'),
48+
array('h1 .foo'),
49+
array('h1 #foo'),
50+
array('h1 [class*=foo]'),
51+
);
4152
}
4253

4354
protected function tokensToString($tokens)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.