github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/columns/ellipsis/ellipsis_test.go (about)

     1  // Copyright 2022 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ellipsis
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func TestEllipsis(t *testing.T) {
    23  	type ellipsisTest struct {
    24  		Input     string
    25  		MaxLength int
    26  		Type      EllipsisType
    27  		Result    string
    28  	}
    29  
    30  	ellipsisTests := []ellipsisTest{
    31  		{
    32  			Input:     "Demo",
    33  			MaxLength: 8,
    34  			Type:      None,
    35  			Result:    "Demo",
    36  		},
    37  		{
    38  			Input:     "D",
    39  			MaxLength: 1,
    40  			Type:      None,
    41  			Result:    "D",
    42  		},
    43  		{
    44  			Input:     "Demo1234",
    45  			MaxLength: 4,
    46  			Type:      None,
    47  			Result:    "Demo",
    48  		},
    49  		{
    50  			Input:     "Demo1234",
    51  			MaxLength: 4,
    52  			Type:      End,
    53  			Result:    "Dem…",
    54  		},
    55  		{
    56  			Input:     "Demo1234",
    57  			MaxLength: 4,
    58  			Type:      Start,
    59  			Result:    "…234",
    60  		},
    61  		{
    62  			Input:     "Demo1234",
    63  			MaxLength: 4,
    64  			Type:      Middle,
    65  			Result:    "De…4",
    66  		},
    67  		{
    68  			Input:     "Demo1234",
    69  			MaxLength: 5,
    70  			Type:      Middle,
    71  			Result:    "De…34",
    72  		},
    73  		{
    74  			Input:     "DemoπŸ–πŸ–πŸ–πŸ–",
    75  			MaxLength: 8,
    76  			Type:      None,
    77  			Result:    "DemoπŸ–πŸ–πŸ–πŸ–",
    78  		},
    79  		{
    80  			Input:     "A",
    81  			MaxLength: 1,
    82  			Type:      None,
    83  			Result:    "A",
    84  		},
    85  		{
    86  			Input:     "Demo",
    87  			MaxLength: 0,
    88  			Type:      None,
    89  			Result:    "",
    90  		},
    91  		{
    92  			Input:     "Demo",
    93  			MaxLength: 1,
    94  			Type:      None,
    95  			Result:    "D",
    96  		},
    97  		{
    98  			Input:     "Demo",
    99  			MaxLength: 1,
   100  			Type:      Start,
   101  			Result:    "…",
   102  		},
   103  		{
   104  			Input:     "Demo",
   105  			MaxLength: 1,
   106  			Type:      End,
   107  			Result:    "…",
   108  		},
   109  		{
   110  			Input:     "Demo",
   111  			MaxLength: 1,
   112  			Type:      Middle,
   113  			Result:    "…",
   114  		},
   115  		{
   116  			Input:     "Exact",
   117  			MaxLength: 5,
   118  			Type:      None,
   119  			Result:    "Exact",
   120  		},
   121  		{
   122  			Input:     "Exact",
   123  			MaxLength: 5,
   124  			Type:      Start,
   125  			Result:    "Exact",
   126  		},
   127  		{
   128  			Input:     "Exact",
   129  			MaxLength: 5,
   130  			Type:      End,
   131  			Result:    "Exact",
   132  		},
   133  		{
   134  			Input:     "Exact",
   135  			MaxLength: 5,
   136  			Type:      Middle,
   137  			Result:    "Exact",
   138  		},
   139  		{
   140  			Input:     "Demo",
   141  			MaxLength: 3,
   142  			Type:      -1,
   143  			Result:    "Dem",
   144  		},
   145  		{
   146  			Input:     "",
   147  			MaxLength: 5,
   148  			Type:      None,
   149  			Result:    "",
   150  		},
   151  		{
   152  			Input:     "",
   153  			MaxLength: 5,
   154  			Type:      Start,
   155  			Result:    "",
   156  		},
   157  		{
   158  			Input:     "",
   159  			MaxLength: 5,
   160  			Type:      End,
   161  			Result:    "",
   162  		},
   163  		{
   164  			Input:     "",
   165  			MaxLength: 5,
   166  			Type:      Middle,
   167  			Result:    "",
   168  		},
   169  		{
   170  			Input:     "Demo",
   171  			MaxLength: 4,
   172  			Type:      None,
   173  			Result:    "Demo",
   174  		},
   175  	}
   176  
   177  	for _, test := range ellipsisTests {
   178  		t.Run(fmt.Sprintf("%s_%d@%s", test.Input, test.MaxLength, test.Type.String()), func(t *testing.T) {
   179  			if res := Shorten([]rune(test.Input), test.MaxLength, test.Type); string(res) != test.Result {
   180  				t.Errorf("%q (%d): got %q, expected %q", test.Input, test.MaxLength, res, test.Result)
   181  			}
   182  		})
   183  	}
   184  }
   185  
   186  func FuzzEllipsis(f *testing.F) {
   187  	f.Add("inputString", 5)
   188  	f.Fuzz(func(t *testing.T, inputString string, maxLength int) {
   189  		correctedLength := maxLength
   190  		if maxLength < 0 {
   191  			correctedLength = 0
   192  		}
   193  		if out := Shorten([]rune(inputString), maxLength, None); len(out) > correctedLength {
   194  			t.Errorf("None of %q (%d): wrong output %q of length %d", inputString, maxLength, out, len(out))
   195  		}
   196  		if out := Shorten([]rune(inputString), maxLength, End); len(out) > correctedLength {
   197  			t.Errorf("End of %q (%d): wrong output %q of length %d", inputString, maxLength, out, len(out))
   198  		}
   199  		if out := Shorten([]rune(inputString), maxLength, Start); len(out) > correctedLength {
   200  			t.Errorf("Start of %q (%d): wrong output %q of length %d", inputString, maxLength, out, len(out))
   201  		}
   202  		if out := Shorten([]rune(inputString), maxLength, Middle); len(out) > correctedLength {
   203  			t.Errorf("Middle of %q (%d): wrong output %q of length %d", inputString, maxLength, out, len(out))
   204  		}
   205  	})
   206  }