...

Source file src/codeberg.org/tslocum/cbind/configuration_test.go

Documentation: codeberg.org/tslocum/cbind

     1  package cbind
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/gdamore/tcell/v3"
    11  )
    12  
    13  const pressTimes = 7
    14  
    15  func TestConfiguration(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	wg := make([]*sync.WaitGroup, len(testCases))
    19  
    20  	config := NewConfiguration()
    21  	for i, c := range testCases {
    22  		wg[i] = new(sync.WaitGroup)
    23  		wg[i].Add(pressTimes)
    24  
    25  		i := i // Capture
    26  		err := config.Set(c.encoded, func(ev *tcell.EventKey) *tcell.EventKey {
    27  			wg[i].Done()
    28  			return nil
    29  		})
    30  		if err != nil {
    31  			t.Fatalf("failed to set keybind for %s: %s", c.encoded, err)
    32  		}
    33  	}
    34  
    35  	done := make(chan struct{})
    36  	timeout := time.After(5 * time.Second)
    37  
    38  	go func() {
    39  		for i := range testCases {
    40  			wg[i].Wait()
    41  		}
    42  
    43  		done <- struct{}{}
    44  	}()
    45  
    46  	errs := make(chan error)
    47  	for j := 0; j < pressTimes; j++ {
    48  		for i, c := range testCases {
    49  			i, c := i, c // Capture
    50  			go func() {
    51  				ev := config.Capture(tcell.NewEventKey(c.key, c.str, c.mod))
    52  				if ev != nil {
    53  					errs <- fmt.Errorf("failed to test capturing keybinds: failed to register case %d event %d %d %s", i, c.mod, c.key, c.str)
    54  				}
    55  			}()
    56  		}
    57  	}
    58  
    59  	select {
    60  	case err := <-errs:
    61  		t.Fatal(err)
    62  	case <-timeout:
    63  		t.Fatal("timeout")
    64  	case <-done:
    65  	}
    66  }
    67  
    68  // Example of creating and using an input configuration.
    69  func ExampleNewConfiguration() {
    70  	// Create a new input configuration to store the key bindings.
    71  	c := NewConfiguration()
    72  
    73  	handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
    74  		// Save
    75  		return nil
    76  	}
    77  
    78  	handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
    79  		// Open
    80  		return nil
    81  	}
    82  
    83  	handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
    84  		// Exit
    85  		return nil
    86  	}
    87  
    88  	// Bind Alt+s.
    89  	if err := c.Set("Alt+s", handleSave); err != nil {
    90  		log.Fatalf("failed to set keybind: %s", err)
    91  	}
    92  
    93  	// Bind Alt+o.
    94  	c.SetRune(tcell.ModAlt, 'o', handleOpen)
    95  
    96  	// Bind Escape.
    97  	c.SetKey(tcell.ModNone, tcell.KeyEscape, handleExit)
    98  
    99  	// Capture input. This will differ based on the framework in use (if any).
   100  	// When using tview or cview, call Application.SetInputCapture before calling
   101  	// Application.Run.
   102  	// app.SetInputCapture(c.Capture)
   103  }
   104  
   105  // Example of capturing key events.
   106  func ExampleConfiguration_Capture() {
   107  	// See the end of the NewConfiguration example.
   108  }
   109  

View as plain text