PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/spec/mspec/spec/guards/guard_spec.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 507 lines | 407 code | 100 blank | 0 comment | 112 complexity | 152dff6d758e2aebb0c1a016d501808d MD5 | raw file
  1. require 'spec_helper'
  2. require 'mspec/guards'
  3. require 'rbconfig'
  4. describe SpecGuard, "#ruby_version_override=" do
  5. after :each do
  6. SpecGuard.ruby_version_override = nil
  7. end
  8. it "returns nil by default" do
  9. SpecGuard.ruby_version_override.should be_nil
  10. end
  11. it "returns the value set by #ruby_version_override=" do
  12. SpecGuard.ruby_version_override = "8.3.2"
  13. SpecGuard.ruby_version_override.should == "8.3.2"
  14. end
  15. end
  16. describe SpecGuard, ".ruby_version" do
  17. before :each do
  18. @ruby_version = Object.const_get :RUBY_VERSION
  19. @ruby_patchlevel = Object.const_get :RUBY_PATCHLEVEL
  20. Object.const_set :RUBY_VERSION, "8.2.3"
  21. Object.const_set :RUBY_PATCHLEVEL, 71
  22. end
  23. after :each do
  24. Object.const_set :RUBY_VERSION, @ruby_version
  25. Object.const_set :RUBY_PATCHLEVEL, @ruby_patchlevel
  26. end
  27. it "returns the version and patchlevel for :full" do
  28. SpecGuard.ruby_version(:full).should == "8.2.3.71"
  29. end
  30. it "returns 0 for negative RUBY_PATCHLEVEL values" do
  31. Object.const_set :RUBY_PATCHLEVEL, -1
  32. SpecGuard.ruby_version(:full).should == "8.2.3.0"
  33. end
  34. it "returns major.minor.tiny for :tiny" do
  35. SpecGuard.ruby_version(:tiny).should == "8.2.3"
  36. end
  37. it "returns major.minor.tiny for :teeny" do
  38. SpecGuard.ruby_version(:tiny).should == "8.2.3"
  39. end
  40. it "returns major.minor for :minor" do
  41. SpecGuard.ruby_version(:minor).should == "8.2"
  42. end
  43. it "defaults to :minor" do
  44. SpecGuard.ruby_version.should == "8.2"
  45. end
  46. it "returns major for :major" do
  47. SpecGuard.ruby_version(:major).should == "8"
  48. end
  49. describe "with ruby_version_override set" do
  50. before :each do
  51. SpecGuard.ruby_version_override = "8.3.2"
  52. end
  53. after :each do
  54. SpecGuard.ruby_version_override = nil
  55. end
  56. it "returns the version and patchlevel for :full" do
  57. SpecGuard.ruby_version(:full).should == "8.3.2.71"
  58. end
  59. it "returns 0 for negative RUBY_PATCHLEVEL values" do
  60. Object.const_set :RUBY_PATCHLEVEL, -1
  61. SpecGuard.ruby_version(:full).should == "8.3.2.0"
  62. end
  63. it "returns major.minor.tiny for :tiny" do
  64. SpecGuard.ruby_version(:tiny).should == "8.3.2"
  65. end
  66. it "returns major.minor.tiny for :teeny" do
  67. SpecGuard.ruby_version(:tiny).should == "8.3.2"
  68. end
  69. it "returns major.minor for :minor" do
  70. SpecGuard.ruby_version(:minor).should == "8.3"
  71. end
  72. it "defaults to :minor" do
  73. SpecGuard.ruby_version.should == "8.3"
  74. end
  75. it "returns major for :major" do
  76. SpecGuard.ruby_version(:major).should == "8"
  77. end
  78. end
  79. end
  80. describe SpecGuard, "#yield?" do
  81. before :each do
  82. MSpec.clear_modes
  83. @guard = SpecGuard.new
  84. end
  85. after :each do
  86. MSpec.unregister :add, @guard
  87. MSpec.clear_modes
  88. SpecGuard.clear_guards
  89. end
  90. it "returns true if MSpec.mode?(:unguarded) is true" do
  91. MSpec.register_mode :unguarded
  92. @guard.yield?.should == true
  93. end
  94. it "returns true if MSpec.mode?(:verify) is true" do
  95. MSpec.register_mode :verify
  96. @guard.yield?.should == true
  97. end
  98. it "returns true if MSpec.mode?(:verify) is true regardless of invert being true" do
  99. MSpec.register_mode :verify
  100. @guard.yield?(true).should == true
  101. end
  102. it "returns true if MSpec.mode?(:report) is true" do
  103. MSpec.register_mode :report
  104. @guard.yield?.should == true
  105. end
  106. it "returns true if MSpec.mode?(:report) is true regardless of invert being true" do
  107. MSpec.register_mode :report
  108. @guard.yield?(true).should == true
  109. end
  110. it "returns true if MSpec.mode?(:report_on) is true and SpecGuards.guards contains the named guard" do
  111. MSpec.register_mode :report_on
  112. SpecGuard.guards << :guard_name
  113. @guard.yield?.should == false
  114. @guard.name = :guard_name
  115. @guard.yield?.should == true
  116. end
  117. it "returns #match? if neither report nor verify mode are true" do
  118. @guard.stub!(:match?).and_return(false)
  119. @guard.yield?.should == false
  120. @guard.stub!(:match?).and_return(true)
  121. @guard.yield?.should == true
  122. end
  123. it "returns #match? if invert is true and neither report nor verify mode are true" do
  124. @guard.stub!(:match?).and_return(false)
  125. @guard.yield?(true).should == true
  126. @guard.stub!(:match?).and_return(true)
  127. @guard.yield?(true).should == false
  128. end
  129. end
  130. describe SpecGuard, "#===" do
  131. it "returns true" do
  132. anything = mock("anything")
  133. SpecGuard.new.===(anything).should == true
  134. end
  135. end
  136. describe SpecGuard, "#implementation?" do
  137. before :all do
  138. @verbose = $VERBOSE
  139. $VERBOSE = nil
  140. end
  141. after :all do
  142. $VERBOSE = @verbose
  143. end
  144. before :each do
  145. @ruby_name = Object.const_get :RUBY_NAME
  146. @guard = SpecGuard.new
  147. end
  148. after :each do
  149. Object.const_set :RUBY_NAME, @ruby_name
  150. end
  151. it "returns true if passed :ruby and RUBY_NAME == 'ruby'" do
  152. Object.const_set :RUBY_NAME, 'ruby'
  153. @guard.implementation?(:ruby).should == true
  154. end
  155. it "returns true if passed :rubinius and RUBY_NAME == 'rbx'" do
  156. Object.const_set :RUBY_NAME, 'rbx'
  157. @guard.implementation?(:rubinius).should == true
  158. end
  159. it "returns true if passed :jruby and RUBY_NAME == 'jruby'" do
  160. Object.const_set :RUBY_NAME, 'jruby'
  161. @guard.implementation?(:jruby).should == true
  162. end
  163. it "returns true if passed :ironruby and RUBY_NAME == 'ironruby'" do
  164. Object.const_set :RUBY_NAME, 'ironruby'
  165. @guard.implementation?(:ironruby).should == true
  166. end
  167. it "returns true if passed :maglev and RUBY_NAME == 'maglev'" do
  168. Object.const_set :RUBY_NAME, 'maglev'
  169. @guard.implementation?(:maglev).should == true
  170. end
  171. it "returns true if passed :ruby and RUBY_NAME matches /^ruby/" do
  172. Object.const_set :RUBY_NAME, 'ruby'
  173. @guard.implementation?(:ruby).should == true
  174. Object.const_set :RUBY_NAME, 'ruby1.8'
  175. @guard.implementation?(:ruby).should == true
  176. Object.const_set :RUBY_NAME, 'ruby1.9'
  177. @guard.implementation?(:ruby).should == true
  178. end
  179. it "returns false when passed an unrecognized name" do
  180. Object.const_set :RUBY_NAME, 'ruby'
  181. @guard.implementation?(:python).should == false
  182. end
  183. end
  184. describe SpecGuard, "#standard?" do
  185. before :each do
  186. @guard = SpecGuard.new
  187. end
  188. it "returns true if #implementation? returns true" do
  189. @guard.should_receive(:implementation?).with(:ruby).and_return(true)
  190. @guard.standard?.should be_true
  191. end
  192. it "returns false if #implementation? returns false" do
  193. @guard.should_receive(:implementation?).with(:ruby).and_return(false)
  194. @guard.standard?.should be_false
  195. end
  196. end
  197. describe SpecGuard, "#platform?" do
  198. before :all do
  199. @verbose = $VERBOSE
  200. $VERBOSE = nil
  201. end
  202. after :all do
  203. $VERBOSE = @verbose
  204. end
  205. before :each do
  206. @ruby_platform = Object.const_get :RUBY_PLATFORM
  207. Object.const_set :RUBY_PLATFORM, 'solarce'
  208. @guard = SpecGuard.new
  209. end
  210. after :each do
  211. Object.const_set :RUBY_PLATFORM, @ruby_platform
  212. end
  213. it "returns false when arg does not match RUBY_PLATFORM" do
  214. @guard.platform?(:ruby).should == false
  215. end
  216. it "returns false when no arg matches RUBY_PLATFORM" do
  217. @guard.platform?(:ruby, :jruby, :rubinius, :maglev).should == false
  218. end
  219. it "returns true when arg matches RUBY_PLATFORM" do
  220. @guard.platform?(:solarce).should == true
  221. end
  222. it "returns true when any arg matches RUBY_PLATFORM" do
  223. @guard.platform?(:ruby, :jruby, :solarce, :rubinius, :maglev).should == true
  224. end
  225. it "returns true when arg is :windows and RUBY_PLATFORM contains 'mswin'" do
  226. Object.const_set :RUBY_PLATFORM, 'i386-mswin32'
  227. @guard.platform?(:windows).should == true
  228. end
  229. it "returns true when arg is :windows and RUBY_PLATFORM contains 'mingw'" do
  230. Object.const_set :RUBY_PLATFORM, 'i386-mingw32'
  231. @guard.platform?(:windows).should == true
  232. end
  233. it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do
  234. Object.const_set :RUBY_PLATFORM, 'i386-mswin32'
  235. @guard.platform?(:linux).should == false
  236. end
  237. it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do
  238. Object.const_set :RUBY_PLATFORM, 'i386-mingw32'
  239. @guard.platform?(:linux).should == false
  240. end
  241. end
  242. describe SpecGuard, "#platform? on JRuby" do
  243. before :all do
  244. @verbose = $VERBOSE
  245. $VERBOSE = nil
  246. end
  247. after :all do
  248. $VERBOSE = @verbose
  249. end
  250. before :each do
  251. @ruby_platform = Object.const_get :RUBY_PLATFORM
  252. Object.const_set :RUBY_PLATFORM, 'java'
  253. @guard = SpecGuard.new
  254. end
  255. after :each do
  256. Object.const_set :RUBY_PLATFORM, @ruby_platform
  257. end
  258. it "returns true when arg is :java and RUBY_PLATFORM contains 'java'" do
  259. @guard.platform?(:java).should == true
  260. end
  261. it "returns true when arg is :windows and RUBY_PLATFORM contains 'java' and os?(:windows) is true" do
  262. RbConfig::CONFIG.stub!(:[]).and_return('mswin32')
  263. @guard.platform?(:windows).should == true
  264. end
  265. it "returns true when RUBY_PLATFORM contains 'java' and os?(argument) is true" do
  266. RbConfig::CONFIG.stub!(:[]).and_return('amiga')
  267. @guard.platform?(:amiga).should == true
  268. end
  269. end
  270. describe SpecGuard, "#wordsize?" do
  271. before :each do
  272. @guard = SpecGuard.new
  273. end
  274. it "returns true when arg is 32 and 1.size is 4" do
  275. @guard.wordsize?(32).should == (1.size == 4)
  276. end
  277. it "returns true when arg is 64 and 1.size is 8" do
  278. @guard.wordsize?(64).should == (1.size == 8)
  279. end
  280. end
  281. describe SpecGuard, "#os?" do
  282. before :each do
  283. @guard = SpecGuard.new
  284. RbConfig::CONFIG.stub!(:[]).and_return('unreal')
  285. end
  286. it "returns true if argument matches RbConfig::CONFIG['host_os']" do
  287. @guard.os?(:unreal).should == true
  288. end
  289. it "returns true if any argument matches RbConfig::CONFIG['host_os']" do
  290. @guard.os?(:bsd, :unreal, :amiga).should == true
  291. end
  292. it "returns false if no argument matches RbConfig::CONFIG['host_os']" do
  293. @guard.os?(:bsd, :netbsd, :amiga, :msdos).should == false
  294. end
  295. it "returns false if argument does not match RbConfig::CONFIG['host_os']" do
  296. @guard.os?(:amiga).should == false
  297. end
  298. it "returns true when arg is :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do
  299. RbConfig::CONFIG.stub!(:[]).and_return('i386-mswin32')
  300. @guard.os?(:windows).should == true
  301. end
  302. it "returns true when arg is :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do
  303. RbConfig::CONFIG.stub!(:[]).and_return('i386-mingw32')
  304. @guard.os?(:windows).should == true
  305. end
  306. it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do
  307. RbConfig::CONFIG.stub!(:[]).and_return('i386-mingw32')
  308. @guard.os?(:linux).should == false
  309. end
  310. it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do
  311. RbConfig::CONFIG.stub!(:[]).and_return('i386-mingw32')
  312. @guard.os?(:linux).should == false
  313. end
  314. end
  315. describe SpecGuard, "#windows?" do
  316. before :each do
  317. @guard = SpecGuard.new
  318. end
  319. it "returns false if not passed :windows" do
  320. @guard.windows?(:linux, 'mswin32').should == false
  321. @guard.windows?(:linux, 'i386-mingw32').should == false
  322. end
  323. it "returns true if passed :windows and the key matches 'mswin' or 'mingw'" do
  324. @guard.windows?(:windows, 'mswin32').should == true
  325. @guard.windows?(:windows, 'i386-mingw32').should == true
  326. end
  327. it "returns false if passed :windows and the key matches neither 'mswin' nor 'mingw'" do
  328. @guard.windows?(:windows, 'darwin9.0').should == false
  329. @guard.windows?(:windows, 'linux').should == false
  330. end
  331. end
  332. describe SpecGuard, "#match?" do
  333. before :each do
  334. @guard = SpecGuard.new
  335. SpecGuard.stub!(:new).and_return(@guard)
  336. end
  337. it "returns true if #platform? or #implementation? return true" do
  338. @guard.stub!(:implementation?).and_return(true)
  339. @guard.stub!(:platform?).and_return(false)
  340. @guard.match?.should == true
  341. @guard.stub!(:implementation?).and_return(false)
  342. @guard.stub!(:platform?).and_return(true)
  343. @guard.match?.should == true
  344. end
  345. it "returns false if #platform? and #implementation? return false" do
  346. @guard.stub!(:implementation?).and_return(false)
  347. @guard.stub!(:platform?).and_return(false)
  348. @guard.match?.should == false
  349. end
  350. end
  351. describe SpecGuard, "#unregister" do
  352. before :each do
  353. MSpec.stub!(:unregister)
  354. @guard = SpecGuard.new
  355. end
  356. it "unregisters from MSpec :add actions" do
  357. MSpec.should_receive(:unregister).with(:add, @guard)
  358. @guard.unregister
  359. end
  360. end
  361. describe SpecGuard, "#record" do
  362. after :each do
  363. SpecGuard.clear
  364. end
  365. it "saves the name of the guarded spec under the name of the guard" do
  366. guard = SpecGuard.new "a", "1.8"..."1.9"
  367. guard.name = :named_guard
  368. guard.record "SomeClass#action returns true"
  369. SpecGuard.report.should == {
  370. 'named_guard a, 1.8...1.9' => ["SomeClass#action returns true"]
  371. }
  372. end
  373. end
  374. describe SpecGuard, ".guards" do
  375. it "returns an Array" do
  376. SpecGuard.guards.should be_kind_of(Array)
  377. end
  378. end
  379. describe SpecGuard, ".clear_guards" do
  380. it "resets the array to empty" do
  381. SpecGuard.guards << :guard
  382. SpecGuard.guards.should == [:guard]
  383. SpecGuard.clear_guards
  384. SpecGuard.guards.should == []
  385. end
  386. end
  387. describe SpecGuard, ".finish" do
  388. before :each do
  389. $stdout = @out = IOStub.new
  390. end
  391. after :each do
  392. $stdout = STDOUT
  393. SpecGuard.clear
  394. end
  395. it "prints the descriptions of the guarded specs" do
  396. guard = SpecGuard.new "a", "1.8"..."1.9"
  397. guard.name = :named_guard
  398. guard.record "SomeClass#action returns true"
  399. guard.record "SomeClass#reverse returns false"
  400. SpecGuard.finish
  401. $stdout.should == %[
  402. 2 specs omitted by guard: named_guard a, 1.8...1.9:
  403. SomeClass#action returns true
  404. SomeClass#reverse returns false
  405. ]
  406. end
  407. end