github.com/influx6/npkg@v0.8.8/ntests/test.go (about)

     1  package ntests
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  // succeedMark is the Unicode codepoint for a check mark.
    11  const succeedMark = "\u2713"
    12  
    13  var logger = log.New(os.Stdout, "", log.Lshortfile)
    14  
    15  // Header logs the info message as a header for other message.
    16  func Header(message string, val ...interface{}) {
    17  	if testing.Verbose() {
    18  		logger.Output(2, fmt.Sprintf("\t%s\n", fmt.Sprintf(message, val...)))
    19  	}
    20  }
    21  
    22  // Info logs the info message using the giving message and values.
    23  func Info(message string, val ...interface{}) {
    24  	if testing.Verbose() {
    25  		logger.Output(2, fmt.Sprintf("\t-\t %s\n", fmt.Sprintf(message, val...)))
    26  	}
    27  }
    28  
    29  // Passed logs the passing of a test using the giving message and values.
    30  func Passed(message string, val ...interface{}) {
    31  	if testing.Verbose() {
    32  		logger.Output(2, fmt.Sprintf("\t%s\t %s\n", succeedMark, fmt.Sprintf(message, val...)))
    33  	}
    34  }
    35  
    36  // PassedWithError logs the passing of a test that expects a giving error
    37  // using the giving message and values.
    38  func PassedWithError(err error, message string, val ...interface{}) {
    39  	if testing.Verbose() {
    40  		logger.Output(2, fmt.Sprintf("\t%s\t %s\n", succeedMark, fmt.Sprintf(message, val...)))
    41  		if err != nil {
    42  			logger.Output(2, fmt.Sprintf("\t-\t Received Expected Error: %+q\n", err))
    43  		}
    44  	}
    45  }
    46  
    47  // failedMark is the Unicode codepoint for an X mark.
    48  const failedMark = "\u2717"
    49  
    50  // Failed logs the failure of a test using the giving message and values.
    51  // Failed calls os.Exit(1) after printing.
    52  func Failed(message string, val ...interface{}) {
    53  	if testing.Verbose() {
    54  		logger.Output(2, fmt.Sprintf("\t%s\t %s\n", failedMark, fmt.Sprintf(message, val...)))
    55  	}
    56  
    57  	os.Exit(1)
    58  }
    59  
    60  // FailedWithError logs the failure of a test using the giving message and values.
    61  // It also shows the error under the comment.
    62  // FailedWithError calls os.Exit(1) after printing.
    63  func FailedWithError(err error, message string, val ...interface{}) {
    64  	if testing.Verbose() {
    65  		logger.Output(2, fmt.Sprintf("\t%s\t %s\n", failedMark, fmt.Sprintf(message, val...)))
    66  		if err != nil {
    67  			logger.Output(2, fmt.Sprintf("\t%s\t Error: %+q\n", "-", err))
    68  		}
    69  	}
    70  
    71  	os.Exit(1)
    72  }
    73  
    74  // Errored logs the error message using the giving message and values.
    75  func Errored(message string, val ...interface{}) {
    76  	if testing.Verbose() {
    77  		logger.Output(2, fmt.Sprintf("\t%s\t %s\n", failedMark, fmt.Sprintf(message, val...)))
    78  	}
    79  }
    80  
    81  // ErroredWithError logs the failure message using the giving message and values.
    82  // It also shows the error under the comment.
    83  func ErroredWithError(err error, message string, val ...interface{}) {
    84  	if testing.Verbose() {
    85  		logger.Output(2, fmt.Sprintf("\t%s\t %s\n", failedMark, fmt.Sprintf(message, val...)))
    86  		if err != nil {
    87  			logger.Output(2, fmt.Sprintf("\t%s\t Error: %+q\n", "-", err))
    88  		}
    89  	}
    90  }