File: testing_test.go

package info (click to toggle)
golang-github-jeremija-gosubmit 0.2.8-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 176 kB
  • sloc: makefile: 7
file content (78 lines) | stat: -rw-r--r-- 1,403 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gosubmit

import (
	"fmt"
	"regexp"
	"testing"
)

type testMock struct {
	log     []string
	failed  bool
	helpers int
}

func (t *testMock) Fatalf(format string, values ...interface{}) {
	t.log = append(t.log, fmt.Sprintf(format, values...))
	t.failed = true
}

func (t *testMock) Helper() {
	t.helpers++
}

func TestTesting_ok(t *testing.T) {
	var f Form
	f.Inputs = Inputs{}
	input := TextInput{}
	input.name = "firstName"
	input.inputType = "text"
	f.Inputs[input.name] = input
	f.URL = "/test"

	mock := &testMock{}
	f.Testing(mock).NewTestRequest(
		Set("firstName", "John"),
	)

	if mock.failed {
		t.Errorf("Should not fail")
	}

	if len(mock.log) > 0 {
		t.Errorf("Should not write to log")
	}
}

func TestTesting_fail(t *testing.T) {
	var f Form
	f.Inputs = Inputs{}
	input := TextInput{}
	input.name = "firstName"
	input.inputType = "text"
	f.Inputs[input.name] = input
	f.URL = "/test"

	mock := &testMock{}
	f.Testing(mock).NewTestRequest(
		Set("a", "John"),
	)

	if !mock.failed {
		t.Errorf("Should have failed")
	}

	if len(mock.log) != 1 {
		t.Errorf("Should write a log entry")
	}

	if mock.helpers != 2 {
		t.Errorf("Should have marked 2 helpers, but got: %d", mock.helpers)
	}

	log := mock.log[0]
	re := regexp.MustCompile("An error occurred: Cannot find input name='a'")
	if !re.MatchString(log) {
		t.Errorf("Expected log entry to match '%s', but was '%s'", re, log)
	}
}