package <%= package_name %>

import (
  "bytes"
  "fmt"
  "io"
  "os"
  "strings"
  "testing"
)

func TestPerson(t *testing.T) {

  p1 := Person{Name: "Test", Age: 20}

  if p1.Name != "Test" {
    t.Error("Must be Test")
  }

  if p1.Age != 20 {
    t.Error("Must be 20")
  }

}

func TestSayHello(t *testing.T) {

  p1 := Person{Name: "Test", Age: 20}

  // Capture Stdout for test
  old := os.Stdout // keep backup of the real stdout
  r, w, _ := os.Pipe()
  os.Stdout = w

  // The real test, get logger, must be an Hello string
  p1.SayHello()

  // Analyse capture output
  outC := make(chan string)
  go func() {
    var buf bytes.Buffer
    io.Copy(&buf, r)
    outC <- buf.String()
  }()

  // back to normal state
  w.Close()
  os.Stdout = old // restoring the real stdout

  // Wait for output from go routine
  out := <-outC

  // Check the output
  testStr := fmt.Sprintf("Hello %s you are %v years old!", p1.Name, p1.Age)
  if !strings.Contains(out, testStr) {
    t.Error("String must contain: " + testStr)
  }
}

