Skip to content

Commit

Permalink
need to turn all print in tests to assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
byteface committed Jul 6, 2020
1 parent af100e8 commit 1fc2bab
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 23 deletions.
2 changes: 1 addition & 1 deletion domonic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- Call Terminal commands using python 3 (this one requires a nix machine)
"""

__version__ = "0.0.9"
__version__ = "0.1.0"
__license__ = 'MIT'

# from typing import *
Expand Down
24 changes: 11 additions & 13 deletions domonic/dom.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""
domonic.dom
~~~~~~~~~
methods on the dom
"""

from typing import *
Expand Down Expand Up @@ -99,12 +97,12 @@ def __init__(self, *args, **kwargs):
# self.args = args
# self.kwargs = kwargs
self.baseURI = 'eventual.technology'
self.baseURIObject = None
self.isConnected = None
self.localName = None
self.namespaceURI = None
# self.baseURIObject = None
self.isConnected = True
# self.localName = None
self.namespaceURI = "http://www.w3.org/1999/xhtml"
self.nextSibling = None
self.nodePrincipal = None
# self.nodePrincipal = None
self.outerText = None
self.ownerDocument = None
self.parentElement = None
Expand Down Expand Up @@ -158,6 +156,10 @@ def nodeType(self):
# pass
return 1

@property
def localName(self):
return self.tagName

@property
def nodeName(self):
''' Returns the name of a node'''
Expand Down Expand Up @@ -265,11 +267,11 @@ def attributes(self) -> List:

def innerHTML(self, *args) -> str:
''' Sets or returns the content of an element'''
self.content = ''.join([each.__str__() for each in args])
self.args = args
return self.content

def html(self, *args) -> str:
return self.innerHTML(*args) # jquery
return self.innerHTML(*args)

def blur(self):
'''Removes focus from an element'''
Expand Down Expand Up @@ -410,10 +412,6 @@ def hasAttributes(self) -> bool:
else:
return False

# def hasChildNodes(self):
'''Returns true if an element has any child nodes, otherwise false'''
# return len(self.args)>1

@property
def id(self):
''' Sets or returns the value of the id attribute of an element'''
Expand Down
2 changes: 2 additions & 0 deletions domonic/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def render(inp, outp=''):


class tag(object):

def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
Expand Down Expand Up @@ -114,6 +115,7 @@ def tag_init(self, *args, **kwargs):


html = type('html', (tag, Document), {'name': 'html', '__init__': tag_init})

body = type('body', (tag, Element), {'name': 'body', '__init__': tag_init})
head = type('head', (tag, Element), {'name': 'head', '__init__': tag_init})
script = type('script', (tag, Element), {'name': 'script', '__init__': tag_init})
Expand Down
4 changes: 4 additions & 0 deletions examples/calc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><head><link rel="stylesheet" href="https://unpkg.com/marx-css/css/marx.min.css" /><script src="https://code.jquery.com/jquery-3.5.1.min.js"></script><script>
function add(){
$('#results').html( Number($('#a').val()) + Number($('#b').val()) )};
</script></head><body><article><div><label>Add numbers:</label><input id="a" /><span>+</span><input id="b" /><button id="calculate_button" onclick="add();">Calculate</button><div>Result:<div id="results"></div></div></div></article></body></html>
37 changes: 37 additions & 0 deletions examples/calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from domonic.javascript import Math
from domonic.html import *


classless_css = link(_rel="stylesheet", _href="https://unpkg.com/marx-css/css/marx.min.css")
jquery = script(_src="https://code.jquery.com/jquery-3.5.1.min.js")

code = script('''
function add(){
$('#results').html( Number($('#a').val()) + Number($('#b').val()) )};
''')

calc = article(
div(
label('Add numbers:'),
input(_id='a'), span('+'), input(_id='b'),
button('Calculate', _id="calculate_button", _onclick="add();"),
div('Result:', div(_id="results"))
)
)

render( html(head(classless_css, jquery, code), body(calc)), 'calc.html' )


# // TODO - serve

# from domonic.terminal import ls, touch
# import time
# ls( "| open .")
# touch( "1.hi")
# time.sleep(1)
# touch( "2.how")
# time.sleep(1)
# touch( "3.are")
# time.sleep(1)
# touch( "4.you")
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

setup(
name = 'domonic',
version = '0.0.9',
version = '0.1.0',
author="@byteface",
author_email="[email protected]",
license="MIT",
url = 'https://github.com/byteface/domonic',
download_url = 'https://github.com/byteface/pypals/archive/0.0.9.tar.gz',
download_url = 'https://github.com/byteface/pypals/archive/0.1.0.tar.gz',
description = 'generate html with python 3',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
56 changes: 49 additions & 7 deletions tests/test_dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@
class domonicTestCase(unittest.TestCase):

def test_dom(self):
sometag = div("asdfasdf", div(), div("yo"), _id="test")
print(sometag.tagName)

print(sometag)
# test div html and innerhtml update content
sometag = div("asdfasdf", div(), div("yo"), _id="someid")
self.assertEqual(sometag.tagName, 'div')
self.assertEqual(str(sometag), '<div id="someid">asdfasdf<div></div><div>yo</div></div>')
sometag.html('test')
print(sometag)
self.assertEqual(str(sometag), '<div id="someid">test</div>')
sometag.innerHTML('test2')
self.assertEqual(str(sometag), '<div id="someid">test2</div>')

# sometag.innerText()

# same test on body tag
bodytag = body("test", _class="why")
self.assertEqual(str(bodytag), '<body class="why">test</body>')
# print(bodytag)

bodytag.html("bugs bunny")
self.assertEqual(str(bodytag), '<body class="why">bugs bunny</body>')
# print('THIS:',bodytag)

# sometag.innerText()
print(sometag.getAttribute('_id'))
self.assertEqual(sometag.getAttribute('_id'), 'someid')
print(sometag.getAttribute('id'))
print(sometag.innerText())
print(sometag.nodeName)
self.assertEqual(sometag.getAttribute('_id'), 'someid')

# print(sometag.innerText())
# print(sometag.nodeName)
# assert(sometag.nodeName, 'DIV') # TODO - i checked one site in chrome, was upper case. not sure if a standard?

print(sometag.setAttribute('id','newid'))
Expand All @@ -54,6 +69,33 @@ def test_dom(self):

print('-END-')

def test_dom_node(self):
sometag = div("asdfasdf", div(), div("yo"), _id="test")
somenewdiv = div('im new')
sometag.appendChild(somenewdiv)
print('>>>>',sometag.args[0])
# print('>>>>',sometag)
print('>>>>',sometag.lastChild())
print('>>>>',sometag.content)

import gc
import pprint
for r in gc.get_referents(somenewdiv):
pprint.pprint(r)

for r in gc.get_referents(sometag):
pprint.pprint(r)



def test_dom_node_again(self):
somebody = body("test", _class="why")#.html("wn")
print(somebody)

somebody = body("test", _class="why").html("fuck off")
print(somebody)



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

0 comments on commit 1fc2bab

Please sign in to comment.