[add] tests

This commit is contained in:
Christian Fraß 2023-03-04 15:10:08 +01:00
parent 48bd734095
commit 9152e8f784
3 changed files with 187 additions and 22 deletions

View file

@ -59,3 +59,26 @@ def shell_command(command):
"stderr": result.stderr.decode(),
}
def format_bytes(bytes_):
units = [
{"label": "B", "digits": 0},
{"label": "KB", "digits": 1},
{"label": "MB", "digits": 1},
{"label": "GB", "digits": 1},
{"label": "TB", "digits": 1},
{"label": "PB", "digits": 1},
]
number = bytes_
index = 0
while ((number >= 1000) and (index < (len(units) - 1))):
number /= 1000
index += 1
return (
("%." + ("%u" % units[index]["digits"]) + "f %s")
% (
number,
units[index]["label"],
)
)

102
source/test/test.py Normal file
View file

@ -0,0 +1,102 @@
definitions = [
{
"name": "lib.string_coin",
"procedure": lambda input_: string_coin(input_["template"], input_["arguments"]),
"cases": [
{
"input": {
"template": "{{sachen}} sind {{farbe}}",
"arguments": {
"farbe": "rot",
},
},
"output": "{{sachen}} sind rot"
},
{
"input": {
"template": "{{sachen}} sind {{farbe}}",
"arguments": {
},
},
"output": "{{sachen}} sind {{farbe}}"
},
{
"input": {
"template": "{{sachen}} sind {{farbe}}",
"arguments": {
"sachen": "rosen",
"farbe": "rot",
},
},
"output": "rosen sind rot"
},
{
"input": {
"template": "{{sachen}} sind {{farbe}}",
"arguments": {
"sachen": "rosen",
"farbe": "rot",
"ort": "frankreich",
},
},
"output": "rosen sind rot"
},
],
},
{
"name": "lib.format_bytes",
"procedure": lambda input_: format_bytes(input_),
"cases": [
{
"input": 999,
"output": "999 B",
},
{
"input": 1000,
"output": "1.0 KB",
},
{
"input": 1000000,
"output": "1.0 MB",
},
{
"input": 1000000000,
"output": "1.0 GB",
},
{
"input": 1000000000000,
"output": "1.0 TB",
},
{
"input": 1000000000000000,
"output": "1.0 PB",
},
{
"input": 1000000000000000000,
"output": "1000.0 PB",
},
],
},
]
for definition in definitions:
for index in range(len(definition["cases"])):
case = definition["cases"][index]
output_actual = definition["procedure"](case["input"])
output_expected = case["output"]
passed = (output_actual == output_expected)
info = {
"input": case["input"],
"output_expected": output_expected,
"output_actual": output_actual,
}
_sys.stderr.write(
"[%s] %s.%u%s\n"
% (
("+" if passed else "x"),
definition["name"],
index,
("" if passed else (": " + _json.dumps(info))),
)
)

View file

@ -4,6 +4,7 @@ import sys as _sys
import os as _os
import json as _json
import stat as _stat
import argparse as _argparse
def file_read(path):
@ -34,7 +35,9 @@ def main():
## consts
dir_source = "source"
dir_build = "build"
sources_logic = [
targets = {
"app": {
"sources": [
_os.path.join(dir_source, "logic", "packages.py"),
_os.path.join(dir_source, "logic", "lib.py"),
_os.path.join(dir_source, "logic", "localization.py"),
@ -44,13 +47,50 @@ def main():
_os.path.join(dir_source, "logic", "checks", "script.py"),
_os.path.join(dir_source, "logic", "checks", "file_state.py"),
_os.path.join(dir_source, "logic", "checks", "http_request.py"),
_os.path.join(dir_source, "logic", "checks", "generic_remote.py"),
_os.path.join(dir_source, "logic", "channels", "_interface.py"),
_os.path.join(dir_source, "logic", "channels", "console.py"),
_os.path.join(dir_source, "logic", "channels", "email.py"),
_os.path.join(dir_source, "logic", "channels", "libnotify.py"),
_os.path.join(dir_source, "logic", "main.py"),
]
path_compilation = _os.path.join(dir_build, "heimdall")
],
"build": _os.path.join(dir_build, "heimdall"),
},
"test": {
"sources": [
_os.path.join(dir_source, "logic", "packages.py"),
_os.path.join(dir_source, "logic", "lib.py"),
_os.path.join(dir_source, "logic", "localization.py"),
_os.path.join(dir_source, "logic", "condition.py"),
_os.path.join(dir_source, "logic", "conf.py"),
_os.path.join(dir_source, "logic", "checks", "_interface.py"),
_os.path.join(dir_source, "logic", "checks", "script.py"),
_os.path.join(dir_source, "logic", "checks", "file_state.py"),
_os.path.join(dir_source, "logic", "checks", "http_request.py"),
_os.path.join(dir_source, "logic", "checks", "generic_remote.py"),
_os.path.join(dir_source, "logic", "channels", "_interface.py"),
_os.path.join(dir_source, "logic", "channels", "console.py"),
_os.path.join(dir_source, "logic", "channels", "email.py"),
_os.path.join(dir_source, "logic", "channels", "libnotify.py"),
_os.path.join(dir_source, "test", "test.py"),
],
"build": _os.path.join(dir_build, "heimdall-test"),
},
}
## args
argument_parser = _argparse.ArgumentParser(
)
argument_parser.add_argument(
"-t",
"--target",
type = str,
choices = ["app", "test"],
default = "app",
dest = "target_name",
help = "which target to build",
)
args = argument_parser.parse_args()
## exec
if (not _os.path.exists(dir_build)):
@ -81,17 +121,17 @@ def main():
)
### logic
for path in sources_logic:
for path in targets[args.target_name]["sources"]:
compilation += (file_read(path) + "\n")
### write to file
if _os.path.exists(path_compilation):
_os.remove(path_compilation)
file_write(path_compilation, compilation)
if _os.path.exists(targets[args.target_name]["build"]):
_os.remove(targets[args.target_name]["build"])
file_write(targets[args.target_name]["build"], compilation)
### postproess
_os.chmod(
path_compilation,
targets[args.target_name]["build"],
(_stat.S_IRWXU | _stat.S_IXGRP | _stat.S_IXOTH)
)