Skip to content

Commit c6f927b

Browse files
committed
Implemented basic tests for Bootstrap and Bulma
2 parents 8a55214 + 3125237 commit c6f927b

8 files changed

Lines changed: 190 additions & 27 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
venv/
22
.idea/
33
__pycache__/
4+
.pytest_cache/
45
*.pyc
56

67
# Setuptools distribution folder.

csskrt/bootstrapCsskrt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44

55
class BootstrapCsskrt(Csskrt):
6-
def __init__(self, fileName, pretty_print):
6+
def __init__(self, fileName):
77
tag_styles = {
88
'input': 'form-control',
99
'select': 'custom-select',
1010
'button': 'btn btn-primary',
1111
'checkbox': 'form-check-input',
1212
}
13-
super().__init__(fileName, pretty_print, tag_styles)
13+
super().__init__(fileName, tag_styles)
1414

1515
def version(self):
1616
return "v4.1"

csskrt/bulmaCsskrt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
class BulmaCsskrt(Csskrt):
6-
def __init__(self, fileName, pretty_print):
6+
def __init__(self, fileName):
77
tag_styles = {
88
'input': 'input',
99
'label': 'label',
@@ -19,7 +19,7 @@ def __init__(self, fileName, pretty_print):
1919
'h5': 'title is-5',
2020
'h6': 'title is-6'
2121
}
22-
super().__init__(fileName, pretty_print, tag_styles)
22+
super().__init__(fileName, tag_styles)
2323

2424
def version(self):
2525
return "v0.7.1"

csskrt/csskrt.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66

77
class Csskrt(ABC):
8-
def __init__(self, filename: str, pretty_print, tag_styles: Dict):
8+
def __init__(self, filename: str, tag_styles: Dict):
99
f = open(filename) # should be able to handle dirs (for later) todo
1010
f_data = f.read()
1111

12-
self.pretty_print = pretty_print
1312
self.file_path = filename
1413
self.soup = BeautifulSoup(f_data, 'html.parser')
1514
self.tag_styles = tag_styles
@@ -149,7 +148,6 @@ def add_list_classes(self, list_tags: dict) -> NoReturn:
149148
# recursive=False to prevent double modifying for nested lists
150149
self.add_class_to_element(li, list_tags['li'])
151150

152-
153151
def add_general_classes(self):
154152
"""
155153
Adds styles to single elements
@@ -163,7 +161,7 @@ def add_general_classes(self):
163161
for elem in self.soup.find_all(tag):
164162
self.add_class_to_element(elem, self.tag_styles[tag])
165163

166-
def output(self) -> NoReturn:
164+
def output(self, pretty_print: bool) -> NoReturn:
167165
"""
168166
Outputs a new file.
169167
:return:
@@ -174,7 +172,7 @@ def output(self) -> NoReturn:
174172

175173
new_file_name = os.path.join(folder, 'output/csskrt_' + file_name + ext)
176174
with open(new_file_name, 'w') as out_file:
177-
if self.pretty_print:
175+
if pretty_print:
178176
out_file.write(str(self.soup))
179177
else:
180178
out_file.write(self.soup.prettify())
@@ -206,7 +204,4 @@ def freshify(self) -> NoReturn:
206204
# Add styles for the rest of the elements
207205
self.add_general_classes()
208206

209-
# Output the modified html file
210-
self.output()
211-
212-
return self.soup
207+
return self.soup

csskrt/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def freshify(filename, framework, pretty_print):
1414
csskrter = None
1515

1616
if framework == 'bootstrap':
17-
csskrter = BootstrapCsskrt(filename, pretty_print)
17+
csskrter = BootstrapCsskrt(filename)
1818
elif framework == 'bulma':
19-
csskrter = BulmaCsskrt(filename, pretty_print)
20-
19+
csskrter = BulmaCsskrt(filename)
2120

2221
csskrter.freshify()
22+
csskrter.output(pretty_print)
2323
print()
2424
print("~~~ Done! ~~~~")
2525

test/test1.html renamed to test/input/test1.html

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ <h2> Forms ! </h2>
1616
<label for="someText"> Here is an input field </label>
1717
<input id="someText" type="text">
1818

19-
<div>
20-
<label> Option 1
21-
<input type="radio" name="my-radio">
22-
</label>
23-
<label> Option 2
24-
<input type="radio" name="my-radio">
25-
</label>
26-
<label> Option 3
27-
<input type="radio" name="my-radio">
28-
</label>
29-
</div>
19+
<label for="opt1"> Option 1 </label>
20+
<input type="radio" id="opt1" name="my-radio">
21+
<label for="opt2"> Option 2 </label>
22+
<input type="radio" id="opt2" name="my-radio">
23+
<label for="opt3"> Option 3 </label>
24+
<input type="radio" id="opt3" name="my-radio">
3025

3126
<button class="my-button-test" type="submit" onclick="alert('submitted')"> Click me</button>
3227
</form>

test/test_bootstrap.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import sys, os
2+
import copy
3+
import unittest
4+
import pytest
5+
from bs4 import BeautifulSoup
6+
7+
sys.path.insert(1, os.path.join(sys.path[0], '..'))
8+
from csskrt.bootstrapCsskrt import BootstrapCsskrt
9+
10+
11+
@pytest.fixture()
12+
def bootstrap_csskrt():
13+
bs_csskrt = BootstrapCsskrt(os.path.join(os.path.dirname(__file__), 'input/test1.html'))
14+
before = copy.copy(bs_csskrt.soup)
15+
bs_csskrt.freshify()
16+
after = bs_csskrt.soup
17+
return before, after
18+
19+
# Fix checkbox case
20+
21+
class TestBootstrapButtons():
22+
def test_compare_num_button_tags(self, bootstrap_csskrt):
23+
before: BeautifulSoup = bootstrap_csskrt[0]
24+
after: BeautifulSoup = bootstrap_csskrt[1]
25+
tag = 'button'
26+
27+
old_tags = before.find_all(tag)
28+
new_tags = after.find_all(tag)
29+
assert (len(old_tags) == len(new_tags))
30+
31+
def test_buttons_styles(self, bootstrap_csskrt):
32+
before: BeautifulSoup = bootstrap_csskrt[0]
33+
after: BeautifulSoup = bootstrap_csskrt[1]
34+
tag = 'button'
35+
style = ['btn', 'btn-primary']
36+
37+
old_tags = before.find_all(tag)
38+
new_tags = after.find_all(tag)
39+
for old_t, new_t in zip(old_tags, new_tags):
40+
old_class = old_t.get('class', [])
41+
new_class = new_t.get('class', [])
42+
43+
if type(new_class) == str: # sometimes get returns str instead of list
44+
new_class = new_class.strip().split(' ')
45+
46+
assert(set(old_class).issubset(new_class))
47+
48+
def test_buttons_content(self, bootstrap_csskrt):
49+
before: BeautifulSoup = bootstrap_csskrt[0]
50+
after: BeautifulSoup = bootstrap_csskrt[1]
51+
tag = 'button'
52+
53+
old_tags = before.find_all(tag)
54+
new_tags = after.find_all(tag)
55+
for old_t, new_t in zip(old_tags, new_tags):
56+
assert old_t.get_text() == new_t.get_text()
57+
58+
59+
class TestBootstrapForm():
60+
def test_number_form_tags(self, bootstrap_csskrt):
61+
before: BeautifulSoup = bootstrap_csskrt[0]
62+
after: BeautifulSoup = bootstrap_csskrt[1]
63+
tag = 'form'
64+
65+
old_tags = before.find_all(tag)
66+
new_tags = after.find_all(tag)
67+
assert (len(old_tags) == len(new_tags))
68+
69+
def test_form_wrapper(self, bootstrap_csskrt):
70+
before: BeautifulSoup = bootstrap_csskrt[0]
71+
after: BeautifulSoup = bootstrap_csskrt[1]
72+
tag = 'form'
73+
wrapper_class = 'form-group'
74+
75+
old_tags = before.find_all(tag)
76+
new_tags = after.find_all(tag)
77+
for form in new_tags:
78+
wrappers = form.find_all('div', recursive=False, attrs={'class': wrapper_class})
79+
inputs = form.find_all('input')
80+
assert len(wrappers) == len(inputs) # 1 input per wrapper ?
81+
82+
for wrapper in wrappers:
83+
inputs = wrapper.find_all('input')
84+
assert len(inputs) == 1
85+
86+
87+

test/test_bulma.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import sys, os
2+
import copy
3+
import pytest
4+
from bs4 import BeautifulSoup
5+
6+
sys.path.insert(1, os.path.join(sys.path[0], '..'))
7+
from csskrt.bulmaCsskrt import BulmaCsskrt
8+
9+
10+
@pytest.fixture()
11+
def bulma_csskrt():
12+
bulma_csskrt = BulmaCsskrt(os.path.join(os.path.dirname(__file__), 'input/test1.html'))
13+
before = copy.copy(bulma_csskrt.soup)
14+
bulma_csskrt.freshify()
15+
after = bulma_csskrt.soup
16+
return before, after
17+
18+
19+
class TestBulmaButtons():
20+
def test_compare_num_button_tags(self, bulma_csskrt):
21+
before: BeautifulSoup = bulma_csskrt[0]
22+
after: BeautifulSoup = bulma_csskrt[1]
23+
tag = 'button'
24+
25+
old_tags = before.find_all(tag)
26+
new_tags = after.find_all(tag)
27+
assert (len(old_tags) == len(new_tags))
28+
29+
def test_buttons_styles(self, bulma_csskrt):
30+
before: BeautifulSoup = bulma_csskrt[0]
31+
after: BeautifulSoup = bulma_csskrt[1]
32+
tag = 'button'
33+
style = ['button']
34+
35+
old_tags = before.find_all(tag)
36+
new_tags = after.find_all(tag)
37+
for old_t, new_t in zip(old_tags, new_tags):
38+
old_class = old_t.get('class', [])
39+
new_class = new_t.get('class', [])
40+
41+
if type(new_class) == str: # sometimes get returns str instead of list
42+
new_class = new_class.strip().split(' ')
43+
44+
assert(set(old_class).issubset(new_class))
45+
46+
def test_buttons_content(self, bulma_csskrt):
47+
before: BeautifulSoup = bulma_csskrt[0]
48+
after: BeautifulSoup = bulma_csskrt[1]
49+
tag = 'button'
50+
51+
old_tags = before.find_all(tag)
52+
new_tags = after.find_all(tag)
53+
for old_t, new_t in zip(old_tags, new_tags):
54+
assert old_t.get_text() == new_t.get_text()
55+
56+
57+
class TestBootstrapForm():
58+
def test_number_form_tags(self, bulma_csskrt):
59+
before: BeautifulSoup = bulma_csskrt[0]
60+
after: BeautifulSoup = bulma_csskrt[1]
61+
tag = 'form'
62+
63+
old_tags = before.find_all(tag)
64+
new_tags = after.find_all(tag)
65+
assert (len(old_tags) == len(new_tags))
66+
67+
def test_form_wrapper(self, bulma_csskrt):
68+
before: BeautifulSoup = bulma_csskrt[0]
69+
after: BeautifulSoup = bulma_csskrt[1]
70+
tag = 'form'
71+
wrapper_class = 'field'
72+
73+
old_tags = before.find_all(tag)
74+
new_tags = after.find_all(tag)
75+
for form in new_tags:
76+
wrappers = form.find_all('div', recursive=False, attrs={'class': wrapper_class})
77+
inputs = form.find_all('input')
78+
assert len(wrappers) == len(inputs) # 1 input per wrapper ?
79+
80+
for wrapper in wrappers:
81+
inputs = wrapper.find_all('input')
82+
assert len(inputs) == 1
83+
84+
85+

0 commit comments

Comments
 (0)