diff --git a/hy_odoo/tests/test_xml_base.hy b/hy_odoo/tests/test_xml_base.hy new file mode 100644 index 0000000..411f36d --- /dev/null +++ b/hy_odoo/tests/test_xml_base.hy @@ -0,0 +1,118 @@ +;; -*- coding: utf-8 -*- +;; +;; Copyright 2019-2021 Fabien Bourgeois +;; +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU Affero General Public License as +;; published by the Free Software Foundation, either version 3 of the +;; License, or (at your option) any later version. +;; +;; This program 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 Affero General Public License for more details. +;; +;; You should have received a copy of the GNU Affero General Public License +;; along with this program. If not, see . + +b" XML Helpers tests " + +(require [hy-odoo.macros.test [*]]) +(import unittest + [functools [partial]] + [xml.etree.ElementTree :as ET] + [os [unlink]] + [hy-odoo.xml-base [XMLDictElement xmln xmlroot xmlchild xml-write]]) + +(defclass TextXMLBase [(. unittest TestCase)] + b"XML Helpers tests" + + (defn test-xmln [self] + b"Text xmln" + ; XMLDictElement + (o-assert-is-instance (xmln) XMLDictElement) + ; Tags + (o-assert-dict-equal (._asdict (xmln)) {"tag" "" "attrs" {} "children" []}) + (o-assert-equal (. (xmln "a tag") tag) "a tag") + ; Attrs + (o-assert-dict-equal (. (xmln :attrs {"a good" "one"}) attrs) {"a good" "one"}) + ; Childrens + (o-assert-list-equal (. (xmln :children [1 2 3]) children) [1 2 3]) + (o-assert-list-equal (. (xmln :children "Some text") children) ["Some text"]) + + (with [(o-assert-raises-regex TypeError "Invalid arguments")] (xmln :children False)) + + ; Ensure that only children after tags is managed + (setv element (xmln "tag" {"something" "inside"})) + (o-assert-is-instance (. element attrs) dict) + (o-assert-is-instance (. element children) list) + (o-assert-is-instance element XMLDictElement) + + (setv element (xmln "tag" ["something" "inside"])) + (o-assert-is-instance (. element attrs) dict) + (o-assert-is-instance (. element children) list) + (o-assert-is-instance element XMLDictElement)) + + (defn test-xmlchild [self] + b"Test xmlchild" + (setv parent (xmlroot {"tag" "root" "attrs" {} "children" []}) + xmlc-par (partial xmlchild parent)) + + ; Bad arguments + (with [(o-assert-raises-regex TypeError "Invalid arguments for xmlchild")] (xmlc-par False)) + ; Need XMLDictElement, not dict + (with [(o-assert-raises-regex TypeError "Invalid arguments for xmlchild")] + (xmlc-par [{"tag" "t" "attrs" {"a" "b"} "children" []}])) + + (xmlc-par ["some text"]) + (o-assert-equal (. parent text) "some text") + + (xmlc-par[(xmln "t" {"a" "b"} [])]) + (setv child (.next (.iter parent "t"))) + (o-assert-equal (. child tag) "t") + (o-assert-dict-equal (. child attrib) {"a" "b"}) + (o-assert-list-equal (list child) []) + + (xmlc-par [(xmln "t2" {1 2} [])]) + (setv child (.next (.iter parent "t2"))) + (o-assert-dict-equal (. child attrib) {"1" "2"}) + + (xmlc-par [(xmln "tchildren" {} [(xmln "subchild" {} [])])]) + (setv child (.next (.iter parent "tchildren")) + subchildren (list child)) + (o-assert-equal (len subchildren) 1) + (o-assert-equal (. (first subchildren) tag) "subchild")) + + (defn test-xmlroot [self] + b"Test xmlroot" + + (setv root (xmlroot {"tag" "root" "attrs" {} "children" []})) + (o-assert-is-instance root (. ET Element)) + + (with [(o-assert-raises-regex TypeError "has no attribute")] (xmlroot False)) + (with [(o-assert-raises-regex KeyError "tag")] (xmlroot {})) + (with [(o-assert-raises-regex KeyError "attrs")] (xmlroot {"tag" "root"}))) + + (defn test-xml-write [self] + b"Test xml-write" + + (setv children [(xmln "child1" {"attr" "value"} []) + (xmln "child2" {} "Some text")] + tree (xmlroot {"tag" "root" "attrs" {} "children" children}) + xmlw (fn [p] (xml_write p tree))) + (o-assert-is-none (xmlw "/badpath")) + (o-assert-is-none (xmlw "/bad.ext")) + + (xmlw --file--) + (setv filepath (.replace --file-- ".hy" "_views.xml")) + (with [output-file (open filepath "r")] + (setv output-xml (.read output-file)) + (o-assert-in "" output-xml) + (o-assert-in "" output-xml) + (o-assert-in "Some text" output-xml) + (unlink filepath)))) + + + +(defmain [] (.main unittest))