""""
Introduction Adventure

Author: Ignacio Avas (iavas@sophilabs.com)
"""
import codecs
import io
import sys
import unittest
from story.adventures import AdventureVerificationError, BaseAdventure
from story.translation import gettext as _


class TestOutput(unittest.TestCase):
    "Introduction Adventure test"
    def __init__(self, sourcefile):
        "Inits the test"
        super(TestOutput, self).__init__()
        self.sourcefile = sourcefile

    def setUp(self):
        self.__old_stdout = sys.stdout
        sys.stdout = self.__mockstdout = io.StringIO()

    def tearDown(self):
        sys.stdout = self.__old_stdout
        self.__mockstdout.close()

    @staticmethod
    def mock_print(stringy):
        "Mock function"
        pass

    def runTest(self):
        "Makes a simple test of the output"
        raw_program = codecs.open(self.sourcefile).read()
        code = compile(raw_program, self.sourcefile, 'exec', optimize=0)
        exec(code)
        self.assertEqual(
            self.__mockstdout.getvalue().lower().strip(),
            'hello world',
            "Should have printed 'Hello World'"
        )


class Adventure(BaseAdventure):
    "Introduction Adventure"
    title = _('Introduction')

    @classmethod
    def test(cls, sourcefile):
        "Test against the provided file"
        suite = unittest.TestSuite()
        suite.addTest(TestOutput(sourcefile))
        result = unittest.TextTestRunner().run(suite)
        if not result.wasSuccessful():
            raise AdventureVerificationError()

@@ -330,26 +330,52 @@
 _(self, 
-sourcefile
+candidate_code, file_name='%3Cinline%3E'
 ):%0A     
@@ -381,14 +381,15 @@
     
+%22%22
 %22Init
-s
  the
@@ -390,24 +390,26 @@
 it the test%22
+%22%22
 %0A        sup
@@ -457,30 +457,73 @@
 elf.
-sourcefile = sourcefil
+candidate_code = candidate_code%0A        self.file_name = file_nam
 e%0A%0A 
@@ -899,108 +899,57 @@
     
-raw_program = codecs.open(self.sourcefile).read()%0A        code = compile(raw_program, self.sourcefil
+code = compile(self.candidate_code, self.file_nam
 e, '
@@ -1193,37 +1193,8 @@
 e):%0A
-    %22Introduction Adventure%22%0A
     
@@ -1276,16 +1276,18 @@
         
+%22%22
 %22Test ag
@@ -1310,16 +1310,18 @@
 ed file%22
+%22%22
 %0A       
@@ -1350,16 +1350,69 @@
 Suite()%0A
+        raw_program = codecs.open(sourcefile).read()%0A
         
@@ -1436,16 +1436,29 @@
 tOutput(
+raw_program, 
 sourcefi
@@ -1602,8 +1602,10 @@
 Error()%0A
+%0A%0A

Refactor introduction to make it the same as the other tests