unit.sh
· 4.3 KiB · Bash
Surowy
#!/usr/bin/env bash
# test/unit.sh — unit test sections
# Tests pure functions directly — no binary, no state changes.
# Sourced by test.command.sh — do not execute directly.
# ============================================
# Helpers
# ============================================
function cmd::test::assert() {
local desc="${1:-}" result="${2:-}" expected="${3:-}"
if [[ "$result" == "$expected" ]]; then
test::pass "$desc"
else
test::fail "${desc} (expected '${expected}', got '${result}')"
fi
}
function cmd::test::assert_true() {
local desc="${1:-}"
shift
if "$@" 2>/dev/null; then
test::pass "$desc"
else
test::fail "$desc (expected true, got false)"
fi
}
function cmd::test::assert_false() {
local desc="${1:-}"
shift
if ! "$@" 2>/dev/null; then
test::pass "$desc"
else
test::fail "$desc (expected false, got true)"
fi
}
# ============================================
# Sections
# ============================================
function cmd::test::run_all_unit_sections() {
cmd::test::unit_subnet
cmd::test::unit_ip
cmd::test::unit_identity
}
function cmd::test::unit_subnet() {
test::section "Unit: subnet CIDR utilities"
load_module subnet
# subnet::prefix
cmd::test::assert "subnet::prefix /24" "$(subnet::prefix '10.1.3.0/24')" "10.1.3"
cmd::test::assert "subnet::prefix /16" "$(subnet::prefix '10.1.0.0/16')" "10.1.0"
cmd::test::assert "subnet::mask /24" "$(subnet::mask '10.1.3.0/24')" "24"
cmd::test::assert "subnet::mask /16" "$(subnet::mask '10.1.0.0/16')" "16"
cmd::test::assert "subnet::base_ip" "$(subnet::base_ip '10.1.3.0/24')" "10.1.3.0"
# subnet::contains
cmd::test::assert_true "subnet::contains inside" subnet::contains "10.1.3.0/24" "10.1.3.5"
cmd::test::assert_true "subnet::contains boundary" subnet::contains "10.1.3.0/24" "10.1.3.254"
cmd::test::assert_false "subnet::contains outside" subnet::contains "10.1.3.0/24" "10.1.4.1"
cmd::test::assert_false "subnet::contains wrong net" subnet::contains "10.1.3.0/24" "192.168.1.1"
# subnet::is_valid_cidr
cmd::test::assert_true "is_valid_cidr valid" subnet::is_valid_cidr "10.1.3.0/24"
cmd::test::assert_true "is_valid_cidr /16" subnet::is_valid_cidr "10.1.0.0/16"
cmd::test::assert_false "is_valid_cidr no mask" subnet::is_valid_cidr "10.1.3.0"
cmd::test::assert_false "is_valid_cidr bad octet" subnet::is_valid_cidr "999.1.3.0/24"
cmd::test::assert_false "is_valid_cidr empty" subnet::is_valid_cidr ""
# subnet::ip_valid_for
cmd::test::assert_true "ip_valid_for valid host" subnet::ip_valid_for "10.1.3.0/24" "10.1.3.5"
cmd::test::assert_false "ip_valid_for network addr" subnet::ip_valid_for "10.1.3.0/24" "10.1.3.0"
cmd::test::assert_false "ip_valid_for broadcast" subnet::ip_valid_for "10.1.3.0/24" "10.1.3.255"
cmd::test::assert_false "ip_valid_for wrong subnet" subnet::ip_valid_for "10.1.3.0/24" "10.1.4.1"
cmd::test::assert_false "ip_valid_for invalid ip" subnet::ip_valid_for "10.1.3.0/24" "not-an-ip"
}
function cmd::test::unit_ip() {
test::section "Unit: ip validation"
load_module ip
cmd::test::assert_true "ip::is_valid plain" ip::is_valid "10.1.3.5"
cmd::test::assert_true "ip::is_valid cidr" ip::is_valid "10.1.3.0/24"
cmd::test::assert_false "ip::is_valid empty" ip::is_valid ""
cmd::test::assert_false "ip::is_valid hostname" ip::is_valid "phone-nuno"
cmd::test::assert_false "ip::is_valid bad oct" ip::is_valid "999.1.3.5"
cmd::test::assert_true "ip::is_cidr with mask" ip::is_cidr "10.1.3.0/24"
cmd::test::assert_false "ip::is_cidr without mask" ip::is_cidr "10.1.3.5"
}
function cmd::test::unit_identity() {
test::section "Unit: identity inference"
load_module identity
cmd::test::assert "infer phone-nuno" "$(identity::infer 'phone-nuno')" "nuno|phone|1"
cmd::test::assert "infer phone-nuno-2" "$(identity::infer 'phone-nuno-2')" "nuno|phone|2"
cmd::test::assert "infer desktop-zephyr" "$(identity::infer 'desktop-zephyr')" "zephyr|desktop|1"
cmd::test::assert "infer laptop-nuno" "$(identity::infer 'laptop-nuno')" "nuno|laptop|1"
cmd::test::assert "infer no convention" "$(identity::infer 'roboclean')" ""
cmd::test::assert "infer guest-zephyr" "$(identity::infer 'guest-zephyr')" ""
}
| 1 | #!/usr/bin/env bash |
| 2 | # test/unit.sh — unit test sections |
| 3 | # Tests pure functions directly — no binary, no state changes. |
| 4 | # Sourced by test.command.sh — do not execute directly. |
| 5 | |
| 6 | # ============================================ |
| 7 | # Helpers |
| 8 | # ============================================ |
| 9 | |
| 10 | function cmd::test::assert() { |
| 11 | local desc="${1:-}" result="${2:-}" expected="${3:-}" |
| 12 | if [[ "$result" == "$expected" ]]; then |
| 13 | test::pass "$desc" |
| 14 | else |
| 15 | test::fail "${desc} (expected '${expected}', got '${result}')" |
| 16 | fi |
| 17 | } |
| 18 | |
| 19 | function cmd::test::assert_true() { |
| 20 | local desc="${1:-}" |
| 21 | shift |
| 22 | if "$@" 2>/dev/null; then |
| 23 | test::pass "$desc" |
| 24 | else |
| 25 | test::fail "$desc (expected true, got false)" |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | function cmd::test::assert_false() { |
| 30 | local desc="${1:-}" |
| 31 | shift |
| 32 | if ! "$@" 2>/dev/null; then |
| 33 | test::pass "$desc" |
| 34 | else |
| 35 | test::fail "$desc (expected false, got true)" |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | # ============================================ |
| 40 | # Sections |
| 41 | # ============================================ |
| 42 | |
| 43 | function cmd::test::run_all_unit_sections() { |
| 44 | cmd::test::unit_subnet |
| 45 | cmd::test::unit_ip |
| 46 | cmd::test::unit_identity |
| 47 | } |
| 48 | |
| 49 | function cmd::test::unit_subnet() { |
| 50 | test::section "Unit: subnet CIDR utilities" |
| 51 | load_module subnet |
| 52 | |
| 53 | # subnet::prefix |
| 54 | cmd::test::assert "subnet::prefix /24" "$(subnet::prefix '10.1.3.0/24')" "10.1.3" |
| 55 | cmd::test::assert "subnet::prefix /16" "$(subnet::prefix '10.1.0.0/16')" "10.1.0" |
| 56 | cmd::test::assert "subnet::mask /24" "$(subnet::mask '10.1.3.0/24')" "24" |
| 57 | cmd::test::assert "subnet::mask /16" "$(subnet::mask '10.1.0.0/16')" "16" |
| 58 | cmd::test::assert "subnet::base_ip" "$(subnet::base_ip '10.1.3.0/24')" "10.1.3.0" |
| 59 | |
| 60 | # subnet::contains |
| 61 | cmd::test::assert_true "subnet::contains inside" subnet::contains "10.1.3.0/24" "10.1.3.5" |
| 62 | cmd::test::assert_true "subnet::contains boundary" subnet::contains "10.1.3.0/24" "10.1.3.254" |
| 63 | cmd::test::assert_false "subnet::contains outside" subnet::contains "10.1.3.0/24" "10.1.4.1" |
| 64 | cmd::test::assert_false "subnet::contains wrong net" subnet::contains "10.1.3.0/24" "192.168.1.1" |
| 65 | |
| 66 | # subnet::is_valid_cidr |
| 67 | cmd::test::assert_true "is_valid_cidr valid" subnet::is_valid_cidr "10.1.3.0/24" |
| 68 | cmd::test::assert_true "is_valid_cidr /16" subnet::is_valid_cidr "10.1.0.0/16" |
| 69 | cmd::test::assert_false "is_valid_cidr no mask" subnet::is_valid_cidr "10.1.3.0" |
| 70 | cmd::test::assert_false "is_valid_cidr bad octet" subnet::is_valid_cidr "999.1.3.0/24" |
| 71 | cmd::test::assert_false "is_valid_cidr empty" subnet::is_valid_cidr "" |
| 72 | |
| 73 | # subnet::ip_valid_for |
| 74 | cmd::test::assert_true "ip_valid_for valid host" subnet::ip_valid_for "10.1.3.0/24" "10.1.3.5" |
| 75 | cmd::test::assert_false "ip_valid_for network addr" subnet::ip_valid_for "10.1.3.0/24" "10.1.3.0" |
| 76 | cmd::test::assert_false "ip_valid_for broadcast" subnet::ip_valid_for "10.1.3.0/24" "10.1.3.255" |
| 77 | cmd::test::assert_false "ip_valid_for wrong subnet" subnet::ip_valid_for "10.1.3.0/24" "10.1.4.1" |
| 78 | cmd::test::assert_false "ip_valid_for invalid ip" subnet::ip_valid_for "10.1.3.0/24" "not-an-ip" |
| 79 | } |
| 80 | |
| 81 | function cmd::test::unit_ip() { |
| 82 | test::section "Unit: ip validation" |
| 83 | load_module ip |
| 84 | |
| 85 | cmd::test::assert_true "ip::is_valid plain" ip::is_valid "10.1.3.5" |
| 86 | cmd::test::assert_true "ip::is_valid cidr" ip::is_valid "10.1.3.0/24" |
| 87 | cmd::test::assert_false "ip::is_valid empty" ip::is_valid "" |
| 88 | cmd::test::assert_false "ip::is_valid hostname" ip::is_valid "phone-nuno" |
| 89 | cmd::test::assert_false "ip::is_valid bad oct" ip::is_valid "999.1.3.5" |
| 90 | |
| 91 | cmd::test::assert_true "ip::is_cidr with mask" ip::is_cidr "10.1.3.0/24" |
| 92 | cmd::test::assert_false "ip::is_cidr without mask" ip::is_cidr "10.1.3.5" |
| 93 | } |
| 94 | |
| 95 | function cmd::test::unit_identity() { |
| 96 | test::section "Unit: identity inference" |
| 97 | load_module identity |
| 98 | |
| 99 | cmd::test::assert "infer phone-nuno" "$(identity::infer 'phone-nuno')" "nuno|phone|1" |
| 100 | cmd::test::assert "infer phone-nuno-2" "$(identity::infer 'phone-nuno-2')" "nuno|phone|2" |
| 101 | cmd::test::assert "infer desktop-zephyr" "$(identity::infer 'desktop-zephyr')" "zephyr|desktop|1" |
| 102 | cmd::test::assert "infer laptop-nuno" "$(identity::infer 'laptop-nuno')" "nuno|laptop|1" |
| 103 | cmd::test::assert "infer no convention" "$(identity::infer 'roboclean')" "" |
| 104 | cmd::test::assert "infer guest-zephyr" "$(identity::infer 'guest-zephyr')" "" |
| 105 | } |