File: test_yaml_migration.rb

package info (click to toggle)
sup-mail 0.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,168 kB
  • sloc: ruby: 12,583; sh: 115; makefile: 9
file content (85 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require "test_helper"

require "sup"
require "psych"

if RUBY_VERSION < "2.1"
describe "Sup's YAML util" do
  describe "Module#yaml_properties" do
    def build_class_with_name name, &b
      Class.new do
        meta_cls = class << self; self; end
        meta_cls.send(:define_method, :name) { name }
        class_exec(&b) unless b.nil?
      end
    end

    after do
      Psych.load_tags = {}
      Psych.dump_tags = {}
    end

    it "defines YAML tag for class" do
      cls = build_class_with_name 'Cls' do
        yaml_properties
      end

      expected_yaml_tag = "!supmua.org,2006-10-01/Cls"

      Psych.load_tags[expected_yaml_tag].must_equal cls
      Psych.dump_tags[cls].must_equal expected_yaml_tag

    end

    it "Loads legacy YAML format as well" do
      cls = build_class_with_name 'Cls' do
        yaml_properties :id
        attr_accessor :id
        def initialize id
          @id = id
        end
      end

      Psych.load_tags["!masanjin.net,2006-10-01/Cls"].must_equal cls

      yaml = <<EOF
--- !masanjin.net,2006-10-01/Cls
id: ID
EOF
      loaded = YAML.load(yaml)

      loaded.id.must_equal 'ID'
      loaded.must_be_kind_of cls
    end

    it "Dumps & loads w/ state re-initialized" do
      cls = build_class_with_name 'Cls' do
        yaml_properties :id
        attr_accessor :id
        attr_reader :flag

        def initialize id
          @id = id
          @flag = true
        end
      end

      instance = cls.new 'ID'

      dumped = YAML.dump(instance)
      loaded = YAML.load(dumped)

      dumped.must_equal <<-EOF
--- !supmua.org,2006-10-01/Cls
id: ID
      EOF

      loaded.id.must_equal 'ID'
      assert loaded.flag
    end
  end
end

else
  puts "Some YAML tests are skipped on Ruby 2.1.0 and newer."
end