# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*-
# vim:sw=4:ts=4
# $Id$
#
require 'test/unit'
require 'yaml'
# [ruby-core:01946]
module YAML_Tests
StructTest = Struct::new( :c )
end
class YAML_Unit_Tests < Test::Unit::TestCase
#
# Convert between YAML and the object to verify correct parsing and
# emitting
#
def assert_to_yaml( obj, yaml )
assert_equal( obj, YAML::load( yaml ) )
assert_equal( obj, YAML::parse( yaml ).transform )
assert_equal( obj, YAML::load( obj.to_yaml ) )
assert_equal( obj, YAML::parse( obj.to_yaml ).transform )
assert_equal( obj, YAML::load(
obj.to_yaml( :UseVersion => true, :UseHeader => true, :SortKeys => true )
) )
end
#
# Test parser only
#
def assert_parse_only( obj, yaml )
assert_equal( obj, YAML::load( yaml ) )
assert_equal( obj, YAML::parse( yaml ).transform )
end
def assert_cycle( obj )
assert_equal( obj, YAML::load( obj.to_yaml ) )
end
def assert_path_segments( path, segments )
YAML::YPath.each_path( path ) { |choice|
assert_equal( choice.segments, segments.shift )
}
assert_equal( segments.length, 0, "Some segments leftover: #{ segments.inspect }" )
end
#
# Make a time with the time zone
#
def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
usec = usec.to_s.to_f * 1000000
val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
if zone != "Z"
hour = zone[0,3].to_i * 3600
min = zone[3,2].to_i * 60
ofs = (hour + min)
val = Time.at( val.to_f - ofs )
end
return val
end
#
# Tests modified from 00basic.t in YAML.pm
#
def test_basic_map
# Simple map
assert_parse_only(
{ 'one' => 'foo', 'three' => 'baz', 'two' => 'bar' }, <<EOY
one: foo
two: bar
three: baz
EOY
)
end
def test_basic_strings
# Common string types
assert_cycle("x")
assert_cycle(":x")
assert_cycle(":")
assert_parse_only(
{ 1 => 'simple string', 2 => 42, 3 => '1 Single Quoted String',
4 => 'YAML\'s Double "Quoted" String', 5 => "A block\n with several\n lines.\n",
6 => "A \"chomped\" block", 7 => "A folded\n string\n", 8 => ": started string" },
<<EOY
1: simple string
2: 42
3: '1 Single Quoted String'
4: "YAML's Double \\\"Quoted\\\" String"
5: |
A block
with several
lines.
6: |-
A "chomped" block
7: >
A
folded
string
8: ": started string"
EOY
)
end
#
# Test the specification examples
# - Many examples have been changes because of whitespace problems that
# caused the two to be inequivalent, or keys to be sorted wrong
#
def test_spec_simple_implicit_sequence
# Simple implicit sequence
assert_to_yaml(
[ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ], <<EOY
- Mark McGwire
- Sammy Sosa
- Ken Griffey
EOY
)
end
def test_spec_simple_implicit_map
# Simple implicit map
assert_to_yaml(
{ 'hr' => 65, 'avg' => 0.278, 'rbi' => 147 }, <<EOY
avg: 0.278
hr: 65
rbi: 147
EOY
)
end
def test_spec_simple_map_with_nested_sequences
# Simple mapping with nested sequences
assert_to_yaml(
{ 'american' =>
[ 'Boston Red Sox', 'Detroit Tigers', 'New York Yankees' ],
'national' =>
[ 'New York Mets', 'Chicago Cubs', 'Atlanta Braves' ] }, <<EOY
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
EOY
)
end
def test_spec_simple_sequence_with_nested_map
# Simple sequence with nested map
assert_to_yaml(
[
{'name' => 'Mark McGwire', 'hr' => 65, 'avg' => 0.278},
{'name' => 'Sammy Sosa', 'hr' => 63, 'avg' => 0.288}
], <<EOY
-
avg: 0.278
hr: 65
name: Mark McGwire
-
avg: 0.288
hr: 63
name: Sammy Sosa
EOY
)
end
def test_spec_sequence_of_sequences
# Simple sequence with inline sequences
assert_parse_only(
[
[ 'name', 'hr', 'avg' ],
[ 'Mark McGwire', 65, 0.278 ],
[ 'Sammy Sosa', 63, 0.288 ]
], <<EOY
- [ name , hr , avg ]
- [ Mark McGwire , 65 , 0.278 ]
- [ Sammy Sosa , 63 , 0.288 ]
EOY
)
end
def test_spec_mapping_of_mappings
# Simple map with inline maps
assert_parse_only(
{ 'Mark McGwire' =>
{ 'hr' => 65, 'avg' => 0.278 },
'Sammy Sosa' =>
{ 'hr' => 63, 'avg' => 0.288 }
}, <<EOY
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {hr: 63,
avg: 0.288}
|