Files
OpenFlare/backend/pkg/util/format_test.go
T
ryan 8ff017b5e8 feat(pkg/util): 补齐版本比较、网络与格式化通用助手
下游 OpenFlare 的边缘守护进程与发布流程需要这些与业务无关的纯函数,
按上游/下游归属规约回流到平台层,避免下游在上游目录里长期携带本地文件:

- version / version_compare:CompareVersions、ParseVersionInfo(版本区间比较)
- network:GetIP、IsPrivateIPv4
- format / value / string / slice:Bytes2Size、Seconds2Time、Interface2String、
  TrimStringFields、UniqueAndCleanStringSlice 与 IdentifiableTimeRecord

验证:go build 通过;go test ./... exit 0(48 包 ok);
check_cordis_architecture.sh 0 violations;golangci-lint 0 issues;gofmt 干净。
2026-08-30 00:36:13 +08:00

53 lines
995 B
Go

// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package util
import (
"testing"
)
func TestBytes2Size(t *testing.T) {
tests := []struct {
input int64
expected string
}{
{0, "0 B"},
{500, "500 B"},
{1023, "1023 B"},
{1024, "1 KB"},
{2048, "2 KB"},
{1024 * 1024, "1 MB"},
{1024 * 1024 * 1024, "1.00 GB"},
{1024 * 1024 * 1024 * 2, "2.00 GB"},
}
for _, tt := range tests {
result := Bytes2Size(tt.input)
if result != tt.expected {
t.Errorf("Bytes2Size(%d) = %q, expected %q", tt.input, result, tt.expected)
}
}
}
func TestSeconds2Time(t *testing.T) {
tests := []struct {
input int
expected string
}{
{0, "0 秒"},
{30, "30 秒"},
{60, "1 分钟 0 秒"},
{125, "2 分钟 5 秒"},
{3600, "1 小时 0 秒"},
{86400, "1 天 0 秒"},
}
for _, tt := range tests {
result := Seconds2Time(tt.input)
if result != tt.expected {
t.Errorf("Seconds2Time(%d) = %q, expected %q", tt.input, result, tt.expected)
}
}
}