Skip to content

Commit

Permalink
refactor: update github workflow and add test files
Browse files Browse the repository at this point in the history
  • Loading branch information
peanut996 committed Nov 18, 2024
1 parent abe9c87 commit bce264e
Show file tree
Hide file tree
Showing 8 changed files with 785 additions and 6 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go Build
name: Go Build and Test

on:
push:
Expand All @@ -10,8 +10,7 @@ on:
branches: [ "*" ]

jobs:

build:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -20,6 +19,10 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: true

- name: Build
run: go build -v ./...

- name: Run Tests
run: go test -race ./...
141 changes: 141 additions & 0 deletions i18n/i18n_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package i18n

import (
"os"
"testing"
)

func TestQueryI18n(t *testing.T) {
tests := []struct {
name string
lang string
messageID string
want string
}{
{
name: "english message",
lang: "en",
messageID: TestThreadCount,
want: "Latency test threads; the more threads, the faster the latency test, but do not set it too high on low-performance devices (such as routers); [maximum 1000]",
},
{
name: "chinese message",
lang: "zh",
messageID: TestThreadCount,
want: "指定延迟测试线程的数量。增加此值可以加快延迟测试过程,但不适合性能较低的设备,如路由器 [默认值为 200,最大为 1000]",
},
{
name: "default to english for unknown language",
lang: "fr",
messageID: TestThreadCount,
want: "Latency test threads; the more threads, the faster the latency test, but do not set it too high on low-performance devices (such as routers); [maximum 1000]",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save current LANG environment variable
oldLang := os.Getenv("LANG")
defer os.Setenv("LANG", oldLang)

// Set test language
os.Setenv("LANG", tt.lang)

// Reset localizer for new language
localizer = nil

got := QueryI18n(tt.messageID)
if got != tt.want {
t.Errorf("QueryI18n() = %v, want %v", got, tt.want)
}
})
}
}

func TestQueryTemplateI18n(t *testing.T) {
tests := []struct {
name string
lang string
messageID string
tpData map[string]interface{}
want string
}{
{
name: "english template",
lang: "en",
messageID: OutputResultFile,
tpData: map[string]interface{}{
"file": "test.csv",
},
want: "Write result to file; add quotes if the path contains spaces; empty value means not writing to a file [-o \"\"]; ",
},
{
name: "chinese template",
lang: "zh",
messageID: OutputResultFile,
tpData: map[string]interface{}{
"file": "test.csv",
},
want: "设置输出结果文件 [默认文件为 \"result.csv\"]",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save current LANG environment variable
oldLang := os.Getenv("LANG")
defer os.Setenv("LANG", oldLang)

// Set test language
os.Setenv("LANG", tt.lang)

// Reset localizer for new language
localizer = nil

got := QueryTemplateI18n(tt.messageID, tt.tpData)
if got != tt.want {
t.Errorf("QueryTemplateI18n() = %v, want %v", got, tt.want)
}
})
}
}

func TestInit(t *testing.T) {
tests := []struct {
name string
lang string
}{
{
name: "initialize with english",
lang: "en",
},
{
name: "initialize with chinese",
lang: "zh",
},
{
name: "initialize with unknown language",
lang: "fr",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save current LANG environment variable
oldLang := os.Getenv("LANG")
defer os.Setenv("LANG", oldLang)

// Set test language
os.Setenv("LANG", tt.lang)

// Reset and reinitialize
localizer = nil
initLocalizer()

// Verify localizer is initialized
if localizer == nil {
t.Error("initLocalizer() failed to initialize localizer")
}
})
}
}
137 changes: 137 additions & 0 deletions task/ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package task

import (
"net"
"testing"
)

func TestIsIPv4(t *testing.T) {
tests := []struct {
name string
ip string
want bool
}{
{
name: "valid ipv4",
ip: "192.168.1.1",
want: true,
},
{
name: "valid ipv6",
ip: "2001:db8::1",
want: false,
},
{
name: "empty string",
ip: "",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isIPv4(tt.ip); got != tt.want {
t.Errorf("isIPv4() = %v, want %v", got, tt.want)
}
})
}
}

func TestIPRanges_FixIP(t *testing.T) {
tests := []struct {
name string
ip string
wantIP string
wantv4 bool
}{
{
name: "ipv4 without mask",
ip: "192.168.1.1",
wantIP: "192.168.1.1/32",
wantv4: true,
},
{
name: "ipv4 with mask",
ip: "192.168.1.0/24",
wantIP: "192.168.1.0/24",
wantv4: true,
},
{
name: "ipv6 without mask",
ip: "2001:db8::1",
wantIP: "2001:db8::1/128",
wantv4: false,
},
{
name: "ipv6 with mask",
ip: "2001:db8::/64",
wantIP: "2001:db8::/64",
wantv4: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := newIPRanges()
got := r.fixIP(tt.ip)
if got != tt.wantIP {
t.Errorf("fixIP() = %v, want %v", got, tt.wantIP)
}
})
}
}

func TestIPRanges_GetIPRange(t *testing.T) {
tests := []struct {
name string
setupIP string
wantMin byte
wantHost byte
}{
{
name: "ipv4 /32",
setupIP: "192.168.1.1/32",
wantMin: 1,
wantHost: 0,
},
{
name: "ipv4 /24",
setupIP: "192.168.1.0/24",
wantMin: 0,
wantHost: 255,
},
{
name: "ipv4 /16",
setupIP: "192.168.0.0/16",
wantMin: 0,
wantHost: 255,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := newIPRanges()
r.parseCIDR(r.fixIP(tt.setupIP))
gotMin, gotHosts := r.getIPRange()
if gotMin != tt.wantMin || gotHosts != tt.wantHost {
t.Errorf("getIPRange() = (%v, %v), want (%v, %v)",
gotMin, gotHosts, tt.wantMin, tt.wantHost)
}
})
}
}

func TestIPRanges_AppendIP(t *testing.T) {
r := newIPRanges()
testIP := net.ParseIP("192.168.1.1")

r.appendIP(testIP)

if len(r.ips) != 1 {
t.Errorf("appendIP() did not append IP, got len = %v, want 1", len(r.ips))
}

if !r.ips[0].IP.Equal(testIP) {
t.Errorf("appendIP() appended wrong IP, got %v, want %v", r.ips[0].IP, testIP)
}
}
Loading

0 comments on commit bce264e

Please sign in to comment.