Skip to content

Commit

Permalink
Implement indentation test and expectation in a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardroche committed Feb 9, 2015
1 parent ba0165c commit 62109d3
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 277 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
- Append `;` on <kbd>Ctrl</kbd>+<kbd>Enter</kbd> in valid contexts
* Minimise auto-complete noise: Completion rules now cancel completions for method declarations and class declarations with modifiers. e.g. typing at `public function |` and `abstract class |`.
* Tests
- Added a language test suite. See [CONTRIBUTING.md](https://github.com/gerardroche/sublime-php-grammar/blob/master/CONTRIBUTING.md) for more details.
- Added a language test suite
- Added a indentation test and expectation in a single file test case

See [CONTRIBUTING.md](https://github.com/gerardroche/sublime-php-grammar/blob/master/CONTRIBUTING.md) for more details.

### Changes

Expand Down
71 changes: 56 additions & 15 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,75 @@ _Note_: Test runner key bindings are disabled by default. To enable them set `"p

## Indentation Tests

Two files are required for an indentation test.
There are two options for writing indentation tests.

1. `test/indentation/name_test.php`
2. `test/indentation/name_test_expect.php`
### Example 1 - Test and expectation in a single file

##### `test/indentation/if_else_test.php`

```
--TEST--
Basic if statement
--FILE--
<?php
echo 'statement 1';
if (true) {
echo 'statement 2';
echo 'statement 3';
if (false) {
echo 'statement 4';
echo 'statement 5';
} else {
echo 'statement 6';
echo 'statement 7';
}
echo 'statement 8';
echo 'statement 9';
}
echo 'statement 10';
?>
--EXPECT--
<?php
echo 'Statement 1';
if (true) {
echo 'Statement 2';
echo 'Statement 3';
if (false) {
echo 'Statement 4';
echo 'Statement 5';
} else {
echo 'Statement 6';
echo 'Statement 7';
}
echo 'Statement 8';
echo 'Statement 9';
}
echo 'Statement 10';
?>
```

The contents of `name_test.php` file is loaded, reindented, and compared to the contents of `name_test_expect.php`. If they match the test passes, if not the test fails.

### Example
### Example 2 - Test and expectation in separate files

##### `test/indentation/if_else_test.php`

```
<?php
echo 'Statement 1';
echo 'statement 1';
if (true) {
echo 'Statement 2';
echo 'Statement 3';
echo 'statement 2';
echo 'statement 3';
if (false) {
echo 'Statement 4';
echo 'Statement 5';
echo 'statement 4';
echo 'statement 5';
} else {
echo 'Statement 6';
echo 'Statement 7';
echo 'statement 6';
echo 'statement 7';
}
echo 'Statement 8';
echo 'Statement 9';
echo 'statement 8';
echo 'statement 9';
}
echo 'Statement 10';
echo 'statement 10';
```

##### `test/indentation/if_else_expect.php`
Expand Down
18 changes: 15 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,23 @@ def getFileContents(self, name, ext):
with open(file_name) as f:
return f.read()

def createFileDataProiderTest(name):
def createFileDataProiderTest(test_file_name):
def indentationTest(self):
self.view.insert(self.getFileContents(name, config.indentation_test_file_extension))
test_content = self.getFileContents(test_file_name, config.indentation_test_file_extension)

if '--TEST--' in test_content:
res = re.split('\n?--(?:TEST|FILE|EXPECT)--\n', test_content)
if not len(res) == 4:
raise RuntimeError('Invalid indentation test: %s' % test_file_name)
test_content = res[2]
expected_content = res[3].rstrip("\n")
else:
expected_content = self.getFileContents(test_file_name, config.indentation_test_expect_file_extension)

self.view.insert(test_content)
self.view.reindent()
self.assertEqual(self.getFileContents(name, config.indentation_test_expect_file_extension), self.view.to_str())
self.assertEqual(expected_content, self.view.to_str())

return indentationTest

class TestLanguage(unittest.TestCase):
Expand Down
22 changes: 22 additions & 0 deletions test/indentation/comments_test.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--TEST--
Comments
--FILE--
<?php

// Single line comment content should preserve indentation i.e. everything
Expand All @@ -14,3 +17,22 @@
x
x
*/
?>
--EXPECT--
<?php

// Single line comment content should preserve indentation i.e. everything
// after the double-slash should be ignored by indentation rules.
// x
// x
// x

/*
Multi line comment content should preserve indentation i.e. everything
between the forward-slash-star stat-forward-slash should be ignored by
indentation rules.
x
x
x
*/
?>
16 changes: 0 additions & 16 deletions test/indentation/comments_test_expect.php

This file was deleted.

44 changes: 40 additions & 4 deletions test/indentation/declare_test.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
--TEST--
declare statment
--FILE--
<?php

function start_with_at_least_one_indent() {
function () {

declare(ticks = 0);

Expand Down Expand Up @@ -31,6 +34,39 @@ function start_with_at_least_one_indent() {

}

var_dump('begin...');
start_with_at_least_one_indent();
var_dump('done.');
?>
--EXPECT--
<?php

function () {

declare(ticks = 0);

declare(ticks = 0)
{
echo "Statement 1\n";
echo "Statement 2\n";
}

declare(ticks = 0) {
echo "Statement 1\n";
echo "Statement 2\n";
}

declare(ticks = 0) :
echo "Statement 1\n"; echo "Statement 2\n";
enddeclare;
var_dump('.');

?>
<?php var_dump('.'); ?>
<?php declare(ticks = 0) : ?>
<?php echo "Statement 1\n"; ?>
<?= "Statement 2\n"; ?>
<?php enddeclare; ?>
<?php var_dump('.'); ?>
<?php

}

?>
36 changes: 0 additions & 36 deletions test/indentation/declare_test_expect.php

This file was deleted.

42 changes: 38 additions & 4 deletions test/indentation/do_test.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
--TEST--
Basic do statement
--FILE--
<?php

function start_with_at_least_one_indent() {
function () {

$i = 1;
do {
Expand Down Expand Up @@ -29,6 +32,37 @@ function start_with_at_least_one_indent() {

}

var_dump('begin...');
start_with_at_least_one_indent();
var_dump('done.');
?>
--EXPECT--
<?php

function () {

$i = 1;
do {
echo "$i\t".($i * $i)."\n";
++$i;
} while ($i <= 4);
var_dump($i);

$i = 1;
do
{
echo "$i\t".($i * $i)."\n";
++$i;
}
while ($i <= 4);
var_dump($i);

$i = 1;
do { echo "$i\t".($i * $i)."\n"; ++$i; } while ($i <= 4);
var_dump($i);

$i = 1;
do { echo "$i\t".($i * $i)."\n"; ++$i; }
while ($i <= 4);
var_dump($i);

}

?>
34 changes: 0 additions & 34 deletions test/indentation/do_test_expect.php

This file was deleted.

25 changes: 25 additions & 0 deletions test/indentation/namespaces_1_test.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--TEST--
Basic namespace
--FILE--
<?php

namespace NS1;
Expand All @@ -15,3 +18,25 @@
use NS1\Sub1 as NS1;
echo "Statement 3 " . __NAMESPACE__ . "\n";
echo "Statement 4 " . __NAMESPACE__ . "\n";

?>
--EXPECT--
<?php

namespace NS1;
echo "Statement 1" . __NAMESPACE__ . "\n";
echo "Statement 2" . __NAMESPACE__ . "\n";

namespace NS1\Sub1;
use NS1;
echo "Statement 1" . __NAMESPACE__ . "\n";
echo "Statement 2" . __NAMESPACE__ . "\n";

namespace NS3\Sub2;
echo "Statement 1 " . __NAMESPACE__ . "\n";
echo "Statement 2 " . __NAMESPACE__ . "\n";
use NS1\Sub1 as NS1;
echo "Statement 3 " . __NAMESPACE__ . "\n";
echo "Statement 4 " . __NAMESPACE__ . "\n";

?>
Loading

0 comments on commit 62109d3

Please sign in to comment.