From 6b9195000eb5404c247288b384d7ca2bacc1ab23 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 6 Jun 2020 10:09:27 +0200 Subject: Add unit tests --- README.md | 6 ++++++ scripts/test | 4 ++++ shell.nix | 1 + src/gui/tasks/test_duration.py | 21 +++++++++++++++++++++ src/util/array.py | 2 +- src/util/range.py | 4 ++-- src/util/test_array.py | 12 ++++++++++++ src/util/test_range.py | 6 ++++++ 8 files changed, 53 insertions(+), 3 deletions(-) create mode 100755 scripts/test create mode 100644 src/gui/tasks/test_duration.py create mode 100644 src/util/test_array.py create mode 100644 src/util/test_range.py diff --git a/README.md b/README.md index 1040b41..e323a4d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ Manage a context-based next-action list, compatible with the nix-shell --run "python src/main.py" ``` +## Tests + +```bash +scripts/test +``` + ## Links - [Python standard library](https://docs.python.org/3.8/library/index.html) diff --git a/scripts/test b/scripts/test new file mode 100755 index 0000000..9b3fae7 --- /dev/null +++ b/scripts/test @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname $0)/../src" +python -m pytest diff --git a/shell.nix b/shell.nix index 608e7c1..bb95254 100644 --- a/shell.nix +++ b/shell.nix @@ -14,6 +14,7 @@ mkShell { propagatedBuildInputs = [ python38Packages.pyqt5 + python38Packages.pytest ]; } diff --git a/src/gui/tasks/test_duration.py b/src/gui/tasks/test_duration.py new file mode 100644 index 0000000..9d5d9b8 --- /dev/null +++ b/src/gui/tasks/test_duration.py @@ -0,0 +1,21 @@ +from gui.tasks.duration import format, parse + +def test_format(): + assert format(0) == "" + assert format(0.5) == "0.5m" + assert format(35) == "35m" + assert format(60) == "1h" + assert format(61) == "1h" + assert format(90) == "1.5h" + assert format(1440) == "1d" + +def test_parse(): + assert parse("") == 0 + assert parse("42") == None + assert parse("hey") == None + assert parse("1h30") == None + assert parse("1h30m") == None + assert parse("17m") == 17 + assert parse("90m") == 90 + assert parse("1.5h") == 90 + assert parse("2d") == 2880 diff --git a/src/util/array.py b/src/util/array.py index 7dd0357..bb4eee3 100644 --- a/src/util/array.py +++ b/src/util/array.py @@ -1,5 +1,5 @@ def insert_position(x, xs, is_reversed: bool) -> int: for i, y in enumerate(xs): - if is_reversed and x > y or not is_reversed and x < y: + if is_reversed and x >= y or not is_reversed and x <= y: return i return len(xs) diff --git a/src/util/range.py b/src/util/range.py index c75232c..bd4b27e 100644 --- a/src/util/range.py +++ b/src/util/range.py @@ -1,10 +1,10 @@ -from typing import NamedTuple +from typing import NamedTuple, List class Range(NamedTuple): start: int length: int -def from_indexes(indexes): +def from_indexes(indexes: List[int]) -> List[Range]: ranges = [] curr_range_start = 0 curr_range_len = 0 diff --git a/src/util/test_array.py b/src/util/test_array.py new file mode 100644 index 0000000..38759b9 --- /dev/null +++ b/src/util/test_array.py @@ -0,0 +1,12 @@ +from array import insert_position + +def test_insert_position(): + assert insert_position(0, [], False) == 0 + assert insert_position(1, [1, 2, 3], False) == 0 + assert insert_position(2, [1, 2, 3], False) == 1 + assert insert_position(3, [1, 2, 3], False) == 2 + assert insert_position(8, [1, 2, 3], False) == 3 + assert insert_position(8, [3, 2, 1], True) == 0 + assert insert_position(3, [3, 2, 1], True) == 0 + assert insert_position(2, [3, 2, 1], True) == 1 + assert insert_position(1, [3, 2, 1], True) == 2 diff --git a/src/util/test_range.py b/src/util/test_range.py new file mode 100644 index 0000000..0bd909b --- /dev/null +++ b/src/util/test_range.py @@ -0,0 +1,6 @@ +from range import from_indexes, Range + +def test_from_indexes(): + assert from_indexes([]) == [] + assert from_indexes([1]) == [Range(1, 1)] + assert from_indexes([9, 6, 0, 10]) == [Range(0, 1), Range(6, 1), Range(9, 2)] -- cgit v1.2.3