<commit_before># -*- coding: utf-8 -*-
"""
 ** OBS - this is not a normal command module! **
 ** You cannot import anything in this module as a command! **

This is part of the Evennia unittest framework, for testing the
stability and integrity of the codebase during updates. This module
test the default command set. It is instantiated by the
src/objects/tests.py module, which in turn is run by as part of the
main test suite started with
 > python game/manage.py test.

"""

#import re, time
#from django.conf import settings
#from src.utils import create, ansi
#from src.server import serversession, sessionhandler
#from src.locks.lockhandler import LockHandler
#from src.server.models import ServerConfig
#from src.comms.models import Channel, Msg, PlayerChannelConnection, ExternalChannelConnection
#from django.contrib.auth.models import User
#from src.players.models import PlayerDB
#from src.objects.models import ObjectDB

from django.utils.unittest import TestCase
from src.players.player import Player
from src.utils import create, utils, ansi

#------------------------------------------------------------
# Command testing
# ------------------------------------------------------------

class TestPlayer(Player):
    def msg(self, message, from_obj=None, data=None):
        "test message"
        #print "in testing msg():", message
        if not self.ndb.stored_msg:
            self.ndb.stored_msg = []
        self.ndb.stored_msg.append(message)

class CommandTest(TestCase):
    """
    Tests a command
    """
    CID = 0
    def setUp(self):
        "sets up testing environment"
        self.room1 = create.create_object("src.objects.objects.Room", key="Room%i"%self.CID)
        self.room1.db.desc = "room_desc"
        self.room2 = create.create_object("src.objects.objects.Room", key="Room%ib"%self.CID)
        self.obj1 = create.create_object("src.objects.objects.Object", key="Obj%i"%self.CID, location=self.room1, home=self.room1)
        self.obj2 = create.create_object("src.objects.objects.Object", key="Obj%ib"%self.CID, location=self.room1, home=self.room1)
        self.player = create.create_player("TestPlayer%i"%self.CID, "<EMAIL>", "<PASSWORD>", typeclass=TestPlayer, create_character=False)
        self.caller = create.create_player("Caller%i"%self.CID, "<EMAIL>", "<PASSWORD>", player_dbobj=self.player.dbobj,
                                           character_typeclass="src.objects.objects.Character",
                                           permissions=["Immortals"], character_home=self.room1, character_location=self.room1)
        self.caller.player = self.player
        self.char = create.create_player("Char%i"%self.CID, "<EMAIL>", "<PASSWORD>", typeclass=TestPlayer,
                                         character_typeclass="src.objects.objects.Character",
                                         permissions=["Immortals"], character_home=self.room1, character_location=self.room1)
        self.char = create.create_script("src.scripts.scripts.Script", key="Script%i"%self.CID)

    def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True):
        """
        Test a command by assigning all the needed
        properties to cmdobj and  running
            cmdobj.at_pre_cmd()
            cmdobj.parse()
            cmdobj.func()
            cmdobj.at_post_cmd()
        The msgreturn value is compared to eventual
        output sent to caller.msg in the game
        """

        cmdobj.caller = self.caller
        cmdobj.cmdstring = cmdobj.key
        cmdobj.args = args
        cmdobj.cmdset = cmdset
        cmdobj.raw_string = cmdobj.key + " " + args
        cmdobj.obj = self.caller
        # test
        self.caller.player.ndb.stored_msg = []
        cmdobj.at_pre_cmd()
        cmdobj.parse()
        cmdobj.func()
        cmdobj.at_post_cmd()
        returned_msg = "|".join(self.caller.player.ndb.stored_msg)
        returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip()
        if msg != None and not returned_msg.startswith(msg.strip()):
            sep1 = "\n" + "="*30 + "Wanted message" + "="*34 + "\n"
            sep2 = "\n" + "="*30 + "Returned message" + "="*32 + "\n"
            sep3 = "\n" + "="*78<commit_msg>First draft of a reworked the unittest framework to handle the new form of chars/players/commands. Not functioning yet.<commit_after># -*- coding: utf-8 -*-
"""
 ** OBS - this is not a normal command module! **
 ** You cannot import anything in this module as a command! **

This is part of the Evennia unittest framework, for testing the
stability and integrity of the codebase during updates. This module
test the default command set. It is instantiated by the
src/objects/tests.py module, which in turn is run by as part of the
main test suite started with
 > python game/manage.py test.

"""

#import re, time
#from django.conf import settings
#from src.utils import create, ansi
#from src.server import serversession, sessionhandler
#from src.locks.lockhandler import LockHandler
#from src.server.models import ServerConfig
#from src.comms.models import Channel, Msg, PlayerChannelConnection, ExternalChannelConnection
#from django.contrib.auth.models import User
#from src.players.models import PlayerDB
#from src.objects.models import ObjectDB

from django.utils.unittest import TestCase
from src.players.player import Player
#from src.objects.Objects import Character
from src.utils import create, utils, ansi

#------------------------------------------------------------
# Command testing
# ------------------------------------------------------------

class TestPlayer(Player):
    def msg(self, message, **kwargs):
        "test message"
        #print "in testing msg():", message
        if not self.ndb.stored_msg:
            self.ndb.stored_msg = []
        self.ndb.stored_msg.append(message)

class CommandTest(TestCase):
    """
    Tests a command
    """
    CID = 0
    def setUp(self):
        "sets up testing environment"
        self.room1 = create.create_object("src.objects.objects.Room", key="Room%i"%self.CID)
        self.room1.db.desc = "room_desc"
        self.room2 = create.create_object("src.objects.objects.Room", key="Room%ib"%self.CID)
        self.obj1 = create.create_object("src.objects.objects.Object", key="Obj%i"%self.CID, location=self.room1, home=self.room1)
        self.obj2 = create.create_object("src.objects.objects.Object", key="Obj%ib"%self.CID, location=self.room1, home=self.room1)
        self.char1 = create.create_object("src.objects.objects.Character", key="Char%i" % self.CID, location=self.room1, home=self.room1)
        self.char2 = create.create_object("src.objects.objects.Character", key="Char%ib" % self.CID, location=self.room1, home=self.room1)
        self.script = create.create_script("src.scripts.scripts.Script", key="Script%i"%self.CID)
        self.player = create.create_player("TestPlayer%i"%self.CID, "<EMAIL>", "<PASSWORD>", typeclass=TestPlayer)

        self.char1.player = self.player
        self.char1.sessid = 1

    def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True):
        """
        Test a command by assigning all the needed
        properties to cmdobj and  running
            cmdobj.at_pre_cmd()
            cmdobj.parse()
            cmdobj.func()
            cmdobj.at_post_cmd()
        The msgreturn value is compared to eventual
        output sent to caller.msg in the game
        """

        cmdobj.caller = self.char1
        cmdobj.cmdstring = cmdobj.key
        cmdobj.args = args
        cmdobj.cmdset = cmdset
        cmdobj.raw_string = cmdobj.key + " " + args
        cmdobj.obj = self.char1
        cmdobj.sessid = 1
        # test
        self.char1.player.ndb.stored_msg = []
        cmdobj.at_pre_cmd()
        cmdobj.parse()
        cmdobj.func()
        cmdobj.at_post_cmd()
        for msg in self.char1.player.ndb.stored_msg:
            # clean out prettytable sugar
            pass
        returned_msg = "|".join(self.char1.player.ndb.stored_msg)
        returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip()
        if msg != None and not returned_msg.startswith(msg.strip()):
            sep1 = "\n" + "="*30 + "Wanted message" + "="*34 + "\n"
            sep2 = "\n" + "="*30 + "Returned message" + "="*32 + "\n"
            sep3 = "\n" + "="*78
{'fraction_non_alphanumeric': 0.14486317810849736, 'fraction_numerical': 0.008041286605856937, 'mean_word_length': 2.564157399486741, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7531917', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2467, 'n_words': 619}