<commit_before>import sys

from behave import given, step, then, use_step_matcher, when
from hamcrest import assert_that, contains_string, equal_to, is_
from cStringIO import StringIO
from tdl.client import Client
from tdl.processing_rules import ProcessingRules

use_step_matcher("re")


# ~~~~~ Setup


@given("I start with a clean broker")
def create_the_queues(context):
    username = 'test'
    context.request_queue = context.broker.add_queue('{}.req'.format(username))
    context.request_queue.purge()
    context.response_queue = context.broker.add_queue('{}.resp'.format(username))
    context.response_queue.purge()
    hostname = 'localhost'
    stomp_port = 21613
    context.client = Client(hostname, stomp_port, username)


@given("the broker is not available")
def client_with_wrong_broker(context):
    incorrect_hostname = 'localhost'
    stomp_port = 11613
    username = 'test'
    context.client = Client(incorrect_hostname, stomp_port, username)


@given("I receive the following requests")
def initialize_request_queue(context):
    requests = table_as_list(context)
    for request in requests:
        context.request_queue.send_text_message(request)
    context.request_count = context.request_queue.get_size()


# ~~~~~ Implementations


def get_implementation(implementation_name):
    test_implementations = {
        'add two numbers': lambda params: int(params[0]) + int(params[1]),
        'increment number': lambda params: int(params[0]) + 1,
        'return null': lambda params: None,
        'throw exception': lambda params: raise_(Exception('faulty user code')),
        'some logic': lambda params: "ok",
        'echo the request': lambda params: params[0],
    }

    if implementation_name in test_implementations:
        return test_implementations[implementation_name]
    else:
        raise KeyError('Not a valid implementation reference: "' + implementation_name + "\"")


@when("I go live with the following processing rules")
def step_impl(context):
    processing_rules = ProcessingRules()
    for row in table_as_list_of_rows(context):
        processing_rules.on(row[0]).call(get_implementation(row[1])).then(row[2])

    with Capturing() as context.stdout_capture:
        context.client.go_live_with(processing_rules)

# ~~~~~ Assertions

@then("the client should consume all requests")
def request_queue_empty(context):
    assert_that(context.request_queue.get_size(), is_(equal_to(0)), "Requests have not been consumed")


@step("the client should publish the following responses")
def response_queue_contains_expected(context):
    expected_responses = table_as_list(context)
    assert_that(context.response_queue.get_message_contents(), is_(equal_to(expected_responses)),
                "The responses are not correct")


@then("the client should display to console")
def the_client_should_display_to_console(context):
    assert_that(
        context.stdout_capture.getvalue(),
        contains_string(context.table.headings[0])
    ) 
    for row in context.table:
        assert_that(
            context.stdout_capture.getvalue(),
            contains_string(row[0])
        ) <commit_msg>Add variables to make steps easier to understand.<commit_after>import sys

from behave import given, step, then, use_step_matcher, when
from hamcrest import assert_that, contains_string, equal_to, is_
from cStringIO import StringIO
from tdl.client import Client
from tdl.processing_rules import ProcessingRules

use_step_matcher("re")


# ~~~~~ Setup


@given("I start with a clean broker")
def create_the_queues(context):
    username = 'test'
    context.request_queue = context.broker.add_queue('{}.req'.format(username))
    context.request_queue.purge()
    context.response_queue = context.broker.add_queue('{}.resp'.format(username))
    context.response_queue.purge()
    hostname = 'localhost'
    stomp_port = 21613
    context.client = Client(hostname, stomp_port, username)


@given("the broker is not available")
def client_with_wrong_broker(context):
    incorrect_hostname = 'localhost'
    stomp_port = 11613
    username = 'test'
    context.client = Client(incorrect_hostname, stomp_port, username)


@given("I receive the following requests")
def initialize_request_queue(context):
    requests = table_as_list(context)
    for request in requests:
        context.request_queue.send_text_message(request)
    context.request_count = context.request_queue.get_size()


# ~~~~~ Implementations


def get_implementation(implementation_name):
    test_implementations = {
        'add two numbers': lambda params: int(params[0]) + int(params[1]),
        'increment number': lambda params: int(params[0]) + 1,
        'return null': lambda params: None,
        'throw exception': lambda params: raise_(Exception('faulty user code')),
        'some logic': lambda params: "ok",
        'echo the request': lambda params: params[0],
    }

    if implementation_name in test_implementations:
        return test_implementations[implementation_name]
    else:
        raise KeyError('Not a valid implementation reference: "' + implementation_name + "\"")


@when("I go live with the following processing rules")
def step_impl(context):
    processing_rules = ProcessingRules()
    for row in table_as_list_of_rows(context):
        method = row[0]
        user_implementation = get_implementation(row[1])
        action = row[2]
        processing_rules.on(method).call(user_implementation).then(action)

    with Capturing() as context.stdout_capture:
        context.client.go_live_with(processing_rules)

# ~~~~~ Assertions

@then("the client should consume all requests")
def request_queue_empty(context):
    assert_that(context.request_queue.get_size(), is_(equal_to(0)), "Requests have not been consumed")


@step("the client should publish the following responses")
def response_queue_contains_expected(context):
    expected_responses = table_as_list(context)
    assert_that(context.response_queue.get_message_contents(), is_(equal_to(expected_responses)),
                "The responses are not correct")


@then("the client should display to console")
def the_client_should_display_to_console(context):
    assert_that(
        context.stdout_capture.getvalue(),
        contains_string(context.table.headings[0])
    ) 
    for row in context.table:
        assert_that(
            context.stdout_capture.getvalue(),
            contains_string(row[0])
        ) 
{'fraction_non_alphanumeric': 0.10297766749379653, 'fraction_numerical': 0.006513647642679901, 'mean_word_length': 4.320957095709571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '728799', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1847, 'n_words': 445}