源码索引 / CURRENT IMPLEMENTATION
learning/go-patterns/patterns/construction/construction_test.go
39 行 · 点击行号可定位;函数链接随声明位置更新,不手填行号。
跳到函数 / 声明
package construction
import (
"fmt"
"testing"
)
func TestNewServerUsesDefaults(t *testing.T) {
server := NewServer()
if got, want := server.Address(), "127.0.0.1:8080"; got != want {
t.Fatalf("Address() = %q, want %q", got, want)
}
if got := server.Config(); got != (Config{Host: "127.0.0.1", Port: 8080}) {
t.Fatalf("Config() = %#v, want defaults", got)
}
}
func TestNewServerAppliesOptions(t *testing.T) {
server := NewServer(
WithHost("api.example.test"),
WithPort(8443),
WithSecure(true),
)
want := Config{Host: "api.example.test", Port: 8443, Secure: true}
if got := server.Config(); got != want {
t.Fatalf("Config() = %#v, want %#v", got, want)
}
if got, want := server.Address(), "api.example.test:8443"; got != want {
t.Fatalf("Address() = %q, want %q", got, want)
}
}
func ExampleNewServer() {
server := NewServer(WithHost("example.test"), WithPort(443), WithSecure(true))
fmt.Println(server.Address(), server.Config().Secure)
// Output: example.test:443 true
}