chore: add various tests

This commit is contained in:
2025-12-13 14:04:32 -05:00
parent bc076a4f44
commit 8fd2aeb6a2
7 changed files with 226 additions and 7 deletions

8
flake.lock generated
View File

@@ -20,16 +20,16 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1754292888, "lastModified": 1764522689,
"narHash": "sha256-1ziydHSiDuSnaiPzCQh1mRFBsM2d2yRX9I+5OPGEmIE=", "narHash": "sha256-SqUuBFjhl/kpDiVaKLQBoD8TLD+/cTUzzgVFoaHrkqY=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "ce01daebf8489ba97bd1609d185ea276efdeb121", "rev": "8bb5646e0bed5dbd3ab08c7a7cc15b75ab4e1d0f",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-25.05", "ref": "nixos-25.11",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }

View File

@@ -2,12 +2,18 @@
description = "Development Environment"; description = "Development Environment";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
}; };
outputs = { self, nixpkgs, flake-utils }: outputs =
flake-utils.lib.eachDefaultSystem (system: { self
, nixpkgs
, flake-utils
,
}:
flake-utils.lib.eachDefaultSystem (
system:
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
in in
@@ -15,6 +21,7 @@
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
packages = with pkgs; [ packages = with pkgs; [
go go
gopls
golangci-lint golangci-lint
nodejs nodejs
tailwindcss tailwindcss

View File

@@ -0,0 +1,22 @@
package formatters
import (
"testing"
"time"
)
func TestFormatDuration(t *testing.T) {
tests := []struct {
dur time.Duration
want string
}{
{0, "N/A"},
{22*24*time.Hour + 7*time.Hour + 39*time.Minute + 31*time.Second, "22d 7h 39m 31s"},
{5*time.Minute + 15*time.Second, "5m 15s"},
}
for _, tc := range tests {
if got := FormatDuration(tc.dur); got != tc.want {
t.Errorf("FormatDuration(%v) = %s, want %s", tc.dur, got, tc.want)
}
}
}

View File

@@ -0,0 +1,22 @@
package formatters
import (
"testing"
)
func TestFormatNumber(t *testing.T) {
tests := []struct {
input int64
want string
}{
{0, "0"},
{19823, "19.8k"},
{1500000, "1.50M"},
{-12345, "-12.3k"},
}
for _, tc := range tests {
if got := FormatNumber(tc.input); got != tc.want {
t.Errorf("FormatNumber(%d) = %s, want %s", tc.input, got, tc.want)
}
}
}

73
pkg/ptr/ptr_test.go Normal file
View File

@@ -0,0 +1,73 @@
package ptr
import (
"testing"
)
func TestOf(t *testing.T) {
// Test with different types
intVal := 42
intPtr := Of(intVal)
if *intPtr != intVal {
t.Errorf("Expected %d, got %d", intVal, *intPtr)
}
stringVal := "hello"
stringPtr := Of(stringVal)
if *stringPtr != stringVal {
t.Errorf("Expected %s, got %s", stringVal, *stringPtr)
}
floatVal := 3.14
floatPtr := Of(floatVal)
if *floatPtr != floatVal {
t.Errorf("Expected %f, got %f", floatVal, *floatPtr)
}
}
func TestDeref(t *testing.T) {
// Test with non-nil pointer
intVal := 42
intPtr := Of(intVal)
result := Deref(intPtr)
if result != intVal {
t.Errorf("Expected %d, got %d", intVal, result)
}
// Test with nil pointer
var nilPtr *int
result = Deref(nilPtr)
if result != 0 {
t.Errorf("Expected 0, got %d", result)
}
// Test with string
stringVal := "hello"
stringPtr := Of(stringVal)
resultStr := Deref(stringPtr)
if resultStr != stringVal {
t.Errorf("Expected %s, got %s", stringVal, resultStr)
}
// Test with nil string pointer
var nilStrPtr *string
resultStr = Deref(nilStrPtr)
if resultStr != "" {
t.Errorf("Expected empty string, got %s", resultStr)
}
}
func TestDerefZeroValue(t *testing.T) {
// Test that Deref returns zero value for nil pointers
var nilInt *int
result := Deref(nilInt)
if result != 0 {
t.Errorf("Expected zero int, got %d", result)
}
var nilString *string
resultStr := Deref(nilString)
if resultStr != "" {
t.Errorf("Expected zero string, got %s", resultStr)
}
}

View File

@@ -0,0 +1,50 @@
package sliceutils
import (
"testing"
)
func TestFirst(t *testing.T) {
// Test with empty slice
var empty []int
result, ok := First(empty)
if ok != false {
t.Errorf("Expected ok=false for empty slice, got %v", ok)
}
if result != 0 {
t.Errorf("Expected zero value for empty slice, got %v", result)
}
// Test with non-empty slice
testSlice := []int{1, 2, 3}
result, ok = First(testSlice)
if ok != true {
t.Errorf("Expected ok=true for non-empty slice, got %v", ok)
}
if result != 1 {
t.Errorf("Expected first element, got %v", result)
}
}
func TestMap(t *testing.T) {
// Test with empty slice
var empty []int
result := Map(empty, func(x int) int { return x * 2 })
if len(result) != 0 {
t.Errorf("Expected empty result for empty input, got %v", result)
}
// Test with non-empty slice
testSlice := []int{1, 2, 3}
result = Map(testSlice, func(x int) int { return x * 2 })
expected := []int{2, 4, 6}
if len(result) != len(expected) {
t.Errorf("Expected length %d, got %d", len(expected), len(result))
}
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected %d at index %d, got %d", expected[i], i, v)
}
}
}

45
pkg/utils/utils_test.go Normal file
View File

@@ -0,0 +1,45 @@
package utils
import (
"testing"
)
func TestTernary(t *testing.T) {
// Test true condition
result := Ternary(true, 42, 13)
if result != 42 {
t.Errorf("Expected 42, got %d", result)
}
// Test false condition
result = Ternary(false, 42, 13)
if result != 13 {
t.Errorf("Expected 13, got %d", result)
}
}
func TestFirstNonZero(t *testing.T) {
// Test with int values
result := FirstNonZero(0, 0, 42, 13)
if result != 42 {
t.Errorf("Expected 42, got %d", result)
}
// Test with string values
resultStr := FirstNonZero("", "", "hello")
if resultStr != "hello" {
t.Errorf("Expected hello, got %s", resultStr)
}
// Test all zero values (strings)
zeroResultStr := FirstNonZero("")
if zeroResultStr != "" {
t.Errorf("Expected empty string, got %s", zeroResultStr)
}
// Test with float values
floatResult := FirstNonZero(0.0, 0.0, 3.14)
if floatResult != 3.14 {
t.Errorf("Expected 3.14, got %f", floatResult)
}
}