PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/activemodel/test/cases/naming_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 290 lines | 228 code | 62 blank | 0 comment | 0 complexity | 6aad36d6c89024f4e04fe2fa40c7982c MD5 | raw file
  1. require 'cases/helper'
  2. require 'models/contact'
  3. require 'models/sheep'
  4. require 'models/track_back'
  5. require 'models/blog_post'
  6. class NamingTest < ActiveModel::TestCase
  7. def setup
  8. @model_name = ActiveModel::Name.new(Post::TrackBack)
  9. end
  10. def test_singular
  11. assert_equal 'post_track_back', @model_name.singular
  12. end
  13. def test_plural
  14. assert_equal 'post_track_backs', @model_name.plural
  15. end
  16. def test_element
  17. assert_equal 'track_back', @model_name.element
  18. end
  19. def test_collection
  20. assert_equal 'post/track_backs', @model_name.collection
  21. end
  22. def test_partial_path
  23. assert_deprecated(/#partial_path.*#to_partial_path/) do
  24. assert_equal 'post/track_backs/track_back', @model_name.partial_path
  25. end
  26. end
  27. def test_human
  28. assert_equal 'Track back', @model_name.human
  29. end
  30. def test_i18n_key
  31. assert_equal :"post/track_back", @model_name.i18n_key
  32. end
  33. end
  34. class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase
  35. def setup
  36. @model_name = ActiveModel::Name.new(Blog::Post, Blog)
  37. end
  38. def test_singular
  39. assert_equal 'blog_post', @model_name.singular
  40. end
  41. def test_plural
  42. assert_equal 'blog_posts', @model_name.plural
  43. end
  44. def test_element
  45. assert_equal 'post', @model_name.element
  46. end
  47. def test_collection
  48. assert_equal 'blog/posts', @model_name.collection
  49. end
  50. def test_partial_path
  51. assert_deprecated(/#partial_path.*#to_partial_path/) do
  52. assert_equal 'blog/posts/post', @model_name.partial_path
  53. end
  54. end
  55. def test_human
  56. assert_equal 'Post', @model_name.human
  57. end
  58. def test_route_key
  59. assert_equal 'posts', @model_name.route_key
  60. end
  61. def test_param_key
  62. assert_equal 'post', @model_name.param_key
  63. end
  64. def test_i18n_key
  65. assert_equal :"blog/post", @model_name.i18n_key
  66. end
  67. end
  68. class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase
  69. def setup
  70. @model_name = ActiveModel::Name.new(Blog::Post)
  71. end
  72. def test_singular
  73. assert_equal 'blog_post', @model_name.singular
  74. end
  75. def test_plural
  76. assert_equal 'blog_posts', @model_name.plural
  77. end
  78. def test_element
  79. assert_equal 'post', @model_name.element
  80. end
  81. def test_collection
  82. assert_equal 'blog/posts', @model_name.collection
  83. end
  84. def test_partial_path
  85. assert_deprecated(/#partial_path.*#to_partial_path/) do
  86. assert_equal 'blog/posts/post', @model_name.partial_path
  87. end
  88. end
  89. def test_human
  90. assert_equal 'Post', @model_name.human
  91. end
  92. def test_route_key
  93. assert_equal 'blog_posts', @model_name.route_key
  94. end
  95. def test_param_key
  96. assert_equal 'blog_post', @model_name.param_key
  97. end
  98. def test_i18n_key
  99. assert_equal :"blog/post", @model_name.i18n_key
  100. end
  101. end
  102. class NamingWithSuppliedModelNameTest < ActiveModel::TestCase
  103. def setup
  104. @model_name = ActiveModel::Name.new(Blog::Post, nil, 'Article')
  105. end
  106. def test_singular
  107. assert_equal 'article', @model_name.singular
  108. end
  109. def test_plural
  110. assert_equal 'articles', @model_name.plural
  111. end
  112. def test_element
  113. assert_equal 'article', @model_name.element
  114. end
  115. def test_collection
  116. assert_equal 'articles', @model_name.collection
  117. end
  118. def test_partial_path
  119. assert_deprecated(/#partial_path.*#to_partial_path/) do
  120. assert_equal 'articles/article', @model_name.partial_path
  121. end
  122. end
  123. def test_human
  124. assert_equal 'Article', @model_name.human
  125. end
  126. def test_route_key
  127. assert_equal 'articles', @model_name.route_key
  128. end
  129. def test_param_key
  130. assert_equal 'article', @model_name.param_key
  131. end
  132. def test_i18n_key
  133. assert_equal :"article", @model_name.i18n_key
  134. end
  135. end
  136. class NamingUsingRelativeModelNameTest < ActiveModel::TestCase
  137. def setup
  138. @model_name = Blog::Post.model_name
  139. end
  140. def test_singular
  141. assert_equal 'blog_post', @model_name.singular
  142. end
  143. def test_plural
  144. assert_equal 'blog_posts', @model_name.plural
  145. end
  146. def test_element
  147. assert_equal 'post', @model_name.element
  148. end
  149. def test_collection
  150. assert_equal 'blog/posts', @model_name.collection
  151. end
  152. def test_human
  153. assert_equal 'Post', @model_name.human
  154. end
  155. def test_route_key
  156. assert_equal 'posts', @model_name.route_key
  157. end
  158. def test_param_key
  159. assert_equal 'post', @model_name.param_key
  160. end
  161. def test_i18n_key
  162. assert_equal :"blog/post", @model_name.i18n_key
  163. end
  164. end
  165. class NamingHelpersTest < Test::Unit::TestCase
  166. def setup
  167. @klass = Contact
  168. @record = @klass.new
  169. @singular = 'contact'
  170. @plural = 'contacts'
  171. @uncountable = Sheep
  172. @singular_route_key = 'contact'
  173. @route_key = 'contacts'
  174. @param_key = 'contact'
  175. end
  176. def test_to_model_called_on_record
  177. assert_equal 'post_named_track_backs', plural(Post::TrackBack.new)
  178. end
  179. def test_singular
  180. assert_equal @singular, singular(@record)
  181. end
  182. def test_singular_for_class
  183. assert_equal @singular, singular(@klass)
  184. end
  185. def test_plural
  186. assert_equal @plural, plural(@record)
  187. end
  188. def test_plural_for_class
  189. assert_equal @plural, plural(@klass)
  190. end
  191. def test_route_key
  192. assert_equal @route_key, route_key(@record)
  193. assert_equal @singular_route_key, singular_route_key(@record)
  194. end
  195. def test_route_key_for_class
  196. assert_equal @route_key, route_key(@klass)
  197. assert_equal @singular_route_key, singular_route_key(@klass)
  198. end
  199. def test_param_key
  200. assert_equal @param_key, param_key(@record)
  201. end
  202. def test_param_key_for_class
  203. assert_equal @param_key, param_key(@klass)
  204. end
  205. def test_uncountable
  206. assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable"
  207. assert !uncountable?(@klass), "Expected 'contact' to be countable"
  208. end
  209. def test_uncountable_route_key
  210. assert_equal "sheep", singular_route_key(@uncountable)
  211. assert_equal "sheep_index", route_key(@uncountable)
  212. end
  213. private
  214. def method_missing(method, *args)
  215. ActiveModel::Naming.send(method, *args)
  216. end
  217. end
  218. class NameWithAnonymousClassTest < Test::Unit::TestCase
  219. def test_anonymous_class_without_name_argument
  220. assert_raises(ArgumentError) do
  221. ActiveModel::Name.new(Class.new)
  222. end
  223. end
  224. def test_anonymous_class_with_name_argument
  225. model_name = ActiveModel::Name.new(Class.new, nil, "Anonymous")
  226. assert_equal "Anonymous", model_name
  227. end
  228. end