-
Notifications
You must be signed in to change notification settings - Fork 0
/
wotd_test.go
144 lines (128 loc) · 4.61 KB
/
wotd_test.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"regexp"
"testing"
"time"
)
func TestFindPhrase(t *testing.T) {
phrase := "abcdefghijklmnopqrstuvwxyz"
want := regexp.MustCompile("lmno")
word, err := findPhrase(phrase, "k", "p")
if !want.MatchString(word) || err != nil {
t.Fatalf(`findPhrase("lmno") = %q, want match for %#q, nil`, word, want)
}
}
func TestErrorPhraseNoStart(t *testing.T) {
phrase := "abcdefghijklmnopqrstuvwxyz"
// want := ""
word, err := findPhrase(phrase, "1", "a")
if word != "" || err == nil {
t.Fatalf(`findPhrase("") = %q, want "", error`, word)
}
}
func TestErrorPhraseNoEnd(t *testing.T) {
phrase := "abcdefghijklmnopqrstuvwxyz"
// want := ""
word, err := findPhrase(phrase, "l", "1")
if word != "" || err == nil {
t.Fatalf(`findPhrase("") = %q, want "", error`, word)
}
}
func TestColor(t *testing.T) {
phrase := "abcdefghijklmnopqrstuvwxyz"
want := "\u001b[1m\u001b[36mabcdefghijklmnopqrstuvwxyz\033[0m"
colorOutput := color(phrase, "\u001b[36m", false)
t.Logf("\n\n\n" + want + "\n" + colorOutput + "\n")
if colorOutput != want {
t.Fatalf(`color("abc", "ansi", false) = %q, want match for %#q, nil`, colorOutput, want)
}
}
func TestColorNoColor(t *testing.T) {
phrase := "abcdefghijklmnopqrstuvwxyz"
word := color(phrase, "legitanythingthisdoesntmatter", true)
if word != phrase {
t.Fatalf(`color("abc", "doesntmatter", true) = %q, want "", error`, word)
}
}
func TestGetWotd(t *testing.T) {
url := "https://www.merriam-webster.com/word-of-the-day/"
date := ""
pageContent, err := getWotd(url, &date)
word, err2 := findPhrase(pageContent, "<h1>", "</h1>")
if word == "" || err != nil || err2 != nil {
t.Fatalf(`getWotd() = %q, want match for %#q, nil`, word, "")
}
}
func TestGetWotdWithDate(t *testing.T) {
url := "https://www.merriam-webster.com/word-of-the-day/"
wantWord := "benign"
wantDef := "<em>Benign</em> means \"not causing harm or injury.\" In medicine, it refers to tumors that are not cancerous."
date := "2021-12-22"
pageContent, err := getWotd(url, &date)
word, err2 := findPhrase(pageContent, "<h1>", "</h1>")
def, err3 := findPhrase(pageContent, "<p>", "</p>")
if word != wantWord || def != wantDef || err != nil || err2 != nil || err3 != nil {
t.Fatalf(`getWotd() = %q, want %#q, nil`, word, wantWord)
}
}
func TestGetWotdWithBadFormatDate(t *testing.T) {
url := "https://www.merriam-webster.com/word-of-the-day/"
date := "2021-12-32"
pageContent, err := getWotd(url, &date)
if err == nil || pageContent != "" {
t.Fatalf(`getWotd() = %q, want %#q, nil`, pageContent, "")
}
}
func TestGetWotdWithBadFormatDate2(t *testing.T) {
url := "https://www.merriam-webster.com/word-of-the-day/"
date := "1-1-2021"
pageContent, err := getWotd(url, &date)
if err == nil || pageContent != "" {
t.Fatalf(`getWotd() = %q, want %#q, nil`, pageContent, "")
}
}
func TestGetWotdWithBadFutureDate(t *testing.T) {
url := "https://www.merriam-webster.com/word-of-the-day/"
date := time.Now().AddDate(0, 1, 0).Format("2006-01-02")
pageContent, err := getWotd(url, &date)
if err == nil || pageContent != "" {
t.Fatalf(`getWotd() = %q, want %#q, nil`, pageContent, "")
}
}
func TestErrorGetWotdBadURL(t *testing.T) {
url := "https://uuu.merriam-webster.com/word-of-the-day/"
date := ""
pageContent, err := getWotd(url, &date)
if pageContent != "" || err == nil {
t.Fatalf(`getWotd() (bad url) = %q, want match for %#q, nil`, pageContent, "")
}
}
func TestErrorGetWotdBadFormat(t *testing.T) {
url := "https://www.merriam-webster.com/word-of-the-day/2000-01-01"
date := ""
pageContent, err := getWotd(url, &date)
word, err := findPhrase(pageContent, "<h1>", "</h1>")
if word != "" || err == nil {
t.Fatalf(`getWotd() (bad web content) = %q, want match for %#q, nil`, word, "")
}
}
func TestGetSourceWebster(t *testing.T) {
source := "merriam"
date := "2021-01-01"
url, wordStart, defStart := getSource(&source, &date)
if url != "https://www.merriam-webster.com/word-of-the-day/" || wordStart != "<h1>" || defStart != "<p>" || date != "2021-01-01" {
t.Fatalf(`getSource("merriam") = %q, %q, %q`, url, defStart, defStart)
}
}
func TestGetSourceDictionary(t *testing.T) {
source := "dictionary"
date := "2021-01-01"
url, wordStart, defStart := getSource(&source, &date)
if url != "https://www.dictionary.com/e/word-of-the-day/" || wordStart != "<h1 class=\"js-fit-text\" style=\"color: #00248B\">" || defStart != "</p>\n\n \n <p>" || date != "" {
t.Fatalf(`getSource("dictionary") = %q, %q, %q`, url, defStart, defStart)
}
}
func TestMain(t *testing.T) {
// This is a bad test, it only checks if it can run without erroring, not if the Printf is accurate
main()
}