PageRenderTime 52ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/mime-types-1.16/test/test_mime_type.rb

http://github.com/IronLanguages/main
Ruby | 356 lines | 300 code | 44 blank | 12 comment | 5 complexity | 9cf2d68a2034a39cf5a74b88244f36ca MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #! /usr/bin/env ruby
  2. #--
  3. # MIME::Types
  4. # A Ruby implementation of a MIME Types information library. Based in spirit
  5. # on the Perl MIME::Types information library by Mark Overmeer.
  6. # http://rubyforge.org/projects/mime-types/
  7. #
  8. # Licensed under the Ruby disjunctive licence with the GNU GPL or the Perl
  9. # Artistic licence. See Licence.txt for more information.
  10. #
  11. # Copyright 2003 - 2009 Austin Ziegler
  12. #++
  13. $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
  14. require 'mime/types'
  15. require 'test/unit' unless defined? $ZENTEST and $ZENTEST
  16. module TestMIME
  17. class TestType < Test::Unit::TestCase #:nodoc:
  18. def setup
  19. @zip = MIME::Type.new('x-appl/x-zip') { |t| t.extensions = ['zip', 'zp'] }
  20. end
  21. def test_class_from_array
  22. assert_nothing_raised do
  23. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit', 'linux')
  24. end
  25. assert_instance_of(MIME::Type, @yaml)
  26. assert_equal('text/yaml', @yaml.simplified)
  27. end
  28. def test_class_from_hash
  29. assert_nothing_raised do
  30. @yaml = MIME::Type.from_hash('Content-Type' => 'text/x-yaml',
  31. 'Content-Transfer-Encoding' => '8bit',
  32. 'System' => 'linux',
  33. 'Extensions' => %w(yaml yml))
  34. end
  35. assert_instance_of(MIME::Type, @yaml)
  36. assert_equal('text/yaml', @yaml.simplified)
  37. end
  38. def test_class_from_mime_type
  39. assert_nothing_raised do
  40. @zip2 = MIME::Type.from_mime_type(@zip)
  41. end
  42. assert_instance_of(MIME::Type, @zip)
  43. assert_equal('appl/zip', @zip.simplified)
  44. assert_not_equal(@zip.object_id, @zip2.object_id)
  45. end
  46. def test_class_simplified
  47. assert_equal(MIME::Type.simplified('text/plain'), 'text/plain')
  48. assert_equal(MIME::Type.simplified('image/jpeg'), 'image/jpeg')
  49. assert_equal(MIME::Type.simplified('application/x-msword'), 'application/msword')
  50. assert_equal(MIME::Type.simplified('text/vCard'), 'text/vcard')
  51. assert_equal(MIME::Type.simplified('application/pkcs7-mime'), 'application/pkcs7-mime')
  52. assert_equal(@zip.simplified, 'appl/zip')
  53. assert_equal(MIME::Type.simplified('x-xyz/abc'), 'xyz/abc')
  54. end
  55. def test_CMP # '<=>'
  56. assert(MIME::Type.new('text/plain') == MIME::Type.new('text/plain'))
  57. assert(MIME::Type.new('text/plain') != MIME::Type.new('image/jpeg'))
  58. assert(MIME::Type.new('text/plain') == 'text/plain')
  59. assert(MIME::Type.new('text/plain') != 'image/jpeg')
  60. assert(MIME::Type.new('text/plain') > MIME::Type.new('text/html'))
  61. assert(MIME::Type.new('text/plain') > 'text/html')
  62. assert(MIME::Type.new('text/html') < MIME::Type.new('text/plain'))
  63. assert(MIME::Type.new('text/html') < 'text/plain')
  64. assert('text/html' == MIME::Type.new('text/html'))
  65. assert('text/html' < MIME::Type.new('text/plain'))
  66. assert('text/plain' > MIME::Type.new('text/html'))
  67. end
  68. def test_ascii_eh
  69. assert(MIME::Type.new('text/plain').ascii?)
  70. assert(!MIME::Type.new('image/jpeg').ascii?)
  71. assert(!MIME::Type.new('application/x-msword').ascii?)
  72. assert(MIME::Type.new('text/vCard').ascii?)
  73. assert(!MIME::Type.new('application/pkcs7-mime').ascii?)
  74. assert(!@zip.ascii?)
  75. end
  76. def test_binary_eh
  77. assert(!MIME::Type.new('text/plain').binary?)
  78. assert(MIME::Type.new('image/jpeg').binary?)
  79. assert(MIME::Type.new('application/x-msword').binary?)
  80. assert(!MIME::Type.new('text/vCard').binary?)
  81. assert(MIME::Type.new('application/pkcs7-mime').binary?)
  82. assert(@zip.binary?)
  83. end
  84. def test_complete_eh
  85. assert_nothing_raised do
  86. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  87. 'linux')
  88. end
  89. assert(@yaml.complete?)
  90. assert_nothing_raised { @yaml.extensions = nil }
  91. assert(!@yaml.complete?)
  92. end
  93. def test_content_type
  94. assert_equal(MIME::Type.new('text/plain').content_type, 'text/plain')
  95. assert_equal(MIME::Type.new('image/jpeg').content_type, 'image/jpeg')
  96. assert_equal(MIME::Type.new('application/x-msword').content_type, 'application/x-msword')
  97. assert_equal(MIME::Type.new('text/vCard').content_type, 'text/vCard')
  98. assert_equal(MIME::Type.new('application/pkcs7-mime').content_type, 'application/pkcs7-mime')
  99. assert_equal(@zip.content_type, 'x-appl/x-zip');
  100. end
  101. def test_encoding
  102. assert_equal(MIME::Type.new('text/plain').encoding, 'quoted-printable')
  103. assert_equal(MIME::Type.new('image/jpeg').encoding, 'base64')
  104. assert_equal(MIME::Type.new('application/x-msword').encoding, 'base64')
  105. assert_equal(MIME::Type.new('text/vCard').encoding, 'quoted-printable')
  106. assert_equal(MIME::Type.new('application/pkcs7-mime').encoding, 'base64')
  107. assert_nothing_raised do
  108. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  109. 'linux')
  110. end
  111. assert_equal(@yaml.encoding, '8bit')
  112. assert_nothing_raised { @yaml.encoding = 'base64' }
  113. assert_equal(@yaml.encoding, 'base64')
  114. assert_nothing_raised { @yaml.encoding = :default }
  115. assert_equal(@yaml.encoding, 'quoted-printable')
  116. assert_raises(ArgumentError) { @yaml.encoding = 'binary' }
  117. assert_equal(@zip.encoding, 'base64')
  118. end
  119. def _test_default_encoding
  120. raise NotImplementedError, 'Need to write test_default_encoding'
  121. end
  122. def _test_docs
  123. raise NotImplementedError, 'Need to write test_docs'
  124. end
  125. def _test_docs_equals
  126. raise NotImplementedError, 'Need to write test_docs_equals'
  127. end
  128. def test_eql?
  129. assert(MIME::Type.new('text/plain').eql?(MIME::Type.new('text/plain')))
  130. assert(!MIME::Type.new('text/plain').eql?(MIME::Type.new('image/jpeg')))
  131. assert(!MIME::Type.new('text/plain').eql?('text/plain'))
  132. assert(!MIME::Type.new('text/plain').eql?('image/jpeg'))
  133. end
  134. def _test_encoding
  135. raise NotImplementedError, 'Need to write test_encoding'
  136. end
  137. def _test_encoding_equals
  138. raise NotImplementedError, 'Need to write test_encoding_equals'
  139. end
  140. def test_extensions
  141. assert_nothing_raised do
  142. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  143. 'linux')
  144. end
  145. assert_equal(@yaml.extensions, %w(yaml yml))
  146. assert_nothing_raised { @yaml.extensions = 'yaml' }
  147. assert_equal(@yaml.extensions, ['yaml'])
  148. assert_equal(@zip.extensions.size, 2)
  149. assert_equal(@zip.extensions, ['zip', 'zp'])
  150. end
  151. def _test_extensions_equals
  152. raise NotImplementedError, 'Need to write test_extensions_equals'
  153. end
  154. def test_like_eh
  155. assert(MIME::Type.new('text/plain').like?(MIME::Type.new('text/plain')))
  156. assert(MIME::Type.new('text/plain').like?(MIME::Type.new('text/x-plain')))
  157. assert(!MIME::Type.new('text/plain').like?(MIME::Type.new('image/jpeg')))
  158. assert(MIME::Type.new('text/plain').like?('text/plain'))
  159. assert(MIME::Type.new('text/plain').like?('text/x-plain'))
  160. assert(!MIME::Type.new('text/plain').like?('image/jpeg'))
  161. end
  162. def test_media_type
  163. assert_equal(MIME::Type.new('text/plain').media_type, 'text')
  164. assert_equal(MIME::Type.new('image/jpeg').media_type, 'image')
  165. assert_equal(MIME::Type.new('application/x-msword').media_type, 'application')
  166. assert_equal(MIME::Type.new('text/vCard').media_type, 'text')
  167. assert_equal(MIME::Type.new('application/pkcs7-mime').media_type, 'application')
  168. assert_equal(MIME::Type.new('x-chemical/x-pdb').media_type, 'chemical')
  169. assert_equal(@zip.media_type, 'appl')
  170. end
  171. def _test_obsolete_eh
  172. raise NotImplementedError, 'Need to write test_obsolete_eh'
  173. end
  174. def _test_obsolete_equals
  175. raise NotImplementedError, 'Need to write test_obsolete_equals'
  176. end
  177. def test_platform_eh
  178. assert_nothing_raised do
  179. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  180. 'oddbox')
  181. end
  182. assert(!@yaml.platform?)
  183. assert_nothing_raised { @yaml.system = nil }
  184. assert(!@yaml.platform?)
  185. assert_nothing_raised { @yaml.system = /#{RUBY_PLATFORM}/ }
  186. assert(@yaml.platform?)
  187. end
  188. def test_raw_media_type
  189. assert_equal(MIME::Type.new('text/plain').raw_media_type, 'text')
  190. assert_equal(MIME::Type.new('image/jpeg').raw_media_type, 'image')
  191. assert_equal(MIME::Type.new('application/x-msword').raw_media_type, 'application')
  192. assert_equal(MIME::Type.new('text/vCard').raw_media_type, 'text')
  193. assert_equal(MIME::Type.new('application/pkcs7-mime').raw_media_type, 'application')
  194. assert_equal(MIME::Type.new('x-chemical/x-pdb').raw_media_type, 'x-chemical')
  195. assert_equal(@zip.raw_media_type, 'x-appl')
  196. end
  197. def test_raw_sub_type
  198. assert_equal(MIME::Type.new('text/plain').raw_sub_type, 'plain')
  199. assert_equal(MIME::Type.new('image/jpeg').raw_sub_type, 'jpeg')
  200. assert_equal(MIME::Type.new('application/x-msword').raw_sub_type, 'x-msword')
  201. assert_equal(MIME::Type.new('text/vCard').raw_sub_type, 'vCard')
  202. assert_equal(MIME::Type.new('application/pkcs7-mime').raw_sub_type, 'pkcs7-mime')
  203. assert_equal(@zip.raw_sub_type, 'x-zip')
  204. end
  205. def test_registered_eh
  206. assert(MIME::Type.new('text/plain').registered?)
  207. assert(MIME::Type.new('image/jpeg').registered?)
  208. assert(!MIME::Type.new('application/x-msword').registered?)
  209. assert(MIME::Type.new('text/vCard').registered?)
  210. assert(MIME::Type.new('application/pkcs7-mime').registered?)
  211. assert(!@zip.registered?)
  212. end
  213. def _test_registered_equals
  214. raise NotImplementedError, 'Need to write test_registered_equals'
  215. end
  216. def test_signature_eh
  217. assert(!MIME::Type.new('text/plain').signature?)
  218. assert(!MIME::Type.new('image/jpeg').signature?)
  219. assert(!MIME::Type.new('application/x-msword').signature?)
  220. assert(MIME::Type.new('text/vCard').signature?)
  221. assert(MIME::Type.new('application/pkcs7-mime').signature?)
  222. end
  223. def test_simplified
  224. assert_equal(MIME::Type.new('text/plain').simplified, 'text/plain')
  225. assert_equal(MIME::Type.new('image/jpeg').simplified, 'image/jpeg')
  226. assert_equal(MIME::Type.new('application/x-msword').simplified, 'application/msword')
  227. assert_equal(MIME::Type.new('text/vCard').simplified, 'text/vcard')
  228. assert_equal(MIME::Type.new('application/pkcs7-mime').simplified, 'application/pkcs7-mime')
  229. assert_equal(MIME::Type.new('x-chemical/x-pdb').simplified, 'chemical/pdb')
  230. end
  231. def test_sub_type
  232. assert_equal(MIME::Type.new('text/plain').sub_type, 'plain')
  233. assert_equal(MIME::Type.new('image/jpeg').sub_type, 'jpeg')
  234. assert_equal(MIME::Type.new('application/x-msword').sub_type, 'msword')
  235. assert_equal(MIME::Type.new('text/vCard').sub_type, 'vcard')
  236. assert_equal(MIME::Type.new('application/pkcs7-mime').sub_type, 'pkcs7-mime')
  237. assert_equal(@zip.sub_type, 'zip')
  238. end
  239. def test_system_equals
  240. assert_nothing_raised do
  241. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  242. 'linux')
  243. end
  244. assert_equal(@yaml.system, %r{linux})
  245. assert_nothing_raised { @yaml.system = /win32/ }
  246. assert_equal(@yaml.system, %r{win32})
  247. assert_nothing_raised { @yaml.system = nil }
  248. assert_nil(@yaml.system)
  249. end
  250. def test_system_eh
  251. assert_nothing_raised do
  252. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  253. 'linux')
  254. end
  255. assert(@yaml.system?)
  256. assert_nothing_raised { @yaml.system = nil }
  257. assert(!@yaml.system?)
  258. end
  259. def test_to_a
  260. assert_nothing_raised do
  261. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  262. 'linux')
  263. end
  264. assert_equal(@yaml.to_a, ['text/x-yaml', %w(yaml yml), '8bit',
  265. /linux/, nil, nil, nil, false])
  266. end
  267. def test_to_hash
  268. assert_nothing_raised do
  269. @yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
  270. 'linux')
  271. end
  272. assert_equal(@yaml.to_hash,
  273. { 'Content-Type' => 'text/x-yaml',
  274. 'Content-Transfer-Encoding' => '8bit',
  275. 'Extensions' => %w(yaml yml),
  276. 'System' => /linux/,
  277. 'Registered' => false,
  278. 'URL' => nil,
  279. 'Obsolete' => nil,
  280. 'Docs' => nil })
  281. end
  282. def test_to_s
  283. assert_equal("#{MIME::Type.new('text/plain')}", 'text/plain')
  284. end
  285. def test_class_constructors
  286. assert_not_nil(@zip)
  287. yaml = MIME::Type.new('text/x-yaml') do |y|
  288. y.extensions = %w(yaml yml)
  289. y.encoding = '8bit'
  290. y.system = 'linux'
  291. end
  292. assert_instance_of(MIME::Type, yaml)
  293. assert_raises(MIME::InvalidContentType) { MIME::Type.new('apps') }
  294. assert_raises(MIME::InvalidContentType) { MIME::Type.new(nil) }
  295. end
  296. def _test_to_str
  297. raise NotImplementedError, 'Need to write test_to_str'
  298. end
  299. def _test_url
  300. raise NotImplementedError, 'Need to write test_url'
  301. end
  302. def _test_url_equals
  303. raise NotImplementedError, 'Need to write test_url_equals'
  304. end
  305. def _test_urls
  306. raise NotImplementedError, 'Need to write test_urls'
  307. end
  308. def __test_use_instead
  309. raise NotImplementedError, 'Need to write test_use_instead'
  310. end
  311. end
  312. end