Golang testing.InternalExample type and RunExamples() function example

package testing

Golang testing.InternalExample type and RunExamples() function usage example

package main

 import (
  //"errors"
  "fmt"
  "strings"
  "testing"
 )

 func compareString(string1 string, string2 string) (bool, error) {
  err := error(nil)

  //if len(string1) <= 0 || len(string2) <= 0 {
  // err = errors.New("String cannot be empty")
  //}

  ok := strings.EqualFold(string2, string1)

  return ok, err
 }

 func main() {

  // http://golang.org/pkg/testing/#InternalExample

  firstExample := testing.InternalExample{Name: "Example", F: func() {}, Output: ""}
  secondExample := testing.InternalExample{Name: "Example", F: func() {}, Output: ""}

  Examples := make([]testing.InternalExample, 2)
  Examples[0] = firstExample
  Examples[1] = secondExample

  // feed all tests examples against
  complete := testing.RunExamples(compareString, Examples)

  fmt.Printf("All test examples completed? : %v\n", complete)
 }

References :

http://golang.org/pkg/testing/#InternalExample

http://golang.org/pkg/testing/#RunExamples

Advertisement