...

Source file src/github.com/aymanbagabas/go-osc52/v2/osc52_test.go

Documentation: github.com/aymanbagabas/go-osc52/v2

     1  package osc52
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestCopy(t *testing.T) {
     9  	cases := []struct {
    10  		name      string
    11  		str       string
    12  		clipboard Clipboard
    13  		mode      Mode
    14  		limit     int
    15  		expected  string
    16  	}{
    17  		{
    18  			name:      "hello world",
    19  			str:       "hello world",
    20  			clipboard: SystemClipboard,
    21  			mode:      DefaultMode,
    22  			limit:     0,
    23  			expected:  "\x1b]52;c;aGVsbG8gd29ybGQ=\x07",
    24  		},
    25  		{
    26  			name:      "empty string",
    27  			str:       "",
    28  			clipboard: SystemClipboard,
    29  			mode:      DefaultMode,
    30  			limit:     0,
    31  			expected:  "\x1b]52;c;\x07",
    32  		},
    33  		{
    34  			name:      "hello world primary",
    35  			str:       "hello world",
    36  			clipboard: PrimaryClipboard,
    37  			mode:      DefaultMode,
    38  			limit:     0,
    39  			expected:  "\x1b]52;p;aGVsbG8gd29ybGQ=\x07",
    40  		},
    41  		{
    42  			name:      "hello world tmux mode",
    43  			str:       "hello world",
    44  			clipboard: SystemClipboard,
    45  			mode:      TmuxMode,
    46  			limit:     0,
    47  			expected:  "\x1bPtmux;\x1b\x1b]52;c;aGVsbG8gd29ybGQ=\x07\x1b\\",
    48  		},
    49  		{
    50  			name:      "hello world screen mode",
    51  			str:       "hello world",
    52  			clipboard: SystemClipboard,
    53  			mode:      ScreenMode,
    54  			limit:     0,
    55  			expected:  "\x1bP\x1b]52;c;aGVsbG8gd29ybGQ=\x07\x1b\\",
    56  		},
    57  		{
    58  			name:      "hello world screen mode longer than 76 bytes string",
    59  			str:       "hello world hello world hello world hello world hello world hello world hello world hello world",
    60  			clipboard: SystemClipboard,
    61  			mode:      ScreenMode,
    62  			limit:     0,
    63  			expected:  "\x1bP\x1b]52;c;aGVsbG8gd29ybGQgaGVsbG8gd29ybGQgaGVsbG8gd29ybGQgaGVsbG8gd29ybGQgaGVsbG8gd29y\x1b\\\x1bPbGQgaGVsbG8gd29ybGQgaGVsbG8gd29ybGQgaGVsbG8gd29ybGQ=\a\x1b\\",
    64  		},
    65  		{
    66  			name:      "hello world with limit 11",
    67  			str:       "hello world",
    68  			clipboard: SystemClipboard,
    69  			mode:      DefaultMode,
    70  			limit:     11,
    71  			expected:  "\x1b]52;c;aGVsbG8gd29ybGQ=\x07",
    72  		},
    73  		{
    74  			name:      "hello world with limit 10",
    75  			str:       "hello world",
    76  			clipboard: SystemClipboard,
    77  			mode:      DefaultMode,
    78  			limit:     10,
    79  			expected:  "",
    80  		},
    81  	}
    82  	for _, c := range cases {
    83  		t.Run(c.name, func(t *testing.T) {
    84  			s := New(c.str)
    85  			s = s.Clipboard(c.clipboard)
    86  			s = s.Mode(c.mode)
    87  			s = s.Limit(c.limit)
    88  			if s.String() != c.expected {
    89  				t.Errorf("expected %q, got %q", c.expected, s.String())
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestQuery(t *testing.T) {
    96  	cases := []struct {
    97  		name      string
    98  		mode      Mode
    99  		clipboard Clipboard
   100  		expected  string
   101  	}{
   102  		{
   103  			name:      "query system clipboard",
   104  			mode:      DefaultMode,
   105  			clipboard: SystemClipboard,
   106  			expected:  "\x1b]52;c;?\x07",
   107  		},
   108  		{
   109  			name:      "query primary clipboard",
   110  			mode:      DefaultMode,
   111  			clipboard: PrimaryClipboard,
   112  			expected:  "\x1b]52;p;?\x07",
   113  		},
   114  		{
   115  			name:      "query system clipboard tmux mode",
   116  			mode:      TmuxMode,
   117  			clipboard: SystemClipboard,
   118  			expected:  "\x1bPtmux;\x1b\x1b]52;c;?\x07\x1b\\",
   119  		},
   120  		{
   121  			name:      "query system clipboard screen mode",
   122  			mode:      ScreenMode,
   123  			clipboard: SystemClipboard,
   124  			expected:  "\x1bP\x1b]52;c;?\x07\x1b\\",
   125  		},
   126  		{
   127  			name:      "query primary clipboard tmux mode",
   128  			mode:      TmuxMode,
   129  			clipboard: PrimaryClipboard,
   130  			expected:  "\x1bPtmux;\x1b\x1b]52;p;?\x07\x1b\\",
   131  		},
   132  		{
   133  			name:      "query primary clipboard screen mode",
   134  			mode:      ScreenMode,
   135  			clipboard: PrimaryClipboard,
   136  			expected:  "\x1bP\x1b]52;p;?\x07\x1b\\",
   137  		},
   138  	}
   139  	for _, c := range cases {
   140  		t.Run(c.name, func(t *testing.T) {
   141  			s := New().Query().Clipboard(c.clipboard).Mode(c.mode)
   142  			if s.String() != c.expected {
   143  				t.Errorf("expected %q, got %q", c.expected, s.Query())
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func TestClear(t *testing.T) {
   150  	cases := []struct {
   151  		name      string
   152  		mode      Mode
   153  		clipboard Clipboard
   154  		expected  string
   155  	}{
   156  		{
   157  			name:      "clear system clipboard",
   158  			mode:      DefaultMode,
   159  			clipboard: SystemClipboard,
   160  			expected:  "\x1b]52;c;!\x07",
   161  		},
   162  		{
   163  			name:      "clear system clipboard tmux mode",
   164  			mode:      TmuxMode,
   165  			clipboard: SystemClipboard,
   166  			expected:  "\x1bPtmux;\x1b\x1b]52;c;!\x07\x1b\\",
   167  		},
   168  		{
   169  			name:      "clear system clipboard screen mode",
   170  			mode:      ScreenMode,
   171  			clipboard: SystemClipboard,
   172  			expected:  "\x1bP\x1b]52;c;!\x07\x1b\\",
   173  		},
   174  	}
   175  	for _, c := range cases {
   176  		t.Run(c.name, func(t *testing.T) {
   177  			s := New().Clear().Clipboard(c.clipboard).Mode(c.mode)
   178  			if s.String() != c.expected {
   179  				t.Errorf("expected %q, got %q", c.expected, s.Clear())
   180  			}
   181  		})
   182  	}
   183  }
   184  
   185  func TestWriteTo(t *testing.T) {
   186  	var buf bytes.Buffer
   187  	cases := []struct {
   188  		name      string
   189  		str       string
   190  		clipboard Clipboard
   191  		mode      Mode
   192  		limit     int
   193  		expected  string
   194  	}{
   195  		{
   196  			name:      "hello world",
   197  			str:       "hello world",
   198  			clipboard: SystemClipboard,
   199  			mode:      DefaultMode,
   200  			limit:     0,
   201  			expected:  "\x1b]52;c;aGVsbG8gd29ybGQ=\x07",
   202  		},
   203  		{
   204  			name:      "empty string",
   205  			str:       "",
   206  			clipboard: SystemClipboard,
   207  			mode:      DefaultMode,
   208  			limit:     0,
   209  			expected:  "\x1b]52;c;\x07",
   210  		},
   211  	}
   212  	for _, c := range cases {
   213  		t.Run(c.name, func(t *testing.T) {
   214  			buf.Reset()
   215  			s := New(c.str)
   216  			s.Clipboard(c.clipboard)
   217  			s.Mode(c.mode)
   218  			s.Limit(c.limit)
   219  			if _, err := s.WriteTo(&buf); err != nil {
   220  				t.Errorf("expected nil, got %v", err)
   221  			}
   222  			if buf.String() != c.expected {
   223  				t.Errorf("expected %q, got %q", c.expected, buf.String())
   224  			}
   225  		})
   226  	}
   227  }
   228  

View as plain text