Skip to content

Commit

Permalink
Refactor: ensure consistent line ending normalization
Browse files Browse the repository at this point in the history
Updated tests to consistently normalize line endings before performing assertions. This improves code readability and ensures uniform assertion practices across all test cases.
  • Loading branch information
koriym committed Nov 27, 2024
1 parent d5c706c commit df0fe4f
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions tests/DependencyCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function testInstanceCompileString(): void
return 'bear';
EOT;
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
$this->assertSame(
$this->normalizeLineEndings($expected),
$this->normalizeLineEndings((string) $code)
);
}

public function testInstanceCompileInt(): void
Expand All @@ -45,7 +48,10 @@ public function testInstanceCompileInt(): void
return 1;
EOT;
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
$this->assertSame(
$this->normalizeLineEndings($expected),
$this->normalizeLineEndings((string) $code)
);
}

public function testInstanceCompileArray(): void
Expand All @@ -57,8 +63,9 @@ public function testInstanceCompileArray(): void
return array(1, 2, 3);
EOT;
$this->assertContains((string) $code, [
str_replace('array(1, 2, 3)', '[1, 2, 3]', $this->normalizeLineEndings($expected)),
$normalizedCode = $this->normalizeLineEndings((string) $code);
$this->assertContains($normalizedCode, [
$this->normalizeLineEndings(str_replace('array(1, 2, 3)', '[1, 2, 3]', $expected)),
$this->normalizeLineEndings($expected),
]);
}
Expand All @@ -84,12 +91,13 @@ public function testDependencyCompile(): void
return $instance;
EOT;
$expected = str_replace('{ANY}', Name::ANY, $expectedTemplate);
$this->assertContains((string) $code, [
str_replace(
$normalizedCode = $this->normalizeLineEndings((string) $code);
$this->assertContains($normalizedCode, [
$this->normalizeLineEndings(str_replace(
'array(\'Ray\\Compiler\\FakeCar\', \'setHandle\', \'handle\')',
'[\'Ray\\Compiler\\FakeCar\', \'setHandle\', \'handle\']',
str_replace('\\\\', '\\', $this->normalizeLineEndings($expected))
),
str_replace('\\\\', '\\', $expected)
)),
$this->normalizeLineEndings($expected),
]);
}
Expand All @@ -108,7 +116,10 @@ public function testDependencyProviderCompile(): void
$isSingleton = false;
return $instance->get();
EOT;
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
$this->assertSame(
$this->normalizeLineEndings($expected),
$this->normalizeLineEndings((string) $code)
);
}

public function testDependencyInstanceCompile(): void
Expand All @@ -121,7 +132,10 @@ public function testDependencyInstanceCompile(): void
return 'momo';
EOT;
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
$this->assertSame(
$this->normalizeLineEndings($expected),
$this->normalizeLineEndings((string) $code)
);
}

public function testDependencyObjectInstanceCompile(): void
Expand All @@ -134,8 +148,9 @@ public function testDependencyObjectInstanceCompile(): void
return unserialize('O:23:"Ray\\Compiler\\FakeEngine":0:{}');
EOT;
$this->assertContains((string) $code, [
str_replace('\\\\', '\\', $this->normalizeLineEndings($expected)),
$normalizedCode = $this->normalizeLineEndings((string) $code);
$this->assertContains($normalizedCode, [
$this->normalizeLineEndings(str_replace('\\\\', '\\', $expected)),
$this->normalizeLineEndings($expected),
]);
}
Expand Down Expand Up @@ -165,10 +180,13 @@ public function testContextualProviderCompile(): void
$isSingleton = false;
return $instance->get();
EOT;
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
$this->assertSame(
$this->normalizeLineEndings($expected),
$this->normalizeLineEndings((string) $code)
);
}

private function normalizeLineEndings($content): string
private function normalizeLineEndings(string $content): string
{
// Convert Windows (CRLF: \r\n) and old Mac (CR: \r) to Unix (LF: \n)
return str_replace(["\r\n", "\r"], "\n", $content);
Expand Down

0 comments on commit df0fe4f

Please sign in to comment.