PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/asserters/float.php

http://github.com/mageekguy/atoum
PHP | 236 lines | 206 code | 29 blank | 1 comment | 0 complexity | bef6f945fb8214b219e9fe98afe6278a MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\asserters;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\asserter,
  6. mageekguy\atoum\tools\diffs,
  7. mageekguy\atoum\tools\variable
  8. ;
  9. require_once __DIR__ . '/../../runner.php';
  10. class float extends atoum\test
  11. {
  12. public function testClass()
  13. {
  14. $this->testedClass->extends('mageekguy\atoum\asserters\integer');
  15. }
  16. public function test__construct()
  17. {
  18. $this
  19. ->if($this->newTestedInstance)
  20. ->then
  21. ->object($this->testedInstance->getGenerator())->isEqualTo(new atoum\asserter\generator())
  22. ->object($this->testedInstance->getAnalyzer())->isEqualTo(new variable\analyzer())
  23. ->object($this->testedInstance->getLocale())->isEqualTo(new atoum\locale())
  24. ->variable($this->testedInstance->getValue())->isNull()
  25. ->boolean($this->testedInstance->wasSet())->isFalse()
  26. ->if($this->newTestedInstance($generator = new asserter\generator(), $analyzer = new variable\analyzer(), $locale = new atoum\locale()))
  27. ->then
  28. ->object($this->testedInstance->getGenerator())->isIdenticalTo($generator)
  29. ->object($this->testedInstance->getAnalyzer())->isIdenticalTo($analyzer)
  30. ->object($this->testedInstance->getLocale())->isIdenticalTo($locale)
  31. ->variable($this->testedInstance->getValue())->isNull()
  32. ->boolean($this->testedInstance->wasSet())->isFalse()
  33. ;
  34. }
  35. public function testSetWith()
  36. {
  37. $this
  38. ->given($asserter = $this->newTestedInstance
  39. ->setLocale($locale = new \mock\atoum\locale())
  40. ->setAnalyzer($analyzer = new \mock\atoum\tools\variable\analyzer())
  41. )
  42. ->if(
  43. $this->calling($locale)->_ = $notFloat = uniqid(),
  44. $this->calling($analyzer)->getTypeOf = $badType = uniqid()
  45. )
  46. ->then
  47. ->exception(function() use ($asserter, & $value) { $asserter->setWith($value = uniqid()); })
  48. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  49. ->hasMessage($notFloat)
  50. ->mock($locale)->call('_')->withArguments('%s is not a float', $badType)->once
  51. ->mock($analyzer)->call('getTypeOf')->withArguments($value)->once
  52. ->string($asserter->getValue())->isEqualTo($value)
  53. ->object($asserter->setWith($value = (float) rand(- PHP_INT_MAX, PHP_INT_MAX)))->isIdenticalTo($asserter)
  54. ->float($asserter->getValue())->isEqualTo($value)
  55. ;
  56. }
  57. public function testIsZero()
  58. {
  59. $this
  60. ->given($asserter = $this->newTestedInstance
  61. ->setLocale($locale = new \mock\atoum\locale())
  62. ->setDiff($diff = new \mock\atoum\tools\diffs\variable())
  63. ->setAnalyzer($analyzer = new \mock\atoum\tools\variable\analyzer())
  64. )
  65. ->then
  66. ->exception(function() use ($asserter) { $asserter->isZero(); })
  67. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  68. ->hasMessage('Value is undefined')
  69. ->exception(function() use ($asserter) { $asserter->isZero; })
  70. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  71. ->hasMessage('Value is undefined')
  72. ->exception(function() use ($asserter) { $asserter->ISZerO; })
  73. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  74. ->hasMessage('Value is undefined')
  75. ->if($asserter->setWith(0.0))
  76. ->then
  77. ->object($asserter->isZero())->isIdenticalTo($asserter)
  78. ->object($asserter->isZero)->isIdenticalTo($asserter)
  79. ->if(
  80. $this->calling($locale)->_ = $notZero = uniqid(),
  81. $this->calling($analyzer)->getTypeOf = $type = uniqid(),
  82. $this->calling($diff)->__toString = $diffValue = uniqid(),
  83. $asserter->setWith($value = (float) rand(1, PHP_INT_MAX))
  84. )
  85. ->then
  86. ->exception(function() use ($asserter) { $asserter->isZero(); })
  87. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  88. ->hasMessage($notZero . PHP_EOL . $diffValue)
  89. ->mock($locale)->call('_')->withArguments('%s is not equal to %s', $asserter, $type)->once
  90. ->mock($analyzer)->call('getTypeOf')->withArguments(0.0)->once
  91. ->mock($diff)
  92. ->call('setExpected')->withArguments(0.0)->once
  93. ->call('setActual')->withArguments($value)->once
  94. ->exception(function() use ($asserter) { $asserter->isZero; })
  95. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  96. ->hasMessage($notZero . PHP_EOL . $diffValue)
  97. ->mock($locale)->call('_')->withArguments('%s is not equal to %s', $asserter, $type)->twice
  98. ->mock($analyzer)->call('getTypeOf')->withArguments(0.0)->twice
  99. ->mock($diff)
  100. ->call('setExpected')->withArguments(0.0)->twice
  101. ->call('setActual')->withArguments($value)->twice
  102. ->exception(function() use ($asserter, & $failMessage) { $asserter->isZero($failMessage = uniqid()); })
  103. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  104. ->hasMessage($failMessage . PHP_EOL . $diffValue)
  105. ;
  106. }
  107. public function testIsNearlyEqualTo()
  108. {
  109. $this
  110. ->given($asserter = $this->newTestedInstance
  111. ->setLocale($locale = new \mock\atoum\locale())
  112. ->setDiff($diff = new \mock\atoum\tools\diffs\variable())
  113. ->setAnalyzer($analyzer = new \mock\atoum\tools\variable\analyzer())
  114. )
  115. ->then
  116. ->exception(function() use ($asserter) { $asserter->isNearlyEqualTo(1.1, 0.1); })
  117. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  118. ->hasMessage('Value is undefined')
  119. ->if(
  120. $asserter->setWith(100.0),
  121. $this->calling($diff)->__toString = $diffValue = uniqid(),
  122. $this->calling($locale)->_ = $notNearlyEqualTo = uniqid(),
  123. $this->calling($analyzer)->getTypeOf = $type = uniqid()
  124. )
  125. ->then
  126. ->object($asserter->isNearlyEqualTo(100.0, 0.0))->isIdenticalTo($asserter)
  127. ->object($asserter->isNearlyEqualTo(100.0, 0.1))->isIdenticalTo($asserter)
  128. ->object($asserter->isNearlyEqualTo(100.05, 0.1))->isIdenticalTo($asserter)
  129. ->object($asserter->isNearlyEqualTo(100.1, 0.1))->isIdenticalTo($asserter)
  130. ->object($asserter->isNearlyEqualTo(99.95, 0.1))->isIdenticalTo($asserter)
  131. ->object($asserter->isNearlyEqualTo(99.99, 0.1))->isIdenticalTo($asserter)
  132. ->object($asserter->isNearlyEqualTo(100.0, 1))->isIdenticalTo($asserter)
  133. ->object($asserter->isNearlyEqualTo(101.0, 0.01))->isIdenticalTo($asserter)
  134. ->exception(function() use ($asserter, & $lessValue) { $asserter->isNearlyEqualTo(101.0, 0.001); })
  135. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  136. ->hasMessage($notNearlyEqualTo . PHP_EOL . $diffValue)
  137. ->mock($locale)->call('_')->withArguments('%s is not nearly equal to %s with epsilon %s', $asserter, $type, 0.001)->once
  138. ->mock($analyzer)->call('getTypeOf')->withArguments(101.0)->once
  139. ->mock($diff)
  140. ->call('setExpected')->withArguments(101.0)->once
  141. ->call('setActual')->withArguments(100.0)->once
  142. ->if($asserter->setWith(101.0))
  143. ->then
  144. ->object($asserter->isNearlyEqualTo(100.0, 0.01))->isIdenticalTo($asserter)
  145. ->exception(function() use ($asserter, & $lessValue) { $asserter->isNearlyEqualTo(100.0, 0.001); })
  146. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  147. ->hasMessage($notNearlyEqualTo . PHP_EOL . $diffValue)
  148. ->mock($locale)->call('_')->withArguments('%s is not nearly equal to %s with epsilon %s', $asserter, $type, 0.001)->twice
  149. ->mock($analyzer)->call('getTypeOf')->withArguments(100.0)->once
  150. ->mock($diff)
  151. ->call('setExpected')->withArguments(100.0)->once
  152. ->call('setActual')->withArguments(100.0)->once
  153. ->if($asserter->setWith(- 10001.0))
  154. ->then
  155. ->object($asserter->isNearlyEqualTo(- 10000.0, 0.0001))->isIdenticalTo($asserter)
  156. ->exception(function() use ($asserter, & $lessValue) { $asserter->isNearlyEqualTo(- 10000.0, 0.00001); })
  157. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  158. ->hasMessage($notNearlyEqualTo . PHP_EOL . $diffValue)
  159. ->mock($locale)->call('_')->withArguments('%s is not nearly equal to %s with epsilon %s', $asserter, $type, 0.00001)->once
  160. ->mock($analyzer)->call('getTypeOf')->withArguments(- 10000.0)->once
  161. ->mock($diff)
  162. ->call('setExpected')->withArguments(- 10000.0)->once
  163. ->call('setActual')->withArguments(- 10001.0)->once
  164. ->if($asserter->setWith(- 1.0001))
  165. ->then
  166. ->object($asserter->isNearlyEqualTo(- 1.0, 0.0001))->isIdenticalTo($asserter)
  167. ->exception(function() use ($asserter, & $lessValue) { $asserter->isNearlyEqualTo(- 1.0, 0.00001); })
  168. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  169. ->hasMessage($notNearlyEqualTo . PHP_EOL . $diffValue)
  170. ->mock($locale)->call('_')->withArguments('%s is not nearly equal to %s with epsilon %s', $asserter, $type, 0.00001)->twice
  171. ->mock($analyzer)->call('getTypeOf')->withArguments(- 1.0)->once
  172. ->mock($diff)
  173. ->call('setExpected')->withArguments(- 1.0)->once
  174. ->call('setActual')->withArguments(- 1.0001)->once
  175. ->if($asserter->setWith(0.0001))
  176. ->then
  177. ->object($asserter->isNearlyEqualTo(- 0.0001, 0.0001))->isIdenticalTo($asserter)
  178. ;
  179. }
  180. /** @php 5.4 */
  181. public function testIsNearlyEqualToWithINF()
  182. {
  183. $this
  184. ->given($asserter = $this->newTestedInstance
  185. ->setLocale($locale = new \mock\atoum\locale())
  186. ->setDiff($diff = new \mock\atoum\tools\diffs\variable())
  187. ->setAnalyzer($analyzer = new \mock\atoum\tools\variable\analyzer())
  188. )
  189. ->if(
  190. $asserter->setWith(INF),
  191. $this->calling($diff)->__toString = $diffValue = uniqid(),
  192. $this->calling($locale)->_ = $notNearlyEqualTo = uniqid(),
  193. $this->calling($analyzer)->getTypeOf = $type = uniqid()
  194. )
  195. ->then
  196. ->object($asserter->isNearlyEqualTo(INF, 1))->isIdenticalTo($asserter)
  197. ->exception(function() use ($asserter, & $lessValue) { $asserter->isNearlyEqualTo(- INF, 1); })
  198. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  199. ->hasMessage($notNearlyEqualTo . PHP_EOL . $diffValue)
  200. ->mock($locale)->call('_')->withArguments('%s is not nearly equal to %s with epsilon %s', $asserter, $type, 1)->once
  201. ->mock($analyzer)->call('getTypeOf')->withArguments(- INF)->once
  202. ->mock($diff)
  203. ->call('setExpected')->withArguments(- INF)->once
  204. ->call('setActual')->withArguments(INF)->once
  205. ;
  206. }
  207. }