| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package kvmanager
- import "testing"
- func TestParseKVAddr(t *testing.T) {
- tests := []struct {
- name string
- input string
- wantNetwork string
- wantTarget string
- }{
- {
- name: "tcp address",
- input: "localhost:8085",
- wantNetwork: "tcp",
- wantTarget: "localhost:8085",
- },
- {
- name: "unix socket",
- input: "unix:.pizzakv.sock",
- wantNetwork: "unix",
- wantTarget: ".pizzakv.sock",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- network, target := parseKVAddr(tt.input)
- if network != tt.wantNetwork {
- t.Errorf("Expected network %q, got %q", tt.wantNetwork, network)
- }
- if target != tt.wantTarget {
- t.Errorf("Expected target %q, got %q", tt.wantTarget, target)
- }
- })
- }
- }
- func TestParseFlags(t *testing.T) {
- tests := []struct {
- name string
- input string
- expected []string
- }{
- {
- name: "empty string",
- input: "",
- expected: nil,
- },
- {
- name: "single flag",
- input: "-iwal",
- expected: []string{"-iwal"},
- },
- {
- name: "multiple flags",
- input: "-iwal -port=9090",
- expected: []string{"-iwal", "-port=9090"},
- },
- {
- name: "flags with quotes",
- input: "-path=\"/tmp/my path\" -verbose",
- expected: []string{"-path=/tmp/my path", "-verbose"},
- },
- {
- name: "flags with extra spaces",
- input: " -iwal -port=9090 ",
- expected: []string{"-iwal", "-port=9090"},
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := parseFlags(tt.input)
- if len(result) != len(tt.expected) {
- t.Errorf("Expected %d args, got %d", len(tt.expected), len(result))
- return
- }
- for i := range result {
- if result[i] != tt.expected[i] {
- t.Errorf("Arg %d: expected %q, got %q", i, tt.expected[i], result[i])
- }
- }
- })
- }
- }
- func TestParsePort(t *testing.T) {
- tests := []struct {
- name string
- input string
- expected int
- wantErr bool
- }{
- {
- name: "valid address",
- input: "localhost:8085",
- expected: 8085,
- wantErr: false,
- },
- {
- name: "valid IP address",
- input: "127.0.0.1:9090",
- expected: 9090,
- wantErr: false,
- },
- {
- name: "invalid format",
- input: "localhost",
- expected: 0,
- wantErr: true,
- },
- {
- name: "invalid port",
- input: "localhost:abc",
- expected: 0,
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, err := ParsePort(tt.input)
- if tt.wantErr {
- if err == nil {
- t.Error("Expected error but got none")
- }
- return
- }
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- return
- }
- if result != tt.expected {
- t.Errorf("Expected port %d, got %d", tt.expected, result)
- }
- })
- }
- }
- func TestKVInfo(t *testing.T) {
- info := &KVInfo{
- PID: 12345,
- Addr: "unix:.pizzakv.sock",
- }
- if info.PID != 12345 {
- t.Errorf("Expected PID 12345, got %d", info.PID)
- }
- if info.Addr != "unix:.pizzakv.sock" {
- t.Errorf("Expected Addr 'unix:.pizzakv.sock', got %s", info.Addr)
- }
- }
|