Adding tests for new yaml parsing stuff and adjusting imports

pull/9286/head
James Cammarata 10 years ago
parent 8ab0749217
commit 93e273333d

@ -0,0 +1,69 @@
# TODO: header
import unittest
from ansible.parsing.yaml import safe_load
from ansible.parsing.yaml.objects import AnsibleMapping
# a single dictionary instance
data1 = '''---
key: value
'''
# multiple dictionary instances
data2 = '''---
- key1: value1
- key2: value2
- key3: value3
- key4: value4
'''
# multiple dictionary instances with other nested
# dictionaries contained within those
data3 = '''---
- key1:
subkey1: subvalue1
subkey2: subvalue2
subkey3:
subsubkey1: subsubvalue1
- key2:
subkey4: subvalue4
- list1:
- list1key1: list1value1
list1key2: list1value2
list1key3: list1value3
'''
class TestSafeLoad(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_safe_load(self):
# test basic dictionary
res = safe_load(data1)
assert type(res) == AnsibleMapping
assert res._line_number == 2
# test data with multiple dictionaries
res = safe_load(data2)
assert len(res) == 4
assert res[0]._line_number == 2
assert res[1]._line_number == 3
assert res[2]._line_number == 5
assert res[3]._line_number == 8
# test data with multiple sub-dictionaries
res = safe_load(data3)
assert len(res) == 3
assert res[0]._line_number == 2
assert res[1]._line_number == 7
assert res[2]._line_number == 9
assert res[0]['key1']._line_number == 3
assert res[1]['key2']._line_number == 8
assert res[2]['list1'][0]._line_number == 10

@ -1,5 +1,5 @@
from yaml import load
from parsing.yaml.loader import AnsibleLoader
from ansible.parsing.yaml.loader import AnsibleLoader
def safe_load(stream):
''' implements yaml.safe_load(), except using our custom loader class '''

@ -1,5 +1,5 @@
from yaml.constructor import Constructor
from parsing.yaml.objects import AnsibleMapping
from ansible.parsing.yaml.objects import AnsibleMapping
class AnsibleConstructor(Constructor):
def construct_yaml_map(self, node):

@ -3,8 +3,8 @@ from yaml.scanner import Scanner
from yaml.parser import Parser
from yaml.resolver import Resolver
from parsing.yaml.composer import AnsibleComposer
from parsing.yaml.constructor import AnsibleConstructor
from ansible.parsing.yaml.composer import AnsibleComposer
from ansible.parsing.yaml.constructor import AnsibleConstructor
class AnsibleLoader(Reader, Scanner, Parser, AnsibleComposer, AnsibleConstructor, Resolver):
def __init__(self, stream):

Loading…
Cancel
Save