menu_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package door
  2. import (
  3. "flag"
  4. "fmt"
  5. "net"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestMenuRender(t *testing.T) {
  13. var bracket string = ColorText("BLUE")
  14. var option string = ColorText("BRI GREEN")
  15. var upper string = ColorText("CYAN")
  16. var lower string = ColorText("MAGENTA")
  17. var render ColorRender = MakeMenuRender(bracket, option, upper, lower)
  18. // Fake menu line
  19. var input string = "[X] BUGZ test"
  20. var got string = render(input)
  21. var expected string = bracket + "[" + option + "X" + bracket + "]"
  22. expected += lower + " " + upper + "BUGZ" + lower + " test"
  23. if got != expected {
  24. t.Errorf("MenuRender expected %#v, got %#v", expected, got)
  25. }
  26. }
  27. func TestMenuSize(t *testing.T) {
  28. defer func() {
  29. if r := recover(); r == nil {
  30. t.Error("Menu Exceeding Width did not panic.")
  31. }
  32. }()
  33. var m Menu = Menu{Panel: Panel{Width: 5,
  34. X: 1,
  35. Y: 1,
  36. Style: DOUBLE,
  37. }}
  38. // len("[A] ABC") = 7 (which is > 5)
  39. m.AddSelection("A", "ABC")
  40. }
  41. func TestMenuConnection(t *testing.T) {
  42. var tmpFile *os.File
  43. var err error
  44. tmpFile, err = os.CreateTemp("", "test-*")
  45. if err != nil {
  46. panic("Cannot create temporary file")
  47. }
  48. // Remember to clean up the file afterwards
  49. defer os.Remove(tmpFile.Name())
  50. // establish network socket connection to set Comm_handle
  51. var server, client net.Conn
  52. server, client = setupSockets()
  53. // We're not testing closed connections, so:
  54. defer server.Close()
  55. defer client.Close()
  56. // pre-load unicode 90x40 response
  57. var buffer []byte = []byte(UnicodeWidthHeight(90, 40))
  58. // "\x1b[1;1R\x1b[2;3R\x1b[40;90R")
  59. _, err = server.Write(buffer)
  60. if err != nil {
  61. t.Error("server.Write:", err)
  62. }
  63. // Access Fd (File descriptor) of client for dropfile
  64. var fd int = socket_to_fd(client)
  65. defer close_fd(fd)
  66. // Create door32.sys file
  67. var node int = 8
  68. var dfc DropfileConfig = DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, node}
  69. _, err = 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",
  70. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  71. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  72. if err != nil {
  73. t.Error("tmpFile.WriteString:", err)
  74. }
  75. err = tmpFile.Close()
  76. if err != nil {
  77. t.Error("tmpFile.Close:", err)
  78. }
  79. var d Door = Door{}
  80. // Reset global variables
  81. Unicode = false
  82. CP437 = false
  83. Full_CP437 = false
  84. Width = 0
  85. Height = 0
  86. // If I call d.Init() more then once flag complains about flag redefined.
  87. // Reset flags
  88. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  89. // preset commandline args so door can init
  90. os.Args = []string{"door", "-d", tmpFile.Name()}
  91. d.Init("menu-test")
  92. defer d.Close()
  93. // clean up log file - not needed in tests
  94. defer os.Remove(fmt.Sprintf("menu-test-%d.log", node))
  95. // Ok!
  96. if !Unicode {
  97. t.Errorf("Unicode not true %t", Unicode)
  98. }
  99. if Width != 90 {
  100. t.Errorf("Width not 90: %d", Width)
  101. }
  102. if Height != 40 {
  103. t.Errorf("Height not 40: %d", Height)
  104. }
  105. clear_socket(server, t)
  106. /*
  107. // These are the commands sent to detect ... throw this all away.
  108. buffer = make([]byte, 1024)
  109. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  110. if err != nil {
  111. t.Error("server.SetReadDeadLine:", err)
  112. }
  113. _, err = server.Read(buffer)
  114. if err != nil {
  115. t.Errorf("server.Read: %s", err)
  116. }
  117. // t.Errorf("Buffer : %#v\n", buffer[:r])
  118. err = server.SetReadDeadline(time.Time{})
  119. if err != nil {
  120. t.Error("server.SetReadDeadLine:", err)
  121. }
  122. */
  123. // Ready to test!
  124. var m Menu = Menu{Panel: Panel{Width: 10,
  125. X: 2,
  126. Y: 2,
  127. Style: DOUBLE,
  128. }}
  129. // Use simple renders for testing
  130. m.SelectedR = func(text string) string {
  131. return ColorText("BLACK ON WHITE") + text
  132. }
  133. m.UnselectedR = func(text string) string {
  134. return ColorText("WHI ON BLA") + text
  135. }
  136. m.AddSelection("A", "ART")
  137. m.AddSelection("B", "BOO")
  138. m.AddSelection("C", "Cat")
  139. var choice int
  140. // preload the key buffer with commands
  141. var keys string = "\x00\x50\x1b[B\r" // down, down, ENTER
  142. _, err = server.Write([]byte(keys))
  143. if err != nil {
  144. t.Error("server.Write:", err)
  145. }
  146. time.Sleep(time.Millisecond)
  147. if d.Disconnect() {
  148. t.Errorf("Disconnected")
  149. }
  150. choice = m.Choose(&d)
  151. if choice < 0 {
  152. t.Errorf("Error <0 from Choose: %d", choice)
  153. }
  154. var option rune = m.GetOption(choice)
  155. if choice != 3 {
  156. t.Errorf("Expected menu option 3, got %d / %c", choice, option)
  157. }
  158. if option != 'C' {
  159. t.Errorf("Expected menu option C, got %c / %d", option, choice)
  160. }
  161. // Read the display output
  162. output := clear_socket(server, t)
  163. /*
  164. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  165. if err != nil {
  166. t.Error("server.SetReadDeadLine:", err)
  167. }
  168. var r int
  169. r, err = server.Read(buffer)
  170. if err != nil {
  171. t.Errorf("server.Read: %s", err)
  172. }
  173. err = server.SetReadDeadline(time.Time{})
  174. if err != nil {
  175. t.Error("server.SetReadDeadLine:", err)
  176. }
  177. output := string(buffer[:r])
  178. */
  179. // t.Errorf("OUTPUT: (len %d) %#v", r, output)
  180. parts := strings.SplitAfter(output, "H")
  181. for _, part := range parts {
  182. t.Logf("Got: %#v", part)
  183. }
  184. keys = "\x1b[A\r" // up, ENTER
  185. _, err = server.Write([]byte(keys))
  186. if err != nil {
  187. t.Errorf("server.Write: %#v", err)
  188. }
  189. time.Sleep(time.Millisecond)
  190. runtime.Gosched()
  191. if d.Disconnect() {
  192. t.Errorf("Disconnected")
  193. }
  194. m.Chosen = 1
  195. choice = m.Choose(&d)
  196. if choice < 0 {
  197. t.Errorf("Error <0 from Choose: %d", choice)
  198. }
  199. option = m.GetOption(choice)
  200. if choice != 1 {
  201. t.Errorf("Expected menu option 1, got %d / %c", choice, option)
  202. }
  203. if option != 'A' {
  204. t.Errorf("Expected menu option A, got %c / %d", option, choice)
  205. }
  206. // Read the display output
  207. clear_socket(server, t)
  208. /*
  209. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  210. if err != nil {
  211. t.Error("server.SetReadDeadLine:", err)
  212. }
  213. _, err = server.Read(buffer)
  214. if err != nil {
  215. t.Errorf("server.Read: %s", err)
  216. }
  217. err = server.SetReadDeadline(time.Time{})
  218. if err != nil {
  219. t.Error("server.SetReadDeadLine:", err)
  220. }
  221. */
  222. // TODO: Check Menu output and verify it is correct.
  223. // output = string(buffer[:r])
  224. // Test HOME, END, selecting an option, 8 & 2.
  225. keys = "\x00\x4f\r" // END, ENTER
  226. _, err = server.Write([]byte(keys))
  227. if err != nil {
  228. t.Errorf("server.Write: %#v", err)
  229. }
  230. time.Sleep(time.Millisecond)
  231. if d.Disconnect() {
  232. t.Errorf("Disconnected")
  233. }
  234. m.Chosen = 1
  235. choice = m.Choose(&d)
  236. if choice < 0 {
  237. t.Errorf("Error <0 from Choose: %d", choice)
  238. }
  239. option = m.GetOption(choice)
  240. if choice != 3 || option != 'C' {
  241. t.Errorf("Expected 3/C, got %d/%c", choice, option)
  242. }
  243. // Read the display output
  244. output = clear_socket(server, t)
  245. /*
  246. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  247. if err != nil {
  248. t.Error("server.SetReadDeadLine:", err)
  249. }
  250. _, err = server.Read(buffer)
  251. if err != nil {
  252. t.Errorf("server.Read: %s", err)
  253. }
  254. err = server.SetReadDeadline(time.Time{})
  255. if err != nil {
  256. t.Error("server.SetReadDeadLine:", err)
  257. }
  258. */
  259. // output = string(buffer[:r])
  260. keys = "\x00\x47\r" // HOME, ENTER
  261. _, err = server.Write([]byte(keys))
  262. if err != nil {
  263. t.Errorf("server.Write: %#v", err)
  264. }
  265. time.Sleep(time.Millisecond)
  266. m.Chosen = 2
  267. choice = m.Choose(&d)
  268. if choice < 0 {
  269. t.Errorf("Error <0 from Choose: %d", choice)
  270. }
  271. option = m.GetOption(choice)
  272. if choice != 1 || option != 'A' {
  273. t.Errorf("Expected 1/A, got %d/%c", choice, option)
  274. }
  275. // Read the display output
  276. output = clear_socket(server, t)
  277. /*
  278. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  279. if err != nil {
  280. t.Error("server.SetReadDeadLine:", err)
  281. }
  282. _, err = server.Read(buffer)
  283. if err != nil {
  284. t.Errorf("server.Read: %s", err)
  285. }
  286. err = server.SetReadDeadline(time.Time{})
  287. if err != nil {
  288. t.Error("server.SetReadDeadLine:", err)
  289. }
  290. */
  291. // output = string(buffer[:r])
  292. _, err = server.Write([]byte("B"))
  293. if err != nil {
  294. t.Errorf("server.Write: %#v", err)
  295. }
  296. time.Sleep(time.Millisecond)
  297. m.Chosen = 0
  298. choice = m.Choose(&d)
  299. if choice < 0 {
  300. t.Errorf("Error <0 from Choose: %d", choice)
  301. }
  302. option = m.GetOption(choice)
  303. if choice != 2 || option != 'B' {
  304. t.Errorf("Expected 2/B, got %d/%c", choice, option)
  305. }
  306. // Read the display output
  307. output = clear_socket(server, t)
  308. /*
  309. // i/o timeout
  310. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
  311. if err != nil {
  312. t.Error("server.SetReadDeadLine:", err)
  313. }
  314. _, err = server.Read(buffer)
  315. if err != nil {
  316. t.Errorf("server.Read: %s", err)
  317. }
  318. err = server.SetReadDeadline(time.Time{})
  319. if err != nil {
  320. t.Error("server.SetReadDeadLine:", err)
  321. }
  322. */
  323. // output = string(buffer[:r])
  324. keys = "2\r" // "Down", ENTER
  325. _, err = server.Write([]byte(keys))
  326. if err != nil {
  327. t.Errorf("server.Write: %#v", err)
  328. }
  329. time.Sleep(time.Millisecond)
  330. m.Chosen = 1
  331. choice = m.Choose(&d)
  332. if choice < 0 {
  333. t.Errorf("Error <0 from Choose: %d", choice)
  334. }
  335. option = m.GetOption(choice)
  336. if choice != 3 || option != 'C' {
  337. t.Errorf("Expected 3/C, got %d/%c", choice, option)
  338. }
  339. // Read the display output
  340. output = clear_socket(server, t)
  341. /*
  342. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  343. if err != nil {
  344. t.Error("server.SetReadDeadLine:", err)
  345. }
  346. _, err = server.Read(buffer)
  347. if err != nil {
  348. t.Errorf("server.Read: %s", err)
  349. }
  350. err = server.SetReadDeadline(time.Time{})
  351. if err != nil {
  352. t.Error("server.SetReadDeadLine:", err)
  353. }
  354. */
  355. // output = string(buffer[:r])
  356. server.Close()
  357. client.Close()
  358. // d.Write("\x00")
  359. time.Sleep(time.Millisecond)
  360. }