...

Source file src/github.com/jedib0t/go-pretty/v6/text/align_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 ExampleAlign_Apply() {
    11  	fmt.Printf("AlignDefault: '%s'\n", AlignDefault.Apply("Jon Snow", 12))
    12  	fmt.Printf("AlignLeft   : '%s'\n", AlignLeft.Apply("Jon Snow", 12))
    13  	fmt.Printf("AlignCenter : '%s'\n", AlignCenter.Apply("Jon Snow", 12))
    14  	fmt.Printf("AlignJustify: '%s'\n", AlignJustify.Apply("Jon Snow", 12))
    15  	fmt.Printf("AlignRight  : '%s'\n", AlignRight.Apply("Jon Snow", 12))
    16  
    17  	// Output: AlignDefault: 'Jon Snow    '
    18  	// AlignLeft   : 'Jon Snow    '
    19  	// AlignCenter : '  Jon Snow  '
    20  	// AlignJustify: 'Jon     Snow'
    21  	// AlignRight  : '    Jon Snow'
    22  }
    23  
    24  func TestAlign_Apply(t *testing.T) {
    25  	// AlignDefault & AlignLeft are the same
    26  	assert.Equal(t, "Jon Snow    ", AlignDefault.Apply("Jon Snow", 12))
    27  	assert.Equal(t, " Jon Snow   ", AlignDefault.Apply(" Jon Snow", 12))
    28  	assert.Equal(t, "            ", AlignDefault.Apply("", 12))
    29  	assert.Equal(t, "Jon Snow    ", AlignLeft.Apply("Jon Snow   ", 12))
    30  	assert.Equal(t, " Jon Snow   ", AlignLeft.Apply(" Jon Snow  ", 12))
    31  	assert.Equal(t, "            ", AlignLeft.Apply("", 12))
    32  
    33  	// AlignCenter
    34  	assert.Equal(t, "  Jon Snow  ", AlignCenter.Apply("Jon Snow ", 12))
    35  	assert.Equal(t, "  Jon Snow  ", AlignCenter.Apply(" Jon Snow", 12))
    36  	assert.Equal(t, "  Jon Snow  ", AlignCenter.Apply(" Jon Snow  ", 12))
    37  	assert.Equal(t, "            ", AlignCenter.Apply("", 12))
    38  
    39  	// AlignJustify
    40  	assert.Equal(t, "Jon     Snow", AlignJustify.Apply("Jon Snow", 12))
    41  	assert.Equal(t, "JS  vs.   DT", AlignJustify.Apply("JS vs. DT", 12))
    42  	assert.Equal(t, "JS   is   AT", AlignJustify.Apply("JS is AT", 12))
    43  	assert.Equal(t, "JS   is   AT", AlignJustify.Apply("JS is  AT", 12))
    44  	assert.Equal(t, "JonSnow     ", AlignJustify.Apply("JonSnow", 12))
    45  	assert.Equal(t, "JonSnow     ", AlignJustify.Apply("  JonSnow", 12))
    46  	assert.Equal(t, "            ", AlignJustify.Apply("", 12))
    47  
    48  	// Align Right
    49  	assert.Equal(t, "    Jon Snow", AlignRight.Apply("Jon Snow", 12))
    50  	assert.Equal(t, "   Jon Snow ", AlignRight.Apply("Jon Snow ", 12))
    51  	assert.Equal(t, "   Jon Snow ", AlignRight.Apply("  Jon Snow ", 12))
    52  	assert.Equal(t, "            ", AlignRight.Apply("", 12))
    53  }
    54  
    55  func TestAlign_Apply_ColoredText(t *testing.T) {
    56  	// AlignDefault & AlignLeft are the same
    57  	assert.Equal(t, "\x1b[33mJon Snow\x1b[0m    ", AlignDefault.Apply("\x1b[33mJon Snow\x1b[0m", 12))
    58  	assert.Equal(t, "\x1b[33m Jon Snow\x1b[0m   ", AlignDefault.Apply("\x1b[33m Jon Snow\x1b[0m", 12))
    59  	assert.Equal(t, "\x1b[33m\x1b[0m            ", AlignDefault.Apply("\x1b[33m\x1b[0m", 12))
    60  	assert.Equal(t, "\x1b[33mJon Snow   \x1b[0m ", AlignLeft.Apply("\x1b[33mJon Snow   \x1b[0m", 12))
    61  	assert.Equal(t, "\x1b[33m Jon Snow  \x1b[0m ", AlignLeft.Apply("\x1b[33m Jon Snow  \x1b[0m", 12))
    62  	assert.Equal(t, "\x1b[33m\x1b[0m            ", AlignLeft.Apply("\x1b[33m\x1b[0m", 12))
    63  
    64  	// AlignCenter
    65  	assert.Equal(t, "  \x1b[33mJon Snow \x1b[0m ", AlignCenter.Apply("\x1b[33mJon Snow \x1b[0m", 12))
    66  	assert.Equal(t, "  \x1b[33m Jon Snow\x1b[0m ", AlignCenter.Apply("\x1b[33m Jon Snow\x1b[0m", 12))
    67  	assert.Equal(t, " \x1b[33m Jon Snow  \x1b[0m", AlignCenter.Apply("\x1b[33m Jon Snow  \x1b[0m", 12))
    68  	assert.Equal(t, "      \x1b[33m\x1b[0m      ", AlignCenter.Apply("\x1b[33m\x1b[0m", 12))
    69  
    70  	// AlignJustify
    71  	assert.Equal(t, "\x1b[33mJon     Snow\x1b[0m", AlignJustify.Apply("\x1b[33mJon Snow\x1b[0m", 12))
    72  	assert.Equal(t, "\x1b[33mJS  vs.   DT\x1b[0m", AlignJustify.Apply("\x1b[33mJS vs. DT\x1b[0m", 12))
    73  	assert.Equal(t, "\x1b[33mJS   is   AT\x1b[0m", AlignJustify.Apply("\x1b[33mJS is AT\x1b[0m", 12))
    74  	assert.Equal(t, "\x1b[33mJS   is   AT\x1b[0m", AlignJustify.Apply("\x1b[33mJS is  AT\x1b[0m", 12))
    75  	assert.Equal(t, "\x1b[33mJonSnow\x1b[0m     ", AlignJustify.Apply("\x1b[33mJonSnow\x1b[0m", 12))
    76  	assert.Equal(t, "\x1b[33m     JonSnow\x1b[0m", AlignJustify.Apply("\x1b[33m  JonSnow\x1b[0m", 12))
    77  	assert.Equal(t, "\x1b[33m\x1b[0m            ", AlignJustify.Apply("\x1b[33m\x1b[0m", 12))
    78  
    79  	// Align Right
    80  	assert.Equal(t, "    \x1b[33mJon Snow\x1b[0m", AlignRight.Apply("\x1b[33mJon Snow\x1b[0m", 12))
    81  	assert.Equal(t, "   \x1b[33mJon Snow \x1b[0m", AlignRight.Apply("\x1b[33mJon Snow \x1b[0m", 12))
    82  	assert.Equal(t, " \x1b[33m  Jon Snow \x1b[0m", AlignRight.Apply("\x1b[33m  Jon Snow \x1b[0m", 12))
    83  	assert.Equal(t, "            \x1b[33m\x1b[0m", AlignRight.Apply("\x1b[33m\x1b[0m", 12))
    84  }
    85  
    86  func ExampleAlign_HTMLProperty() {
    87  	fmt.Printf("AlignDefault: '%s'\n", AlignDefault.HTMLProperty())
    88  	fmt.Printf("AlignLeft   : '%s'\n", AlignLeft.HTMLProperty())
    89  	fmt.Printf("AlignCenter : '%s'\n", AlignCenter.HTMLProperty())
    90  	fmt.Printf("AlignJustify: '%s'\n", AlignJustify.HTMLProperty())
    91  	fmt.Printf("AlignRight  : '%s'\n", AlignRight.HTMLProperty())
    92  
    93  	// Output: AlignDefault: ''
    94  	// AlignLeft   : 'align="left"'
    95  	// AlignCenter : 'align="center"'
    96  	// AlignJustify: 'align="justify"'
    97  	// AlignRight  : 'align="right"'
    98  }
    99  
   100  func TestAlign_HTMLProperty(t *testing.T) {
   101  	aligns := map[Align]string{
   102  		AlignDefault: "",
   103  		AlignLeft:    "left",
   104  		AlignCenter:  "center",
   105  		AlignJustify: "justify",
   106  		AlignRight:   "right",
   107  	}
   108  	for align, htmlStyle := range aligns {
   109  		assert.Contains(t, align.HTMLProperty(), htmlStyle)
   110  	}
   111  }
   112  
   113  func ExampleAlign_MarkdownProperty() {
   114  	fmt.Printf("AlignDefault: '%s'\n", AlignDefault.MarkdownProperty())
   115  	fmt.Printf("AlignLeft   : '%s'\n", AlignLeft.MarkdownProperty())
   116  	fmt.Printf("AlignCenter : '%s'\n", AlignCenter.MarkdownProperty())
   117  	fmt.Printf("AlignJustify: '%s'\n", AlignJustify.MarkdownProperty())
   118  	fmt.Printf("AlignRight  : '%s'\n", AlignRight.MarkdownProperty())
   119  
   120  	// Output: AlignDefault: ' --- '
   121  	// AlignLeft   : ':--- '
   122  	// AlignCenter : ':---:'
   123  	// AlignJustify: ' --- '
   124  	// AlignRight  : ' ---:'
   125  }
   126  
   127  func TestAlign_MarkdownProperty(t *testing.T) {
   128  	aligns := map[Align]string{
   129  		AlignDefault: " --- ",
   130  		AlignLeft:    ":--- ",
   131  		AlignCenter:  ":---:",
   132  		AlignJustify: " --- ",
   133  		AlignRight:   " ---:",
   134  	}
   135  	for align, markdownSeparator := range aligns {
   136  		assert.Contains(t, align.MarkdownProperty(), markdownSeparator)
   137  	}
   138  }
   139  

View as plain text