# -*- coding: iso-8859-1 -*-
from __future__ import division # 1/2 == .5 (par defaut, 1/2 == 0)
from __future__ import with_statement

from customtest import *
#from feuille import *

class TestGeolibFeuille(CustomTest):

    def test_abreviations(self):
        f = Feuille(titre = u"Feuille de travail n°1")
        o = f.objets

        self.assert_(o.has_key("Point"))
        self.assert_(o.has_key("Variable"))
        self.assert_(o.has_key("Texte"))
        self.assert_(o.has_key("point"))
        self.assert_(o.has_key("variable"))
        self.assert_(o.has_key("texte"))

        o.txt = ["salut"]
        self.assert_(isinstance(o.txt, Texte))
        self.assert_(o.txt.texte == "salut")
        o.s = [u"Hé, ça marche !"]
        self.assert_(isinstance(o.s, Texte))
        self.assert_(o.s.texte == u"Hé, ça marche !")

        o.A = (1, 2)
        o.k = 7
        self.assert_(isinstance(o.A, Point))
        self.assert_(isinstance(o.k, Variable))
        o.h = 'A.x'
        o.A.x = 15
        self.assert_(o.h == 15)
        o.h = "A.x-10"
        self.assert_(o.h == 5)
        self.assert_(o.h - 3 == 2)
        o.h = math.pi/3
        self.assertAlmostEqual(math.cos(o.h), 1/2)
        o.B = (-1, 3)
        o.u = o.A>o.B
        self.assert_(isinstance(o.u, Vecteur))
        self.assert_(o.u.coordonnees == (o.B.x - o.A.x, o.B.y - o.A.y))
        o.C = 2-3j
        self.assert_(isinstance(o.C, Point))
        self.assert_(o.C.z == 2-3j)
        o.C.x = "A.x"
        print 'o.C.x.val:', o.C.x.val, type(o.C.x.val)
        self.assert_(isinstance(o.C.x.val, (float, int, long)))
        self.assert_(o.C.x == o.A.x)
        o.A.coordonnees = -11, 3
        self.assert_(o.C.coordonnees[0] == -11)
        o.B.x = "A.x + 1"
        self.assert_(isinstance(o.B.x.val, (float, int, long)))
        self.assert_(o.B.x == o.A.x + 1)
        o.A.coordonnees = 30, -5
        self.assert_(o.B.coordonnees[0] == 31)
        o.A(-3.6, 0.4)
        self.assert_(o.C.coordonnees[0] ==-3.6)
        # 'o.EFG = Triangle' doit être accepté comme alias de 'o.EFG = Triangle()'
        o.EFG = Triangle
        self.assert_(isinstance(o.EFG, Triangle))

    def test_nommage_automatique(self):
        f = Feuille()
        M1 = f.objets._ = Point()
        self.assert_("M1" in f.objets)
        M2 = f.objets._ = Point(1, 3)
        self.assert_("M2" in f.objets)
        f.objets._ = Droite(M1, M2)
        self.assert_("d1" in f.objets)
        f.objets._ = Cercle(M1, M2)
        self.assert_("c1" in f.objets)
        f.objets._ = Segment(M1, M2)
        self.assert_("s1" in f.objets)

    def test_nommage_intelligent(self):
        f = Feuille()
        o = f.objets

        o.AB = Segment()
        self.assert_(o.AB.point1.nom == "A")
        self.assert_(o.AB.point2.nom == "B")
        del o.AB
        del o.B

        o.AB = Segment(o.A)
        self.assert_(o.AB.point2.nom == "B")

        o.D = Point()
        o.CD = Segment(point2 = o.D)
        self.assert_(o.CD.point1.nom == "C")

        o.clear()

        o.ABCDEFGHIJKLMNOPQRSTUVWXYZ = Polygone(26)
        self.assert_(  list(pt.nom for pt in o.ABCDEFGHIJKLMNOPQRSTUVWXYZ.points)\
                            == list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

        o.clear()

        o.EFG = Triangle()
        self.assert_(o.EFG.point1.nom == "E")
        self.assert_(o.EFG.point2.nom == "F")
        self.assert_(o.EFG.point3.nom == "G")

        o.MNP = Triangle_rectangle()
        self.assert_(o.MNP.point1.nom == "M")
        self.assert_(o.MNP.point2.nom == "N")
        self.assert_(o.MNP.sommets[2].nom == "P")
        self.assert_(o.P.style("legende") == wxgeo.NOM)


        o.ABCD = Carre()
        self.assert_(o.ABCD.point1.nom == "A")
        self.assert_(o.ABCD.point2.nom == "B")
        self.assert_(o.ABCD.sommets[2].nom == "C")
        self.assert_(o.ABCD.sommets[3].nom == "D")

    def test_acces_objets(self):
        f = Feuille()
        o = f.objets
        o.A = (1, 2)
        self.assert_(o["A"] is o.A)
        o.A_prime = (7, -1.5)
        self.assert_(o["A`"] is o.A_prime)
        exec("b=Vecteur_libre()", o)
        self.assert_("b" in o.keys())
        exec("del b", o)
        self.assert_("b" not in o.keys())

    def test_sauvegarde(self):
        f = Feuille(titre = "Ma feuille")
        o = f.objets
        o.A = (1, 2)
        o.B = (-1, 3)
        o.k = 7
        o.s = Segment(o.A, o.B)
        f.sauvegarder()

    def test_rattachement_objets(self):
        A=Point()
#        self.assert_(A.x == 0 and A.y == 0)
        x0, y0 = A.xy
        self.assert_(isinstance(Point.__feuille__, DescripteurFeuille))
        self.assert_(A.__feuille__ is None)
        f = Feuille()
        f.objets.A = A
        self.assert_(A.__feuille__ is f)
        self.assert_(A.x != x0 and A.y != y0)
        xmin, xmax, ymin, ymax = f.fenetre
        self.assert_(xmin <= A.x <= xmax and ymin <= A.y <= ymax)

    def test_variables_composees_1(self):
        f = Feuille()
        A = f.objets.A = Point()
        B = f.objets.B = Point()
        A(-5, 13)
        B.x = A.x
        self.assert_(B.x == B.coordonnees[0] == A.x == -5)
        A(1, 9)
        self.assert_(B.x == B.coordonnees[0] == -5 and A.x == 1)
        B.x="A.x"
        A(17, 5)
        self.assert_(B.coordonnees[0] == B.x == 17)

    def test_variables_composees_2(self):
        f = Feuille()
        f.objets.M1 = Point(-1.27482678984, 1.69976905312, legende=2)
        f.objets.M2 = Point(2.42032332564, 1.25635103926, legende=2)
        f.objets.s1 = Segment(f.objets.M1,f.objets.M2)
        f.objets.M1(-2.77136258661, 2.91916859122)
        f.objets.M1(4.74826789838, -1.07159353349)
        f.objets.M5 = Point(-5.11778290993, 2.30946882217, legende=2)
        f.objets.M6 = Point(-1.86605080831, 3.25173210162, legende=2)
        f.objets.s4 = Segment(f.objets.M5,f.objets.M6)
        f.objets.M5(-5.59815242494, 2.34642032333)
        f.objets.M1(-2.42032332564, -1.60739030023)
        f.objets.M6(-1.86605080831, 3.25173210162)
        f.objets.M6.renommer('B', legende = 1)
        f.objets.M6 = Point(2.91916859122, 3.5103926097, legende=2)
        f.objets.M6.style(**{'legende': 3, 'label': u'B.x'})
        f.objets.B(-1.18244803695, 1.25635103926)
        f.objets.M6.supprimer()
        f.objets.B(-2.21709006928, 2.64203233256)
        f.objets.M6 = Point(-6.6143187067, 0.443418013857, legende=2)
        f.objets.M6.renommer('C', legende = 1)
        f.objets.C.x=f.objets.B.x
        f.objets.B(-3.17782909931, 3.36258660508)
        f.objets.C.x="B.x"
        f.objets.B(-4.74826789838, 3.47344110855)
        f.objets.B(-1.99538106236, 3.63972286374)
        self.assert_(f.objets.C.coordonnees[0] == f.objets.C.x == f.objets.B.coordonnees[0] == f.objets.B.x)

    def test_polygones_et_representants_de_vecteurs(self):
        f = Feuille()
        f.objets.A = A = rand_pt()
        f.objets.B = B = rand_pt()
        f.objets.C = C = rand_pt()
        f.objets.p = Parallelogramme(A, B, C)
        f.objets.S1.renommer("D")
        s = repr(f.objets.p)
        del f.objets.p
        self.assert_("D" not in f.objets)
        exec("p=" + s, f.objets)
        self.assert_("D" in f.objets)
        self.assert_(f.objets.D is f.objets.p.sommets[3])

    def test_relier_point_axes(self):
        f = Feuille()
        f.objets.M1 = Point(-1.27482678984, 1.69976905312, legende=2)
        f.objets.M2 = Point(2.42032332564, 1.25635103926, legende=2)
        f.objets.s1 = Segment(f.objets.M1, f.objets.M2)
        f.objets.M1.relier_axe_x()
        f.objets.M1.relier_axe_y()

    def test_noms_latex(self):
        f = Feuille()
        f.objets.A = Point()
        self.assert_(f.objets.A.nom == "A")
        self.assert_(f.objets.A.nom_latex == "$A$")
        f.objets.B1 = Point()
        self.assert_(f.objets.B1.nom == "B1")
        self.assert_(f.objets.B1.nom_latex == "$B_{1}$")
        f.objets.C17 = Point()
        self.assert_(f.objets.C17.nom == "C17")
        self.assert_(f.objets.C17.nom_latex == "$C_{17}$")
        f.objets.objet5 = Point()
        self.assert_(f.objets.objet5.nom == "objet5")
        self.assert_(f.objets.objet5.nom_latex == "$objet_{5}$")
        f.objets.Delta = Point()
        self.assert_(f.objets.Delta.nom == "Delta")
        self.assert_(f.objets.Delta.nom_latex == "$\\Delta$")
        f.objets.delta = Point()
        self.assert_(f.objets.delta.nom == "delta")
        self.assert_(f.objets.delta.nom_latex == "$\\delta$")
        f.objets.phi15 = Point()
        self.assert_(f.objets.phi15.nom == "phi15")
        self.assert_(f.objets.phi15.nom_latex == "$\\phi_{15}$")
        f.objets.A_prime_prime = Point()
        self.assert_(f.objets.A_prime_prime.nom == "A_prime_prime")
        self.assert_(f.objets.A_prime_prime.nom_latex == "$A''$")
        f.objets["A'B'"] = Point()
        self.assert_(f.objets.A_primeB_prime.nom == "A_primeB_prime")
        self.assert_(f.objets.A_primeB_prime.nom_latex == "$A'B'$")
        f.objets.A_prime71 = Point()
        self.assert_(f.objets.A_prime71.nom == "A_prime71")
        self.assert_(f.objets.A_prime71.nom_latex == "$A'_{71}$")
        f.objets.A17B22 = Point()
        self.assert_(f.objets.A17B22.nom == "A17B22")
        self.assert_(f.objets.A17B22.nom_latex == "$A_{17}B_{22}$")

        f.objets.C_prime = Cercle()
        self.assert_(f.objets.C_prime.nom_latex == "$\\mathscr{C}'$")
        f.objets.u_prime = Vecteur()
        self.assert_(f.objets.u_prime.nom_latex == "$\\vec u'$")

    def test_formules(self):
        f = Feuille()
        o = f.objets
        o.A = Point(math.e, 3)
        o.M = Point()
        o.M.label(u'{1/ln(A.x)}', True)
        self.assert_(eval(o.M.label()) == 1)

    def test_constantes(self):
        f = Feuille()
        self.assertAlmostEqual(f.objets.pi, 3.1415926535897931)
        self.assertAlmostEqual(f.objets.e, 2.7182818284590451)

    def test_modification_variable(self):
        f = Feuille()
        o = f.objets
        o.a = Variable(1)
        o.fa = "5*sin(4/(a+.5))-1.5"
        o.A = Point(o.a, o.fa)
        o.fa = "-5*sin(4/(a+.5))+5.5"
        o.A = Point(o.a, o.fa)
        self.assertAlmostEqual(o.A.x, o.a)
        self.assertAlmostEqual(o.A.y, o.fa)

    def test_info(self):
        f = Feuille()
        o = f.objets
        with contexte(decimales = 2):
            A = o.A = Point(5, 7)
            self.assert_(A.info == u"Point A de coordonnées (5, 7)")
            B = o.B = Point(6.5, 9.3)
            self.assert_(B.info == u"Point B de coordonnées (6.5, 9.3)")
            s = o.s = Segment(A, B)
            self.assert_(s.info == u"Segment s de longueur 2.75")
            c = o.c = Cercle(s)
            self.assert_(c.info == u"Cercle c de rayon 1.37")
            d = o.d = Droite(A, B)
            self.assert_(d.info == u"Droite d d'équation -2.3 x + 1.5 y + 1 = 0")
            C = o.C = Point(-1.5, 2.7)
            a = o.a = Arc_cercle(A, B, C)
            self.assert_(a.info == u'Arc a de longueur 7.5')
            alpha = o.alpha = Angle(A, B, C)
            self.assert_(alpha.info == u'Angle alpha de valeur 0.3')
        with contexte(decimales = 3):
            self.assert_(a.info == u'Arc a de longueur 7.505')


    def test_executer(self):
        f = Feuille()
        o = f.objets
        f.executer("A = (1, 2)")
        f.executer("A.x += 1")
        self.assert_(o.A.x == 2)
        f.executer("A' = 3, 4")
        f.executer("s = [A A']")
        f.executer("I = Milieu(s)")
        self.assertAlmostEqual(o.I.xy, (2.5, 3))
        f.executer("del")
        self.assert_("I" not in o.noms)
        self.assert_("A_prime" in o.noms)
        f.executer("del")
        f.executer("del")
        self.assert_("A_prime" not in o.noms)
        f.executer("= (1, 2)")
        self.assert_(o.M1.coordonnees == (1, 2))
        f.executer("txt0 = `Bonjour !`")
        f.executer(r"txt1 = `$P\`ere et m\`ere ont un accent grave.$`")
        f.executer("chaine_vide = ``")
        self.assert_(o.txt0.texte == "Bonjour !")
        self.assert_(o.txt1.texte == r"$P\`ere et m\`ere ont un accent grave.$")
        self.assert_(o.chaine_vide.texte == "")
        f.executer("M = (5, 7)")
        f.executer("C = _")
        self.assert_(o.C.x == 5)




if __name__ == '__main__':
    unittest.main()
