github.com/core-coin/go-core/v2@v2.1.9/internal/utesting/utesting_test.go (about)

     1  // Copyright 2020 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package utesting
    18  
    19  import (
    20  	"bytes"
    21  	"regexp"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestTest(t *testing.T) {
    27  	tests := []Test{
    28  		{
    29  			Name: "successful test",
    30  			Fn:   func(t *T) {},
    31  		},
    32  		{
    33  			Name: "failing test",
    34  			Fn: func(t *T) {
    35  				t.Log("output")
    36  				t.Error("failed")
    37  			},
    38  		},
    39  		{
    40  			Name: "panicking test",
    41  			Fn: func(t *T) {
    42  				panic("oh no")
    43  			},
    44  		},
    45  	}
    46  	results := RunTests(tests, nil)
    47  
    48  	if results[0].Failed || results[0].Output != "" {
    49  		t.Fatalf("wrong result for successful test: %#v", results[0])
    50  	}
    51  	if !results[1].Failed || results[1].Output != "output\nfailed\n" {
    52  		t.Fatalf("wrong result for failing test: %#v", results[1])
    53  	}
    54  	if !results[2].Failed || !strings.HasPrefix(results[2].Output, "panic: oh no\n") {
    55  		t.Fatalf("wrong result for panicking test: %#v", results[2])
    56  	}
    57  }
    58  
    59  var outputTests = []Test{
    60  	{
    61  		Name: "TestWithLogs",
    62  		Fn: func(t *T) {
    63  			t.Log("output line 1")
    64  			t.Log("output line 2\noutput line 3")
    65  		},
    66  	},
    67  	{
    68  		Name: "TestNoLogs",
    69  		Fn:   func(t *T) {},
    70  	},
    71  	{
    72  		Name: "FailWithLogs",
    73  		Fn: func(t *T) {
    74  			t.Log("output line 1")
    75  			t.Error("failed 1")
    76  		},
    77  	},
    78  	{
    79  		Name: "FailMessage",
    80  		Fn: func(t *T) {
    81  			t.Error("failed 2")
    82  		},
    83  	},
    84  	{
    85  		Name: "FailNoOutput",
    86  		Fn: func(t *T) {
    87  			t.Fail()
    88  		},
    89  	},
    90  }
    91  
    92  func TestOutput(t *testing.T) {
    93  	var buf bytes.Buffer
    94  	RunTests(outputTests, &buf)
    95  
    96  	want := regexp.MustCompile(`
    97  ^-- RUN TestWithLogs
    98   output line 1
    99   output line 2
   100   output line 3
   101  -- OK TestWithLogs \([^)]+\)
   102  -- OK TestNoLogs \([^)]+\)
   103  -- RUN FailWithLogs
   104   output line 1
   105   failed 1
   106  -- FAIL FailWithLogs \([^)]+\)
   107  -- RUN FailMessage
   108   failed 2
   109  -- FAIL FailMessage \([^)]+\)
   110  -- FAIL FailNoOutput \([^)]+\)
   111  2/5 tests passed.
   112  $`[1:])
   113  	if !want.MatchString(buf.String()) {
   114  		t.Fatalf("output does not match: %q", buf.String())
   115  	}
   116  }
   117  
   118  func TestOutputTAP(t *testing.T) {
   119  	var buf bytes.Buffer
   120  	RunTAP(outputTests, &buf)
   121  
   122  	want := `
   123  1..5
   124  ok 1 TestWithLogs
   125  # output line 1
   126  # output line 2
   127  # output line 3
   128  ok 2 TestNoLogs
   129  not ok 3 FailWithLogs
   130  # output line 1
   131  # failed 1
   132  not ok 4 FailMessage
   133  # failed 2
   134  not ok 5 FailNoOutput
   135  `
   136  	if buf.String() != want[1:] {
   137  		t.Fatalf("output does not match: %q", buf.String())
   138  	}
   139  }