Skip to content

Commit

Permalink
coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 1, 2021
1 parent 5b955f8 commit 62ac4c4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 56 deletions.
4 changes: 1 addition & 3 deletions src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public function encode($var, int $flags = 0): string
throw new Exception('Invalid UTF-8 sequence: ' . $var);
}
if (strpos($var, "\n") !== false) {
$res = preg_replace_callback('#[^\\\\]|\\\\(.)#s', function ($m) {
return ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0];
}, $res);
$res = preg_replace_callback('#[^\\\\]|\\\\(.)#s', fn ($m) => ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0], $res);
$res = '"""' . "\n\t" . substr($res, 1, -1) . "\n" . '"""';
}
return $res;
Expand Down
6 changes: 3 additions & 3 deletions tests/Neon/Decoder.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Assert::same([
'b' => 2,
], Neon::decode(
' a: 1
b: 2'
b: 2',
));


Expand Down Expand Up @@ -87,7 +87,7 @@ Assert::same([
y:
-
a: x
'
',
));


Expand Down Expand Up @@ -230,7 +230,7 @@ Assert::equal(
Neon::decode('
- 2016-06-03
2016-06-03: b
')
'),
);


Expand Down
2 changes: 1 addition & 1 deletion tests/Neon/Decoder.deprecated.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require __DIR__ . '/../bootstrap.php';

Assert::same(
[true, true, false, false],
@Neon::decode('[On, on, Off, off]')
@Neon::decode('[On, on, Off, off]'),
);

Assert::error(function () {
Expand Down
14 changes: 7 additions & 7 deletions tests/Neon/Decoder.inline.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@ Assert::type(Nette\Neon\Entity::class, Neon::decode('@item(a, b)'));

Assert::equal(
new Entity('@item', ['a', 'b']),
Neon::decode('@item(a, b)')
Neon::decode('@item(a, b)'),
);


Assert::equal(
new Entity('@item<item>', ['a', 'b']),
Neon::decode('@item<item>(a, b)')
Neon::decode('@item<item>(a, b)'),
);


Assert::equal(
new Entity('item', ['a', 'b']),
Neon::decode('item (a, b)')
Neon::decode('item (a, b)'),
);


Assert::equal(
new Entity([], []),
Neon::decode('[]()')
Neon::decode('[]()'),
);


Expand All @@ -86,7 +86,7 @@ Assert::equal(
new Entity('first', ['a', 'b']),
new Entity('second'),
]),
Neon::decode('first(a, b)second')
Neon::decode('first(a, b)second'),
);


Expand All @@ -95,15 +95,15 @@ Assert::equal(
new Entity('first', ['a', 'b']),
new Entity('second', [1, 2]),
]),
Neon::decode('first(a, b)second(1, 2)')
Neon::decode('first(a, b)second(1, 2)'),
);

Assert::equal(
new Entity(Neon::CHAIN, [
new Entity(1, []),
new Entity(2, []),
]),
Neon::decode('1() 2()')
Neon::decode('1() 2()'),
);

// JSON compatibility
Expand Down
28 changes: 14 additions & 14 deletions tests/Neon/Decoder.multiline.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ Assert::same(
Neon::decode('"""
multi
lines
"""')
"""'),
);

Assert::same(
"multi\"\"\"\nlines'''",
Neon::decode('"""
multi"""
lines\'\'\'
"""')
"""'),
);

// single quoted
Expand All @@ -36,7 +36,7 @@ Assert::same(
Neon::decode("'''
multi\"\"\"
lines'''
'''")
'''"),
);

// ignore space before end
Expand All @@ -45,7 +45,7 @@ Assert::same(
Neon::decode("'''
multi
lines
\t '''")
\t '''"),
);

// require new line after start
Expand Down Expand Up @@ -73,7 +73,7 @@ Assert::same(
\tmulti
lines
\t\tstring
'''")
'''"),
);

Assert::same(
Expand All @@ -82,7 +82,7 @@ Assert::same(
\t multi
\t lines
\t\t string
\t\t'''")
\t\t'''"),
);

// first empty line
Expand All @@ -92,7 +92,7 @@ Assert::same(
\t multi
\t lines
\t\t'''")
\t\t'''"),
);

// last empty line
Expand All @@ -102,7 +102,7 @@ Assert::same(
\t multi
\t lines
\t\t'''")
\t\t'''"),
);

// escaping
Expand All @@ -111,20 +111,20 @@ Assert::same(
Neon::decode('"""
\t multi
\t lines
"""')
"""'),
);

//no content
Assert::same(
'',
Neon::decode('"""
"""')
"""'),
);

Assert::same(
'',
Neon::decode("'''
'''")
'''"),
);

Assert::exception(function () {
Expand All @@ -142,7 +142,7 @@ a: '''
multi
lines
'''
")
"),
);

Assert::same(
Expand All @@ -152,7 +152,7 @@ a: '''
multi
lines
'''
")
"),
);

Assert::same(
Expand All @@ -165,5 +165,5 @@ Assert::same(
multi
lines
'''
")
"),
);
8 changes: 4 additions & 4 deletions tests/Neon/Decoder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,17 @@ foreach (array_merge($dataSet['invalid syntax'], $dataSet['invalid encoding']) a
// datetime
Assert::equal(
new DateTimeImmutable('2016-06-03T19:00:00+02:00'),
Neon::decode('2016-06-03 19:00:00 +0200')
Neon::decode('2016-06-03 19:00:00 +0200'),
);
Assert::equal(
new DateTimeImmutable('2016-06-03T19:00:00+02:00'),
Neon::decode('2016-06-03 19:00:00 +02:00')
Neon::decode('2016-06-03 19:00:00 +02:00'),
);
Assert::equal(
new DateTimeImmutable('2016-06-03T19:00:00'),
Neon::decode('2016-06-03 19:00:00')
Neon::decode('2016-06-03 19:00:00'),
);
Assert::equal(
new DateTimeImmutable('2016-06-03T00:00:00'),
Neon::decode('2016-06-03')
Neon::decode('2016-06-03'),
);
48 changes: 24 additions & 24 deletions tests/Neon/Encoder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,123 +21,123 @@ Assert::same(
false, 'FALSE', 'fAlSe', 'false',
null, 'NULL', 'nUlL', 'null',
'yes', 'no', 'on', 'off',
])
]),
);

Assert::same(
'[1, 1.0, 0, 0.0, -1, -1.2, "1", "1.0", "-1"]',
Neon::encode([1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1'])
Neon::encode([1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1']),
);

Assert::same(
'["1", "0xAA", "0o12", "0b110", "+1", "-1", ".50", "1e10"]',
Neon::encode(['1', '0xAA', '0o12', '0b110', '+1', '-1', '.50', '1e10'])
Neon::encode(['1', '0xAA', '0o12', '0b110', '+1', '-1', '.50', '1e10']),
);

Assert::same(
'[\, "\'", "\"", "\r", "\\\\ \r"]',
Neon::encode(['\\', "'", '"', "\r", "\\ \r"])
Neon::encode(['\\', "'", '"', "\r", "\\ \r"]),
);

Assert::same(
"{multi: [\"\"\"\n\tone\n\ttwo\n\tthree\\\\ne \"'\t\n\"\"\"]}",
Neon::encode(['multi' => ["one\ntwo\nthree\\ne \"'\t"]])
Neon::encode(['multi' => ["one\ntwo\nthree\\ne \"'\t"]]),
);

Assert::same(
'["[", "]", "{", "}", ":", ": ", "=", "#"]',
Neon::encode(['[', ']', '{', '}', ':', ': ', '=', '#'])
Neon::encode(['[', ']', '{', '}', ':', ': ', '=', '#']),
);

Assert::same(
'[1, 2, 3]',
Neon::encode([1, 2, 3])
Neon::encode([1, 2, 3]),
);

Assert::same(
'{1: 1, 2: 2, 3: 3}',
Neon::encode([1 => 1, 2, 3])
Neon::encode([1 => 1, 2, 3]),
);

Assert::same(
'{foo: 1, bar: [2, 3]}',
Neon::encode(['foo' => 1, 'bar' => [2, 3]])
Neon::encode(['foo' => 1, 'bar' => [2, 3]]),
);

Assert::same(
'item(a, b)',
Neon::encode(Neon::decode('item(a, b)'))
Neon::encode(Neon::decode('item(a, b)')),
);

Assert::same(
'item<item>(a, b)',
Neon::encode(Neon::decode('item<item>(a, b)'))
Neon::encode(Neon::decode('item<item>(a, b)')),
);

Assert::same(
'item(foo: a, bar: b)',
Neon::encode(Neon::decode('item(foo: a, bar: b)'))
Neon::encode(Neon::decode('item(foo: a, bar: b)')),
);

Assert::same(
'[]()',
Neon::encode(Neon::decode('[]()'))
Neon::encode(Neon::decode('[]()')),
);

$entity = new Entity('ent');
$entity->attributes = null;
Assert::same(
'ent()',
Neon::encode($entity)
Neon::encode($entity),
);

Assert::same(
'",žlu/ťoučký"',
Neon::encode(',žlu/ťoučký')
Neon::encode(',žlu/ťoučký'),
);

Assert::same(
"foo: 1\nbar:\n\tx:\n\t\t- 1\n\t\t- 2\n\n\ty:\n\t\t- 3\n\t\t- 4\n\nbaz: null\n",
Neon::encode(['foo' => 1, 'bar' => ['x' => [1, 2], 'y' => [3, 4]], 'baz' => null], Neon::BLOCK)
Neon::encode(['foo' => 1, 'bar' => ['x' => [1, 2], 'y' => [3, 4]], 'baz' => null], Neon::BLOCK),
);

Assert::same(
'ent(1)inner(2, 3)',
Neon::encode(Neon::decode('ent(1)inner(2, 3)'))
Neon::encode(Neon::decode('ent(1)inner(2, 3)')),
);
Assert::same(
'foo(1, 2)::bar(3)',
Neon::encode(Neon::decode('foo(1,2)::bar(3)'))
Neon::encode(Neon::decode('foo(1,2)::bar(3)')),
);

Assert::same(
'2016-06-03 19:00:00 +0200',
Neon::encode(new DateTime('2016-06-03T19:00:00+02:00'))
Neon::encode(new DateTime('2016-06-03T19:00:00+02:00')),
);

Assert::same(
'2016-06-03 19:00:00 +0200',
Neon::encode(new DateTimeImmutable('2016-06-03T19:00:00+02:00'))
Neon::encode(new DateTimeImmutable('2016-06-03T19:00:00+02:00')),
);

Assert::same(
'{foo: bar}',
Neon::encode((object) ['foo' => 'bar'])
Neon::encode((object) ['foo' => 'bar']),
);

Assert::same(
'[]',
Neon::encode((object) [])
Neon::encode((object) []),
);

Assert::same(
'[]',
Neon::encode(new stdClass)
Neon::encode(new stdClass),
);

Assert::same(
'[]',
Neon::encode([], Neon::BLOCK)
Neon::encode([], Neon::BLOCK),
);

Assert::exception(function () {
Expand Down

0 comments on commit 62ac4c4

Please sign in to comment.