#! /usr/bin/env python
# -*- coding: utf-8 -*-

import logging
log = logging.getLogger()
from unittest import TestCase
from war.world import WorldObject, World, Coord
from war.protocol import Transport, Protocol

class SockMock:
    def send(self, data):
        pass
    def recv(self, buf_len):
        return ""

class OwnerMock:
    def __init__(self):
        self.transport = Transport(Protocol('mock'))
        self.transport.listen = lambda: None
        self.transport(SockMock(), None)

class TestWorldObject(TestCase):
    def setUp(self):
        self.w = World(20, 21)
        self.o = self.w.spawn(WorldObject, owner=OwnerMock())

    def test_move(self):
        o, w = self.o, self.w

        o.move(Coord(2, 3))

        assert (o.coord == Coord(0,0)).all(), "precondition fail"
        assert (o.coord_delta == Coord(2,3)).all(), "precondition fail"

        o.tick()

        self.assertTrue((o.coord == Coord(1,1)).all(),
            "object not on the expected position")

        self.assertTrue((o.coord_delta == Coord(1,2)).all(),
            "objects delta for next tick is incorrect")

        o.ticked(False)
        o.tick()

        self.assertTrue((o.coord == Coord(2,2)).all(),
            "object not on the expected position")

        self.assertTrue((o.coord_delta == Coord(0,1)).all(),
            "objects delta for next tick is incorrect")

        self.assertFalse(w.position(Coord(0,0)).object, "old position is not"
            "freed")
        self.assertEqual(w.position(o.coord).object, o)

    def test_remove(self):
        self.w.remove(self.o)
        self.assertFalse(self.o in self.w.objects)

    def test_move_outside(self):
        self.o.move(Coord(-1, 0))
        self.o.tick()
        self.assertEqual(self.o.coord, None)

    def test_move_zerodelta(self):
        o, w = self.o, self.w

        o.move(Coord(0,0))
        o.tick() # test shouldnt raise
