class TransformerTestCase(object):
    def get_pattern_for_spec(self, patterns, spec):
        for pattern in patterns:
            if pattern.search(spec):
                return pattern


from spec2scl import settings
from spec2scl import specfile

class TransformerTestCase(object):
    def make_prep(self, spec):
        # just create one of settings.RUNTIME_SECTIONS, so that we can test all the matching
        return '%prep\n' + spec

    def get_pattern_for_spec(self, handler, spec_text):
        spec = specfile.Specfile(spec_text)
        for s_name, s_text in spec.sections:
            for i, pattern in enumerate(handler.matches):
                if pattern.search(s_text) and s_name in handler.sections[i]:
                    return pattern


Improve our custom test case
Improve our custom test case

- create a make_prep method that allows quick creation of prep section from anything for good testing of custom transformers (that usually don't transform header section)
- improve get_pattern_for_spec with section checking

+ from spec2scl import settings
+ from spec2scl import specfile
+ 
  class TransformerTestCase(object):
+     def make_prep(self, spec):
+         # just create one of settings.RUNTIME_SECTIONS, so that we can test all the matching
+         return '%prep\n' + spec
+ 
-     def get_pattern_for_spec(self, patterns, spec):
+     def get_pattern_for_spec(self, handler, spec_text):
-         for pattern in patterns:
-             if pattern.search(spec):
+         spec = specfile.Specfile(spec_text)
+         for s_name, s_text in spec.sections:
+             for i, pattern in enumerate(handler.matches):
+                 if pattern.search(s_text) and s_name in handler.sections[i]:
-                 return pattern
+                     return pattern
  
  
Improve our custom test case
## Code Before:
class TransformerTestCase(object):
    def get_pattern_for_spec(self, patterns, spec):
        for pattern in patterns:
            if pattern.search(spec):
                return pattern


## Instruction:
Improve our custom test case
## Code After:
from spec2scl import settings
from spec2scl import specfile

class TransformerTestCase(object):
    def make_prep(self, spec):
        # just create one of settings.RUNTIME_SECTIONS, so that we can test all the matching
        return '%prep\n' + spec

    def get_pattern_for_spec(self, handler, spec_text):
        spec = specfile.Specfile(spec_text)
        for s_name, s_text in spec.sections:
            for i, pattern in enumerate(handler.matches):
                if pattern.search(s_text) and s_name in handler.sections[i]:
                    return pattern

