Browse Source

ReadDropfile Tests. Goto Tests.

Steve Thielemann 3 years ago
parent
commit
6646d6c616
2 changed files with 97 additions and 2 deletions
  1. 2 2
      door/door.go
  2. 95 0
      door/door_test.go

+ 2 - 2
door/door.go

@@ -93,8 +93,8 @@ func (d *Door) TimeUsed() time.Duration {
 func (d *Door) ReadDropfile(filename string) {
 	file, err := os.Open(filename)
 	if err != nil {
-		fmt.Printf("Open(%s): %s\n", filename, err)
-		os.Exit(2)
+		panic(fmt.Sprintf("Open(%s): %s\n", filename, err))
+		// os.Exit(2)
 	}
 
 	defer file.Close()

+ 95 - 0
door/door_test.go

@@ -0,0 +1,95 @@
+package door
+
+import (
+	"fmt"
+	"io/ioutil"
+	"os"
+	"testing"
+	"time"
+)
+
+func TestGoto(t *testing.T) {
+	GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},
+		"\x1b[20;10H":  {10, 20},
+		"\x1b[80;120H": {120, 80},
+		"\x1b[1;1H":    {1, 1},
+	}
+
+	for text, code := range GotoMap {
+		gt := Goto(code[0], code[1])
+		if text != gt {
+			t.Errorf("Goto: Expected %#v (%#v), got %#v", text, code, gt)
+		}
+	}
+}
+
+func TestReadDropfileFail(t *testing.T) {
+	d := Door{}
+
+	defer func() {
+		if r := recover(); r == nil {
+			t.Error("ReadDropfile did not panic on missing dropfile.")
+		}
+	}()
+
+	d.ReadDropfile("This_File_Will_Not_Be_Found")
+}
+
+func TestReadDropFile(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())
+
+	dfc := DropfileConfig{2, 20, 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.WriteString("2\n20\n0\nTest BBSID\n1701\nReal Username\nHandle This\n880\n25\n0\n12\n")
+	tmpFile.Close()
+
+	d := Door{}
+	d.ReadDropfile(tmpFile.Name())
+
+	if dfc.Comm_type != d.Config.Comm_type {
+		t.Errorf("Comm Type expected %#v, got %#v", dfc.Comm_type, d.Config.Comm_type)
+	}
+	if dfc.Comm_handle != d.Config.Comm_handle {
+		t.Errorf("Comm Handle expected %#v, got %#v", dfc.Comm_handle, d.Config.Comm_handle)
+	}
+	if dfc.BBSID != d.Config.BBSID {
+		t.Errorf("BBSID expected %#v, got %#v", dfc.BBSID, d.Config.BBSID)
+	}
+	if dfc.User_number != d.Config.User_number {
+		t.Errorf("User Number expected %#v, got %#v", dfc.User_number, d.Config.User_number)
+	}
+	if dfc.Real_name != d.Config.Real_name {
+		t.Errorf("Real Name expected %#v, got %#v", dfc.Real_name, d.Config.Real_name)
+	}
+	if dfc.Handle != d.Config.Handle {
+		t.Errorf("Handle expected %#v, got %#v", dfc.Handle, d.Config.Handle)
+	}
+	if dfc.Time_left != d.Config.Time_left {
+		t.Errorf("Time Left expected %#v, got %#v", dfc.Time_left, d.Config.Time_left)
+	}
+	if dfc.Node != d.Config.Node {
+		t.Errorf("Node expected %#v, got %#v", dfc.Node, d.Config.Node)
+	}
+	start := time.Now()
+	timeout := time.Now().Add(time.Duration(dfc.Time_left) * time.Minute)
+
+	// Verify the start time and timeout values have been set correctly.
+	startDelta := start.Sub(d.StartTime)
+	timeoutDelta := timeout.Sub(d.TimeOut)
+
+	if startDelta.Seconds() > 1 {
+		t.Errorf("Start Time differences: test %#v door %#v delta %#v", start, d.StartTime, startDelta)
+	}
+	if timeoutDelta.Seconds() > 1 {
+		t.Errorf("TimeOut differences: test %#v door %#v delta %#v", timeout, d.TimeOut, timeoutDelta)
+	}
+}