-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update github workflow and add test files
- Loading branch information
Showing
8 changed files
with
785 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.