# -*- coding: utf-8 -*-
"""
    Pygments tests with example files
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2006-2008 by Georg Brandl.
    :license: BSD, see LICENSE for more details.
"""

import os
import unittest

from pygments import highlight
from pygments.lexers import get_lexer_for_filename, get_lexer_by_name
from pygments.token import Error
from pygments.util import ClassNotFound


class ExampleFileTest(unittest.TestCase):
    pass

lfd = 0

# generate methods
testdir = os.path.dirname(__file__)
for fn in os.listdir(os.path.join(testdir, 'examplefiles')):
    absfn = os.path.join(testdir, 'examplefiles', fn)
    if not os.path.isfile(absfn):
        continue

    try:
        lx = get_lexer_for_filename(absfn)
    except ClassNotFound:
        try:
            name, rest = fn.split("_", 1)
            lx = get_lexer_by_name(name)
        except ClassNotFound:
            raise AssertionError('no lexer found for file %r' % fn)

    def test(self, lx=lx, absfn=absfn):
        text = file(absfn, 'U').read()
        text = text.strip('\n') + '\n'
        text = text.decode('latin1')
        ntext = []
        for type, val in lx.get_tokens(text):
            ntext.append(val)
            self.failIf(type == Error, 'lexer generated error token for '+absfn)
        if u''.join(ntext) != text:
            self.fail('round trip failed for '+absfn)

    setattr(ExampleFileTest, 'test_%i' % lfd, test)
    lfd += 1

@@ -402,88 +402,57 @@
 d%0A%0A%0A
-class ExampleFileTest(unittest.TestCase):%0A    pass%0A%0Alfd = 0%0A%0A# generate methods%0A
+# generate methods%0Adef test_example_files():%0A    
 test
@@ -483,16 +483,20 @@
 file__)%0A
+    
 for fn i
@@ -548,16 +548,20 @@
 les')):%0A
+    
     absf
@@ -602,24 +602,28 @@
 files', fn)%0A
+    
     if not o
@@ -652,16 +652,20 @@
         
+    
 continue
@@ -666,29 +666,37 @@
 ntinue%0A%0A    
+  
+  
 try:%0A
+    
         lx =
@@ -722,24 +722,28 @@
 name(absfn)%0A
+    
     except C
@@ -756,37 +756,45 @@
 tFound:%0A        
+ 
+   
 try:%0A
+    
             name
@@ -823,32 +823,36 @@
  1)%0A            
+   
+ 
 lx = get_lexer_b
@@ -864,32 +864,36 @@
 e(name)%0A        
+    
 except ClassNotF
@@ -910,16 +910,20 @@
         
+ 
+   
 raise As
@@ -974,49 +974,74 @@
 fn)%0A
-%0A    def test(self, lx=lx, absfn=
+        yield check_lexer, lx, absfn%0A%0Adef check_lexer(lx, 
 absfn):%0A
     
@@ -1032,28 +1032,24 @@
 lx, absfn):%0A
-    
     text = f
@@ -1071,28 +1071,24 @@
 .read()%0A    
-  
-  
 text = text.
@@ -1110,20 +1110,16 @@
 %5Cn'%0A    
-   
- 
 text = t
@@ -1139,20 +1139,16 @@
 atin1')%0A
-    
     ntex
@@ -1154,20 +1154,16 @@
 xt = %5B%5D%0A
-    
     for 
@@ -1204,20 +1204,16 @@
         
- 
-   
 ntext.ap
@@ -1234,30 +1234,21 @@
     
-    self.failIf(
+assert 
 type 
-=
+!
 = Er
@@ -1290,20 +1290,17 @@
 or '
-+
+ + 
 absfn
-)%0A    
+%0A
     
@@ -1339,21 +1339,28 @@
     
-    self.fail
+raise AssertionError
 ('ro
@@ -1384,78 +1384,14 @@
 or '
-+
+ + 
 absfn)%0A
-%0A    setattr(ExampleFileTest, 'test_%25i' %25 lfd, test)%0A    lfd += 1%0A

make use of test generators for test_examplefiles