Skip to content

Commit b6b1dd8

Browse files
authored
GitHub testing (#31)
remove Travis, add Github actions
1 parent 5841b11 commit b6b1dd8

6 files changed

Lines changed: 134 additions & 53 deletions

File tree

.github/workflows/test.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
name: ${{ matrix.name }}
6+
runs-on: ${{ matrix.os }}
7+
strategy:
8+
fail-fast: false
9+
matrix:
10+
include:
11+
- name: php-5.6
12+
os: ubuntu-18.04
13+
php-version: '5.6'
14+
- name: php-7.0
15+
os: ubuntu-18.04
16+
php-version: '7.0'
17+
- name: php-7.1
18+
os: ubuntu-18.04
19+
php-version: '7.1'
20+
- name: ubuntu-18
21+
os: ubuntu-18.04
22+
php-version: '7.2'
23+
- name: ubuntu-20
24+
os: ubuntu-20.04
25+
php-version: '7.4'
26+
- name: ubuntu-latest
27+
os: ubuntu-latest
28+
steps:
29+
- if: ${{ matrix.php-version }}
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
php-version: ${{ matrix.php-version }}
33+
34+
- uses: actions/checkout@v2
35+
36+
- name: Validate composer.json and composer.lock
37+
run: composer validate --strict
38+
39+
- name: Install dependencies
40+
run: composer install --prefer-dist --no-progress
41+
42+
- name: phpcs
43+
run: ./vendor/bin/phpcs -n --standard=PSR2 src/ tests/
44+
45+
- name: PHP version
46+
run: php -v
47+
48+
- name: tests
49+
run: php ./tests/HJSONParserTest.php

.travis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# hjson-php
22

3-
[![Build Status](https://img.shields.io/travis/hjson/hjson-php.svg?style=flat-square)](http://travis-ci.org/hjson/hjson-php)
3+
[![Build Status](https://github.com/hjson/hjson-php/workflows/test/badge.svg)](https://github.com/hjson/hjson-php/actions)
44
[![Packagist](https://img.shields.io/packagist/v/laktak/hjson.svg?style=flat-square)](https://packagist.org/packages/laktak/hjson)
55

66
[Hjson](https://hjson.github.io), the Human JSON. A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments.

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"ext-mbstring": "*"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^5.7 || ^6.5",
2928
"squizlabs/php_codesniffer": "^3"
3029
},
3130
"autoload": {

phpunit.xml

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/HJSONParserTest.php

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,65 @@
22

33
namespace HJSON\Tests;
44

5+
require_once('src/HJSON/HJSONParser.php');
6+
require_once('src/HJSON/HJSONStringifier.php');
7+
require_once('src/HJSON/HJSONException.php');
8+
require_once('src/HJSON/HJSONUtils.php');
9+
510
use HJSON\HJSONParser;
611
use HJSON\HJSONStringifier;
712
use HJSON\HJSONException;
8-
use PHPUnit\Framework\TestCase;
913

10-
class HJSONParserTest extends TestCase
14+
class HJSONParserTest
1115
{
1216

13-
protected function setUp()
17+
public function assertEquals($a, $b)
18+
{
19+
if ($a !== $b) {
20+
echo "\n\n";
21+
$a2 = preg_split('/\r\n|\r|\n/', $a);
22+
$b2 = preg_split('/\r\n|\r|\n/', $b);
23+
$indexA = $indexB = 0;
24+
for (; $indexB < count($b2); $indexA++, $indexB++) {
25+
if ($indexB >= count($a2) || $b2[$indexB] !== $a2[$indexB]) {
26+
$indexA = $indexB;
27+
echo "Expected ($this->lastFilename, line $indexB):\n\n";
28+
while ($indexB < count($b2) && (
29+
$indexB >= count($a2) ||
30+
$b2[$indexB] !== $a2[$indexB])) {
31+
echo "|".$b2[$indexB++]."|\n";
32+
}
33+
34+
echo "\n\nGot:\n\n";
35+
while ($indexA < count($a2) && $indexA < $indexB) {
36+
echo "|".$a2[$indexA++]."|\n";
37+
}
38+
39+
echo "\n\n";
40+
}
41+
}
42+
43+
if ($indexA < count($a2)) {
44+
echo "\n\nGot trailing lines vs $this->lastFilename:\n\n";
45+
while ($indexA < count($a2)) {
46+
echo "|".$a2[$indexA++]."|\n";
47+
}
48+
echo "\n\n";
49+
}
50+
51+
throw new HJSONException();
52+
}
53+
}
54+
55+
public function setUp()
1456
{
15-
parent::setUp();
1657
$this->rootDir = dirname(__FILE__).DIRECTORY_SEPARATOR."assets";
58+
$this->lastFilename = '';
1759
}
1860

1961
private function load($file, $cr)
2062
{
63+
$this->lastFilename = $file;
2164
$text = file_get_contents($this->rootDir.DIRECTORY_SEPARATOR.$file);
2265
$std = mb_ereg_replace('/\r/', "", $text); // make sure we have unix style text regardless of the input
2366
return $cr ? mb_ereg_replace("\n", "\r\n", $std) : $std;
@@ -37,6 +80,13 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
3780
$this->assertEquals($arrayData, json_decode(json_encode($data), true));
3881

3982
if (!$shouldFail) {
83+
if ($isJson) {
84+
// compare Hjson parse to JSON parse
85+
$json1 = json_encode($data);
86+
$json2 = json_encode(json_decode($text));
87+
$this->assertEquals($json1, $json2);
88+
}
89+
4090
$text1 = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
4191
$stringifier = new HJSONStringifier();
4292
$hjson1 = $stringifier->stringify($data, [
@@ -46,15 +96,9 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
4696
]);
4797
$result = json_decode($this->load("{$name}_result.json", $inputCr));
4898
$text2 = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
49-
$hjson2 = $this->load("{$name}_result.hjson", $outputCr);
5099
$this->assertEquals($text1, $text2);
100+
$hjson2 = $this->load("{$name}_result.hjson", $outputCr);
51101
$this->assertEquals($hjson1, $hjson2);
52-
if ($isJson) {
53-
// also compare Hjson parse to JSON parse
54-
$json1 = json_encode($data);
55-
$json2 = json_encode(json_decode($text));
56-
$this->assertEquals($json1, $json2);
57-
}
58102
} else {
59103
$this->markTestIncomplete('This test succeeded on data that should fail.');
60104
}
@@ -67,6 +111,7 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
67111

68112
public function testAll()
69113
{
114+
$hasFailure = false;
70115
$files = array_diff(scandir($this->rootDir), ['..', '.']);
71116
foreach ($files as $file) {
72117
$name = explode('_test.', $file);
@@ -81,10 +126,34 @@ public function testAll()
81126
continue;
82127
}
83128

84-
$this->runEach($name, $file, $isJson, false, false);
85-
$this->runEach($name, $file, $isJson, false, true);
86-
$this->runEach($name, $file, $isJson, true, false);
87-
$this->runEach($name, $file, $isJson, true, true);
129+
try {
130+
$this->runEach($name, $file, $isJson, false, false);
131+
} catch (HJSONException $e) {
132+
$hasFailure = true;
133+
}
134+
try {
135+
$this->runEach($name, $file, $isJson, false, true);
136+
} catch (HJSONException $e) {
137+
$hasFailure = true;
138+
}
139+
try {
140+
$this->runEach($name, $file, $isJson, true, false);
141+
} catch (HJSONException $e) {
142+
$hasFailure = true;
143+
}
144+
try {
145+
$this->runEach($name, $file, $isJson, true, true);
146+
} catch (HJSONException $e) {
147+
$hasFailure = true;
148+
}
149+
}
150+
151+
if ($hasFailure) {
152+
exit(1);
88153
}
89154
}
90155
}
156+
157+
$tester = new HJSONParserTest;
158+
$tester->setUp();
159+
$tester->testAll();

0 commit comments

Comments
 (0)