File: TAP.py

package info (click to toggle)
task 2.3.0%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,728 kB
  • ctags: 4,813
  • sloc: cpp: 34,267; perl: 18,509; python: 298; sh: 257; makefile: 73; ruby: 32
file content (132 lines) | stat: -rw-r--r-- 4,517 bytes parent folder | download
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
################################################################################
## taskwarrior - a command line task list manager.
##
## Copyright 2006-2014, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################

import re

class TAP:
  """TAP-compliant unit test class."""

  def __init__(self, planned = 0):
    self.planned = planned
    self.counter = 0
    self.passed = 0
    self.failed = 0
    self.skipped = 0
    print "1..%d " % self.planned

  def __del__(self):
    percentPassed = 0.0 
    if self.planned > 0:
      percentPassed = (100.0 * self.passed) / max (self.planned, self.passed + self.failed + self.skipped)

    if self.counter < self.planned:
      print "# Only %d tests, out of a planned %d were run." % (self.counter, self.planned)
      self.skipped += self.planned - self.counter
    elif self.counter > self.planned:
      print "# %d tests were run, but only %d were planned." % (self.counter, self.planned)

    print "# %d passed, %d failed, %d skipped. %.2f%% passed." % (self.passed, self.failed, self.skipped, percentPassed)

  def plan(self, planned):
    self.planned = planned
    print "1..%d " % self.planned

  def planMore(self, extra):
    self.planned += extra
    print "1..%d" % self.planned

  def ok(self, expression, description):
    self.counter += 1
    if bool(expression):
      self.passed += 1
      print "ok %d - %s" % (self.counter, description)
    else:
      self.failed += 1
      print "not ok %d - %s" % (self.counter, description)

  def notok(self, expression, description):
    self.counter += 1
    if not bool(expression):
      self.passed += 1
      print "ok %d - %s" % (self.counter, description)
    else:
      self.failed += 1
      print "not ok %d - %s" % (self.counter, description)

  def equals(self, actual, expected, description):
    self.counter += 1
    if actual == expected:
      self.passed += 1
      print "ok %d - %s" % (self.counter, description)
    else:
      self.failed += 1
      print "not ok %d - %s" % (self.counter, description)
      print "# expected:", expected, "\n#      got:", actual

  def like(self, actual, pattern, description):
    self.counter += 1
    if re.search(pattern, actual):
      self.passed += 1
      print "ok %d - %s" % (self.counter, description)
    else:
      self.failed += 1
      print "not ok %d - %s" % (self.counter, description)

  def unlike(self, actual, pattern, description):
    self.counter += 1
    if re.search(pattern, actual):
      self.failed += 1
      print "not ok %d - %s" % (self.counter, description)
    else:
      self.passed += 1
      print "ok %d - %s" % (self.counter, description)

  def diag(self, stuff):
    for line in stuff.strip().split("\n"):
      print "#", line.strip()

  def skip(self, message):
    self.counter += 1
    self.skipped += 1
    print "skip %d %s" % (self.counter, message)

  def passed(self, message):
    self.counter += 1
    self.passed += 1
    print "ok %d %s" % (self.counter, message)

  def fail(self, message):
    self.counter += 1
    self.failed += 1
    print "not ok %d %s" % (self.counter, message)

  def skip(self, message):
    self.counter += 1
    self.skipped += 1
    print "skip %d %s" % (self.counter, message)

################################################################################