aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_writer.py
blob: 5b0a8c4f6645e7b9e4cc18a4742a85faa2e9036f (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import unittest
import yamlish

SCHEDULE = [
  {
    "name": 'Simple scalar',
    "in": 1,
    "out": [ '--- 1', '...', ],
  },
  {
    "name": 'Undef',
    "in": None,
    "out": [ '--- ~', '...', ],
  },
  {
    "name": 'Unprintable',
    "in": "\x01\n\t",
    "out": [ '--- "\x01\n\t"', '...', ],
  },
  {
    "name": 'Simple array',
    "in": [ 1, 2, 3 ],
    "out": [ '---', '- 1', '- 2', '- 3', '...', ],
  },
  {
    "name": 'Array, two elements, None',
    "in": [ None, None ],
    "out": [ '---', '- ~', '- ~', '...', ],
  },
  {
    "name": 'Nested array',
    "in": [ 1, 2, [ 3, 4 ], 5 ],
    "out":
     [ '---', '- 1', '- 2', '-', '  - 3', '  - 4', '- 5', '...', ],
  },
  {
    "name": 'Simple hash',
    "in": { "one": '1', "two": '2', "three": '3' },
    "out": [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ],
  },
  {
    "name": 'Nested hash',
    "in": {
      "one": '1',
      "two": '2',
      "more": { "three": '3', "four": '4' }
    },
    "out": [
      '---',
      'more:',
      '  four: 4',
      '  three: 3',
      'one: 1',
      'two: 2',
      '...',
    ],
  },
  {
    "name": 'Unprintable key',
    "in": { "one": '1', "\x02": '2', "three": '3' },
    "out": [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ],
  },
  {
    "name": 'Empty key',
    "in": { '': 'empty' },
    "out": [ '---', "'': empty", '...', ],
  },
  {
    "name": 'Empty value',
    "in": { '': '' },
    "out": [ '---', "'': ''", '...', ],
  },
  {
    "name": 'Complex',
    "in": {
      'bill-to': {
        'given': 'Chris',
        'address': {
          'city': 'Royal Oak',
          'postal': '48046',
          'lines': "458 Walkman Dr.\nSuite #292\n",
          'state': 'MI'
        },
        'family': 'Dumars'
      },
      'invoice': '34843',
      'date': '2001-01-23',
      'tax': '251.42',
      'product': [
        {
          'sku': 'BL394D',
          'quantity': '4',
          'price': '450.00',
          'description': 'Basketball'
        },
        {
          'sku': 'BL4438H',
          'quantity': '1',
          'price': '2392.00',
          'description': 'Super Hoop'
        }
      ],
      'comments':
       "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
      'total': '4443.52'
    },
    "out": [
      "---",
      "bill-to:",
      "  address:",
      "    city: 'Royal Oak'",
      "    lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
      "    postal: 48046",
      "    state: MI",
      "  family: Dumars",
      "  given: Chris",
      "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"",
      "date: 2001-01-23",
      "invoice: 34843",
      "product:",
      "  -",
      "    description: Basketball",
      "    price: 450.00",
      "    quantity: 4",
      "    sku: BL394D",
      "  -",
      "    description: 'Super Hoop'",
      "    price: 2392.00",
      "    quantity: 1",
      "    sku: BL4438H",
      "tax: 251.42",
      "total: 4443.52",
      "...",
    ],
  },
]

# plan(tests = len(SCHEDULE) * 5)

class TestWriter(unittest.TestCase):
    def test_writer(self):
        for test in SCHEDULE:
            name = test['name']
            yaml = yamlish.Writer()
            self.assert_(True, "%s: Created" % name)
            self.assert_(isinstance(yaml, yamlish.Writer))

            got = []
            data = test['in']

            try:
                yaml.write(data, got)
            except Exception as exc:
                dollar_at = exc
                raise

            # FIXME just to say ... THERE IS NO 'error' key in SCHEDULE!!!
            err = test['error']
            if err:
                if not err.search(dollar_at): # FIXME $@ (or dollar_at) is described
                    # in perlvar(1) the error status of the last eval(), which
                    # means that yaml.read(test['in'])
                    # if everything is alright, then it is None
                    self.assertFalse("%s: Error message" % name)
                    raise Exception(dollar_at)
                self.assert_(not got, "%s: No result" % name)
            else:
                want = test['out']
                self.assert_(not dollar_at, "%s: No error\n%s" % (name, dollar_at))

                self.assertEqual(got, want, """%s: Result matches
                    expected = %s
                    
                    observed = %s
                    """ % (name, want, got))

                yr = yamlish.Reader()

                # Now try parsing it
                parsed = yr.read(got) # FIXME got has an iterator

                self.assertEqual(parsed, data, """%s: Reparse OK
                    expected = %s
                    
                    observed = %s
                    """ % (name, data, parsed))