Commits

Anonymous committed 13953ba

Add tests for dictorder parameter. Add tests/lib/README.txt.

Comments (0)

Files changed (3)

tests/data/test_dictorder.dictorder

+++++++
+{address: 123 main st, name: tom, zip: '07003'}
+---
+{name: tom, address: 123 main st, zip: '07003'}
+++++++
+- - awesome
+  - {address: 61 early st, name: mary, zip: '07003'}
+  - {address: 45 late st, name: sara, zip: 07960}
+  - good
+  - {address: 45 late st, name: tom, zip: 07960}
+---
+- - awesome
+  - {name: mary, address: 61 early st, zip: '07003'}
+  - {name: sara, address: 45 late st, zip: 07960}
+  - good
+  - {name: tom, address: 45 late st, zip: 07960}
+++++++
+console:
+- connections:
+  - {link: A1/B, name: 16A-2}
+  name: wallsocket
+- connections:
+  - {link: A1/A, name: 1}
+  - {link: A1/C, name: 2}
+  name: p-s1
+- connections:
+  - {link: 'p-b1:1', name: 1}
+  - {link: 'p-b2:1', name: 2}
+  - {link: 'g1:1', name: 8}
+  name: A1/A
+- connections:
+  - {link: 'p-b1:2', name: 1}
+  - {link: 'g3:2', name: 7}
+  name: A1/B
+name: main
+---
+name: main
+console:
+- name: wallsocket
+  connections:
+  - {name: 16A-2, link: A1/B}
+- name: p-s1
+  connections:
+  - {name: 1, link: A1/A}
+  - {name: 2, link: A1/C}
+- name: A1/A
+  connections:
+  - {name: 1, link: 'p-b1:1'}
+  - {name: 2, link: 'p-b2:1'}
+  - {name: 8, link: 'g1:1'}
+- name: A1/B
+  connections:
+  - {name: 1, link: 'p-b1:2'}
+  - {name: 7, link: 'g3:2'}

tests/lib/README.txt

+
+Creating a new test module:
+---------------------------
+
+1. Create a test_*.py file.
+
+New test modules are put in tests/lib.  For example, to add a test
+called "foo" create tests/lib/test_foo.py
+
+Any function named test_* will be run by the test framework if there
+are data files to feed it.  For example, in test_marks.py there
+is a function called test_marks() which has an attribute:
+
+    test_marks.unittest = ['.marks']
+
+This means any data file that ends with .marks will be fed to this function.
+
+If unittest has multiple items, only data files will be selected if ones
+exist with all the extensions.
+
+For example tests/lib/test_constructor.py has this line:
+
+    test_constructor_types.unittest = ['.data', '.code']
+
+If data files foo.data, foo.code and bar.code exist, test_constructor_types
+will be fed foo.data and foo.code but not bar.code (because there
+is no bar.data).
+
+2. Activate the test module.
+
+Import the module in tests/lib/test_yaml.py.  i.e. add a line like:
+from test_foo import *
+
+Note: test_marks and test_constructor.py are relatively simple
+examples to look at when creating your own test module.
+
+
+Running tests:
+---------------
+
+To run all tests:
+    make testall
+
+To just run test tests in module test_dictorder.py:
+    make ; YAML_TEST_FILENAMES=test_dictorder /usr/bin/python tests/lib/test_build.py
+Verbose:
+    make ; YAML_TEST_FILENAMES=test_dictorder /usr/bin/python tests/lib/test_build.py -v
+

tests/lib/test_dictorder.py

+
+import yaml
+
+def _name_first(items):
+    items.sort()
+    for i in range(0, len(items)):
+        if items[i][0] == 'name':
+            items.insert(0, items.pop(i))
+
+def test_dictorder(dictorder_filename, verbose=False):
+    inputs = open(dictorder_filename, 'rb').read().split('++++++\n')[1:]
+    for input in inputs:
+        normal, reordered = input.split('---\n')
+        normal_parsed = yaml.load(normal)
+        format1 = yaml.dump(normal_parsed, indent=2)
+        format2 = yaml.dump(normal_parsed, indent=2, dictorder=lambda x: x.sort())
+        format3 = yaml.dump(normal_parsed, indent=2, dictorder=_name_first)
+        reordered_parsed = yaml.load(reordered)
+
+        if verbose:
+            print '-' * 40, 'SORTED', '-' * 40
+            print repr(normal)
+        assert normal_parsed == reordered_parsed, repr(reordered_parsed)
+        assert format1 == normal, format1
+        assert format2 == normal, format2
+        if verbose:
+            print '-' * 40, 'NAME FIRST', '-' * 40
+            print repr(reordered)
+        assert format3 == reordered, format3
+
+        format_da = yaml.dump_all([normal_parsed], indent=2, dictorder=_name_first)
+        assert format_da == reordered, format_da
+        format_sd = yaml.safe_dump(normal_parsed, indent=2, dictorder=_name_first)
+        assert format_sd == reordered, format_sd
+        format_sda = yaml.safe_dump_all([normal_parsed], indent=2, dictorder=_name_first)
+        assert format_sda == reordered, format_sda
+
+test_dictorder.unittest = ['.dictorder']
+
+if __name__ == '__main__':
+    import test_appliance
+    test_appliance.run(globals())
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.