aboutsummaryrefslogtreecommitdiffstats
path: root/local/testing/ExpectationPluginTestCase.py
blob: a1c59bd3315d600ac7b75b7323689f4053e0df34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
###
from supybot.ircutils import stripFormatting
from supybot.log import info
from supybot.test import *

from copy import deepcopy

from sys import stdout
from time import sleep

import re
import urllib.request, urllib.parse, urllib.error

class ExpectationPluginTestCase(PluginTestCase):
    plugins       = {}

    def describe(self, query, *args):
        m = self._feedMsg('get ' + query)
        manyEs = tcolors.FAIL + 'E' * len(args) + tcolors.ENDC
        if m is None:
            print(manyEs)
            raise TimeoutError(query)
        if m.args[1].startswith('Error:'):
            print(manyEs)
            self.fail('%r errored: %s' % (query, m.args[1]))

        errors = []
        for expectation in args:
            expectation.reply = m.args[1]
            if expectation.evaluate():
                stdout.write(tcolors.OKGREEN + '.' + tcolors.ENDC)
            else:
                stdout.write(tcolors.FAIL + 'F' + tcolors.ENDC)
                errors.append(expectation.getSummary())
            stdout.flush()
        stdout.write('')
        stdout.flush()

        if errors:
            print("\n%sWhile describing %s" % (tcolors.FAIL, query))
            for error in errors:
                print("- Failed to assert that %s" % (error,))
            print(tcolors.ENDC)
            self.fail("%i assertions failed while describing %s" % (len(errors), query))

    def sendRequest(self, file):
        """ Opens the `samples` folder and sends a file as a request
            to the plugin's server """
        if file in self.files:
            content = self.files[file]
        else:
            with open('samples/' + file + '.json', 'r') as content_file:
                content = content_file.read()
            self.files[file] = content
        content = bytes('payload=' + content, 'utf-8')
        urllib.request.urlopen('http://localhost:' + str(self.port), content)
        # Tests on Travis fail randomly, so let's slow things down...
        sleep(1)

    def conf(self, name, value):
        """Sets one of the plugin's configuration values"""
        conf.supybot.plugins.get("Github").get(name).setValue(value)

    def testDocumentation(self):
        if self.__class__ == ExpectationPluginTestCase:
            return
        else:
            PluginTestCase.testDocumentation(self)

    def setUp(self):
        # Prevent supybot's testDocumentation from going mad
        PluginTestCase.setUp(self)

    def tearDown(self):
        # Add a space between our LEDs and python's OK message
        PluginTestCase.tearDown(self)
        stdout.write(' ')
        stdout.flush()

class tcolors:
    HEADER  = '\033[95m'
    OKBLUE  = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL    = '\033[91m'
    ENDC    = '\033[0m'

def expect(query):
    m = GithubTestCase._feedMsg('get' + query)
    return Expectation(m.args[1])

def it():
    return Expectation()

class Expectation(object):
    def __init__(self):
        self.error = ''
        self.negation = False

        self.should = self
        self.to     = self
        self.should_not = self.negate()

    def evaluate(self):
        if self.negation is True:
            return not self.assertion()
        else:
            return self.assertion()

    def negate(self):
        other = deepcopy(self)
        other.negation = not other.negation
        return other

    def cleanReply(self):
        return ircutils.stripFormatting(self.reply)

    def getSummary(self):
        if self.assertionParameter:
            return self.summary % (self.cleanReply(), self.assertionParameter)
        else:
            return self.summary % (self.cleanReply())

    def contain(self, what):
        self.assertion = self.contains
        self.assertionParameter = what
        if self.negation:
            verb = "does not contain"
        else:
            verb = "contains"
        self.summary = "'%s' " +verb+ " '%s'"
        return self

    def contains(self, flags=re.I):
        return re.search(self.assertionParameter, self.cleanReply(), flags)

class Object(object):
    pass

© 2014-2024 Faster IT GmbH | imprint | privacy policy