Mercurial > nagslang
view nagslang/tests/test_yamlish.py @ 120:e6e7a471146a
Support numeric types
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Mon, 02 Sep 2013 15:31:19 +0200 |
parents | b75de48a618c |
children | 423266f0b9e4 |
line wrap: on
line source
from unittest import TestCase from nagslang.yamlish import load_s, dump_s try: import yaml except ImportError: yaml = None # pyflakes:ignore try: from unittest import SkipTest except ImportError: from pytest import skip SkipTest = skip.Exception # pyflakes:ignore class TestParse(TestCase): def assertParsesAs(self, text, expected): self.assertEqual(load_s(text.strip()), expected) def test_dict_list_1(self): self.assertParsesAs(''' foo: - bar - baz ''', {'foo': ['bar', 'baz']}) def test_dict_list_2(self): self.assertParsesAs(''' foo: - bar - baz ''', {'foo': ['bar', 'baz']}) class TestRoundTrip(TestCase): def roundtrip(self, data): text = self.dump_s(data) print '\n=== Begin ===\n%s\n=== End ===' % text self.assertEqual(self.load_s(text), data) def dump_s(self, data): return dump_s(data) def load_s(self, text): return load_s(text) def test_simple_dict(self): self.roundtrip({'foo': 'bar'}) def test_dict_of_dicts(self): self.roundtrip({'foo': {'bar': 'baz'}}) def test_dict_tree(self): self.roundtrip({ 'foo': { 'bar': { 'baz': 'qux' }, 'quux': 'corge', } }) def test_dict_list(self): self.roundtrip({ 'foo': ['bar', 'baz'], }) def test_nested_lists(self): self.roundtrip({ 'foo': [['bar', 'baz', 'qux'], 'quux'], }) def test_list_of_dicts(self): self.roundtrip({ 'foo': [ {'bar': 'baz'}, {'qux': 'quux'}, ], }) def test_quoted(self): # a literal true is True, but 'true' is a string self.roundtrip({'foo': 'true'}) def test_literals(self): self.roundtrip({'foo': [True, False, None]}) def test_numeric(self): self.roundtrip({'foo': [1, 2.0, -1, -2.5]}) class TestFromPyYAML(TestRoundTrip): def dump_s(self, data): if yaml is None: raise SkipTest('yaml module unavailable') return yaml.dump(data, default_flow_style=False) class TestToPyYAML(TestRoundTrip): def load_s(self, text): if yaml is None: raise SkipTest('yaml module unavailable') return yaml.load(text)