Ticket #5: testrecutuple.py

File testrecutuple.py, 4.2 KB (added by Peter Murphy (pkmurphy at postmaster dot co dot uk), 8 years ago)

A small test file for recursive tuples

Line 
1# This is the testrecu module. It contains code that tests the output and input of
2# YAML with recursive structures. We concentrate on tuples, especially.
3
4import yaml;
5
6# We start with sequences.
7
8print "We start with sequences."
9
10RecSeq1 = [];
11RecSeq1.append(RecSeq1);
12print RecSeq1;
13RecSeq1Dump = yaml.dump(RecSeq1);
14print RecSeq1Dump;
15RecSeq1Load = yaml.load(RecSeq1Dump);
16print RecSeq1Load;
17
18
19
20RecSeq2 = [];
21RecSeq2.append(u"a");
22RecSeq2.append(RecSeq2);
23RecSeq2.append(u"b");
24print RecSeq2;
25RecSeq2Dump = yaml.dump(RecSeq2);
26print RecSeq2Dump;
27RecSeq2Load = yaml.load(RecSeq2Dump);
28print RecSeq2Load;
29
30
31RecSeq3 = [];
32RecSeq3a = [];
33RecSeq3.append(RecSeq3a);
34RecSeq3a.append(RecSeq3);
35print RecSeq3;
36RecSeq3Dump = yaml.dump(RecSeq3);
37print RecSeq3Dump;
38RecSeq3Load = yaml.load(RecSeq3Dump);
39print RecSeq3Load;
40
41
42RecSeq4 = [];
43RecSeq4a = [];
44RecSeq4.append(RecSeq4a);
45RecSeq4a.append(u"a");
46RecSeq4a.append(RecSeq4);
47RecSeq4a.append(u"b");
48print RecSeq4;
49RecSeq4Dump = yaml.dump(RecSeq4);
50print RecSeq4Dump;
51RecSeq4Load = yaml.load(RecSeq4Dump);
52print RecSeq4Load;
53
54print "End sequences."
55print "We start with maps."
56
57RecMap1 = {};
58RecMap1["a"]= RecMap1;
59print RecMap1;
60RecMap1Dump = yaml.dump(RecMap1);
61print RecMap1Dump;
62RecMap1Load = yaml.load(RecMap1Dump);
63print RecMap1Load;
64
65RecMap2 = {};
66RecMap2["a"] = u"d";
67RecMap2["b"] = RecMap1;
68RecMap2["c"] = u"e";
69print RecMap2;
70RecMap2Dump = yaml.dump(RecMap2);
71print RecMap2Dump;
72RecMap2Load = yaml.load(RecMap2Dump);
73print RecMap2Load;
74
75RecMap3 = {};
76RecMap3a = {};
77RecMap3["a"] = RecMap3a;
78RecMap3a["b"] = RecMap3;
79print RecMap3;
80RecMap3Dump = yaml.dump(RecMap3);
81print RecMap3Dump;
82RecMap3Load = yaml.load(RecMap3Dump);
83print RecMap3Load;
84
85RecMap4 = {};
86RecMap4a = {};
87RecMap4["a"] = RecMap4a;
88RecMap4a["b"] = u"c";
89RecMap4a["d"] = RecMap4;
90RecMap4a["e"] = u"f";
91print RecMap4;
92RecMap4Dump = yaml.dump(RecMap4);
93print RecMap4Dump;
94RecMap4Load = yaml.load(RecMap4Dump);
95print RecMap4Load;
96print "End maps."
97
98# We try tuple code.
99
100print "This is the tuple code.";
101
102OurList = [];
103OurTuple = tuple([OurList]);
104OurList.append(OurTuple);
105print OurTuple;
106V = yaml.dump(OurTuple);
107print V;
108W = yaml.load(V);
109print W;
110
111print OurList;
112V = yaml.dump(OurList);
113print V;
114W = yaml.load(V);
115print W;
116
117
118A = [];
119B = ("a", A);
120A.append(B);
121print A;
122V = yaml.dump(A);
123print V;
124W = yaml.load(V);
125print W;
126print B;
127V = yaml.dump(B);
128print V;
129W = yaml.load(V);
130print W;
131
132
133A = [];
134B = (A, "a");
135A.append(B);
136print A;
137V = yaml.dump(A);
138print V;
139W = yaml.load(V);
140print W;
141print B;
142V = yaml.dump(B);
143print V;
144W = yaml.load(V);
145print W;
146
147A = [];
148B = (A, "a", "b",);
149A.append(B);
150print A;
151V = yaml.dump(A);
152print V;
153W = yaml.load(V);
154print W;
155print B;
156V = yaml.dump(B);
157print V;
158W = yaml.load(V);
159print W;
160
161A = [];
162B = ("a", A, "b",);
163A.append(B);
164print A;
165V = yaml.dump(A);
166print V;
167W = yaml.load(V);
168print W;
169print B;
170V = yaml.dump(B);
171print V;
172W = yaml.load(V);
173print W;
174
175A = [];
176B = ("a", "b", A,);
177A.append(B);
178print A;
179V = yaml.dump(A);
180print V;
181W = yaml.load(V);
182print W;
183print B;
184V = yaml.dump(B);
185print V;
186W = yaml.load(V);
187print W;
188
189A = [];
190B = {};
191C = ("a", A, "b", B, "d");
192A.append(C);
193B["c"] = C;
194print A
195V = yaml.dump(A);
196print V;
197W = yaml.load(V);
198print W;
199print B
200V = yaml.dump(B);
201print V;
202W = yaml.load(V);
203print W;
204print C
205V = yaml.dump(C);
206print V;
207W = yaml.load(V);
208print W;
209
210print "This is the end of the tuple code."
211print "This is the recursive object test."
212
213class TestRecurseClass:
214    def __init__(self):
215        self.a = "a";
216        self.b = self;
217        self.c = "b";
218       
219D = TestRecurseClass();
220print D;
221E = yaml.dump(D);
222print E;
223F = yaml.load(E);
224print F;
225
226print "This is the end of the recursive test code."
227print "This is some test code for classes."
228
229Q = "--- !!map { !!python/tuple [ 'a', ]: 'b',}"
230R = yaml.load(Q);
231print R;
232
233
234Q = "--- &A !!set { foo: *A }"
235R = yaml.load(Q);
236print R;
237
238print "This should work."
239
240class C: 
241    pass;
242
243c = C()
244s = set([c])
245c.s = s
246print c;
247D = yaml.dump(c);
248print D;
249E = yaml.load(D);
250print E;
251
252
253print "This should not work."
254
255Q = "--- &A !!set { *A }"
256R = yaml.load(Q);
257print R;
258
259