changeset 14:9d61abb3cfaf

Better subclass handling.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 06 May 2012 15:58:03 +0200
parents c0966997e0c5
children 8865ba0c9c38
files gamelib/lab.py gamelib/products.py gamelib/research.py gamelib/tests/test_lab.py
diffstat 4 files changed, 27 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/lab.py	Sun May 06 15:56:22 2012 +0200
+++ b/gamelib/lab.py	Sun May 06 15:58:03 2012 +0200
@@ -1,32 +1,13 @@
-from gamelib import research, products
-
-
-def is_subclass(item, superclass):
-    return (
-        isinstance(item, type)
-        and issubclass(item, superclass)
-        and not item is superclass)
-
+# -*- test-case-name: gamelib.tests.test_lab -*-
 
-def list_products():
-    for item in dir(products):
-        # Ugh!
-        item = getattr(products, item)
-        if is_subclass(item, products.Product):
-            yield item
-
-
-def list_research():
-    for item in dir(research):
-        # Ugh!
-        item = getattr(research, item)
-        if is_subclass(item, research.ResearchArea):
-            yield item
+from gamelib import research, products
 
 
 class Lab(object):
     def __init__(self):
         self.science = []
+        self.new_research = research.ResearchArea.__subclasses__()
+        self.new_products = products.Product.__subclasses__()
 
     def spend_points(self, things, basic_research):
         new_stuff = []
@@ -55,18 +36,14 @@
 
     def find_new_products(self, research_area):
         available_products = []
-        for product_class in list_products():
-            if self._get_science(product_class):
-                continue
+        for product_class in self.new_products:
             if self._meet_requirements(product_class):
                 available_products.append(product_class)
         return available_products
 
     def find_new_research(self, basic_research):
         available_research = []
-        for research_class in list_research():
-            if self._get_science(research_class):
-                continue
+        for research_class in self.new_research:
             if self._meet_requirements(research_class):
                 available_research.append(research_class)
         return available_research
--- a/gamelib/products.py	Sun May 06 15:56:22 2012 +0200
+++ b/gamelib/products.py	Sun May 06 15:58:03 2012 +0200
@@ -27,5 +27,5 @@
 class DoomsdayVirus(Product):
     NAME = "Doomsday virus"
     PREREQUISITES = (
-        (research.BioGenetics, 5),
+        (research.Biogenetics, 5),
         )
--- a/gamelib/research.py	Sun May 06 15:56:22 2012 +0200
+++ b/gamelib/research.py	Sun May 06 15:58:03 2012 +0200
@@ -21,7 +21,7 @@
     NAME = "Rocketry"
 
 
-class BioGenetics(ResearchArea):
+class Biogenetics(ResearchArea):
     NAME = "Biogenetics"
 
 
@@ -30,7 +30,7 @@
 
 
 class Fusion(ResearchArea):
-    NAME = "ResearchArea"
+    NAME = "Fusion"
 
 
 class Medical(ResearchArea):
@@ -47,3 +47,11 @@
         (Robotics, 1),
         (Rocketry, 2),
         )
+
+
+class ArtificialIntelligence(ResearchArea):
+    NAME = "Artificial Intelligence"
+    PREREQUISITES = (
+        (Robotics, 4),
+        (Psychology, 2),
+        )
--- a/gamelib/tests/test_lab.py	Sun May 06 15:56:22 2012 +0200
+++ b/gamelib/tests/test_lab.py	Sun May 06 15:58:03 2012 +0200
@@ -1,13 +1,19 @@
 from unittest import TestCase
 
 from gamelib.lab import Lab
+from gamelib import research, products
 
 
 class TestLab(TestCase):
+    def setUp(self):
+        self.lab = Lab()
+
     def test_find_new_products(self):
-        lab = Lab()
-        print lab.find_new_products(None)
+        new_products = self.lab.find_new_products(None)
+        self.assertTrue(products.MachineGun in new_products)
+        self.assertTrue(products.DoomsdayVirus not in new_products)
 
     def test_find_new_research(self):
-        lab = Lab()
-        print lab.find_new_research(None)
+        new_research = self.lab.find_new_research(None)
+        self.assertTrue(research.Tesla in new_research)
+        self.assertTrue(research.Space not in new_research)