def testUseHeader(self): emitter = _syck.Emitter(StringIO.StringIO(), headless=True) emitter.emit(EXAMPLE) self.assert_(emitter.output.getvalue().find('---') == -1) emitter = _syck.Emitter(StringIO.StringIO(), use_header=True) emitter.emit(EXAMPLE) self.assert_(emitter.output.getvalue().find('---') != -1)
def testHeadless(self): emitter = _syck.Emitter(StringIO.StringIO(), headless=False) emitter.emit(CYCLE) self.assert_(emitter.output.getvalue().find('---') != -1) emitter = _syck.Emitter(StringIO.StringIO(), headless=True) emitter.emit(CYCLE) self.assert_(emitter.output.getvalue().find('---') == -1)
def testAttributes(self): output = StringIO.StringIO() emitter = _syck.Emitter(output) self.assertEqual(type(emitter), _syck.Emitter) self.assertEqual(emitter.output, output) self.assertEqual(emitter.headless, False) self.assertEqual(emitter.use_header, False) self.assertEqual(emitter.use_version, False) self.assertEqual(emitter.explicit_typing, False) self.assertEqual(emitter.style, None) self.assertEqual(emitter.best_width, 80) self.assertEqual(emitter.indent, 2) emitter = _syck.Emitter(output, headless=True, use_header=True, use_version=True, explicit_typing=True, style='fold', best_width=100, indent=4) self.assertEqual(emitter.headless, True) self.assertEqual(emitter.use_header, True) self.assertEqual(emitter.use_version, True) self.assertEqual(emitter.explicit_typing, True) self.assertEqual(emitter.style, 'fold') self.assertEqual(emitter.best_width, 100) self.assertEqual(emitter.indent, 4)
def testGarbage(self): gc.collect() output = [] emitter = _syck.Emitter(output) output.append(emitter) del output, emitter self.assertEqual(gc.collect(), 2)
def testSyckBugWithComplexKeys(self): broken = 0 for key in COMPLEX_ITEMS: for value in COMPLEX_ITEMS: node = _syck.Map({key: value}) emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(node) parser = _syck.Parser(emitter.output.getvalue()) self.assertEqual(strip(node), strip(parser.parse()))
def testTags(self): document = _syck.Seq() for tag in TAGS: document.value.append(_syck.Scalar('foo', tag=tag)) emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(document) parser = _syck.Parser(emitter.output.getvalue()) document = parser.parse() self.assertEqual(len(document.value), len(TAGS)) for index in range(len(document.value)): node = document.value[index] self.assertEqual(node.tag, TAGS[index])
def testDouble(self): class Stream: def __init__(self): self.emitter = None def write(self, data): if self.emitter: self.emitter.emit(EXAMPLE) emitter = _syck.Emitter(Stream()) emitter.output.emitter = emitter self.assertRaises(RuntimeError, lambda: emitter.emit(EXAMPLE))
def testDocuments(self): emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(EXAMPLE) emitter.emit(EXAMPLE) emitter.emit(EXAMPLE) parser = _syck.Parser(emitter.output.getvalue()) self.assertEqual(strip(EXAMPLE), strip(parser.parse())) self.assertEqual(strip(EXAMPLE), strip(parser.parse())) self.assertEqual(strip(EXAMPLE), strip(parser.parse())) self.assert_(not parser.eof) parser.parse() self.assert_(parser.eof)
def _testTree(self, tree): emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(tree) #print emitter.output.getvalue() parser = _syck.Parser(emitter.output.getvalue()) self.assertEqual(strip(tree), strip(parser.parse()))
def testSyckBugWithTrailingSpace(self): emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(_syck.Scalar('foo ', tag="tag:yaml.org,2002:str")) parser = _syck.Parser(emitter.output.getvalue()) self.assertEqual(parser.parse().value, 'foo ')
def testPairs(self): emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(PAIRS) parser = _syck.Parser(emitter.output.getvalue()) node = parser.parse() self.assertEqual(strip(PAIRS), strip(node))
def testBadOutput(self): emitter = _syck.Emitter(None) self.assertRaises(AttributeError, lambda: emitter.emit(EXAMPLE))
def testMap(self): for invalid in INVALID_MAP: emitter = _syck.Emitter(StringIO.StringIO()) self.assertRaises(TypeError, lambda: emitter.emit(invalid))
def testRoot(self): emitter = _syck.Emitter(StringIO.StringIO()) self.assertRaises(TypeError, lambda: emitter.emit(INVALID_ROOT))
def testInvalidTypesAndValues(self): self.assertRaises(TypeError, lambda: _syck.Emitter(None, headless='on')) self.assertRaises(TypeError, lambda: _syck.Emitter(None, use_header='on')) self.assertRaises(TypeError, lambda: _syck.Emitter(None, use_version='on')) self.assertRaises(TypeError, lambda: _syck.Emitter(None, explicit_typing='on')) self.assertRaises(ValueError, lambda: _syck.Emitter(None, style='no_quote')) self.assertRaises(TypeError, lambda: _syck.Emitter(None, best_width='100')) self.assertRaises(ValueError, lambda: _syck.Emitter(None, best_width=0)) self.assertRaises(ValueError, lambda: _syck.Emitter(None, best_width=-100)) self.assertRaises(TypeError, lambda: _syck.Emitter(None, indent='2')) self.assertRaises(ValueError, lambda: _syck.Emitter(None, indent=0)) self.assertRaises(ValueError, lambda: _syck.Emitter(None, indent=-2))
def testAliases(self): emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(ALIASES) parser = _syck.Parser(emitter.output.getvalue()) node = parser.parse() self.assert_(node.value[0] is node.value[1])
def testComplexExample(self): emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(COMPLEX_EXAMPLE) parser = _syck.Parser(emitter.output.getvalue()) node = parser.parse() self.assertEqual(strip(COMPLEX_EXAMPLE), strip(node))