km-psych 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. data/README.rdoc +129 -0
  2. data/ext/psych/emitter.c +488 -0
  3. data/ext/psych/emitter.h +8 -0
  4. data/ext/psych/extconf.rb +22 -0
  5. data/ext/psych/parser.c +349 -0
  6. data/ext/psych/parser.h +6 -0
  7. data/ext/psych/psych.c +34 -0
  8. data/ext/psych/psych.h +20 -0
  9. data/ext/psych/to_ruby.c +41 -0
  10. data/ext/psych/to_ruby.h +8 -0
  11. data/ext/psych/yaml_tree.c +24 -0
  12. data/ext/psych/yaml_tree.h +8 -0
  13. data/lib/km-psych.rb +244 -0
  14. data/lib/psych/coder.rb +86 -0
  15. data/lib/psych/core_ext.rb +38 -0
  16. data/lib/psych/deprecated.rb +82 -0
  17. data/lib/psych/handler.rb +221 -0
  18. data/lib/psych/json.rb +6 -0
  19. data/lib/psych/json/stream.rb +32 -0
  20. data/lib/psych/json/tree_builder.rb +32 -0
  21. data/lib/psych/nodes.rb +77 -0
  22. data/lib/psych/nodes/alias.rb +18 -0
  23. data/lib/psych/nodes/document.rb +60 -0
  24. data/lib/psych/nodes/mapping.rb +56 -0
  25. data/lib/psych/nodes/node.rb +42 -0
  26. data/lib/psych/nodes/scalar.rb +67 -0
  27. data/lib/psych/nodes/sequence.rb +81 -0
  28. data/lib/psych/nodes/stream.rb +37 -0
  29. data/lib/psych/omap.rb +4 -0
  30. data/lib/psych/parser.rb +44 -0
  31. data/lib/psych/scalar_scanner.rb +105 -0
  32. data/lib/psych/set.rb +4 -0
  33. data/lib/psych/stream.rb +53 -0
  34. data/lib/psych/tree_builder.rb +94 -0
  35. data/lib/psych/visitors.rb +5 -0
  36. data/lib/psych/visitors/emitter.rb +41 -0
  37. data/lib/psych/visitors/json_tree.rb +14 -0
  38. data/lib/psych/visitors/to_ruby.rb +263 -0
  39. data/lib/psych/visitors/visitor.rb +27 -0
  40. data/lib/psych/visitors/yaml_tree.rb +342 -0
  41. data/test/psych/helper.rb +63 -0
  42. data/test/psych/json/test_stream.rb +75 -0
  43. data/test/psych/test_alias_and_anchor.rb +26 -0
  44. data/test/psych/test_array.rb +19 -0
  45. data/test/psych/test_boolean.rb +36 -0
  46. data/test/psych/test_class.rb +17 -0
  47. data/test/psych/test_coder.rb +169 -0
  48. data/test/psych/test_date_time.rb +17 -0
  49. data/test/psych/test_deprecated.rb +210 -0
  50. data/test/psych/test_document.rb +46 -0
  51. data/test/psych/test_emitter.rb +88 -0
  52. data/test/psych/test_encoding.rb +179 -0
  53. data/test/psych/test_engine_manager.rb +57 -0
  54. data/test/psych/test_exception.rb +39 -0
  55. data/test/psych/test_hash.rb +30 -0
  56. data/test/psych/test_json_tree.rb +43 -0
  57. data/test/psych/test_null.rb +19 -0
  58. data/test/psych/test_object.rb +27 -0
  59. data/test/psych/test_omap.rb +68 -0
  60. data/test/psych/test_parser.rb +216 -0
  61. data/test/psych/test_psych.rb +133 -0
  62. data/test/psych/test_scalar.rb +11 -0
  63. data/test/psych/test_scalar_scanner.rb +70 -0
  64. data/test/psych/test_serialize_subclasses.rb +38 -0
  65. data/test/psych/test_set.rb +49 -0
  66. data/test/psych/test_stream.rb +49 -0
  67. data/test/psych/test_string.rb +49 -0
  68. data/test/psych/test_struct.rb +51 -0
  69. data/test/psych/test_symbol.rb +17 -0
  70. data/test/psych/test_to_yaml_properties.rb +63 -0
  71. data/test/psych/test_tree_builder.rb +79 -0
  72. data/test/psych/test_yaml.rb +1251 -0
  73. data/test/psych/visitors/test_emitter.rb +124 -0
  74. data/test/psych/visitors/test_to_ruby.rb +325 -0
  75. data/test/psych/visitors/test_yaml_tree.rb +149 -0
  76. metadata +187 -0
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require_relative 'helper'
4
+
5
+ module Psych
6
+ class TestScalar < TestCase
7
+ def test_utf_8
8
+ assert_equal "日本語", Psych.load("--- 日本語")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,70 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestScalarScanner < TestCase
5
+ def test_scan_time
6
+ [ '2001-12-15T02:59:43.1Z',
7
+ '2001-12-14t21:59:43.10-05:00',
8
+ '2001-12-14 21:59:43.10 -5',
9
+ '2010-01-06 00:00:00 -08:00',
10
+ '2001-12-15 2:59:43.10',
11
+ ].each do |time|
12
+ ss = Psych::ScalarScanner.new
13
+ assert_instance_of Time, ss.tokenize(time)
14
+ end
15
+ end
16
+
17
+ attr_reader :ss
18
+
19
+ def setup
20
+ super
21
+ @ss = Psych::ScalarScanner.new
22
+ end
23
+
24
+ def test_scan_date
25
+ date = '1980-12-16'
26
+ token = @ss.tokenize date
27
+ assert_equal 1980, token.year
28
+ assert_equal 12, token.month
29
+ assert_equal 16, token.day
30
+ end
31
+
32
+ def test_scan_inf
33
+ assert_equal(1 / 0.0, ss.tokenize('.inf'))
34
+ end
35
+
36
+ def test_scan_minus_inf
37
+ assert_equal(-1 / 0.0, ss.tokenize('-.inf'))
38
+ end
39
+
40
+ def test_scan_nan
41
+ assert ss.tokenize('.nan').nan?
42
+ end
43
+
44
+ def test_scan_null
45
+ assert_equal nil, ss.tokenize('null')
46
+ assert_equal nil, ss.tokenize('~')
47
+ assert_equal nil, ss.tokenize('')
48
+ end
49
+
50
+ def test_scan_symbol
51
+ assert_equal :foo, ss.tokenize(':foo')
52
+ end
53
+
54
+ def test_scan_sexagesimal_float
55
+ assert_equal 685230.15, ss.tokenize('190:20:30.15')
56
+ end
57
+
58
+ def test_scan_sexagesimal_int
59
+ assert_equal 685230, ss.tokenize('190:20:30')
60
+ end
61
+
62
+ def test_scan_float
63
+ assert_equal 1.2, ss.tokenize('1.2')
64
+ end
65
+
66
+ def test_scan_true
67
+ assert_equal true, ss.tokenize('true')
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestSerializeSubclasses < TestCase
5
+ class SomeObject
6
+ def initialize one, two
7
+ @one = one
8
+ @two = two
9
+ end
10
+
11
+ def == other
12
+ @one == other.instance_eval { @one } &&
13
+ @two == other.instance_eval { @two }
14
+ end
15
+ end
16
+
17
+ def test_some_object
18
+ so = SomeObject.new('foo', [1,2,3])
19
+ assert_equal so, Psych.load(Psych.dump(so))
20
+ end
21
+
22
+ class StructSubclass < Struct.new(:foo)
23
+ def initialize foo, bar
24
+ super(foo)
25
+ @bar = bar
26
+ end
27
+
28
+ def == other
29
+ super(other) && @bar == other.instance_eval{ @bar }
30
+ end
31
+ end
32
+
33
+ def test_struct_subclass
34
+ so = StructSubclass.new('foo', [1,2,3])
35
+ assert_equal so, Psych.load(Psych.dump(so))
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestSet < TestCase
5
+ def setup
6
+ super
7
+ @set = Psych::Set.new
8
+ @set['foo'] = 'bar'
9
+ @set['bar'] = 'baz'
10
+ end
11
+
12
+ def test_dump
13
+ assert_match(/!set/, Psych.dump(@set))
14
+ end
15
+
16
+ def test_roundtrip
17
+ assert_cycle(@set)
18
+ end
19
+
20
+ ###
21
+ # FIXME: Syck should also support !!set as shorthand
22
+ def test_load_from_yaml
23
+ loaded = Psych.load(<<-eoyml)
24
+ --- !set
25
+ foo: bar
26
+ bar: baz
27
+ eoyml
28
+ assert_equal(@set, loaded)
29
+ end
30
+
31
+ def test_loaded_class
32
+ assert_instance_of(Psych::Set, Psych.load(Psych.dump(@set)))
33
+ end
34
+
35
+ def test_set_shorthand
36
+ loaded = Psych.load(<<-eoyml)
37
+ --- !!set
38
+ foo: bar
39
+ bar: baz
40
+ eoyml
41
+ assert_instance_of(Psych::Set, loaded)
42
+ end
43
+
44
+ def test_set_self_reference
45
+ @set['self'] = @set
46
+ assert_cycle(@set)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestStream < TestCase
5
+ def test_explicit_documents
6
+ io = StringIO.new
7
+ stream = Psych::Stream.new(io)
8
+ stream.start
9
+ stream.push({ 'foo' => 'bar' })
10
+
11
+ assert !stream.finished?, 'stream not finished'
12
+ stream.finish
13
+ assert stream.finished?, 'stream finished'
14
+
15
+ assert_match(/^---/, io.string)
16
+ assert_match(/\.\.\.$/, io.string)
17
+ end
18
+
19
+ def test_start_takes_block
20
+ io = StringIO.new
21
+ stream = Psych::Stream.new(io)
22
+ stream.start do |emitter|
23
+ emitter.push({ 'foo' => 'bar' })
24
+ end
25
+
26
+ assert stream.finished?, 'stream finished'
27
+ assert_match(/^---/, io.string)
28
+ assert_match(/\.\.\.$/, io.string)
29
+ end
30
+
31
+ def test_no_backreferences
32
+ io = StringIO.new
33
+ stream = Psych::Stream.new(io)
34
+ stream.start do |emitter|
35
+ x = { 'foo' => 'bar' }
36
+ emitter.push x
37
+ emitter.push x
38
+ end
39
+
40
+ assert stream.finished?, 'stream finished'
41
+ assert_match(/^---/, io.string)
42
+ assert_match(/\.\.\.$/, io.string)
43
+ assert_equal 2, io.string.scan('---').length
44
+ assert_equal 2, io.string.scan('...').length
45
+ assert_equal 2, io.string.scan('foo').length
46
+ assert_equal 2, io.string.scan('bar').length
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestString < TestCase
5
+ def test_binary_string_null
6
+ string = "\x00"
7
+ yml = Psych.dump string
8
+ assert_match(/binary/, yml)
9
+ assert_equal string, Psych.load(yml)
10
+ end
11
+
12
+ def test_binary_string
13
+ string = binary_string
14
+ yml = Psych.dump string
15
+ assert_match(/binary/, yml)
16
+ assert_equal string, Psych.load(yml)
17
+ end
18
+
19
+ def test_non_binary_string
20
+ string = binary_string(0.29)
21
+ yml = Psych.dump string
22
+ refute_match(/binary/, yml)
23
+ assert_equal string, Psych.load(yml)
24
+ end
25
+
26
+ def test_string_with_ivars
27
+ food = "is delicious"
28
+ ivar = "on rock and roll"
29
+ food.instance_variable_set(:@we_built_this_city, ivar)
30
+
31
+ str = Psych.load Psych.dump food
32
+ assert_equal ivar, food.instance_variable_get(:@we_built_this_city)
33
+ end
34
+
35
+ def test_binary
36
+ string = [0, 123,22, 44, 9, 32, 34, 39].pack('C*')
37
+ assert_cycle string
38
+ end
39
+
40
+ def binary_string percentage = 0.31, length = 100
41
+ string = ''
42
+ (percentage * length).to_i.times do |i|
43
+ string << "\b"
44
+ end
45
+ string << 'a' * (length - string.length)
46
+ string
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'helper'
2
+
3
+ class PsychStructWithIvar < Struct.new(:foo)
4
+ attr_reader :bar
5
+ def initialize *args
6
+ super
7
+ @bar = 'hello'
8
+ end
9
+ end
10
+
11
+ module Psych
12
+ class TestStruct < TestCase
13
+ class StructSubclass < Struct.new(:foo)
14
+ def initialize foo, bar
15
+ super(foo)
16
+ @bar = bar
17
+ end
18
+ end
19
+
20
+ def test_self_referential_struct
21
+ ss = StructSubclass.new(nil, 'foo')
22
+ ss.foo = ss
23
+
24
+ loaded = Psych.load(Psych.dump(ss))
25
+ assert_instance_of(StructSubclass, loaded.foo)
26
+
27
+ # FIXME: This seems to cause an infinite loop. wtf. Must report a bug
28
+ # in ruby.
29
+ # assert_equal(ss, loaded)
30
+ end
31
+
32
+ def test_roundtrip
33
+ thing = PsychStructWithIvar.new('bar')
34
+ struct = Psych.load(Psych.dump(thing))
35
+
36
+ assert_equal 'hello', struct.bar
37
+ assert_equal 'bar', struct.foo
38
+ end
39
+
40
+ def test_load
41
+ obj = Psych.load(<<-eoyml)
42
+ --- !ruby/struct:PsychStructWithIvar
43
+ :foo: bar
44
+ :@bar: hello
45
+ eoyml
46
+
47
+ assert_equal 'hello', obj.bar
48
+ assert_equal 'bar', obj.foo
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestSymbol < TestCase
5
+ def test_cycle
6
+ assert_cycle :a
7
+ end
8
+
9
+ def test_stringy
10
+ assert_cycle :"1"
11
+ end
12
+
13
+ def test_load_quoted
14
+ assert_equal :"1", Psych.load("--- :'1'\n")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,63 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestToYamlProperties < MiniTest::Unit::TestCase
5
+ class Foo
6
+ attr_accessor :a, :b, :c
7
+ def initialize
8
+ @a = 1
9
+ @b = 2
10
+ @c = 3
11
+ end
12
+
13
+ def to_yaml_properties
14
+ [:@a, :@b]
15
+ end
16
+ end
17
+
18
+ def test_object_dump_yaml_properties
19
+ foo = Psych.load(Psych.dump(Foo.new))
20
+ assert_equal 1, foo.a
21
+ assert_equal 2, foo.b
22
+ assert_nil foo.c
23
+ end
24
+
25
+ class Bar < Struct.new(:foo, :bar)
26
+ attr_reader :baz
27
+ def initialize *args
28
+ super
29
+ @baz = 'hello'
30
+ end
31
+
32
+ def to_yaml_properties
33
+ []
34
+ end
35
+ end
36
+
37
+ def test_struct_dump_yaml_properties
38
+ bar = Psych.load(Psych.dump(Bar.new('a', 'b')))
39
+ assert_equal 'a', bar.foo
40
+ assert_equal 'b', bar.bar
41
+ assert_nil bar.baz
42
+ end
43
+
44
+ def test_string_dump
45
+ string = "okonomiyaki"
46
+ class << string
47
+ def to_yaml_properties
48
+ [:@tastes]
49
+ end
50
+ end
51
+
52
+ string.instance_variable_set(:@tastes, 'delicious')
53
+ v = Psych.load Psych.dump string
54
+ assert_equal 'delicious', v.instance_variable_get(:@tastes)
55
+ end
56
+
57
+ def test_string_load_syck
58
+ str = Psych.load("--- !str \nstr: okonomiyaki\n:@tastes: delicious\n")
59
+ assert_equal 'okonomiyaki', str
60
+ assert_equal 'delicious', str.instance_variable_get(:@tastes)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,79 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestTreeBuilder < TestCase
5
+ def setup
6
+ super
7
+ @parser = Psych::Parser.new TreeBuilder.new
8
+ @parser.parse(<<-eoyml)
9
+ %YAML 1.1
10
+ ---
11
+ - foo
12
+ - {
13
+ bar : &A !!str baz,
14
+ boo : *A
15
+ }
16
+ - *A
17
+ eoyml
18
+ @tree = @parser.handler.root
19
+ end
20
+
21
+ def test_stream
22
+ assert_instance_of Nodes::Stream, @tree
23
+ end
24
+
25
+ def test_documents
26
+ assert_equal 1, @tree.children.length
27
+ assert_instance_of Nodes::Document, @tree.children.first
28
+ doc = @tree.children.first
29
+
30
+ assert_equal [1,1], doc.version
31
+ assert_equal [], doc.tag_directives
32
+ assert_equal false, doc.implicit
33
+ end
34
+
35
+ def test_sequence
36
+ doc = @tree.children.first
37
+ assert_equal 1, doc.children.length
38
+
39
+ seq = doc.children.first
40
+ assert_instance_of Nodes::Sequence, seq
41
+ assert_nil seq.anchor
42
+ assert_nil seq.tag
43
+ assert_equal true, seq.implicit
44
+ assert_equal Nodes::Sequence::BLOCK, seq.style
45
+ end
46
+
47
+ def test_scalar
48
+ doc = @tree.children.first
49
+ seq = doc.children.first
50
+
51
+ assert_equal 3, seq.children.length
52
+ scalar = seq.children.first
53
+ assert_instance_of Nodes::Scalar, scalar
54
+ assert_equal 'foo', scalar.value
55
+ assert_nil scalar.anchor
56
+ assert_nil scalar.tag
57
+ assert_equal true, scalar.plain
58
+ assert_equal false, scalar.quoted
59
+ assert_equal Nodes::Scalar::PLAIN, scalar.style
60
+ end
61
+
62
+ def test_mapping
63
+ doc = @tree.children.first
64
+ seq = doc.children.first
65
+ map = seq.children[1]
66
+
67
+ assert_instance_of Nodes::Mapping, map
68
+ end
69
+
70
+ def test_alias
71
+ doc = @tree.children.first
72
+ seq = doc.children.first
73
+ assert_equal 3, seq.children.length
74
+ al = seq.children[2]
75
+ assert_instance_of Nodes::Alias, al
76
+ assert_equal 'A', al.anchor
77
+ end
78
+ end
79
+ end