daskol

opts_test.go

Aug 2nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.28 KB | None | 0 0
  1. // Here we make several attempts to imagine the best ever semantic for type
  2. // initialization with numerous parameters.
  3. package main
  4.  
  5. import (
  6.     "errors"
  7.     "strconv"
  8.     "testing"
  9. )
  10.  
  11. var Err222 = errors.New("стватья 222 УК РФ")
  12.  
  13. type FooOptions struct {
  14.     Host string
  15.     Port uint16
  16. }
  17.  
  18. type Foo struct {
  19.     opts FooOptions
  20. }
  21.  
  22. func (f *Foo) Hostname() string {
  23.     return f.opts.Host + ":" + strconv.Itoa(int(f.opts.Port))
  24. }
  25.  
  26. // Pack parameters into structure and pass it to type constructor.
  27. func NewFoo1(opts FooOptions) (*Foo, error) {
  28.     if opts.Port == 0 {
  29.         opts.Port = 8080
  30.     } else if opts.Port == 1488 {
  31.         return nil, Err222
  32.     }
  33.  
  34.     return &Foo{opts}, nil
  35. }
  36.  
  37. func TestNewFoo1(t *testing.T) {
  38.     var foo, err = NewFoo1(FooOptions{Host: "localhost"})
  39.  
  40.     if err != nil {
  41.         t.Errorf("NewFoo1(): %s", err)
  42.     }
  43.  
  44.     if foo.Hostname() != "localhost:8080" {
  45.         t.Errorf("wrong hostname: %s", foo.Hostname())
  46.     }
  47. }
  48.  
  49. // Let FooOptions struct be Foo type builder.
  50. func (o *FooOptions) NewFoo2() (*Foo, error) {
  51.     if o.Port == 0 {
  52.         o.Port = 8080
  53.     } else if o.Port == 1488 {
  54.         return nil, Err222
  55.     }
  56.  
  57.     return &Foo{*o}, nil
  58. }
  59.  
  60. func TestNewFoo2(t *testing.T) {
  61.     var opts = FooOptions{Host: "localhost"}
  62.     var foo, err = opts.NewFoo2()
  63.  
  64.     if err != nil {
  65.         t.Errorf("NewFoo2(): %s", err)
  66.     }
  67.  
  68.     if foo.Hostname() != "localhost:8080" {
  69.         t.Errorf("wrong hostname: %s", foo.Hostname())
  70.     }
  71. }
  72.  
  73. // Enumerate option functors that mutate the state of an instantce of Foo type.
  74. type option func(foo *Foo) error
  75.  
  76. func Host(value string) option {
  77.     return func(f *Foo) error {
  78.         f.opts.Host = value
  79.         return nil
  80.     }
  81. }
  82.  
  83. func Port(value uint16) option {
  84.     return func(f *Foo) error {
  85.         if value == 0 {
  86.             value = 8080
  87.         } else if value == 1488 {
  88.             return Err222
  89.         }
  90.  
  91.         f.opts.Port = value
  92.         return nil
  93.     }
  94. }
  95.  
  96. func NewFoo3(opts ...option) (*Foo, error) {
  97.     foo := &Foo{}
  98.     foo.opts = FooOptions{
  99.         Port: 8080, // XXX: weird
  100.     }
  101.  
  102.     for _, opt := range opts {
  103.         if err := opt(foo); err != nil {
  104.             return nil, err
  105.         }
  106.     }
  107.     return foo, nil
  108. }
  109.  
  110. func TestNewFoo3(t *testing.T) {
  111.     var foo, err = NewFoo3(Host("localhost"))
  112.  
  113.     if err != nil {
  114.         t.Errorf("NewFoo2(): %s", err)
  115.     }
  116.  
  117.     if foo.Hostname() != "localhost:8080" {
  118.         t.Errorf("wrong hostname: %s", foo.Hostname())
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment