|
1 | 1 | import json
|
2 |
| -import unittest |
3 | 2 | from typing import Any
|
4 | 3 |
|
5 | 4 | from jsonasobj2 import JsonObj, as_json
|
|
99 | 98 | """
|
100 | 99 |
|
101 | 100 |
|
102 |
| -class FormatUtilsTestCase(unittest.TestCase): |
103 |
| - def test_formats(self): |
104 |
| - self.assertEqual("ThisIsIt", camelcase("this is it")) |
105 |
| - self.assertEqual("ThisIsIT", camelcase(" this is iT ")) |
106 |
| - self.assertEqual("un camelcased", uncamelcase("UnCamelcased")) |
107 |
| - self.assertEqual("oneword", uncamelcase("Oneword")) |
108 |
| - self.assertEqual("one_word", uncamelcase("one_word")) |
109 |
| - self.assertEqual("another word", uncamelcase("anotherWord")) |
110 |
| - self.assertEqual("IBeY", camelcase("i be y ")) |
111 |
| - self.assertEqual("ThisIsIt", camelcase("This__is_it")) |
112 |
| - |
113 |
| - self.assertEqual("this_is_it", underscore(" this is it ")) |
114 |
| - self.assertEqual("this_is_it", underscore("this is it")) |
115 |
| - |
116 |
| - self.assertEqual("thisIsIt", lcamelcase(" this is\t it\n")) |
117 |
| - |
118 |
| - self.assertEqual("abc", be(" abc\n")) |
119 |
| - self.assertEqual("", be(None)) |
120 |
| - self.assertEqual("", be(" ")) |
121 |
| - |
122 |
| - def test_linestuff(self): |
123 |
| - text = ( |
124 |
| - "This is a mess'o test that goes on for a long way. It has some carriage\n returns embedded in it " |
125 |
| - "but otherwise it drags on and on and on until the cows come home. Splitline covers this we hope." |
126 |
| - ) |
127 |
| - self.assertEqual( |
128 |
| - [ |
129 |
| - "This is a mess'o test that goes on for a long way. It has some carriage returns embedded" |
130 |
| - " in it but otherwise it ", |
131 |
| - "drags on and on and on until the cows come home. Splitline covers this we hope. ", |
132 |
| - ], |
133 |
| - split_line(text), |
134 |
| - ) |
135 |
| - self.assertEqual( |
136 |
| - [ |
137 |
| - "This is a mess'o ", |
138 |
| - "test that goes on ", |
139 |
| - "for a long way. It ", |
140 |
| - "has some carriage ", |
141 |
| - "returns embedded in ", |
142 |
| - "it but otherwise it ", |
143 |
| - "drags on and on and ", |
144 |
| - "on until the cows ", |
145 |
| - "come home. ", |
146 |
| - "Splitline covers ", |
147 |
| - "this we hope. ", |
148 |
| - ], |
149 |
| - split_line(text, 20), |
150 |
| - ) |
151 |
| - self.assertEqual(["X" * 100 + " "], split_line("X" * 100, 20)) |
152 |
| - self.assertEqual( |
153 |
| - ( |
154 |
| - "This is a mess'o test that goes on for a long way. It has some carriage\n" |
155 |
| - "\treturns embedded in it but otherwise it drags on and on and on until the " |
156 |
| - "cows come home. Splitline covers this we \n" |
157 |
| - "\thope. " |
158 |
| - ), |
159 |
| - wrapped_annotation(text), |
160 |
| - ) |
161 |
| - |
162 |
| - def test_empty_functions(self): |
163 |
| - """Test the various forms of is_empty""" |
164 |
| - for thing in empty_things: |
165 |
| - self.assertTrue(is_empty(thing), msg=f"{thing} should clock in as empty") |
166 |
| - for thing in non_empty_things: |
167 |
| - self.assertFalse(is_empty(thing)) |
168 |
| - obj = JsonObj([]) |
169 |
| - assert is_empty(obj) |
170 |
| - |
171 |
| - def test_remove_empty_items(self): |
172 |
| - """Test the various remove empty items paths""" |
173 |
| - seen = set() |
174 |
| - save = list() # Keep garbage collection from reusing ids |
175 |
| - for thing, expected in things_removed: |
176 |
| - actual = remove_empty_items(thing) |
177 |
| - self.assertEqual(expected, actual, msg=f"Input = {thing}") |
178 |
| - self.assertFalse(isinstance(actual, JsonObj), msg="JSON objects are never returned") |
179 |
| - if isinstance(expected, (dict, list)): |
180 |
| - self.assertNotEqual(id(expected), id(actual), msg=f"Copy of {thing} was not returned") |
181 |
| - self.assertNotIn(id(actual), seen, msg="remove_empty_items should always return a new thing") |
182 |
| - save.append(actual) |
183 |
| - seen.add(id(actual)) |
184 |
| - |
185 |
| - def test_enumerations_case(self): |
186 |
| - self.assertEqual( |
187 |
| - """[ |
| 101 | +def test_formats(): |
| 102 | + assert "ThisIsIt" == camelcase("this is it") |
| 103 | + assert "ThisIsIT" == camelcase(" this is iT ") |
| 104 | + assert "un camelcased" == uncamelcase("UnCamelcased") |
| 105 | + assert "oneword" == uncamelcase("Oneword") |
| 106 | + assert "one_word" == uncamelcase("one_word") |
| 107 | + assert "another word" == uncamelcase("anotherWord") |
| 108 | + assert "IBeY" == camelcase("i be y ") |
| 109 | + assert "ThisIsIt" == camelcase("This__is_it") |
| 110 | + |
| 111 | + assert "this_is_it" == underscore(" this is it ") |
| 112 | + assert "this_is_it" == underscore("this is it") |
| 113 | + |
| 114 | + assert "thisIsIt" == lcamelcase(" this is\t it\n") |
| 115 | + |
| 116 | + assert "abc" == be(" abc\n") |
| 117 | + assert "" == be(None) |
| 118 | + assert "" == be(" ") |
| 119 | + |
| 120 | + |
| 121 | +def test_linestuff(): |
| 122 | + text = ( |
| 123 | + "This is a mess'o test that goes on for a long way. It has some carriage\n returns embedded in it " |
| 124 | + "but otherwise it drags on and on and on until the cows come home. Splitline covers this we hope." |
| 125 | + ) |
| 126 | + assert [ |
| 127 | + "This is a mess'o test that goes on for a long way. It has some carriage returns embedded" |
| 128 | + " in it but otherwise it ", |
| 129 | + "drags on and on and on until the cows come home. Splitline covers this we hope. ", |
| 130 | + ] == split_line(text) |
| 131 | + assert [ |
| 132 | + "This is a mess'o ", |
| 133 | + "test that goes on ", |
| 134 | + "for a long way. It ", |
| 135 | + "has some carriage ", |
| 136 | + "returns embedded in ", |
| 137 | + "it but otherwise it ", |
| 138 | + "drags on and on and ", |
| 139 | + "on until the cows ", |
| 140 | + "come home. ", |
| 141 | + "Splitline covers ", |
| 142 | + "this we hope. ", |
| 143 | + ] == split_line(text, 20) |
| 144 | + assert ["X" * 100 + " "] == split_line("X" * 100, 20) |
| 145 | + assert ( |
| 146 | + "This is a mess'o test that goes on for a long way. It has some carriage\n" |
| 147 | + "\treturns embedded in it but otherwise it drags on and on and on until the " |
| 148 | + "cows come home. Splitline covers this we \n" |
| 149 | + "\thope. " |
| 150 | + ) == wrapped_annotation(text) |
| 151 | + |
| 152 | + |
| 153 | +def test_empty_functions(): |
| 154 | + """Test the various forms of is_empty""" |
| 155 | + for thing in empty_things: |
| 156 | + assert is_empty(thing), f"{thing} should clock in as empty" |
| 157 | + for thing in non_empty_things: |
| 158 | + assert not is_empty(thing) |
| 159 | + obj = JsonObj([]) |
| 160 | + assert is_empty(obj) |
| 161 | + |
| 162 | + |
| 163 | +def test_remove_empty_items(): |
| 164 | + """Test the various remove empty items paths""" |
| 165 | + seen = set() |
| 166 | + save = list() # Keep garbage collection from reusing ids |
| 167 | + for thing, expected in things_removed: |
| 168 | + actual = remove_empty_items(thing) |
| 169 | + assert expected == actual, f"Input = {thing}" |
| 170 | + assert not isinstance(actual, JsonObj), "JSON objects are never returned" |
| 171 | + if isinstance(expected, (dict, list)): |
| 172 | + assert id(expected) != id(actual), f"Copy of {thing} was not returned" |
| 173 | + assert id(actual) not in seen, "remove_empty_items should always return a new thing" |
| 174 | + save.append(actual) |
| 175 | + seen.add(id(actual)) |
| 176 | + |
| 177 | + |
| 178 | +def test_enumerations_case(): |
| 179 | + assert """[ |
188 | 180 | "state",
|
189 | 181 | "1"
|
190 |
| -]""", |
191 |
| - as_json(remove_empty_items(json.loads(issue_157_1), hide_protected_keys=True)), |
192 |
| - ) |
193 |
| - self.assertEqual( |
194 |
| - """[ |
| 182 | +]""" == as_json(remove_empty_items(json.loads(issue_157_1), hide_protected_keys=True)) |
| 183 | + assert """[ |
195 | 184 | "namedstate",
|
196 | 185 | "production"
|
197 |
| -]""", |
198 |
| - as_json(remove_empty_items(json.loads(issue_157_2), hide_protected_keys=True)), |
199 |
| - ) |
200 |
| - |
201 |
| - |
202 |
| -if __name__ == "__main__": |
203 |
| - unittest.main() |
| 186 | +]""" == as_json(remove_empty_items(json.loads(issue_157_2), hide_protected_keys=True)) |
0 commit comments