/test/test_attributes.rb

https://github.com/jmazzi/attrtastic · Ruby · 421 lines · 344 code · 76 blank · 1 comment · 4 complexity · c9b72bac793bf1590c8f1dafaab949d9 MD5 · raw file

  1. require 'helper'
  2. class TestAttributes < TestCase
  3. context "attributes" do
  4. setup do
  5. setup_fixtures
  6. end
  7. should "run block" do
  8. block_run = false
  9. @user_builder.attributes do
  10. block_run = true
  11. end
  12. assert block_run
  13. end
  14. should "generate output even when no block given, outputting all attributes (from #attribute_names) " do
  15. expected = html <<-EOHTML
  16. <div class="attributes">
  17. <ol>
  18. <li class="attribute">
  19. <span class="label">Name</span>
  20. <span class="value">IT Pro Blog</span>
  21. </li>
  22. <li class="attribute">
  23. <span class="label">Url</span>
  24. <span class="value">http://www.it.pro.blog</span>
  25. </li>
  26. <li class="attribute">
  27. <span class="label">Author full name</span>
  28. <span class="value">Doe, John</span>
  29. </li>
  30. </ol>
  31. </div>
  32. EOHTML
  33. actual = @blog_builder.attributes
  34. assert_equal expected, actual
  35. end
  36. should "generate output with block given" do
  37. expected = html <<-EOHTML
  38. <div class="attributes">
  39. <ol>
  40. </ol>
  41. </div>
  42. EOHTML
  43. actual = @user_builder.attributes do end
  44. assert_equal expected, actual
  45. end
  46. should "show header" do
  47. expected = html <<-EOHTML
  48. <div class="attributes">
  49. <div class="legend"><span>Legend</span></div>
  50. <ol>
  51. </ol>
  52. </div>
  53. EOHTML
  54. actual = @user_builder.attributes "Legend" do end
  55. assert_equal expected, actual
  56. #@template.output_buffer.clear
  57. actual = @user_builder.attributes :name => "Legend" do end
  58. assert_equal expected, actual
  59. end
  60. context "with fields list" do
  61. should "generate output" do
  62. expected = html <<-EOHTML
  63. <div class="attributes">
  64. <ol>
  65. <li class="attribute">
  66. <span class="label">Full name</span>
  67. <span class="value">Doe, John</span>
  68. </li>
  69. <li class="attribute">
  70. <span class="label">Email</span>
  71. <span class="value">john@doe.com</span>
  72. </li>
  73. </ol>
  74. </div>
  75. EOHTML
  76. actual = @user_builder.attributes :full_name, :email
  77. assert_equal expected, actual
  78. end
  79. should "show header" do
  80. expected = html <<-EOHTML
  81. <div class="attributes contact">
  82. <div class="legend"><span>Contact</span></div>
  83. <ol>
  84. <li class="attribute">
  85. <span class="label">Full name</span>
  86. <span class="value">Doe, John</span>
  87. </li>
  88. <li class="attribute">
  89. <span class="label">Title</span>
  90. <span class="value"></span>
  91. </li>
  92. <li class="attribute">
  93. <span class="label">Email</span>
  94. <span class="value">john@doe.com</span>
  95. </li>
  96. </ol>
  97. </div>
  98. EOHTML
  99. actual = @user_builder.attributes "Contact", :full_name, :title, :email, :html => {:class => "contact"}, :display_empty => true
  100. assert_equal expected, actual
  101. end
  102. end
  103. context "with :for option" do
  104. should "yield block" do
  105. block_run = false
  106. @blog_builder.attributes :for => nil do |author|
  107. block_run = true
  108. end
  109. assert block_run
  110. end
  111. end
  112. context "with :for => :method_name pointing to single object" do
  113. should "allow to access inner object" do
  114. @blog_builder.attributes :for => :author do |author|
  115. assert_equal @blog.author, author.record
  116. assert_equal @blog.author, author.object
  117. end
  118. end
  119. should "generate output for given inner object" do
  120. actual = @blog_builder.attributes :for => :author do |author|
  121. expected = html <<-EOHTML
  122. <li class="attribute">
  123. <span class="label">Full name</span>
  124. <span class="value">Doe, John</span>
  125. </li>
  126. EOHTML
  127. actual = author.attribute :full_name
  128. assert_equal expected, actual
  129. end
  130. expected = html <<-EOHTML
  131. <div class="attributes user">
  132. <ol>
  133. </ol>
  134. </div>
  135. EOHTML
  136. assert_equal expected, actual
  137. end
  138. should "show header" do
  139. expected = html <<-EOHTML
  140. <div class="attributes user">
  141. <div class="legend"><span>Author</span></div>
  142. <ol>
  143. </ol>
  144. </div>
  145. EOHTML
  146. actual = @blog_builder.attributes "Author", :for => :author do |author|
  147. end
  148. assert_equal expected, actual
  149. end
  150. should "work with field list" do
  151. expected = html <<-EOHTML
  152. <div class="attributes user">
  153. <ol>
  154. <li class="attribute">
  155. <span class="label">Full name</span>
  156. <span class="value">Doe, John</span>
  157. </li>
  158. </ol>
  159. </div>
  160. EOHTML
  161. actual = @blog_builder.attributes :full_name, :for => :author
  162. assert_equal expected, actual
  163. end
  164. end
  165. context "with :for => object" do
  166. should "allow to acces given object" do
  167. @blog_builder.attributes :for => @user do |author|
  168. assert_equal @user, author.record
  169. assert_equal @user, author.object
  170. end
  171. end
  172. should "generate output for given inner object" do
  173. actual = @blog_builder.attributes :for => @user do |author|
  174. expected = html <<-EOHTML
  175. <li class="attribute">
  176. <span class="label">Full name</span>
  177. <span class="value">Doe, John</span>
  178. </li>
  179. EOHTML
  180. actual = author.attribute :full_name
  181. assert_equal expected, actual
  182. end
  183. expected = html <<-EOHTML
  184. <div class="attributes user">
  185. <ol>
  186. </ol>
  187. </div>
  188. EOHTML
  189. assert_equal expected, actual
  190. end
  191. should "show header" do
  192. actual = @blog_builder.attributes "Author", :for => @user do |author|
  193. end
  194. expected = html <<-EOHTML
  195. <div class="attributes user">
  196. <div class="legend"><span>Author</span></div>
  197. <ol>
  198. </ol>
  199. </div>
  200. EOHTML
  201. assert_equal expected, actual
  202. end
  203. should "work with field list" do
  204. expected = html <<-EOHTML
  205. <div class="attributes user">
  206. <ol>
  207. <li class="attribute">
  208. <span class="label">Full name</span>
  209. <span class="value">Doe, John</span>
  210. </li>
  211. </ol>
  212. </div>
  213. EOHTML
  214. actual = @user_builder.attributes :full_name, :for => @user
  215. assert_equal expected, actual
  216. end
  217. end
  218. context "with :for => :method_name pointing to collection" do
  219. should "allow to access inner objects one by one" do
  220. posts = []
  221. @blog_builder.attributes :for => :posts do |post|
  222. posts << post.record
  223. end
  224. assert_equal @blog.posts, posts
  225. end
  226. should "generate output for given objects" do
  227. expected = html <<-EOHTML
  228. <div class="attributes post">
  229. <ol>
  230. </ol>
  231. </div>
  232. <div class="attributes post">
  233. <ol>
  234. </ol>
  235. </div>
  236. EOHTML
  237. actual = @blog_builder.attributes :for => :posts do |post|
  238. end
  239. assert_equal expected, actual
  240. end
  241. should "show header" do
  242. expected = html <<-EOHTML
  243. <div class="attributes post">
  244. <div class="legend"><span>Post</span></div>
  245. <ol>
  246. </ol>
  247. </div>
  248. <div class="attributes post">
  249. <div class="legend"><span>Post</span></div>
  250. <ol>
  251. </ol>
  252. </div>
  253. EOHTML
  254. actual = @blog_builder.attributes "Post", :for => :posts do |post|
  255. end
  256. assert_equal expected, actual
  257. end
  258. should "work with field list" do
  259. expected = html <<-EOHTML
  260. <div class="attributes post">
  261. <ol>
  262. <li class="attribute">
  263. <span class="label">Title</span>
  264. <span class="value">Hello World!</span>
  265. </li>
  266. </ol>
  267. </div>
  268. <div class="attributes post">
  269. <ol>
  270. <li class="attribute">
  271. <span class="label">Title</span>
  272. <span class="value">Sorry</span>
  273. </li>
  274. </ol>
  275. </div>
  276. EOHTML
  277. actual = @blog_builder.attributes :title, :for => :posts
  278. assert_equal expected, actual
  279. end
  280. end
  281. context "with :for => collection" do
  282. should "allow to access inner objects one by one" do
  283. posts = []
  284. @blog_builder.attributes :for => @blog.posts do |post|
  285. posts << post.record
  286. end
  287. assert_equal @blog.posts, posts
  288. end
  289. should "generate output for given objects" do
  290. expected = html <<-EOHTML
  291. <div class="attributes post">
  292. <ol>
  293. </ol>
  294. </div>
  295. <div class="attributes post">
  296. <ol>
  297. </ol>
  298. </div>
  299. EOHTML
  300. actual = @blog_builder.attributes :for => @blog.posts do |post|
  301. end
  302. assert_equal expected, actual
  303. end
  304. should "show header" do
  305. expected = html <<-EOHTML
  306. <div class="attributes post">
  307. <div class="legend"><span>Post</span></div>
  308. <ol>
  309. </ol>
  310. </div>
  311. <div class="attributes post">
  312. <div class="legend"><span>Post</span></div>
  313. <ol>
  314. </ol>
  315. </div>
  316. EOHTML
  317. actual = @blog_builder.attributes "Post", :for => @blog.posts do |post|
  318. end
  319. assert_equal expected, actual
  320. end
  321. should "work with field list" do
  322. expected = html <<-EOHTML
  323. <div class="attributes post">
  324. <ol>
  325. <li class="attribute">
  326. <span class="label">Title</span>
  327. <span class="value">Hello World!</span>
  328. </li>
  329. </ol>
  330. </div>
  331. <div class="attributes post">
  332. <ol>
  333. <li class="attribute">
  334. <span class="label">Title</span>
  335. <span class="value">Sorry</span>
  336. </li>
  337. </ol>
  338. </div>
  339. EOHTML
  340. actual = @blog_builder.attributes :title, :for => @blog.posts
  341. assert_equal expected, actual
  342. end
  343. end
  344. end
  345. end