github.com/kopoli/go-terminal-size@v0.0.0-20170219200355-5c97524c8b54/size_test.go (about)

     1  package tsize
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestGetSize(t *testing.T) {
     9  	defSize := Size{10, 20}
    10  	fakeSize(defSize)
    11  	defer unFakeSize()
    12  
    13  	s, err := GetSize()
    14  
    15  	if err != nil {
    16  		t.Fatal("Failed with", err)
    17  	}
    18  
    19  	if s.Width != defSize.Width || s.Height != defSize.Height {
    20  		t.Fatal("Terminal size should not be", s.Width, s.Height)
    21  	}
    22  }
    23  
    24  func TestFgetSize(t *testing.T) {
    25  	_, err := FgetSize(nil)
    26  
    27  	if err != ErrNotATerminal {
    28  		t.Fatal("Should fail with", ErrNotATerminal)
    29  	}
    30  }
    31  
    32  func TestSizeListener(t *testing.T) {
    33  	defSize := Size{10, 20}
    34  	fakeSize(defSize)
    35  	defer unFakeSize()
    36  
    37  	sc, err := NewSizeListener()
    38  
    39  	if err != nil {
    40  		t.Fatal("Creating SizeChanger failed with", err)
    41  	}
    42  
    43  	triggerFakeResize()
    44  	select {
    45  	case s := <-sc.Change:
    46  		if s.Width != defSize.Width || s.Height != defSize.Height {
    47  			t.Fatal("Terminal size should not be", s.Width, s.Height)
    48  		}
    49  	case <-time.After(1 * time.Second):
    50  		t.Fatal("Resize didn't trigger")
    51  	}
    52  
    53  	sc.Close()
    54  	if sc.Change != nil {
    55  		t.Fatal("Closing should nil the Change channel")
    56  	}
    57  }