From 93e273333d4859e83e64830d5ce52e52d3822317 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 7 Oct 2014 15:52:58 -0500 Subject: [PATCH] Adding tests for new yaml parsing stuff and adjusting imports --- test/v2/parsing/__init__.py | 0 test/v2/parsing/yaml/__init__.py | 2 + test/v2/parsing/yaml/test_yaml.py | 69 ++++++++++++++++++++++++++ v2/ansible/parsing/yaml/__init__.py | 2 +- v2/ansible/parsing/yaml/constructor.py | 2 +- v2/ansible/parsing/yaml/loader.py | 4 +- 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 test/v2/parsing/__init__.py create mode 100644 test/v2/parsing/yaml/__init__.py create mode 100644 test/v2/parsing/yaml/test_yaml.py diff --git a/test/v2/parsing/__init__.py b/test/v2/parsing/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/v2/parsing/yaml/__init__.py b/test/v2/parsing/yaml/__init__.py new file mode 100644 index 00000000000..ec86ee61015 --- /dev/null +++ b/test/v2/parsing/yaml/__init__.py @@ -0,0 +1,2 @@ +# TODO: header + diff --git a/test/v2/parsing/yaml/test_yaml.py b/test/v2/parsing/yaml/test_yaml.py new file mode 100644 index 00000000000..c32365dcbfe --- /dev/null +++ b/test/v2/parsing/yaml/test_yaml.py @@ -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 diff --git a/v2/ansible/parsing/yaml/__init__.py b/v2/ansible/parsing/yaml/__init__.py index 040a91d6892..730515089e6 100644 --- a/v2/ansible/parsing/yaml/__init__.py +++ b/v2/ansible/parsing/yaml/__init__.py @@ -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 ''' diff --git a/v2/ansible/parsing/yaml/constructor.py b/v2/ansible/parsing/yaml/constructor.py index fd4a35e7afc..8d0ed2b8f62 100644 --- a/v2/ansible/parsing/yaml/constructor.py +++ b/v2/ansible/parsing/yaml/constructor.py @@ -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): diff --git a/v2/ansible/parsing/yaml/loader.py b/v2/ansible/parsing/yaml/loader.py index d9dc4fea89c..9b15a7f3c16 100644 --- a/v2/ansible/parsing/yaml/loader.py +++ b/v2/ansible/parsing/yaml/loader.py @@ -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):