# -*- coding: utf-8 -*-

from vincetools.compat import unittest

class GroupByKeyTests(unittest.TestCase):

    def _run_it(self, iterable, keyfunc=lambda x: x.split('\t', 1)[0]):
        from midas.tools import group_by_key
        return group_by_key(iterable, keyfunc)

    def test_general_case(self):
        data = ('{0}\t3'.format(e) for e in ['foo'] * 5 + ['bar'] * 3)
        result = [ list(block) for block in self._run_it(data) ]
        self.assertEqual(result, [['foo\t3']*5, ['bar\t3']*3])
        
    def test_single_key(self):
        result = [ list(block) for block in self._run_it(['foo\t3']*1) ]
        self.assertEqual(result, [['foo\t3']*1])

@@ -58,16 +58,162 @@
 ittest%0A%0A
+class KeyFuncTestCase(unittest.TestCase):%0A%0A    @property%0A    def _keyfunc(self):%0A        from midas.tools import get_key%0A        return get_key%0A%0A%0A
 class Gr
@@ -226,25 +226,23 @@
 eyTests(
-unittest.
+KeyFunc
 TestCase
@@ -289,37 +289,12 @@
 unc=
-lambda x: x.split('%5Ct', 1)%5B0%5D
+None
 ):%0A 
@@ -371,24 +371,30 @@
 y(iterable, 
+self._
 keyfunc)%0A%0A  
@@ -785,8 +785,544 @@
 3'%5D*1%5D)%0A
+%0Aclass CountByKeyTests(KeyFuncTestCase):%0A%0A    def _run_it(self, iterable):%0A        from midas.tools import count_by_key%0A        return count_by_key(iterable, self._keyfunc)%0A%0A    def test_on_empty_sequence(self):%0A        self.assertEqual(dict(), self._run_it(%5B%5D))%0A%0A    def test_on_sequence(self):%0A        sequence = %5B '%7B0%7D%5Ctblah'.format(key)%0A                     for key in %5B'foo'%5D*2 + %5B'bar'%5D*1 %5D%0A        self.assertEqual(dict(%5B('foo', 2), %0A                               ('bar', 1)%5D),%0A                         self._run_it(sequence))%0A%0A

test count by key