# Path: viceroy/api.py
# def build_test_case(class_name, source_file, scanner_class,
#                     base_class, **extra_attrs):
#     with open(source_file) as fobj:
#         source = fobj.read()
# 
#     scanner = scanner_class(source)
# 
#     test_method_names = set(scanner)
# 
#     attrs = {'viceroy_source_file': source_file}
#     for test_name in test_method_names:
#         full_name = 'test_{}'.format(test_name)
#         attrs[full_name] = test_method_proxy(full_name, test_name)
# 
#     attrs.update(extra_attrs)
# 
#     return type(class_name, (base_class,), attrs)
# 
# Path: viceroy/api.py
# class ViceroyTestCase(unittest.TestCase):
#     def viceroy_server(cls):
#     def viceroy_driver(cls):
#     def viceroy_get_results(cls):
#     def setUpClass(cls):
#     def tearDownClass(cls):
# def test_method_proxy(full_name, short_name):
#     def test_method(self):
# def build_test_case(class_name, source_file, scanner_class,
#                     base_class, **extra_attrs):
# 
# Path: viceroy/constants.py
# VICEROY_JS_PATH = os.path.join(VICEROY_STATIC_ROOT, 'viceroy.js')
# 
# Path: viceroy/contrib/flask.py
# class ViceroyFlaskTestCase(ViceroyTestCase, TestCase):
#     viceroy_flask_app = None
#     viceroy_flask_port = 5000
#     viceroy_flask_silent = True
# 
#     def _pre_setup(self):
#         pass
# 
#     def _post_teardown(self):
#         pass
# 
#     @classmethod
#     def viceroy_get_flask_app(cls):
#         return cls.viceroy_flask_app
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_server(cls):
#         app = cls.viceroy_get_flask_app()
#         app.config['TESTING'] = True
#         app.config['VICEROY_TESTING'] = True
#         port = cls.viceroy_flask_port
#         app.config['LIVESERVER_PORT'] = port
#         cls.viceroy_flask_process = None
#         try:
#             cls._start_flask_server(app)
#             yield port
#         finally:
#             cls._stop_flask_server()
# 
#     @classmethod
#     def _start_flask_server(cls, app):
#         cls.viceroy_flask_process = multiprocessing.Process(
#             target=run_app, kwargs={
#                 'app': app,
#                 'port': cls.viceroy_flask_port,
#                 'silent': cls.viceroy_flask_silent
#             }
#         )
#         cls.viceroy_flask_process.start()
#         while not _server_started(cls.viceroy_flask_port):
#             time.sleep(0.01)
# 
#     @classmethod
#     def _stop_flask_server(cls):
#         if cls.viceroy_flask_process is not None:
#             cls.viceroy_flask_process.terminate()
#             cls.viceroy_flask_process.join()
# 
# Path: viceroy/scanner.py
# class BaseScanner(object):
#     test_methods = {}
# 
#     def __init__(self, source):
#         self.tree = Parser().parse(source)
# 
#     def __iter__(self):
#         for node in self.tree:
#             for foo in self.visit(node):
#                 yield foo
# 
#     def visit(self, node):
#         method_name = 'visit_{}'.format(node.__class__.__name__)
#         handler = getattr(self, method_name, self.visit_children)
#         for foo in handler(node):
#             yield foo
# 
#     def visit_children(self, node):
#         for child in node:
#             for foo in self.visit(child):
#                 yield foo
# 
#     def visit_FunctionCall(self, node):
#         key = node.identifier.to_ecma()
#         if key in self.test_methods:
#             argument = self.test_methods[key]
#             if callable(argument):
#                 yield argument(node)
#             else:
#                 yield self.extract_name(node.args[argument])
#         for foo in self.visit_children(node):
#             yield foo
# 
#     def extract_name(self, node):
#         if isinstance(node, String):
#             s = node.to_ecma()
#             if s[0] == s[-1] and s[0] in ["'", '"']:
#                 return s[1:-1]
#             else:
#                 return s
#         else:
#             warnings.warn(
#                 "Unsupported test name type {!r}, "
#                 "only strings are supported".format(
#                     node
#                 ),
#                 UnsupportedIdentifier
#             )
#             raise StopIteration()
# 
# Path: viceroy/tests/utils.py
# class ViceroyScanner(BaseScanner):
#     test_methods = {
#         'VICEROY.store_result': 0,
#         'VICEROY.success': 0,
#         'VICEROY.fail': 0,
#         'VICEROY.skip': 0,
#         'VICEROY.start_test': 0,
#     }


import os
import unittest
from flask import Flask
from viceroy.api import build_test_case
from viceroy.api import JavascriptError
from viceroy.constants import VICEROY_JS_PATH
from viceroy.contrib.flask import ViceroyFlaskTestCase
from viceroy.scanner import BaseScanner
from .utils import ViceroyScanner


@test_app.route('/viceroy.js')
@fail_app.route('/viceroy.js')
def viceroy():
    return VICEROY_JS


@test_app.route('/tests.js')
def success():
    return SUCCESS_TESTS


@fail_app.route('/tests.js')
def fail():
    return FAIL_TESTS


class BaseTestCase(ViceroyFlaskTestCase):
    viceroy_flask_app = test_app

ViceroySuccessTests = build_test_case(
    'ViceroySuccessTests',
    SUCCESS_TESTS_FILE_PATH,
    ViceroyScanner,
    BaseTestCase
)


class ViceroyFailureTests(build_test_case('Base', FAIL_TESTS_FILE_PATH,

# Path: viceroy/api.py
# def build_test_case(class_name, source_file, scanner_class,
#                     base_class, **extra_attrs):
#     with open(source_file) as fobj:
#         source = fobj.read()
# 
#     scanner = scanner_class(source)
# 
#     test_method_names = set(scanner)
# 
#     attrs = {'viceroy_source_file': source_file}
#     for test_name in test_method_names:
#         full_name = 'test_{}'.format(test_name)
#         attrs[full_name] = test_method_proxy(full_name, test_name)
# 
#     attrs.update(extra_attrs)
# 
#     return type(class_name, (base_class,), attrs)
# 
# Path: viceroy/api.py
# class ViceroyTestCase(unittest.TestCase):
#     def viceroy_server(cls):
#     def viceroy_driver(cls):
#     def viceroy_get_results(cls):
#     def setUpClass(cls):
#     def tearDownClass(cls):
# def test_method_proxy(full_name, short_name):
#     def test_method(self):
# def build_test_case(class_name, source_file, scanner_class,
#                     base_class, **extra_attrs):
# 
# Path: viceroy/constants.py
# VICEROY_JS_PATH = os.path.join(VICEROY_STATIC_ROOT, 'viceroy.js')
# 
# Path: viceroy/contrib/flask.py
# class ViceroyFlaskTestCase(ViceroyTestCase, TestCase):
#     viceroy_flask_app = None
#     viceroy_flask_port = 5000
#     viceroy_flask_silent = True
# 
#     def _pre_setup(self):
#         pass
# 
#     def _post_teardown(self):
#         pass
# 
#     @classmethod
#     def viceroy_get_flask_app(cls):
#         return cls.viceroy_flask_app
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_server(cls):
#         app = cls.viceroy_get_flask_app()
#         app.config['TESTING'] = True
#         app.config['VICEROY_TESTING'] = True
#         port = cls.viceroy_flask_port
#         app.config['LIVESERVER_PORT'] = port
#         cls.viceroy_flask_process = None
#         try:
#             cls._start_flask_server(app)
#             yield port
#         finally:
#             cls._stop_flask_server()
# 
#     @classmethod
#     def _start_flask_server(cls, app):
#         cls.viceroy_flask_process = multiprocessing.Process(
#             target=run_app, kwargs={
#                 'app': app,
#                 'port': cls.viceroy_flask_port,
#                 'silent': cls.viceroy_flask_silent
#             }
#         )
#         cls.viceroy_flask_process.start()
#         while not _server_started(cls.viceroy_flask_port):
#             time.sleep(0.01)
# 
#     @classmethod
#     def _stop_flask_server(cls):
#         if cls.viceroy_flask_process is not None:
#             cls.viceroy_flask_process.terminate()
#             cls.viceroy_flask_process.join()
# 
# Path: viceroy/scanner.py
# class BaseScanner(object):
#     test_methods = {}
# 
#     def __init__(self, source):
#         self.tree = Parser().parse(source)
# 
#     def __iter__(self):
#         for node in self.tree:
#             for foo in self.visit(node):
#                 yield foo
# 
#     def visit(self, node):
#         method_name = 'visit_{}'.format(node.__class__.__name__)
#         handler = getattr(self, method_name, self.visit_children)
#         for foo in handler(node):
#             yield foo
# 
#     def visit_children(self, node):
#         for child in node:
#             for foo in self.visit(child):
#                 yield foo
# 
#     def visit_FunctionCall(self, node):
#         key = node.identifier.to_ecma()
#         if key in self.test_methods:
#             argument = self.test_methods[key]
#             if callable(argument):
#                 yield argument(node)
#             else:
#                 yield self.extract_name(node.args[argument])
#         for foo in self.visit_children(node):
#             yield foo
# 
#     def extract_name(self, node):
#         if isinstance(node, String):
#             s = node.to_ecma()
#             if s[0] == s[-1] and s[0] in ["'", '"']:
#                 return s[1:-1]
#             else:
#                 return s
#         else:
#             warnings.warn(
#                 "Unsupported test name type {!r}, "
#                 "only strings are supported".format(
#                     node
#                 ),
#                 UnsupportedIdentifier
#             )
#             raise StopIteration()
# 
# Path: viceroy/tests/utils.py
# class ViceroyScanner(BaseScanner):
#     test_methods = {
#         'VICEROY.store_result': 0,
#         'VICEROY.success': 0,
#         'VICEROY.fail': 0,
#         'VICEROY.skip': 0,
#         'VICEROY.start_test': 0,
#     }


# Path: viceroy/tests/test_core.py
import os
import unittest
from flask import Flask
from viceroy.api import build_test_case
from viceroy.api import JavascriptError
from viceroy.constants import VICEROY_JS_PATH
from viceroy.contrib.flask import ViceroyFlaskTestCase
from viceroy.scanner import BaseScanner
from .utils import ViceroyScanner



@test_app.route('/viceroy.js')
@fail_app.route('/viceroy.js')
def viceroy():
    return VICEROY_JS


@test_app.route('/tests.js')
def success():
    return SUCCESS_TESTS


@fail_app.route('/tests.js')
def fail():
    return FAIL_TESTS


class BaseTestCase(ViceroyFlaskTestCase):
    viceroy_flask_app = test_app

ViceroySuccessTests = build_test_case(
    'ViceroySuccessTests',
    SUCCESS_TESTS_FILE_PATH,
    ViceroyScanner,
    BaseTestCase
)


class ViceroyFailureTests(build_test_case('Base', FAIL_TESTS_FILE_PATH,

                                          ViceroyScanner, BaseTestCase)):