def test_foo_promotion(self): base = Ent() ent = Foo.load(base, promote=True) self.assertTrue(isinstance(ent, Foo)) ent = Foo.load(base, promote=False) self.assertFalse(isinstance(ent, Foo)) ent1 = Foo.load(base, promote=True) ent = Bar.load(ent1, promote=True) self.assertTrue(isinstance(ent, Bar)) ent1 = Foo.load(base, promote=True) ent = Bar.load(ent1, promote=False) self.assertTrue(isinstance(ent, Foo)) self.assertFalse(isinstance(ent, Bar)) ent1 = Foo.load(base, promote=False) ent = Bar.load(ent1, promote=True) self.assertTrue(isinstance(ent, Bar)) ent1 = Foo.load(base, promote=False) ent = Bar.load(ent1, promote=False) self.assertTrue(isinstance(ent, Ent)) self.assertFalse(isinstance(ent, Foo)) self.assertFalse(isinstance(ent, Bar))
def test_dump_types(self): self.assertEqual(dump(1), '1\n...\n') self.assertEqual(dump(1.0), '1.0\n...\n') self.assertEqual(dump(True), 'true\n...\n') self.assertEqual(dump(None), 'null\n...\n') self.assertEqual(dump([]), '[]\n') self.assertEqual(dump({}), '{}\n') self.assertEqual(dump(Ent()), '{}\n')
def test_dump_types(self): self.assertEqual(dumps(1), '1') self.assertEqual(dumps(1.0), '1.0') self.assertEqual(dumps(True), 'true') self.assertEqual(dumps(None), 'null') self.assertEqual(dumps([]), '[]') self.assertEqual(dumps({}), '{}') self.assertEqual(dumps(Ent()), '{}')
def test_constructor_vs_load(self): ent1 = Ent(self.structure) ent2 = Ent.load(self.structure) self.assertEqual(ent1.scalar, ent2.scalar) self.assertEqual(ent1.list, ent2.list) self.assertEqual(ent1.hash.scalar, ent2.hash.scalar) self.assertEqual(ent1.hash.list, ent2.hash.list) self.assertEqual(ent1.hash.hash.scalar, ent2.hash.hash.scalar) self.assertEqual(ent1.hashes[0].scalar, ent2.hashes[0].scalar) self.assertEqual(ent1.hashes[0].list, ent2.hashes[0].list) self.assertEqual(ent1.hashes[0].hash.scalar, ent2.hashes[0].hash.scalar) self.assertEqual(ent1.hashes[1].scalar, ent2.hashes[1].scalar) self.assertEqual(ent1.hashes[1].list, ent2.hashes[1].list) self.assertEqual(ent1.hashes[1].hash.scalar, ent2.hashes[1].hash.scalar)
def test_nesting(self): ent = Ent.load(self.structure) self.assertEqual(ent.hash.scalar, 1) self.assertEqual(len(ent.hash.list), 4) self.assertEqual(ent.hash.list[2], 3) self.assertEqual(ent.hash.hash.scalar, 1) self.assertEqual(ent.hashes[0].scalar, 1) self.assertEqual(ent.hashes[0].list[1], 2) self.assertEqual(ent.hashes[1].hash.scalar, 1)
def test_foo_stays_foo(self): foo = Foo() ent = Ent(foo=foo) self.assertTrue(isinstance(ent.foo, Foo)) ent = Ent(foos=[foo]) self.assertTrue(isinstance(ent.foos[0], Foo)) ent = Ent(hash={'foo': foo}) self.assertTrue(isinstance(ent.hash.foo, Foo)) ent = Ent.load(foo) self.assertTrue(isinstance(ent, Foo)) ent = Ent.load([foo]) self.assertTrue(isinstance(ent[0], Foo)) ent = Ent.load({'foo': foo}) self.assertTrue(isinstance(ent.foo, Foo))
def test_attributes(self): ent = Ent.load(self.structure) self.assertEqual(ent.scalar, 1) self.assertIsInstance(ent.list, list) self.assertEqual(len(ent.list), 4) self.assertEqual(ent.list[2], 3) self.assertIsInstance(ent.hash, Ent) self.assertIsInstance(ent.hashes, list) self.assertEqual(len(ent.hashes), 2) self.assertIsInstance(ent.hashes[0], Ent)
def test_dict_access(self): ent = Ent.load(self.structure) self.assertEqual(len(ent.hash), 3) self.assertTrue('scalar' in ent.hash)
def spawnEnt(self, kind): x = Ent(kind) self.ents.append(x) return x
def object_hook(obj): return Ent.load(obj)
def spawnPlayer(self): ply = Ent("player") ply.x = self.scrsq/2 ply.y = self.scrsq/2 self.ents.insert(0, ply)
def safe_load(*args, **kwargs): obj = yaml.safe_load(*args, **kwargs) return Ent.load(obj)