github.com/ethereum/go-ethereum@v1.16.1/internal/utesting/utesting_test.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum 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  	t.Parallel()
    28  
    29  	tests := []Test{
    30  		{
    31  			Name: "successful test",
    32  			Fn:   func(t *T) {},
    33  		},
    34  		{
    35  			Name: "failing test",
    36  			Fn: func(t *T) {
    37  				t.Log("output")
    38  				t.Error("failed")
    39  			},
    40  		},
    41  		{
    42  			Name: "panicking test",
    43  			Fn: func(t *T) {
    44  				panic("oh no")
    45  			},
    46  		},
    47  	}
    48  	results := RunTests(tests, nil)
    49  
    50  	if results[0].Failed || results[0].Output != "" {
    51  		t.Fatalf("wrong result for successful test: %#v", results[0])
    52  	}
    53  	if !results[1].Failed || results[1].Output != "output\nfailed\n" {
    54  		t.Fatalf("wrong result for failing test: %#v", results[1])
    55  	}
    56  	if !results[2].Failed || !strings.HasPrefix(results[2].Output, "panic: oh no\n") {
    57  		t.Fatalf("wrong result for panicking test: %#v", results[2])
    58  	}
    59  }
    60  
    61  var outputTests = []Test{
    62  	{
    63  		Name: "TestWithLogs",
    64  		Fn: func(t *T) {
    65  			t.Log("output line 1")
    66  			t.Log("output line 2\noutput line 3")
    67  		},
    68  	},
    69  	{
    70  		Name: "TestNoLogs",
    71  		Fn:   func(t *T) {},
    72  	},
    73  	{
    74  		Name: "FailWithLogs",
    75  		Fn: func(t *T) {
    76  			t.Log("output line 1")
    77  			t.Error("failed 1")
    78  		},
    79  	},
    80  	{
    81  		Name: "FailMessage",
    82  		Fn: func(t *T) {
    83  			t.Error("failed 2")
    84  		},
    85  	},
    86  	{
    87  		Name: "FailNoOutput",
    88  		Fn: func(t *T) {
    89  			t.Fail()
    90  		},
    91  	},
    92  }
    93  
    94  func TestOutput(t *testing.T) {
    95  	t.Parallel()
    96  
    97  	var buf bytes.Buffer
    98  	RunTests(outputTests, &buf)
    99  
   100  	want := regexp.MustCompile(`
   101  ^-- RUN TestWithLogs
   102   output line 1
   103   output line 2
   104   output line 3
   105  -- OK TestWithLogs \([^)]+\)
   106  -- OK TestNoLogs \([^)]+\)
   107  -- RUN FailWithLogs
   108   output line 1
   109   failed 1
   110  -- FAIL FailWithLogs \([^)]+\)
   111  -- RUN FailMessage
   112   failed 2
   113  -- FAIL FailMessage \([^)]+\)
   114  -- FAIL FailNoOutput \([^)]+\)
   115  2/5 tests passed.
   116  $`[1:])
   117  	if !want.MatchString(buf.String()) {
   118  		t.Fatalf("output does not match: %q", buf.String())
   119  	}
   120  }
   121  
   122  func TestOutputTAP(t *testing.T) {
   123  	t.Parallel()
   124  
   125  	var buf bytes.Buffer
   126  	RunTAP(outputTests, &buf)
   127  
   128  	want := `
   129  1..5
   130  ok 1 TestWithLogs
   131  # output line 1
   132  # output line 2
   133  # output line 3
   134  ok 2 TestNoLogs
   135  not ok 3 FailWithLogs
   136  # output line 1
   137  # failed 1
   138  not ok 4 FailMessage
   139  # failed 2
   140  not ok 5 FailNoOutput
   141  `
   142  	if buf.String() != want[1:] {
   143  		t.Fatalf("output does not match: %q", buf.String())
   144  	}
   145  }