PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/syck/test_yaml.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1413 lines | 1267 code | 68 blank | 78 comment | 2 complexity | 8759708a10799d1a3cd080c5a87fa2b5 MD5 | raw file
  1. # -*- mode: ruby; ruby-indent-level: 4; tab-width: 4; indent-tabs-mode: t -*-
  2. # vim:sw=4:ts=4
  3. # $Id$
  4. #
  5. require 'test/unit'
  6. require 'yaml'
  7. require 'syck/ypath'
  8. # [ruby-core:01946]
  9. module YAML_Tests
  10. StructTest = Struct::new( :c )
  11. end
  12. module Syck
  13. class YAML_Unit_Tests < Test::Unit::TestCase
  14. def setup
  15. @current_engine = YAML::ENGINE.yamler
  16. YAML::ENGINE.yamler = 'syck'
  17. end
  18. def teardown
  19. YAML::ENGINE.yamler = @current_engine
  20. end
  21. # [ruby-core:34969]
  22. def test_regexp_with_n
  23. assert_cycle(Regexp.new('',0,'n'))
  24. end
  25. #
  26. # Convert between YAML and the object to verify correct parsing and
  27. # emitting
  28. #
  29. def assert_to_yaml( obj, yaml, msg = nil )
  30. assert_equal( obj, YAML::load( yaml ), msg )
  31. assert_equal( obj, YAML::parse( yaml ).transform, msg )
  32. assert_equal( obj, YAML::load( obj.to_yaml ), msg )
  33. assert_equal( obj, YAML::parse( obj.to_yaml ).transform, msg )
  34. assert_equal( obj, YAML::load(
  35. obj.to_yaml( :UseVersion => true, :UseHeader => true, :SortKeys => true )
  36. ), msg )
  37. end
  38. #
  39. # Test parser only
  40. #
  41. def assert_parse_only( obj, yaml, msg = nil )
  42. assert_equal( obj, YAML::load( yaml ), msg )
  43. assert_equal( obj, YAML::parse( yaml ).transform, msg )
  44. end
  45. def assert_cycle( obj, msg = nil )
  46. assert_equal( obj, YAML::load( obj.to_yaml ), msg )
  47. end
  48. def assert_path_segments( path, segments, msg = nil )
  49. YAML::YPath.each_path( path ) { |choice|
  50. assert_equal( choice.segments, segments.shift, msg )
  51. }
  52. assert_equal( segments.length, 0, "Some segments leftover: #{ segments.inspect }" )
  53. end
  54. #
  55. # Make a time with the time zone
  56. #
  57. def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
  58. usec = Rational(usec.to_s) * 1000000
  59. val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
  60. if zone != "Z"
  61. hour = zone[0,3].to_i * 3600
  62. min = zone[3,2].to_i * 60
  63. ofs = (hour + min)
  64. val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
  65. end
  66. return val
  67. end
  68. #
  69. # Tests modified from 00basic.t in YAML.pm
  70. #
  71. def test_basic_map
  72. # Simple map
  73. assert_parse_only(
  74. { 'one' => 'foo', 'three' => 'baz', 'two' => 'bar' }, <<EOY
  75. one: foo
  76. two: bar
  77. three: baz
  78. EOY
  79. )
  80. end
  81. def test_basic_strings
  82. # Common string types
  83. assert_cycle("x")
  84. assert_cycle(":x")
  85. assert_cycle(":")
  86. assert_parse_only(
  87. { 1 => 'simple string', 2 => 42, 3 => '1 Single Quoted String',
  88. 4 => 'YAML\'s Double "Quoted" String', 5 => "A block\n with several\n lines.\n",
  89. 6 => "A \"chomped\" block", 7 => "A folded\n string\n", 8 => ": started string" },
  90. <<EOY
  91. 1: simple string
  92. 2: 42
  93. 3: '1 Single Quoted String'
  94. 4: "YAML's Double \\\"Quoted\\\" String"
  95. 5: |
  96. A block
  97. with several
  98. lines.
  99. 6: |-
  100. A "chomped" block
  101. 7: >
  102. A
  103. folded
  104. string
  105. 8: ": started string"
  106. EOY
  107. )
  108. end
  109. #
  110. # Test the specification examples
  111. # - Many examples have been changes because of whitespace problems that
  112. # caused the two to be inequivalent, or keys to be sorted wrong
  113. #
  114. def test_spec_simple_implicit_sequence
  115. # Simple implicit sequence
  116. assert_to_yaml(
  117. [ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ], <<EOY
  118. - Mark McGwire
  119. - Sammy Sosa
  120. - Ken Griffey
  121. EOY
  122. )
  123. end
  124. def test_spec_simple_implicit_map
  125. # Simple implicit map
  126. assert_to_yaml(
  127. { 'hr' => 65, 'avg' => 0.278, 'rbi' => 147 }, <<EOY
  128. avg: 0.278
  129. hr: 65
  130. rbi: 147
  131. EOY
  132. )
  133. end
  134. def test_spec_simple_map_with_nested_sequences
  135. # Simple mapping with nested sequences
  136. assert_to_yaml(
  137. { 'american' =>
  138. [ 'Boston Red Sox', 'Detroit Tigers', 'New York Yankees' ],
  139. 'national' =>
  140. [ 'New York Mets', 'Chicago Cubs', 'Atlanta Braves' ] }, <<EOY
  141. american:
  142. - Boston Red Sox
  143. - Detroit Tigers
  144. - New York Yankees
  145. national:
  146. - New York Mets
  147. - Chicago Cubs
  148. - Atlanta Braves
  149. EOY
  150. )
  151. end
  152. def test_spec_simple_sequence_with_nested_map
  153. # Simple sequence with nested map
  154. assert_to_yaml(
  155. [
  156. {'name' => 'Mark McGwire', 'hr' => 65, 'avg' => 0.278},
  157. {'name' => 'Sammy Sosa', 'hr' => 63, 'avg' => 0.288}
  158. ], <<EOY
  159. -
  160. avg: 0.278
  161. hr: 65
  162. name: Mark McGwire
  163. -
  164. avg: 0.288
  165. hr: 63
  166. name: Sammy Sosa
  167. EOY
  168. )
  169. end
  170. def test_spec_sequence_of_sequences
  171. # Simple sequence with inline sequences
  172. assert_parse_only(
  173. [
  174. [ 'name', 'hr', 'avg' ],
  175. [ 'Mark McGwire', 65, 0.278 ],
  176. [ 'Sammy Sosa', 63, 0.288 ]
  177. ], <<EOY
  178. - [ name , hr , avg ]
  179. - [ Mark McGwire , 65 , 0.278 ]
  180. - [ Sammy Sosa , 63 , 0.288 ]
  181. EOY
  182. )
  183. end
  184. def test_spec_mapping_of_mappings
  185. # Simple map with inline maps
  186. assert_parse_only(
  187. { 'Mark McGwire' =>
  188. { 'hr' => 65, 'avg' => 0.278 },
  189. 'Sammy Sosa' =>
  190. { 'hr' => 63, 'avg' => 0.288 }
  191. }, <<EOY
  192. Mark McGwire: {hr: 65, avg: 0.278}
  193. Sammy Sosa: {hr: 63,
  194. avg: 0.288}
  195. EOY
  196. )
  197. end
  198. def test_ambiguous_comments
  199. # [ruby-talk:88012]
  200. assert_to_yaml( "Call the method #dave", <<EOY )
  201. --- "Call the method #dave"
  202. EOY
  203. end
  204. def test_spec_nested_comments
  205. # Map and sequences with comments
  206. assert_parse_only(
  207. { 'hr' => [ 'Mark McGwire', 'Sammy Sosa' ],
  208. 'rbi' => [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY
  209. hr: # 1998 hr ranking
  210. - Mark McGwire
  211. - Sammy Sosa
  212. rbi:
  213. # 1998 rbi ranking
  214. - Sammy Sosa
  215. - Ken Griffey
  216. EOY
  217. )
  218. end
  219. def test_spec_anchors_and_aliases
  220. # Anchors and aliases
  221. assert_parse_only(
  222. { 'hr' =>
  223. [ 'Mark McGwire', 'Sammy Sosa' ],
  224. 'rbi' =>
  225. [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY
  226. hr:
  227. - Mark McGwire
  228. # Name "Sammy Sosa" scalar SS
  229. - &SS Sammy Sosa
  230. rbi:
  231. # So it can be referenced later.
  232. - *SS
  233. - Ken Griffey
  234. EOY
  235. )
  236. assert_to_yaml(
  237. [{"arrival"=>"EDI", "departure"=>"LAX", "fareref"=>"DOGMA", "currency"=>"GBP"}, {"arrival"=>"MEL", "departure"=>"SYD", "fareref"=>"MADF", "currency"=>"AUD"}, {"arrival"=>"MCO", "departure"=>"JFK", "fareref"=>"DFSF", "currency"=>"USD"}], <<EOY
  238. -
  239. &F fareref: DOGMA
  240. &C currency: GBP
  241. &D departure: LAX
  242. &A arrival: EDI
  243. - { *F: MADF, *C: AUD, *D: SYD, *A: MEL }
  244. - { *F: DFSF, *C: USD, *D: JFK, *A: MCO }
  245. EOY
  246. )
  247. assert_to_yaml(
  248. {"ALIASES"=>["fareref", "currency", "departure", "arrival"], "FARES"=>[{"arrival"=>"EDI", "departure"=>"LAX", "fareref"=>"DOGMA", "currency"=>"GBP"}, {"arrival"=>"MEL", "departure"=>"SYD", "fareref"=>"MADF", "currency"=>"AUD"}, {"arrival"=>"MCO", "departure"=>"JFK", "fareref"=>"DFSF", "currency"=>"USD"}]}, <<EOY
  249. ---
  250. ALIASES: [&f fareref, &c currency, &d departure, &a arrival]
  251. FARES:
  252. - *f: DOGMA
  253. *c: GBP
  254. *d: LAX
  255. *a: EDI
  256. - *f: MADF
  257. *c: AUD
  258. *d: SYD
  259. *a: MEL
  260. - *f: DFSF
  261. *c: USD
  262. *d: JFK
  263. *a: MCO
  264. EOY
  265. )
  266. end
  267. def test_spec_mapping_between_sequences
  268. # Complex key #1
  269. dj = Date.new( 2001, 7, 23 )
  270. assert_parse_only(
  271. { [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ],
  272. [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ] }, <<EOY
  273. ? # PLAY SCHEDULE
  274. - Detroit Tigers
  275. - Chicago Cubs
  276. :
  277. - 2001-07-23
  278. ? [ New York Yankees,
  279. Atlanta Braves ]
  280. : [ 2001-07-02, 2001-08-12,
  281. 2001-08-14 ]
  282. EOY
  283. )
  284. # Complex key #2
  285. assert_parse_only(
  286. { [ 'New York Yankees', 'Atlanta Braves' ] =>
  287. [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ),
  288. Date.new( 2001, 8, 14 ) ],
  289. [ 'Detroit Tigers', 'Chicago Cubs' ] =>
  290. [ Date.new( 2001, 7, 23 ) ]
  291. }, <<EOY
  292. ?
  293. - New York Yankees
  294. - Atlanta Braves
  295. :
  296. - 2001-07-02
  297. - 2001-08-12
  298. - 2001-08-14
  299. ?
  300. - Detroit Tigers
  301. - Chicago Cubs
  302. :
  303. - 2001-07-23
  304. EOY
  305. )
  306. end
  307. def test_spec_sequence_key_shortcut
  308. # Shortcut sequence map
  309. assert_parse_only(
  310. { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ),
  311. 'bill-to' => 'Chris Dumars', 'product' =>
  312. [ { 'item' => 'Super Hoop', 'quantity' => 1 },
  313. { 'item' => 'Basketball', 'quantity' => 4 },
  314. { 'item' => 'Big Shoes', 'quantity' => 1 } ] }, <<EOY
  315. invoice: 34843
  316. date : 2001-01-23
  317. bill-to: Chris Dumars
  318. product:
  319. - item : Super Hoop
  320. quantity: 1
  321. - item : Basketball
  322. quantity: 4
  323. - item : Big Shoes
  324. quantity: 1
  325. EOY
  326. )
  327. end
  328. def test_spec_sequence_in_sequence_shortcut
  329. # Seq-in-seq
  330. assert_parse_only( [ [ [ 'one', 'two', 'three' ] ] ], <<EOY )
  331. - - - one
  332. - two
  333. - three
  334. EOY
  335. end
  336. def test_spec_sequence_shortcuts
  337. # Sequence shortcuts combined
  338. assert_parse_only(
  339. [
  340. [
  341. [ [ 'one' ] ],
  342. [ 'two', 'three' ],
  343. { 'four' => nil },
  344. [ { 'five' => [ 'six' ] } ],
  345. [ 'seven' ]
  346. ],
  347. [ 'eight', 'nine' ]
  348. ], <<EOY )
  349. - - - - one
  350. - - two
  351. - three
  352. - four:
  353. - - five:
  354. - six
  355. - - seven
  356. - - eight
  357. - nine
  358. EOY
  359. end
  360. def test_spec_single_literal
  361. # Literal scalar block
  362. assert_parse_only( [ "\\/|\\/|\n/ | |_\n" ], <<EOY )
  363. - |
  364. \\/|\\/|
  365. / | |_
  366. EOY
  367. end
  368. def test_spec_single_folded
  369. # Folded scalar block
  370. assert_parse_only(
  371. [ "Mark McGwire's year was crippled by a knee injury.\n" ], <<EOY
  372. - >
  373. Mark McGwire\'s
  374. year was crippled
  375. by a knee injury.
  376. EOY
  377. )
  378. end
  379. def test_spec_preserve_indent
  380. # Preserve indented spaces
  381. assert_parse_only(
  382. "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n", <<EOY
  383. --- >
  384. Sammy Sosa completed another
  385. fine season with great stats.
  386. 63 Home Runs
  387. 0.288 Batting Average
  388. What a year!
  389. EOY
  390. )
  391. end
  392. def test_spec_indentation_determines_scope
  393. assert_parse_only(
  394. { 'name' => 'Mark McGwire', 'accomplishment' => "Mark set a major league home run record in 1998.\n",
  395. 'stats' => "65 Home Runs\n0.278 Batting Average\n" }, <<EOY
  396. name: Mark McGwire
  397. accomplishment: >
  398. Mark set a major league
  399. home run record in 1998.
  400. stats: |
  401. 65 Home Runs
  402. 0.278 Batting Average
  403. EOY
  404. )
  405. end
  406. #
  407. # Reports from N.Easterly & J.Trupiano : Tests with patch from daz
  408. # [ruby-core:23006] [Bug #1311] http://redmine.ruby-lang.org/issues/show/1311
  409. #
  410. def test_scan_scalar_nl
  411. bug1311 = '[ruby-core:23006]'
  412. assert_cycle(<<EoY, bug1311)
  413. a
  414. b
  415. EoY
  416. assert_cycle(<<EoY, bug1311)
  417. a
  418. b
  419. c
  420. EoY
  421. assert_cycle(<<EoY, bug1311)
  422. a
  423. b
  424. EoY
  425. assert_cycle(" Do I work?\nNo indent", bug1311)
  426. assert_cycle(" \n Do I work?\nNo indent", bug1311)
  427. assert_cycle("\n Do I work?\nNo indent", bug1311)
  428. assert_cycle("\n", bug1311)
  429. assert_cycle("\n\n", bug1311)
  430. assert_cycle("\r\n", bug1311)
  431. assert_cycle <<EoY, '[ruby-core:28777]'
  432. Domain name:
  433. ckgteam.co.uk
  434. Registrant:
  435. James Gregory
  436. Registrant type:
  437. UK Individual
  438. Registrant's address:
  439. The registrant is a non-trading individual who has opted to have their
  440. address omitted from the WHOIS service.
  441. Registrar:
  442. Webfusion Ltd t/a 123-Reg.co.uk [Tag = 123-REG]
  443. URL: http://www.123-reg.co.uk
  444. Relevant dates:
  445. Registered on: 16-Nov-2009
  446. Renewal date: 16-Nov-2011
  447. Last updated: 25-Nov-2009
  448. Registration status:
  449. Registered until renewal date.
  450. Name servers:
  451. ns1.slicehost.net
  452. ns2.slicehost.net
  453. ns3.slicehost.net
  454. WHOIS lookup made at 11:56:46 19-Mar-2010
  455. --
  456. This WHOIS information is provided for free by Nominet UK the central registry
  457. for .uk domain names. This information and the .uk WHOIS are:
  458. Copyright Nominet UK 1996 - 2010.
  459. You may not access the .uk WHOIS or use any data from it except as permitted
  460. by the terms of use available in full at http://www.nominet.org.uk/whois, which
  461. includes restrictions on: (A) use of the data for advertising, or its
  462. repackaging, recompilation, redistribution or reuse (B) obscuring, removing
  463. or hiding any or all of this notice and (C) exceeding query rate or volume
  464. limits. The data is provided on an 'as-is' basis and may lag behind the
  465. register. Access may be withdrawn or restricted at any time.
  466. EoY
  467. end
  468. def test_spec_multiline_scalars
  469. # Multiline flow scalars
  470. assert_parse_only(
  471. { 'plain' => 'This unquoted scalar spans many lines.',
  472. 'quoted' => "So does this quoted scalar.\n" }, <<EOY
  473. plain: This unquoted
  474. scalar spans
  475. many lines.
  476. quoted: "\\
  477. So does this quoted
  478. scalar.\\n"
  479. EOY
  480. )
  481. end
  482. def test_spec_type_int
  483. assert_parse_only(
  484. { 'canonical' => 12345, 'decimal' => 12345, 'octal' => '014'.oct, 'hexadecimal' => '0xC'.hex }, <<EOY
  485. canonical: 12345
  486. decimal: +12,345
  487. octal: 014
  488. hexadecimal: 0xC
  489. EOY
  490. )
  491. assert_parse_only(
  492. { 'canonical' => 685230, 'decimal' => 685230, 'octal' => 02472256, 'hexadecimal' => 0x0A74AE, 'sexagesimal' => 685230 }, <<EOY)
  493. canonical: 685230
  494. decimal: +685,230
  495. octal: 02472256
  496. hexadecimal: 0x0A,74,AE
  497. sexagesimal: 190:20:30
  498. EOY
  499. end
  500. def test_spec_type_float
  501. assert_parse_only(
  502. { 'canonical' => 1230.15, 'exponential' => 1230.15, 'fixed' => 1230.15,
  503. 'negative infinity' => -1.0/0.0 }, <<EOY)
  504. canonical: 1.23015e+3
  505. exponential: 12.3015e+02
  506. fixed: 1,230.15
  507. negative infinity: -.inf
  508. EOY
  509. nan = YAML::load( <<EOY )
  510. not a number: .NaN
  511. EOY
  512. assert( nan['not a number'].nan? )
  513. end
  514. def test_spec_type_misc
  515. assert_parse_only(
  516. { nil => nil, true => true, false => false, 'string' => '12345' }, <<EOY
  517. null: ~
  518. true: yes
  519. false: no
  520. string: '12345'
  521. EOY
  522. )
  523. end
  524. def test_spec_complex_invoice
  525. # Complex invoice type
  526. id001 = { 'given' => 'Chris', 'family' => 'Dumars', 'address' =>
  527. { 'lines' => "458 Walkman Dr.\nSuite #292\n", 'city' => 'Royal Oak',
  528. 'state' => 'MI', 'postal' => 48046 } }
  529. assert_parse_only(
  530. { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ),
  531. 'bill-to' => id001, 'ship-to' => id001, 'product' =>
  532. [ { 'sku' => 'BL394D', 'quantity' => 4,
  533. 'description' => 'Basketball', 'price' => 450.00 },
  534. { 'sku' => 'BL4438H', 'quantity' => 1,
  535. 'description' => 'Super Hoop', 'price' => 2392.00 } ],
  536. 'tax' => 251.42, 'total' => 4443.52,
  537. 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n" }, <<EOY
  538. invoice: 34843
  539. date : 2001-01-23
  540. bill-to: &id001
  541. given : Chris
  542. family : !str Dumars
  543. address:
  544. lines: |
  545. 458 Walkman Dr.
  546. Suite #292
  547. city : Royal Oak
  548. state : MI
  549. postal : 48046
  550. ship-to: *id001
  551. product:
  552. - !map
  553. sku : BL394D
  554. quantity : 4
  555. description : Basketball
  556. price : 450.00
  557. - sku : BL4438H
  558. quantity : 1
  559. description : Super Hoop
  560. price : 2392.00
  561. tax : 251.42
  562. total: 4443.52
  563. comments: >
  564. Late afternoon is best.
  565. Backup contact is Nancy
  566. Billsmer @ 338-4338.
  567. EOY
  568. )
  569. end
  570. def test_spec_log_file
  571. doc_ct = 0
  572. YAML::load_documents( <<EOY
  573. ---
  574. Time: 2001-11-23 15:01:42 -05:00
  575. User: ed
  576. Warning: >
  577. This is an error message
  578. for the log file
  579. ---
  580. Time: 2001-11-23 15:02:31 -05:00
  581. User: ed
  582. Warning: >
  583. A slightly different error
  584. message.
  585. ---
  586. Date: 2001-11-23 15:03:17 -05:00
  587. User: ed
  588. Fatal: >
  589. Unknown variable "bar"
  590. Stack:
  591. - file: TopClass.py
  592. line: 23
  593. code: |
  594. x = MoreObject("345\\n")
  595. - file: MoreClass.py
  596. line: 58
  597. code: |-
  598. foo = bar
  599. EOY
  600. ) { |doc|
  601. case doc_ct
  602. when 0
  603. assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 01, 42, 00, "-05:00" ),
  604. 'User' => 'ed', 'Warning' => "This is an error message for the log file\n" } )
  605. when 1
  606. assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 02, 31, 00, "-05:00" ),
  607. 'User' => 'ed', 'Warning' => "A slightly different error message.\n" } )
  608. when 2
  609. assert_equal( doc, { 'Date' => mktime( 2001, 11, 23, 15, 03, 17, 00, "-05:00" ),
  610. 'User' => 'ed', 'Fatal' => "Unknown variable \"bar\"\n",
  611. 'Stack' => [
  612. { 'file' => 'TopClass.py', 'line' => 23, 'code' => "x = MoreObject(\"345\\n\")\n" },
  613. { 'file' => 'MoreClass.py', 'line' => 58, 'code' => "foo = bar" } ] } )
  614. end
  615. doc_ct += 1
  616. }
  617. assert_equal( doc_ct, 3 )
  618. end
  619. def test_spec_root_fold
  620. y = YAML::load( <<EOY
  621. --- >
  622. This YAML stream contains a single text value.
  623. The next stream is a log file - a sequence of
  624. log entries. Adding an entry to the log is a
  625. simple matter of appending it at the end.
  626. EOY
  627. )
  628. assert_equal( y, "This YAML stream contains a single text value. The next stream is a log file - a sequence of log entries. Adding an entry to the log is a simple matter of appending it at the end.\n" )
  629. end
  630. def test_spec_root_mapping
  631. y = YAML::load( <<EOY
  632. # This stream is an example of a top-level mapping.
  633. invoice : 34843
  634. date : 2001-01-23
  635. total : 4443.52
  636. EOY
  637. )
  638. assert_equal( y, { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), 'total' => 4443.52 } )
  639. end
  640. def test_spec_oneline_docs
  641. doc_ct = 0
  642. YAML::load_documents( <<EOY
  643. # The following is a sequence of three documents.
  644. # The first contains an empty mapping, the second
  645. # an empty sequence, and the last an empty string.
  646. --- {}
  647. --- [ ]
  648. --- ''
  649. EOY
  650. ) { |doc|
  651. case doc_ct
  652. when 0
  653. assert_equal( doc, {} )
  654. when 1
  655. assert_equal( doc, [] )
  656. when 2
  657. assert_equal( doc, '' )
  658. end
  659. doc_ct += 1
  660. }
  661. assert_equal( doc_ct, 3 )
  662. end
  663. def test_spec_domain_prefix
  664. customer_proc = proc { |type, val|
  665. if Hash === val
  666. scheme, domain, type = type.split( ':', 3 )
  667. val['type'] = "domain #{type}"
  668. val
  669. else
  670. raise ArgumentError, "Not a Hash in domain.tld,2002/invoice: " + val.inspect
  671. end
  672. }
  673. YAML.add_domain_type( "domain.tld,2002", 'invoice', &customer_proc )
  674. YAML.add_domain_type( "domain.tld,2002", 'customer', &customer_proc )
  675. assert_parse_only( { "invoice"=> { "customers"=> [ { "given"=>"Chris", "type"=>"domain customer", "family"=>"Dumars" } ], "type"=>"domain invoice" } }, <<EOY
  676. # 'http://domain.tld,2002/invoice' is some type family.
  677. invoice: !domain.tld,2002/^invoice
  678. # 'seq' is shorthand for 'http://yaml.org/seq'.
  679. # This does not effect '^customer' below
  680. # because it is does not specify a prefix.
  681. customers: !seq
  682. # '^customer' is shorthand for the full
  683. # notation 'http://domain.tld,2002/customer'.
  684. - !^customer
  685. given : Chris
  686. family : Dumars
  687. EOY
  688. )
  689. end
  690. def test_spec_throwaway
  691. assert_parse_only(
  692. {"this"=>"contains three lines of text.\nThe third one starts with a\n# character. This isn't a comment.\n"}, <<EOY
  693. ### These are four throwaway comment ###
  694. ### lines (the second line is empty). ###
  695. this: | # Comments may trail lines.
  696. contains three lines of text.
  697. The third one starts with a
  698. # character. This isn't a comment.
  699. # These are three throwaway comment
  700. # lines (the first line is empty).
  701. EOY
  702. )
  703. end
  704. def test_spec_force_implicit
  705. # Force implicit
  706. assert_parse_only(
  707. { 'integer' => 12, 'also int' => 12, 'string' => '12' }, <<EOY
  708. integer: 12
  709. also int: ! "12"
  710. string: !str 12
  711. EOY
  712. )
  713. end
  714. def test_spec_private_types
  715. doc_ct = 0
  716. YAML::parse_documents( <<EOY
  717. # Private types are per-document.
  718. ---
  719. pool: !!ball
  720. number: 8
  721. color: black
  722. ---
  723. bearing: !!ball
  724. material: steel
  725. EOY
  726. ) { |doc|
  727. case doc_ct
  728. when 0
  729. assert_equal( doc['pool'].type_id, 'x-private:ball' )
  730. assert_equal( doc['pool'].transform.value, { 'number' => 8, 'color' => 'black' } )
  731. when 1
  732. assert_equal( doc['bearing'].type_id, 'x-private:ball' )
  733. assert_equal( doc['bearing'].transform.value, { 'material' => 'steel' } )
  734. end
  735. doc_ct += 1
  736. }
  737. assert_equal( doc_ct, 2 )
  738. end
  739. def test_spec_url_escaping
  740. YAML.add_domain_type( "domain.tld,2002", "type0" ) { |type, val|
  741. "ONE: #{val}"
  742. }
  743. YAML.add_domain_type( "domain.tld,2002", "type%30" ) { |type, val|
  744. "TWO: #{val}"
  745. }
  746. assert_parse_only(
  747. { 'same' => [ 'ONE: value', 'ONE: value' ], 'different' => [ 'TWO: value' ] }, <<EOY
  748. same:
  749. - !domain.tld,2002/type\\x30 value
  750. - !domain.tld,2002/type0 value
  751. different: # As far as the YAML parser is concerned
  752. - !domain.tld,2002/type%30 value
  753. EOY
  754. )
  755. end
  756. def test_spec_override_anchor
  757. # Override anchor
  758. a001 = "The alias node below is a repeated use of this value.\n"
  759. assert_parse_only(
  760. { 'anchor' => 'This scalar has an anchor.', 'override' => a001, 'alias' => a001 }, <<EOY
  761. anchor : &A001 This scalar has an anchor.
  762. override : &A001 >
  763. The alias node below is a
  764. repeated use of this value.
  765. alias : *A001
  766. EOY
  767. )
  768. end
  769. def test_spec_explicit_families
  770. YAML.add_domain_type( "somewhere.com,2002", 'type' ) { |type, val|
  771. "SOMEWHERE: #{val}"
  772. }
  773. assert_parse_only(
  774. { 'not-date' => '2002-04-28', 'picture' => "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236i^\020' \202\n\001\000;", 'hmm' => "SOMEWHERE: family above is short for\nhttp://somewhere.com/type\n" }, <<EOY
  775. not-date: !str 2002-04-28
  776. picture: !binary |
  777. R0lGODlhDAAMAIQAAP//9/X
  778. 17unp5WZmZgAAAOfn515eXv
  779. Pz7Y6OjuDg4J+fn5OTk6enp
  780. 56enmleECcgggoBADs=
  781. hmm: !somewhere.com,2002/type |
  782. family above is short for
  783. http://somewhere.com/type
  784. EOY
  785. )
  786. end
  787. def test_spec_application_family
  788. # Testing the clarkevans.com graphs
  789. YAML.add_domain_type( "clarkevans.com,2002", 'graph/shape' ) { |type, val|
  790. if Array === val
  791. val << "Shape Container"
  792. val
  793. else
  794. raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect
  795. end
  796. }
  797. one_shape_proc = Proc.new { |type, val|
  798. if Hash === val
  799. type = type.split( /:/ )
  800. val['TYPE'] = "Shape: #{type[2]}"
  801. val
  802. else
  803. raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect
  804. end
  805. }
  806. YAML.add_domain_type( "clarkevans.com,2002", 'graph/circle', &one_shape_proc )
  807. YAML.add_domain_type( "clarkevans.com,2002", 'graph/line', &one_shape_proc )
  808. YAML.add_domain_type( "clarkevans.com,2002", 'graph/text', &one_shape_proc )
  809. assert_parse_only(
  810. [[{"radius"=>7, "center"=>{"x"=>73, "y"=>129}, "TYPE"=>"Shape: graph/circle"}, {"finish"=>{"x"=>89, "y"=>102}, "TYPE"=>"Shape: graph/line", "start"=>{"x"=>73, "y"=>129}}, {"TYPE"=>"Shape: graph/text", "value"=>"Pretty vector drawing.", "start"=>{"x"=>73, "y"=>129}, "color"=>16772795}, "Shape Container"]], <<EOY
  811. - !clarkevans.com,2002/graph/^shape
  812. - !^circle
  813. center: &ORIGIN {x: 73, y: 129}
  814. radius: 7
  815. - !^line # !clarkevans.com,2002/graph/line
  816. start: *ORIGIN
  817. finish: { x: 89, y: 102 }
  818. - !^text
  819. start: *ORIGIN
  820. color: 0xFFEEBB
  821. value: Pretty vector drawing.
  822. EOY
  823. )
  824. end
  825. def test_spec_float_explicit
  826. assert_parse_only(
  827. [ 10.0, 10.0, 10.0, 10.0 ], <<EOY
  828. # All entries in the sequence
  829. # have the same type and value.
  830. - 10.0
  831. - !float 10
  832. - !yaml.org,2002/^float '10'
  833. - !yaml.org,2002/float "\\
  834. 1\\
  835. 0"
  836. EOY
  837. )
  838. end
  839. def test_spec_builtin_seq
  840. # Assortment of sequences
  841. assert_parse_only(
  842. { 'empty' => [], 'in-line' => [ 'one', 'two', 'three', 'four', 'five' ],
  843. 'nested' => [ 'First item in top sequence', [ 'Subordinate sequence entry' ],
  844. "A multi-line sequence entry\n", 'Sixth item in top sequence' ] }, <<EOY
  845. empty: []
  846. in-line: [ one, two, three # May span lines,
  847. , four, # indentation is
  848. five ] # mostly ignored.
  849. nested:
  850. - First item in top sequence
  851. -
  852. - Subordinate sequence entry
  853. - >
  854. A multi-line
  855. sequence entry
  856. - Sixth item in top sequence
  857. EOY
  858. )
  859. end
  860. def test_spec_builtin_map
  861. # Assortment of mappings
  862. assert_parse_only(
  863. { 'empty' => {}, 'in-line' => { 'one' => 1, 'two' => 2 },
  864. 'spanning' => { 'one' => 1, 'two' => 2 },
  865. 'nested' => { 'first' => 'First entry', 'second' =>
  866. { 'key' => 'Subordinate mapping' }, 'third' =>
  867. [ 'Subordinate sequence', {}, 'Previous mapping is empty.',
  868. { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' },
  869. 'The previous entry is equal to the following one.',
  870. { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' } ],
  871. 12.0 => 'This key is a float.', "?\n" => 'This key had to be protected.',
  872. "\a" => 'This key had to be escaped.',
  873. "This is a multi-line folded key\n" => "Whose value is also multi-line.\n",
  874. [ 'This key', 'is a sequence' ] => [ 'With a sequence value.' ] } }, <<EOY
  875. empty: {}
  876. in-line: { one: 1, two: 2 }
  877. spanning: { one: 1,
  878. two: 2 }
  879. nested:
  880. first : First entry
  881. second:
  882. key: Subordinate mapping
  883. third:
  884. - Subordinate sequence
  885. - { }
  886. - Previous mapping is empty.
  887. - A key: value pair in a sequence.
  888. A second: key:value pair.
  889. - The previous entry is equal to the following one.
  890. -
  891. A key: value pair in a sequence.
  892. A second: key:value pair.
  893. !float 12 : This key is a float.
  894. ? >
  895. ?
  896. : This key had to be protected.
  897. "\\a" : This key had to be escaped.
  898. ? >
  899. This is a
  900. multi-line
  901. folded key
  902. : >
  903. Whose value is
  904. also multi-line.
  905. ?
  906. - This key
  907. - is a sequence
  908. :
  909. - With a sequence value.
  910. # The following parses correctly,
  911. # but Ruby 1.6.* fails the comparison!
  912. # ?
  913. # This: key
  914. # is a: mapping
  915. # :
  916. # with a: mapping value.
  917. EOY
  918. )
  919. end
  920. def test_spec_builtin_literal_blocks
  921. # Assortment of literal scalar blocks
  922. assert_parse_only(
  923. {"both are equal to"=>" This has no newline.", "is equal to"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n", "also written as"=>" This has no newline.", "indented and chomped"=>" This has no newline.", "empty"=>"", "literal"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n"}, <<EOY
  924. empty: |
  925. literal: |
  926. The \\ ' " characters may be
  927. freely used. Leading white
  928. space is significant.
  929. Line breaks are significant.
  930. Thus this value contains one
  931. empty line and ends with a
  932. single line break, but does
  933. not start with one.
  934. is equal to: "The \\ ' \\" characters may \\
  935. be\\nfreely used. Leading white\\n space \\
  936. is significant.\\n\\nLine breaks are \\
  937. significant.\\nThus this value contains \\
  938. one\\nempty line and ends with a\\nsingle \\
  939. line break, but does\\nnot start with one.\\n"
  940. # Comments may follow a nested
  941. # scalar value. They must be
  942. # less indented.
  943. # Modifiers may be combined in any order.
  944. indented and chomped: |2-
  945. This has no newline.
  946. also written as: |-2
  947. This has no newline.
  948. both are equal to: " This has no newline."
  949. EOY
  950. )
  951. str1 = "This has one newline.\n"
  952. str2 = "This has no newline."
  953. str3 = "This has two newlines.\n\n"
  954. assert_parse_only(
  955. { 'clipped' => str1, 'same as "clipped" above' => str1,
  956. 'stripped' => str2, 'same as "stripped" above' => str2,
  957. 'kept' => str3, 'same as "kept" above' => str3 }, <<EOY
  958. clipped: |
  959. This has one newline.
  960. same as "clipped" above: "This has one newline.\\n"
  961. stripped: |-
  962. This has no newline.
  963. same as "stripped" above: "This has no newline."
  964. kept: |+
  965. This has two newlines.
  966. same as "kept" above: "This has two newlines.\\n\\n"
  967. EOY
  968. )
  969. end
  970. def test_spec_span_single_quote
  971. assert_parse_only( {"third"=>"a single quote ' must be escaped.", "second"=>"! : \\ etc. can be used freely.", "is same as"=>"this contains six spaces\nand one line break", "empty"=>"", "span"=>"this contains six spaces\nand one line break"}, <<EOY
  972. empty: ''
  973. second: '! : \\ etc. can be used freely.'
  974. third: 'a single quote '' must be escaped.'
  975. span: 'this contains
  976. six spaces
  977. and one
  978. line break'
  979. is same as: "this contains six spaces\\nand one line break"
  980. EOY
  981. )
  982. end
  983. def test_spec_span_double_quote
  984. assert_parse_only( {"is equal to"=>"this contains four spaces", "third"=>"a \" or a \\ must be escaped.", "second"=>"! : etc. can be used freely.", "empty"=>"", "fourth"=>"this value ends with an LF.\n", "span"=>"this contains four spaces"}, <<EOY
  985. empty: ""
  986. second: "! : etc. can be used freely."
  987. third: "a \\\" or a \\\\ must be escaped."
  988. fourth: "this value ends with an LF.\\n"
  989. span: "this contains
  990. four \\
  991. spaces"
  992. is equal to: "this contains four spaces"
  993. EOY
  994. )
  995. end
  996. def test_spec_builtin_time
  997. # Time
  998. assert_parse_only(
  999. { "space separated" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ),
  1000. "canonical" => mktime( 2001, 12, 15, 2, 59, 43, ".10" ),
  1001. "date (noon UTC)" => Date.new( 2002, 12, 14),
  1002. "valid iso8601" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ) }, <<EOY
  1003. canonical: 2001-12-15T02:59:43.1Z
  1004. valid iso8601: 2001-12-14t21:59:43.10-05:00
  1005. space separated: 2001-12-14 21:59:43.10 -05:00
  1006. date (noon UTC): 2002-12-14
  1007. EOY
  1008. )
  1009. end
  1010. def test_spec_builtin_binary
  1011. arrow_gif = "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236iiiccc\243\243\243\204\204\204\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371!\376\016Made with GIMP\000,\000\000\000\000\f\000\f\000\000\005, \216\2010\236\343@\024\350i\020\304\321\212\010\034\317\200M$z\357\3770\205p\270\2601f\r\e\316\001\303\001\036\020' \202\n\001\000;"
  1012. assert_parse_only(
  1013. { 'canonical' => arrow_gif, 'base64' => arrow_gif,
  1014. 'description' => "The binary value above is a tiny arrow encoded as a gif image.\n" }, <<EOY
  1015. canonical: !binary "\\
  1016. R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOf\\
  1017. n515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaW\\
  1018. NjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++\\
  1019. f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUg\\
  1020. d2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuN\\
  1021. AFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84Bww\\
  1022. EeECcgggoBADs="
  1023. base64: !binary |
  1024. R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOf
  1025. n515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaW
  1026. NjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++
  1027. f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUg
  1028. d2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuN
  1029. AFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84Bww
  1030. EeECcgggoBADs=
  1031. description: >
  1032. The binary value above is a tiny arrow
  1033. encoded as a gif image.
  1034. EOY
  1035. )
  1036. end
  1037. def test_ruby_regexp
  1038. # Test Ruby regular expressions
  1039. assert_to_yaml(
  1040. { 'simple' => /a.b/, 'complex' => %r'\A"((?:[^"]|\")+)"',
  1041. 'case-insensitive' => /George McFly/i }, <<EOY
  1042. case-insensitive: !ruby/regexp "/George McFly/i"
  1043. complex: !ruby/regexp "/\\\\A\\"((?:[^\\"]|\\\\\\")+)\\"/"
  1044. simple: !ruby/regexp "/a.b/"
  1045. EOY
  1046. )
  1047. end
  1048. #
  1049. # Test of Ranges
  1050. #
  1051. def test_ranges
  1052. # Simple numeric
  1053. assert_to_yaml( 1..3, <<EOY )
  1054. --- !ruby/range 1..3
  1055. EOY
  1056. # Simple alphabetic
  1057. assert_to_yaml( 'a'..'z', <<EOY )
  1058. --- !ruby/range a..z
  1059. EOY
  1060. # Float
  1061. assert_to_yaml( 10.5...30.3, <<EOY )
  1062. --- !ruby/range 10.5...30.3
  1063. EOY
  1064. end
  1065. def test_ruby_struct
  1066. # Ruby structures
  1067. book_struct = Struct::new( "BookStruct", :author, :title, :year, :isbn )
  1068. assert_to_yaml(
  1069. [ book_struct.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ),
  1070. book_struct.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002,
  1071. book_struct.new( "This should be the ISBN", "but I have another struct here", 2002, "None" )
  1072. ) ], <<EOY
  1073. - !ruby/struct:BookStruct
  1074. author: Yukihiro Matsumoto
  1075. title: Ruby in a Nutshell
  1076. year: 2002
  1077. isbn: 0-596-00214-9
  1078. - !ruby/struct:BookStruct
  1079. author:
  1080. - Dave Thomas
  1081. - Andy Hunt
  1082. title: The Pickaxe
  1083. year: 2002
  1084. isbn: !ruby/struct:BookStruct
  1085. author: This should be the ISBN
  1086. title: but I have another struct here
  1087. year: 2002
  1088. isbn: None
  1089. EOY
  1090. )
  1091. assert_to_yaml( YAML_Tests::StructTest.new( 123 ), <<EOY )
  1092. --- !ruby/struct:YAML_Tests::StructTest
  1093. c: 123
  1094. EOY
  1095. end
  1096. def test_ruby_rational
  1097. assert_to_yaml( Rational(1, 2), <<EOY )
  1098. --- !ruby/object:Rational
  1099. numerator: 1
  1100. denominator: 2
  1101. EOY
  1102. # Read YAML dumped by the ruby 1.8.3.
  1103. assert_to_yaml( Rational(1, 2), "!ruby/object:Rational 1/2\n" )
  1104. assert_raise( ArgumentError ) { YAML.load("!ruby/object:Rational INVALID/RATIONAL\n") }
  1105. end
  1106. def test_ruby_complex
  1107. assert_to_yaml( Complex(3, 4), <<EOY )
  1108. --- !ruby/object:Complex
  1109. image: 4
  1110. real: 3
  1111. EOY
  1112. # Read YAML dumped by the ruby 1.8.3.
  1113. assert_to_yaml( Complex(3, 4), "!ruby/object:Complex 3+4i\n" )
  1114. assert_raise( ArgumentError ) { YAML.load("!ruby/object:Complex INVALID+COMPLEXi\n") }
  1115. end
  1116. def test_emitting_indicators
  1117. assert_to_yaml( "Hi, from Object 1. You passed: please, pretty please", <<EOY
  1118. --- "Hi, from Object 1. You passed: please, pretty please"
  1119. EOY
  1120. )
  1121. end
  1122. #
  1123. # Test the YAML::Stream class -- INACTIVE at the moment
  1124. #
  1125. def test_document
  1126. y = YAML::Stream.new( :Indent => 2, :UseVersion => 0 )
  1127. y.add(
  1128. { 'hi' => 'hello', 'map' =>
  1129. { 'good' => 'two' },
  1130. 'time' => Time.now,
  1131. 'try' => /^po(.*)$/,
  1132. 'bye' => 'goodbye'
  1133. }
  1134. )
  1135. y.add( { 'po' => 'nil', 'oper' => 90 } )
  1136. y.add( { 'hi' => 'wow!', 'bye' => 'wow!' } )
  1137. y.add( { [ 'Red Socks', 'Boston' ] => [ 'One', 'Two', 'Three' ] } )
  1138. y.add( [ true, false, false ] )
  1139. end
  1140. #
  1141. # Test YPath choices parsing
  1142. #
  1143. def test_ypath_parsing
  1144. assert_path_segments( "/*/((one|three)/name|place)|//place",
  1145. [ ["*", "one", "name"],
  1146. ["*", "three", "name"],
  1147. ["*", "place"],
  1148. ["/", "place"] ]
  1149. )
  1150. end
  1151. #
  1152. # Tests from Tanaka Akira on [ruby-core]
  1153. #
  1154. def test_akira
  1155. # Commas in plain scalars [ruby-core:1066]
  1156. assert_to_yaml(
  1157. {"A"=>"A,","B"=>"B"}, <<EOY
  1158. A: "A,"
  1159. B: B
  1160. EOY
  1161. )
  1162. # Double-quoted keys [ruby-core:1069]
  1163. assert_to_yaml(
  1164. {"1"=>2, "2"=>3}, <<EOY
  1165. '1': 2
  1166. "2": 3
  1167. EOY
  1168. )
  1169. # Anchored mapping [ruby-core:1071]
  1170. assert_to_yaml(
  1171. [{"a"=>"b"}] * 2, <<EOY
  1172. - &id001
  1173. a: b
  1174. - *id001
  1175. EOY
  1176. )
  1177. # Stress test [ruby-core:1071]
  1178. # a = []; 1000.times { a << {"a"=>"b", "c"=>"d"} }
  1179. # YAML::load( a.to_yaml )
  1180. end
  1181. #
  1182. # Test Time.now cycle
  1183. #
  1184. def test_time_now_cycle
  1185. #
  1186. # From Minero Aoki [ruby-core:2305]
  1187. #
  1188. require 'yaml'
  1189. t = Time.now
  1190. t = Time.at(t.tv_sec, t.tv_usec)
  1191. 5.times do
  1192. assert_cycle(t)
  1193. end
  1194. end
  1195. #
  1196. # Test Range cycle
  1197. #
  1198. def test_range_cycle
  1199. #
  1200. # From Minero Aoki [ruby-core:02306]
  1201. #
  1202. assert_cycle("a".."z")
  1203. #
  1204. # From Nobu Nakada [ruby-core:02311]
  1205. #
  1206. assert_cycle(0..1)
  1207. assert_cycle(1.0e20 .. 2.0e20)
  1208. assert_cycle("0".."1")
  1209. assert_cycle(".."..."...")
  1210. assert_cycle(".rb"..".pl")
  1211. assert_cycle(".rb"...".pl")
  1212. assert_cycle('"'...".")
  1213. assert_cycle("'"...".")
  1214. end
  1215. #
  1216. # Circular references
  1217. #
  1218. def test_circular_references
  1219. a = []; a[0] = a; a[1] = a
  1220. inspect_str = "[[...], [...]]"
  1221. assert_equal( inspect_str, YAML::load( a.to_yaml ).inspect )
  1222. end
  1223. #
  1224. # Test Symbol cycle
  1225. #
  1226. def test_symbol_cycle
  1227. #
  1228. # From Aaron Schrab [ruby-Bugs:2535]
  1229. #
  1230. assert_cycle(:"^foo")
  1231. end
  1232. #
  1233. # Test Numeric cycle
  1234. #
  1235. class NumericTest < Numeric
  1236. def initialize(value)
  1237. @value = value
  1238. end
  1239. def ==(other)
  1240. @value == other.instance_eval{ @value }
  1241. end
  1242. end
  1243. def test_numeric_cycle
  1244. assert_cycle(1) # Fixnum
  1245. assert_cycle(111111111111111111111111111111111) # Bignum
  1246. assert_cycle(NumericTest.new(3)) # Subclass of Numeric
  1247. end
  1248. #
  1249. # Test empty map/seq in map cycle
  1250. #
  1251. def test_empty_map_key
  1252. #
  1253. # empty seq as key
  1254. #
  1255. o = YAML.load({[]=>""}.to_yaml)
  1256. assert_equal(Hash, o.class)
  1257. assert_equal([[]], o.keys)
  1258. #
  1259. # empty map as key
  1260. #
  1261. o = YAML.load({{}=>""}.to_yaml)
  1262. assert_equal(Hash, o.class)
  1263. assert_equal([{}], o.keys)
  1264. end
  1265. #
  1266. # contributed by riley lynch [ruby-Bugs-8548]
  1267. #
  1268. def test_object_id_collision
  1269. omap = YAML::Omap.new
  1270. 1000.times { |i| omap["key_#{i}"] = { "value" => i } }
  1271. raise "id collision in ordered map" if omap.to_yaml =~ /id\d+/
  1272. end
  1273. def test_date_out_of_range
  1274. assert_nothing_raised{YAML::load('1900-01-01T00:00:00+00:00')}
  1275. end
  1276. def test_normal_exit
  1277. YAML.load("2000-01-01 00:00:00.#{"0"*1000} +00:00\n")
  1278. # '[ruby-core:13735]'
  1279. end
  1280. end
  1281. end