LimeTesterDouble.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimeTesterDouble extends LimeTesterInteger
  12. {
  13. const
  14. EPSILON = 0.0000000001;
  15. protected
  16. $type = 'double';
  17. public function __construct($value)
  18. {
  19. parent::__construct((double)$value);
  20. }
  21. public function __toString()
  22. {
  23. if ($this->value == round($this->value))
  24. {
  25. return sprintf('%.1f', $this->value);
  26. }
  27. else
  28. {
  29. return (string)$this->value;
  30. }
  31. }
  32. public function is(LimeTesterInterface $expected)
  33. {
  34. if (is_infinite($this->value) && is_infinite($expected->value))
  35. {
  36. return;
  37. }
  38. if (abs($this->value - $expected->value) >= self::EPSILON)
  39. {
  40. throw new LimeAssertionFailedException($this, $expected);
  41. }
  42. }
  43. public function isnt(LimeTesterInterface $expected)
  44. {
  45. if ((is_infinite($this->value) && is_infinite($expected->value)) || abs($this->value - $expected->value) < self::EPSILON)
  46. {
  47. throw new LimeAssertionFailedException($this, $expected);
  48. }
  49. }
  50. public function same(LimeTesterInterface $expected)
  51. {
  52. $this->is($expected);
  53. if (gettype($this->value) != gettype($expected->value))
  54. {
  55. throw new LimeAssertionFailedException($this, $expected);
  56. }
  57. }
  58. public function isntSame(LimeTesterInterface $expected)
  59. {
  60. try
  61. {
  62. $this->is($expected);
  63. }
  64. catch (LimeAssertionFailedException $e)
  65. {
  66. if (gettype($this->value) == gettype($expected->value))
  67. {
  68. throw $e;
  69. }
  70. }
  71. }
  72. }