123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package door
- import (
- "flag"
- "fmt"
- "io/ioutil"
- "net"
- "os"
- "strings"
- "testing"
- "time"
- )
- func TestMenuConnection(t *testing.T) {
- tmpFile, err := ioutil.TempFile(os.TempDir(), "test-")
- if err != nil {
- panic("Cannot create temporary file")
- }
- // Remember to clean up the file afterwards
- defer os.Remove(tmpFile.Name())
- // establish network socket connection to set Comm_handle
- server, client := setupSockets()
- // We're not testing closed connections, so:
- defer server.Close()
- defer client.Close()
- // unicode 90x40 response
- buffer := []byte("\x1b[1;1R\x1b[2;3R\x1b[40;90R")
- server.Write(buffer)
- // Access Fd (File descriptor) of client for dropfile
- client_conn := client.(*net.TCPConn)
- client_file, err := client_conn.File()
- var fd int = int(client_file.Fd())
- // Create door32.sys file
- dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
- tmpFile.WriteString(fmt.Sprintf("%d\n%d\n%d\n%s\n%d\n%s\n%s\n%d\n%d\n%d\n%d\n",
- dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
- dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
- tmpFile.Close()
- d := Door{}
- // If I call d.Init() more then once flag complains about flag redefined.
- // Reset flags
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
- // preset commandline args so door can init
- os.Args = []string{"door", "-d", tmpFile.Name()}
- d.Init("menu-test")
- // clean up log file
- defer os.Remove("menu-test-12.log")
- // Ok!
- if !Unicode {
- t.Errorf("Unicode not true %t", Unicode)
- }
- if Width != 90 {
- t.Errorf("Width not 90: %d", Width)
- }
- if Height != 40 {
- t.Errorf("Height not 40: %d", Height)
- }
- // These are the commands sent to detect ... throw this all away.
- buffer = make([]byte, 1024)
- server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
- _, err = server.Read(buffer)
- // t.Errorf("Buffer : %#v\n", buffer[:r])
- server.SetReadDeadline(time.Time{})
- // Ready to test!
- m := Menu{Panel: Panel{Width: 10,
- X: 2,
- Y: 2,
- Style: DOUBLE,
- }}
- // Use simple renders for testing
- m.SelectedR = func(text string) string {
- return ColorText("BLACK ON WHITE") + text
- }
- m.UnselectedR = func(text string) string {
- return ColorText("WHI ON BLA") + text
- }
- m.AddSelection("A", "ART")
- m.AddSelection("B", "BOO")
- m.AddSelection("C", "Cat")
- var choice int
- // preload the key buffer with commands
- // BUG: I can't use \x1b[A because of the way extended codes work there.
- keys := "\x00\x50\x00\x50\r" // down, down, ENTER
- server.Write([]byte(keys))
- choice = m.Choose(&d)
- if choice != 3 {
- t.Errorf("Expected menu option C, got %d / %c", choice, m.GetOption(choice))
- }
- // Read the display output
- server.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
- r, err := server.Read(buffer)
- server.SetReadDeadline(time.Time{})
- output := string(buffer[:r])
- // t.Errorf("OUTPUT: (len %d) %#v", r, output)
- parts := strings.SplitAfter(output, "H")
- for _, part := range parts {
- t.Logf("Got: %#v", part)
- }
- m.Chosen = 1
- keys = "\x00\x48\r" // up, ENTER
- server.Write([]byte(keys))
- choice = m.Choose(&d)
- if choice != 1 {
- t.Errorf("Expected menu option A, got %d / %c", choice, m.GetOption(choice))
- }
- // Read the display output
- server.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
- r, err = server.Read(buffer)
- server.SetReadDeadline(time.Time{})
- output = string(buffer[:r])
- }
|