PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/amexsbs/railsapp/vendor/plugins/paperclip/test/interpolations_test.rb

https://bitbucket.org/AcireStudios/social-app-demo
Ruby | 127 lines | 108 code | 19 blank | 0 comment | 2 complexity | d60f35fdba3dc2f6454d0adc5c345955 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, MIT
  1. require 'test/helper'
  2. class InterpolationsTest < Test::Unit::TestCase
  3. should "return all methods but the infrastructure when sent #all" do
  4. methods = Paperclip::Interpolations.all
  5. assert ! methods.include?(:[])
  6. assert ! methods.include?(:[]=)
  7. assert ! methods.include?(:all)
  8. methods.each do |m|
  9. assert Paperclip::Interpolations.respond_to?(m)
  10. end
  11. end
  12. should "return the Rails.root" do
  13. assert_equal Rails.root, Paperclip::Interpolations.rails_root(:attachment, :style)
  14. end
  15. should "return the Rails.env" do
  16. assert_equal Rails.env, Paperclip::Interpolations.rails_env(:attachment, :style)
  17. end
  18. should "return the class of the Interpolations module when called with no params" do
  19. assert_equal Module, Paperclip::Interpolations.class
  20. end
  21. should "return the class of the instance" do
  22. attachment = mock
  23. attachment.expects(:instance).returns(attachment)
  24. attachment.expects(:class).returns("Thing")
  25. assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
  26. end
  27. should "return the basename of the file" do
  28. attachment = mock
  29. attachment.expects(:original_filename).returns("one.jpg").times(2)
  30. assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
  31. end
  32. should "return the extension of the file" do
  33. attachment = mock
  34. attachment.expects(:original_filename).returns("one.jpg")
  35. attachment.expects(:styles).returns({})
  36. assert_equal "jpg", Paperclip::Interpolations.extension(attachment, :style)
  37. end
  38. should "return the extension of the file as the format if defined in the style" do
  39. attachment = mock
  40. attachment.expects(:original_filename).never
  41. attachment.expects(:styles).returns({:style => {:format => "png"}})
  42. assert_equal "png", Paperclip::Interpolations.extension(attachment, :style)
  43. end
  44. should "return the id of the attachment" do
  45. attachment = mock
  46. attachment.expects(:id).returns(23)
  47. attachment.expects(:instance).returns(attachment)
  48. assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
  49. end
  50. should "return the partitioned id of the attachment" do
  51. attachment = mock
  52. attachment.expects(:id).returns(23)
  53. attachment.expects(:instance).returns(attachment)
  54. assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
  55. end
  56. should "return the name of the attachment" do
  57. attachment = mock
  58. attachment.expects(:name).returns("file")
  59. assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
  60. end
  61. should "return the style" do
  62. assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
  63. end
  64. should "return the default style" do
  65. attachment = mock
  66. attachment.expects(:default_style).returns(:default_style)
  67. assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
  68. end
  69. should "reinterpolate :url" do
  70. attachment = mock
  71. attachment.expects(:url).with(:style, false).returns("1234")
  72. assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
  73. end
  74. should "raise if infinite loop detcted reinterpolating :url" do
  75. attachment = Object.new
  76. class << attachment
  77. def url(*args)
  78. Paperclip::Interpolations.url(self, :style)
  79. end
  80. end
  81. assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
  82. end
  83. should "return the filename as basename.extension" do
  84. attachment = mock
  85. attachment.expects(:styles).returns({})
  86. attachment.expects(:original_filename).returns("one.jpg").times(3)
  87. assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
  88. end
  89. should "return the filename as basename.extension when format supplied" do
  90. attachment = mock
  91. attachment.expects(:styles).returns({:style => {:format => :png}})
  92. attachment.expects(:original_filename).returns("one.jpg").times(2)
  93. assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
  94. end
  95. should "return the timestamp" do
  96. now = Time.now
  97. attachment = mock
  98. attachment.expects(:instance_read).with(:updated_at).returns(now)
  99. assert_equal now.to_s, Paperclip::Interpolations.timestamp(attachment, :style)
  100. end
  101. should "call all expected interpolations with the given arguments" do
  102. Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
  103. Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
  104. Paperclip::Interpolations.expects(:notreal).never
  105. value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
  106. assert_equal ":notreal/1234/attachments", value
  107. end
  108. end