###############################################################################
# File:        UnitTest_PlanetGen.py
# Appointment: GUI unittest for planet generation 
# Project:     GalaxyU
# Author:      AR
# History:
# 25.10.2013 Created
###############################################################################
import unittest
import sys
sys.path.append("../")
import guPlanetGenerator
import guObjects
import pygame
import zx

###############################################################################
class TestPlanetGen(unittest.TestCase):
    def setUp(self):
        pass

    #--------------------------------------------------------------------------
    def test_bad_colouring(self):
        # Bad colours (bigger than 1.1)
        self.assertEqual(guPlanetGenerator.GetColouring(100, 100), guPlanetGenerator.UNKNOWN_PLANET_COLOURING)
        self.assertEqual(guPlanetGenerator.GetColouring(1.01, 1.01), guPlanetGenerator.UNKNOWN_PLANET_COLOURING)
        self.assertEqual(guPlanetGenerator.GetColouring(1.01, 0.1), guPlanetGenerator.UNKNOWN_PLANET_COLOURING)


    #--------------------------------------------------------------------------
    def test_good_colouring(self):
        self.assertEqual(guPlanetGenerator.GetColouring(0, 0), [1,1,1])
        self.assertEqual(guPlanetGenerator.GetColouring(-223, 0), [1,1,1])
        
        oldLC = 0
        row = 0
        for liveCond in guPlanetGenerator.STAR_PROPERTIES_LIST:
            col = 0
            oldRes = 0
            for resources in guPlanetGenerator.STAR_PROPERTIES_LIST:
                self.assertEqual(guPlanetGenerator.GetColouring(liveCond, resources), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                self.assertEqual(guPlanetGenerator.GetColouring(liveCond - 0.01, resources), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                self.assertEqual(guPlanetGenerator.GetColouring(liveCond - 0.01, resources - 0.01), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                self.assertEqual(guPlanetGenerator.GetColouring(oldLC + 0.01, oldRes + 0.01), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                oldRes = resources
                col += 1
            oldLC = liveCond 
            row += 1


    #--------------------------------------------------------------------------
    def test_planets_colouring(self):
        self.assertEqual(guPlanetGenerator.GetPlanetColouring(None), guPlanetGenerator.UNKNOWN_PLANET_COLOURING)

        oldLC = 0
        row = 0
        for liveCond in guPlanetGenerator.STAR_PROPERTIES_LIST:
            col = 0
            oldRes = 0
            for resources in guPlanetGenerator.STAR_PROPERTIES_LIST:
                self.assertEqual(guPlanetGenerator.GetPlanetColouring(guObjects.CStar(0, "name", 1,1,1000, resources, liveCond)), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                self.assertEqual(guPlanetGenerator.GetPlanetColouring(guObjects.CStar(0, "name", 1,1,1000, resources, liveCond - 0.01)), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                self.assertEqual(guPlanetGenerator.GetPlanetColouring(guObjects.CStar(0, "name", 1,1,1000, resources - 0.01, liveCond - 0.01)), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                self.assertEqual(guPlanetGenerator.GetPlanetColouring(guObjects.CStar(0, "name", 1,1,1000, oldRes + 0.01, oldLC + 0.01)), guPlanetGenerator.COLOUR_MATRIX[5*row + col])
                oldRes = resources
                col += 1
            oldLC = liveCond 
            row += 1


    #--------------------------------------------------------------------------
    def test_generating_planet(self):
        pygame.init()
        self.assertTrue(guPlanetGenerator.GeneratePlanet(10, [1,1,2]) is not None)
        self.assertTrue(guPlanetGenerator.GeneratePlanet(5, [1,1,2], zx.CDelay(0.000001)) is not None)
        
        # Generates None planet with cancelled delay
        delay = zx.CDelay(10)
        delay.Cancel()
        self.assertTrue(guPlanetGenerator.GeneratePlanet(500, [1,1,2], delay) is None)
        

################################################################################
if __name__ == "__main__":
    unittest.main()
