testmodule.py :  » Development » Lyntin » lyntin-4.2 » lyntin » modules » testsystem » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Lyntin 
Lyntin » lyntin 4.2 » lyntin » modules » testsystem » testmodule.py
#########################################################################
# This file is part of Lyntin.
#
# Lyntin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Lyntin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# copyright (c) Free Software Foundation 2001-2007
#
# $Id: testmodule.py,v 1.2 2007/07/24 00:39:03 willhelm Exp $
#########################################################################

import os, glob
import exported, modules.modutils, utils

index = __file__.rfind(os.sep)
if index == -1:
  path = "." + os.sep
else:
  path = __file__[:index]

if not path.endswith(os.sep):
  path += os.sep

commands_dict = {}

buffer = []

def capture_mud_filter(args):
  """
  Captures mud output.
  """
  buffer.append(str(args[1]) + "\n")

def capture_user_filter(args):
  """
  Captures ui output.
  """
  buffer.append(str(args[0]))

  
def runtest_cmd(ses, args, input):
  """
  Runs a test.

  Tests are essentially a commands file with some additional testing
  commands for the tester to handle.  The tester sits on the ui
  hook and copies all the data from when it begins to when it
  ends.  Then it takes this data and compares it against the benchmark
  (or if there is not benchmark, it creates one).  A test is successful
  if the benchmark equals the results of the current test run.

  It's pretty primitive--but it will work for most things.
  """
  global buffer

  test = args["test"]

  if not test:
    exported.write_message("available tests in %s:" % path)
    listing = glob.glob(os.path.join(path, "*.test"))
    listing = [mem.replace(path, "").replace(".test", "") for mem in listing]
    listing.sort()
    exported.write_message(utils.columnize(listing, indent=3))
    return

  exported.write_message("test started.")
  try:
    f = open (path + test + ".test", "r")
    lines = f.readlines()
    f.close()
  except:
    exported.write_traceback("runtest: cannot open test file %s." % test)
    return

  # bind ourselves to the hook so we capture all the data
  buffer = []
  exported.hook_register("to_user_hook", capture_user_filter)
  exported.hook_register("to_mud_hook", capture_mud_filter)

  for mem in lines:
    exported.write_message(">>> " + mem)
    exported.lyntin_command(mem, session=ses)

  exported.hook_unregister("to_user_hook", capture_user_filter)
  exported.hook_unregister("to_mud_hook", capture_mud_filter)

  results = "".join(buffer)

  try:
    f = open(path + test + ".benchmark", "r")
    benchmark = "".join(f.readlines())
    f.close()
  except:
    exported.write_traceback("runtest: cannot open benchmark--trying to save a new one.")
    try:
      f = open(path + test + ".benchmark", "w")
      f.write(results)
      f.close()
      exported.write_message("runtest: benchmark saved.")
    except:
      exported.write_traceback("runtest: could not save benchmark either.")
    return

  import difflib
  d = difflib.Differ()
  exported.write_message("----\n")
  exported.write_message("----\n")
  exported.write_message("diffing....")

  cmpresult = list(d.compare(benchmark.splitlines(1), results.splitlines(1)))
  cmpresult2 = [ mem[2:] for mem in cmpresult ]

  if cmpresult2 != benchmark.splitlines(1):
    exported.write_message("".join(cmpresult))
    exported.write_message("test failed.")
  else:
    exported.write_message("test passed.")

commands_dict["runtest"] = (runtest_cmd, "test=")

def load():
  modules.modutils.load_commands(commands_dict)


def unload():
  modules.modutils.unload_commands(commands_dict)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.