code.gitea.io/gitea@v1.19.3/modules/web/routing/funcinfo_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package routing
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  func Test_shortenFilename(t *testing.T) {
    12  	tests := []struct {
    13  		filename string
    14  		fallback string
    15  		expected string
    16  	}{
    17  		{
    18  			"code.gitea.io/routers/common/logger_context.go",
    19  			"NO_FALLBACK",
    20  			"common/logger_context.go",
    21  		},
    22  		{
    23  			"common/logger_context.go",
    24  			"NO_FALLBACK",
    25  			"common/logger_context.go",
    26  		},
    27  		{
    28  			"logger_context.go",
    29  			"NO_FALLBACK",
    30  			"logger_context.go",
    31  		},
    32  		{
    33  			"",
    34  			"USE_FALLBACK",
    35  			"USE_FALLBACK",
    36  		},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(fmt.Sprintf("shortenFilename('%s')", tt.filename), func(t *testing.T) {
    40  			if gotShort := shortenFilename(tt.filename, tt.fallback); gotShort != tt.expected {
    41  				t.Errorf("shortenFilename('%s'), expect '%s', but get '%s'", tt.filename, tt.expected, gotShort)
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func Test_trimAnonymousFunctionSuffix(t *testing.T) {
    48  	tests := []struct {
    49  		name string
    50  		want string
    51  	}{
    52  		{
    53  			"notAnonymous",
    54  			"notAnonymous",
    55  		},
    56  		{
    57  			"anonymous.func1",
    58  			"anonymous",
    59  		},
    60  		{
    61  			"notAnonymous.funca",
    62  			"notAnonymous.funca",
    63  		},
    64  		{
    65  			"anonymous.func100",
    66  			"anonymous",
    67  		},
    68  		{
    69  			"anonymous.func100.func6",
    70  			"anonymous.func100",
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			if got := trimAnonymousFunctionSuffix(tt.name); got != tt.want {
    76  				t.Errorf("trimAnonymousFunctionSuffix() = %v, want %v", got, tt.want)
    77  			}
    78  		})
    79  	}
    80  }