import base64
import random
import string
import unittest
import zlib
from robot.result.elementhandlers import TextCache
from robot.utils.asserts import assert_equals, assert_true

import robot.result.jsparser as jsparser

class _ContextTesting(unittest.TestCase):

    def setUp(self):
        self._context = jsparser.Context()

    def _verify_ids(self, values, ids):
        results = []
        for value in values:
            results.append(self._context.get_id(value))
        assert_equals(ids, results)


class TestTextContext(_ContextTesting):

    def _verify_text(self, values, ids, dump):
        self._verify_ids(values, ids)
        assert_equals(self._context.dump_texts(), dump)

    def test_add_empty_string(self):
        self._verify_text([''], [0] , ['*'])

    def test_add_text(self):
        self._verify_text(['Hello!'], [1] , ['*', '*Hello!'])

    def test_add_several_texts(self):
        self._verify_text(['Hello!', '', 'Foo'], [1, 0, 2] , ['*', '*Hello!', '*Foo'])

class TestIntegerContext(_ContextTesting):

    def _verify_integer(self, values, ids):
        self._verify_ids(values, ids)
        assert_equals(self._context.dump_integers(), values)

    def test_add_integer(self):
        self._verify_integer([0], [-1])

    def test_add_several_integers(self):
        self._verify_integer([1, -234, 700], [-1, -2, -3])

class TestTextCache(unittest.TestCase):

    def setUp(self):
        self._text_cache = TextCache()

    def _verify_text(self, string, expected):
        self._text_cache.add(string)
        assert_equals(['*', expected], self._text_cache.dump())

    def _compress(self, text):
        return self._text_cache._compress(text)

    def test_short_test_is_not_compressed(self):
        self._verify_text('short', '*short')

    def test_long_test_is_compressed(self):
        long_string = 'long'*1000
        self._verify_text(long_string, self._compress(long_string))

    def test_coded_string_is_at_most_1_characters_longer_than_raw(self):
        for i in range(300):
            id = self._text_cache.add(self._generate_random_string(i))
            assert_true(i+1 >= len(self._text_cache.dump()[id]),
                        msg='len(self._text_cache.dump()[id]) (%s) > i+1 (%s)'  % (len(self._text_cache.dump()[id]), i+1))

    def test_long_random_strings_are_compressed(self):
        for i in range(30):
            value = self._generate_random_string(300)
            id = self._text_cache.add(value)
            assert_equals(self._compress(value), self._text_cache.dump()[id])

    def _generate_random_string(self, length):
        return ''.join(random.choice(string.digits) for _ in range(length))

            
            

@@ -4,14 +4,12 @@
 ort 
-base64
+time
 %0D%0Aim
@@ -57,21 +57,8 @@
 st%0D%0A
-import zlib%0D%0A
 from
@@ -1445,32 +1445,182 @@
 f setUp(self):%0D%0A
+        # To make test reproducable log the random seed if test fails%0D%0A        self._seed = long(time.time() * 256)%0D%0A        random.seed(self._seed)%0D%0A
         self._te
@@ -2452,12 +2452,60 @@
 (%25s)
-'  %25
+ %5Btest seed = %25s%5D'  %25 %5C%0D%0A                           
  (le
@@ -2539,16 +2539,28 @@
 d%5D), i+1
+, self._seed
 ))%0D%0A%0D%0A  
@@ -2743,16 +2743,16 @@
 value)%0D%0A
-
         
@@ -2819,16 +2819,98 @@
 mp()%5Bid%5D
+,%5C%0D%0A                          msg='Did not compress %5Btest seed = %25s%5D' %25 self._seed
 )%0D%0A%0D%0A   

Add test seed logging to tests using randomness