import unittest
from random import randint
import sys

sys.path.insert(0, '../')

from SmilesParser import SmilesParser

class SmilesParserTest(unittest.TestCase):
    #test parsper

    def setUp(self):
      self.random_int = randint(2,20)
      self.test_cases = { ('C'*self.random_int)+'CC' : [('C'*self.random_int)+'CC'],
                          'C'*self.random_int+'(C)C': ['C'*self.random_int, ['C'], 'C'],
                          'C('+'C'*self.random_int+')C' : [ 'C',['C'*self.random_int+''],'C'], 
                          'C(C)'+'C'*self.random_int  :  ['C', ['C'],'C'*self.random_int]
                        }
     
      self.parser = SmilesParser('C')

    def test_parsing(self): 
      for key in sorted(self.test_cases.iterkeys()):
        self.parser.parse(key)
        self.assertTrue(self.parser.to_list() == self.test_cases[key])
       

    def test_contains_branch(self):  
      self.parser.parse('C(C)C'*self.random_int)
      self.assertTrue(self.parser.contains_branch() == True)

      self.parser.parse('CCC'*self.random_int)
      self.assertTrue(self.parser.contains_branch() == False)

      self.parser.parse('CCC(CC)C(CC)CCC'*self.random_int)
      self.assertTrue(self.parser.contains_branch() == True)
      
    def test_to_r_list(self):
      for key in sorted(self.test_cases.iterkeys()):
        self.parser.parse(key)
        self.assertTrue(self.parser.to_r_list() == self.test_cases[key][::-1])
        
    def test_get_parent_carbons(self):
      self.parser.parse('C'*self.random_int)
      self.assertTrue(self.parser.get_parent_carbons() == 'C'*self.random_int)
       
    def test_get_smiles(self):
      for key in sorted(self.test_cases.iterkeys()):
        self.parser.parse(key)
        self.assertTrue(self.parser.get_smiles() == key)
        
    def test_all_carbons(self):
      for key in sorted(self.test_cases.iterkeys()):
        self.parser.parse(key)
        self.assertTrue(self.parser.get_all_carbons() == self.random_int + 2)
    
    def test_longest_parent(self):
      parent = 'C'*self.random_int
      smallest = 'C'*(self.random_int - 1)
      mid = '(C)'*self.random_int
      self.parser.parse((parent+mid+smallest+mid)*self.random_int)
      self.assertTrue(self.parser.longest_parent() == parent)

    def test_longest_branch(self):
      unchained = 'C'*self.random_int
      branch = '('+unchained+')'
      smallest = '('+'C'*(self.random_int-1)+')'
      mid = 'C'*self.random_int
      self.parser.parse((branch+mid+smallest+mid)*self.random_int)
      self.assertTrue(self.parser.longest_branch() == unchained)

    def test_get_grouping(self):
      mid = 'C'*self.random_int
      count = 0
      self.assertTrue(count == 0)

    def test_get_grouping(self):
      mid = 'C'*self.random_int
      count = 0
      self.assertTrue(count == 0)

    def test_get_grouping(self):
      mid = 'C'*self.random_int
      count = 0
      self.assertTrue(count == 0)
  
if __name__ == '__main__':
    unittest.main()

