...

Source file src/github.com/jedib0t/go-pretty/v6/text/string_test.go

Documentation: github.com/jedib0t/go-pretty/v6/text

     1  package text
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func ExampleInsertEveryN() {
    11  	fmt.Printf("InsertEveryN(\"Ghost\", '-', 0): %#v\n", InsertEveryN("Ghost", '-', 0))
    12  	fmt.Printf("InsertEveryN(\"Ghost\", '-', 1): %#v\n", InsertEveryN("Ghost", '-', 1))
    13  	fmt.Printf("InsertEveryN(\"Ghost\", '-', 2): %#v\n", InsertEveryN("Ghost", '-', 2))
    14  	fmt.Printf("InsertEveryN(\"Ghost\", '-', 3): %#v\n", InsertEveryN("Ghost", '-', 3))
    15  	fmt.Printf("InsertEveryN(\"Ghost\", '-', 4): %#v\n", InsertEveryN("Ghost", '-', 4))
    16  	fmt.Printf("InsertEveryN(\"Ghost\", '-', 5): %#v\n", InsertEveryN("Ghost", '-', 5))
    17  	fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 0): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 0))
    18  	fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 1): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 1))
    19  	fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 2): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 2))
    20  	fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 3): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 3))
    21  	fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 4): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 4))
    22  	fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 5): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 5))
    23  
    24  	// Output: InsertEveryN("Ghost", '-', 0): "Ghost"
    25  	// InsertEveryN("Ghost", '-', 1): "G-h-o-s-t"
    26  	// InsertEveryN("Ghost", '-', 2): "Gh-os-t"
    27  	// InsertEveryN("Ghost", '-', 3): "Gho-st"
    28  	// InsertEveryN("Ghost", '-', 4): "Ghos-t"
    29  	// InsertEveryN("Ghost", '-', 5): "Ghost"
    30  	// InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 0): "\x1b[33mGhost\x1b[0m"
    31  	// InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 1): "\x1b[33mG-h-o-s-t\x1b[0m"
    32  	// InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 2): "\x1b[33mGh-os-t\x1b[0m"
    33  	// InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 3): "\x1b[33mGho-st\x1b[0m"
    34  	// InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 4): "\x1b[33mGhos-t\x1b[0m"
    35  	// InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 5): "\x1b[33mGhost\x1b[0m"
    36  }
    37  
    38  func TestInsertEveryN(t *testing.T) {
    39  	assert.Equal(t, "Ghost", InsertEveryN("Ghost", '-', 0))
    40  	assert.Equal(t, "Gツhツoツsツt", InsertEveryN("Ghost", 'ツ', 1))
    41  	assert.Equal(t, "G-h-o-s-t", InsertEveryN("Ghost", '-', 1))
    42  	assert.Equal(t, "Gh-os-t", InsertEveryN("Ghost", '-', 2))
    43  	assert.Equal(t, "Gho-st", InsertEveryN("Ghost", '-', 3))
    44  	assert.Equal(t, "Ghos-t", InsertEveryN("Ghost", '-', 4))
    45  	assert.Equal(t, "Ghost", InsertEveryN("Ghost", '-', 5))
    46  	assert.Equal(t, "\x1b[33mGhost\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 0))
    47  	assert.Equal(t, "\x1b[33mGツhツoツsツt\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", 'ツ', 1))
    48  	assert.Equal(t, "\x1b[33mG-h-o-s-t\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 1))
    49  	assert.Equal(t, "\x1b[33mGh-os-t\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 2))
    50  	assert.Equal(t, "\x1b[33mGho-st\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 3))
    51  	assert.Equal(t, "\x1b[33mGhos-t\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 4))
    52  	assert.Equal(t, "\x1b[33mGhost\x1b[0m", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 5))
    53  }
    54  
    55  func ExampleLongestLineLen() {
    56  	fmt.Printf("LongestLineLen(\"\"): %d\n", LongestLineLen(""))
    57  	fmt.Printf("LongestLineLen(\"\\n\\n\"): %d\n", LongestLineLen("\n\n"))
    58  	fmt.Printf("LongestLineLen(\"Ghost\"): %d\n", LongestLineLen("Ghost"))
    59  	fmt.Printf("LongestLineLen(\"Ghostツ\"): %d\n", LongestLineLen("Ghostツ"))
    60  	fmt.Printf("LongestLineLen(\"Winter\\nIs\\nComing\"): %d\n", LongestLineLen("Winter\nIs\nComing"))
    61  	fmt.Printf("LongestLineLen(\"Mother\\nOf\\nDragons\"): %d\n", LongestLineLen("Mother\nOf\nDragons"))
    62  	fmt.Printf("LongestLineLen(\"\\x1b[33mMother\\x1b[0m\\nOf\\nDragons\"): %d\n", LongestLineLen("\x1b[33mMother\x1b[0m\nOf\nDragons"))
    63  
    64  	// Output: LongestLineLen(""): 0
    65  	// LongestLineLen("\n\n"): 0
    66  	// LongestLineLen("Ghost"): 5
    67  	// LongestLineLen("Ghostツ"): 7
    68  	// LongestLineLen("Winter\nIs\nComing"): 6
    69  	// LongestLineLen("Mother\nOf\nDragons"): 7
    70  	// LongestLineLen("\x1b[33mMother\x1b[0m\nOf\nDragons"): 7
    71  }
    72  
    73  func TestLongestLineLen(t *testing.T) {
    74  	assert.Equal(t, 0, LongestLineLen(""))
    75  	assert.Equal(t, 0, LongestLineLen("\n\n"))
    76  	assert.Equal(t, 5, LongestLineLen("Ghost"))
    77  	assert.Equal(t, 7, LongestLineLen("Ghostツ"))
    78  	assert.Equal(t, 6, LongestLineLen("Winter\nIs\nComing"))
    79  	assert.Equal(t, 7, LongestLineLen("Mother\nOf\nDragons"))
    80  	assert.Equal(t, 7, LongestLineLen("\x1b[33mMother\x1b[0m\nOf\nDragons"))
    81  }
    82  
    83  func TestOverrideRuneWidthEastAsianWidth(t *testing.T) {
    84  	originalValue := rwCondition.EastAsianWidth
    85  	defer func() {
    86  		rwCondition.EastAsianWidth = originalValue
    87  	}()
    88  
    89  	OverrideRuneWidthEastAsianWidth(true)
    90  	assert.Equal(t, 2, RuneWidthWithoutEscSequences("╋"))
    91  	OverrideRuneWidthEastAsianWidth(false)
    92  	assert.Equal(t, 1, RuneWidthWithoutEscSequences("╋"))
    93  
    94  	// Note for posterity. We want the length of the box drawing character to
    95  	// be reported as 1. However, with an environment where LANG is set to
    96  	// something like 'zh_CN.UTF-8', the value being returned is 2, which breaks
    97  	// text alignment/padding logic in this library.
    98  	//
    99  	// If a future version of runewidth is able to address this internally and
   100  	// return 1 for the above, the function being tested can be marked for
   101  	// deprecation.
   102  }
   103  
   104  func ExamplePad() {
   105  	fmt.Printf("%#v\n", Pad("Ghost", 0, ' '))
   106  	fmt.Printf("%#v\n", Pad("Ghost", 3, ' '))
   107  	fmt.Printf("%#v\n", Pad("Ghost", 5, ' '))
   108  	fmt.Printf("%#v\n", Pad("\x1b[33mGhost\x1b[0m", 7, '-'))
   109  	fmt.Printf("%#v\n", Pad("\x1b[33mGhost\x1b[0m", 10, '.'))
   110  
   111  	// Output: "Ghost"
   112  	// "Ghost"
   113  	// "Ghost"
   114  	// "\x1b[33mGhost\x1b[0m--"
   115  	// "\x1b[33mGhost\x1b[0m....."
   116  }
   117  
   118  func TestPad(t *testing.T) {
   119  	assert.Equal(t, "Ghost", Pad("Ghost", 0, ' '))
   120  	assert.Equal(t, "Ghost", Pad("Ghost", 3, ' '))
   121  	assert.Equal(t, "Ghost", Pad("Ghost", 5, ' '))
   122  	assert.Equal(t, "Ghost  ", Pad("Ghost", 7, ' '))
   123  	assert.Equal(t, "Ghost.....", Pad("Ghost", 10, '.'))
   124  	assert.Equal(t, "\x1b[33mGhost\x1b[0  ", Pad("\x1b[33mGhost\x1b[0", 7, ' '))
   125  	assert.Equal(t, "\x1b[33mGhost\x1b[0.....", Pad("\x1b[33mGhost\x1b[0", 10, '.'))
   126  }
   127  
   128  func ExampleRepeatAndTrim() {
   129  	fmt.Printf("RepeatAndTrim(\"\", 5): %#v\n", RepeatAndTrim("", 5))
   130  	fmt.Printf("RepeatAndTrim(\"Ghost\", 0): %#v\n", RepeatAndTrim("Ghost", 0))
   131  	fmt.Printf("RepeatAndTrim(\"Ghost\", 3): %#v\n", RepeatAndTrim("Ghost", 3))
   132  	fmt.Printf("RepeatAndTrim(\"Ghost\", 5): %#v\n", RepeatAndTrim("Ghost", 5))
   133  	fmt.Printf("RepeatAndTrim(\"Ghost\", 7): %#v\n", RepeatAndTrim("Ghost", 7))
   134  	fmt.Printf("RepeatAndTrim(\"Ghost\", 10): %#v\n", RepeatAndTrim("Ghost", 10))
   135  
   136  	// Output: RepeatAndTrim("", 5): ""
   137  	// RepeatAndTrim("Ghost", 0): ""
   138  	// RepeatAndTrim("Ghost", 3): "Gho"
   139  	// RepeatAndTrim("Ghost", 5): "Ghost"
   140  	// RepeatAndTrim("Ghost", 7): "GhostGh"
   141  	// RepeatAndTrim("Ghost", 10): "GhostGhost"
   142  }
   143  
   144  func TestRepeatAndTrim(t *testing.T) {
   145  	assert.Equal(t, "", RepeatAndTrim("", 5))
   146  	assert.Equal(t, "", RepeatAndTrim("Ghost", 0))
   147  	assert.Equal(t, "Gho", RepeatAndTrim("Ghost", 3))
   148  	assert.Equal(t, "Ghost", RepeatAndTrim("Ghost", 5))
   149  	assert.Equal(t, "GhostGh", RepeatAndTrim("Ghost", 7))
   150  	assert.Equal(t, "GhostGhost", RepeatAndTrim("Ghost", 10))
   151  	assert.Equal(t, "───", RepeatAndTrim("─", 3))
   152  }
   153  
   154  func ExampleRuneCount() {
   155  	fmt.Printf("RuneCount(\"\"): %d\n", RuneCount(""))
   156  	fmt.Printf("RuneCount(\"Ghost\"): %d\n", RuneCount("Ghost"))
   157  	fmt.Printf("RuneCount(\"Ghostツ\"): %d\n", RuneCount("Ghostツ"))
   158  	fmt.Printf("RuneCount(\"\\x1b[33mGhost\\x1b[0m\"): %d\n", RuneCount("\x1b[33mGhost\x1b[0m"))
   159  	fmt.Printf("RuneCount(\"\\x1b[33mGhost\\x1b[0\"): %d\n", RuneCount("\x1b[33mGhost\x1b[0"))
   160  
   161  	// Output: RuneCount(""): 0
   162  	// RuneCount("Ghost"): 5
   163  	// RuneCount("Ghostツ"): 7
   164  	// RuneCount("\x1b[33mGhost\x1b[0m"): 5
   165  	// RuneCount("\x1b[33mGhost\x1b[0"): 5
   166  }
   167  
   168  func TestRuneCount(t *testing.T) {
   169  	assert.Equal(t, 0, RuneCount(""))
   170  	assert.Equal(t, 5, RuneCount("Ghost"))
   171  	assert.Equal(t, 7, RuneCount("Ghostツ"))
   172  	assert.Equal(t, 5, RuneCount("\x1b[33mGhost\x1b[0m"))
   173  	assert.Equal(t, 5, RuneCount("\x1b[33mGhost\x1b[0"))
   174  }
   175  
   176  func ExampleRuneWidth() {
   177  	fmt.Printf("RuneWidth('A'): %d\n", RuneWidth('A'))
   178  	fmt.Printf("RuneWidth('ツ'): %d\n", RuneWidth('ツ'))
   179  	fmt.Printf("RuneWidth('⊙'): %d\n", RuneWidth('⊙'))
   180  	fmt.Printf("RuneWidth('︿'): %d\n", RuneWidth('︿'))
   181  	fmt.Printf("RuneWidth(rune(27)): %d\n", RuneWidth(rune(27))) // ANSI escape sequence
   182  
   183  	// Output: RuneWidth('A'): 1
   184  	// RuneWidth('ツ'): 2
   185  	// RuneWidth('⊙'): 1
   186  	// RuneWidth('︿'): 2
   187  	// RuneWidth(rune(27)): 0
   188  }
   189  
   190  func TestRuneWidth(t *testing.T) {
   191  	assert.Equal(t, 1, RuneWidth('A'))
   192  	assert.Equal(t, 2, RuneWidth('ツ'))
   193  	assert.Equal(t, 1, RuneWidth('⊙'))
   194  	assert.Equal(t, 2, RuneWidth('︿'))
   195  	assert.Equal(t, 0, RuneWidth(rune(27))) // ANSI escape sequence
   196  }
   197  
   198  func ExampleRuneWidthWithoutEscSequences() {
   199  	fmt.Printf("RuneWidthWithoutEscSequences(\"\"): %d\n", RuneWidthWithoutEscSequences(""))
   200  	fmt.Printf("RuneWidthWithoutEscSequences(\"Ghost\"): %d\n", RuneWidthWithoutEscSequences("Ghost"))
   201  	fmt.Printf("RuneWidthWithoutEscSequences(\"Ghostツ\"): %d\n", RuneWidthWithoutEscSequences("Ghostツ"))
   202  	fmt.Printf("RuneWidthWithoutEscSequences(\"\\x1b[33mGhost\\x1b[0m\"): %d\n", RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"))
   203  	fmt.Printf("RuneWidthWithoutEscSequences(\"\\x1b[33mGhost\\x1b[0\"): %d\n", RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"))
   204  
   205  	// Output: RuneWidthWithoutEscSequences(""): 0
   206  	// RuneWidthWithoutEscSequences("Ghost"): 5
   207  	// RuneWidthWithoutEscSequences("Ghostツ"): 7
   208  	// RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"): 5
   209  	// RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"): 5
   210  }
   211  
   212  func TestRuneWidthWithoutEscSequences(t *testing.T) {
   213  	assert.Equal(t, 0, RuneWidthWithoutEscSequences(""))
   214  	assert.Equal(t, 5, RuneWidthWithoutEscSequences("Ghost"))
   215  	assert.Equal(t, 7, RuneWidthWithoutEscSequences("Ghostツ"))
   216  	assert.Equal(t, 5, RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"))
   217  	assert.Equal(t, 5, RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"))
   218  }
   219  
   220  func ExampleSnip() {
   221  	fmt.Printf("Snip(\"Ghost\", 0, \"~\"): %#v\n", Snip("Ghost", 0, "~"))
   222  	fmt.Printf("Snip(\"Ghost\", 1, \"~\"): %#v\n", Snip("Ghost", 1, "~"))
   223  	fmt.Printf("Snip(\"Ghost\", 3, \"~\"): %#v\n", Snip("Ghost", 3, "~"))
   224  	fmt.Printf("Snip(\"Ghost\", 5, \"~\"): %#v\n", Snip("Ghost", 5, "~"))
   225  	fmt.Printf("Snip(\"Ghost\", 7, \"~\"): %#v\n", Snip("Ghost", 7, "~"))
   226  	fmt.Printf("Snip(\"\\x1b[33mGhost\\x1b[0m\", 7, \"~\"): %#v\n", Snip("\x1b[33mGhost\x1b[0m", 7, "~"))
   227  
   228  	// Output: Snip("Ghost", 0, "~"): "Ghost"
   229  	// Snip("Ghost", 1, "~"): "~"
   230  	// Snip("Ghost", 3, "~"): "Gh~"
   231  	// Snip("Ghost", 5, "~"): "Ghost"
   232  	// Snip("Ghost", 7, "~"): "Ghost"
   233  	// Snip("\x1b[33mGhost\x1b[0m", 7, "~"): "\x1b[33mGhost\x1b[0m"
   234  }
   235  
   236  func TestSnip(t *testing.T) {
   237  	assert.Equal(t, "Ghost", Snip("Ghost", 0, "~"))
   238  	assert.Equal(t, "~", Snip("Ghost", 1, "~"))
   239  	assert.Equal(t, "Gh~", Snip("Ghost", 3, "~"))
   240  	assert.Equal(t, "Ghost", Snip("Ghost", 5, "~"))
   241  	assert.Equal(t, "Ghost", Snip("Ghost", 7, "~"))
   242  	assert.Equal(t, "\x1b[33mGhost\x1b[0m", Snip("\x1b[33mGhost\x1b[0m", 7, "~"))
   243  }
   244  
   245  func ExampleTrim() {
   246  	fmt.Printf("Trim(\"Ghost\", 0): %#v\n", Trim("Ghost", 0))
   247  	fmt.Printf("Trim(\"Ghost\", 3): %#v\n", Trim("Ghost", 3))
   248  	fmt.Printf("Trim(\"Ghost\", 6): %#v\n", Trim("Ghost", 6))
   249  	fmt.Printf("Trim(\"\\x1b[33mGhost\\x1b[0m\", 0): %#v\n", Trim("\x1b[33mGhost\x1b[0m", 0))
   250  	fmt.Printf("Trim(\"\\x1b[33mGhost\\x1b[0m\", 3): %#v\n", Trim("\x1b[33mGhost\x1b[0m", 3))
   251  	fmt.Printf("Trim(\"\\x1b[33mGhost\\x1b[0m\", 6): %#v\n", Trim("\x1b[33mGhost\x1b[0m", 6))
   252  
   253  	// Output: Trim("Ghost", 0): ""
   254  	// Trim("Ghost", 3): "Gho"
   255  	// Trim("Ghost", 6): "Ghost"
   256  	// Trim("\x1b[33mGhost\x1b[0m", 0): ""
   257  	// Trim("\x1b[33mGhost\x1b[0m", 3): "\x1b[33mGho\x1b[0m"
   258  	// Trim("\x1b[33mGhost\x1b[0m", 6): "\x1b[33mGhost\x1b[0m"
   259  }
   260  
   261  func TestTrim(t *testing.T) {
   262  	assert.Equal(t, "", Trim("Ghost", 0))
   263  	assert.Equal(t, "Gho", Trim("Ghost", 3))
   264  	assert.Equal(t, "Ghost", Trim("Ghost", 6))
   265  	assert.Equal(t, "\x1b[33mGho\x1b[0m", Trim("\x1b[33mGhost\x1b[0m", 3))
   266  	assert.Equal(t, "\x1b[33mGhost\x1b[0m", Trim("\x1b[33mGhost\x1b[0m", 6))
   267  }
   268  

View as plain text