Skip to content

Commit

Permalink
style: go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Watt3r committed Dec 31, 2021
1 parent 88a8b4b commit d076a5a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 110 deletions.
77 changes: 38 additions & 39 deletions wotd.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
package main

import (
"fmt"
"flag"
"time"
"io/ioutil"
"net/http"
"log"
"strings"
"regexp"
"github.com/fatih/color"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
)

func findPhrase(pageContent string, start string, end string) (string, error) {
wordIndex := strings.Index(pageContent, start)
// Error if start not found
if wordIndex == -1 {
return "", fmt.Errorf("could not find phrase")
}
wordIndex += len(start)
wordEndIndex := strings.Index(pageContent[wordIndex:], end) + wordIndex
// Error if end not found
if wordIndex > wordEndIndex {
return "", fmt.Errorf("could not find phrase")
}
word := []byte(pageContent[wordIndex:wordEndIndex])
strWord := fmt.Sprintf("%s", word)
return strWord, nil
wordIndex := strings.Index(pageContent, start)
// Error if start not found
if wordIndex == -1 {
return "", fmt.Errorf("could not find phrase")
}
wordIndex += len(start)
wordEndIndex := strings.Index(pageContent[wordIndex:], end) + wordIndex
// Error if end not found
if wordIndex > wordEndIndex {
return "", fmt.Errorf("could not find phrase")
}
word := []byte(pageContent[wordIndex:wordEndIndex])
strWord := fmt.Sprintf("%s", word)
return strWord, nil
}

func getWotd(url string, date *string) (string, error) {
// Validate date
if *date != "" {
parsedDate, err := time.Parse("2006-01-02", *date)
if err != nil || parsedDate.After(time.Now()) {
return "", fmt.Errorf("invalid date format")
}
}
// Validate date
if *date != "" {
parsedDate, err := time.Parse("2006-01-02", *date)
if err != nil || parsedDate.After(time.Now()) {
return "", fmt.Errorf("invalid date format")
}
}

resp, err := http.Get(url + *date)
if err != nil {
return "", fmt.Errorf("error with getting Wotd from url")
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
resp, err := http.Get(url + *date)
if err != nil {
return "", fmt.Errorf("error with getting Wotd from url")
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)

// Simple parse HTML
pageContent := string(data)
return pageContent, nil
// Simple parse HTML
pageContent := string(data)
return pageContent, nil
}

func color(word string, color string, noColor bool) string {
Expand Down
142 changes: 71 additions & 71 deletions wotd_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package main

import (
"testing"
"regexp"
"time"
"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)
}
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, )
}
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, )
}
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) {
Expand All @@ -52,75 +52,75 @@ func TestColorNoColor(t *testing.T) {
}

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, "")
}
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)
}
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, "")
}
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, "")
}
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, "")
}
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, "")
}
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, "")
}
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 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()
// This is a bad test, it only checks if it can run without erroring, not if the Printf is accurate
main()
}

0 comments on commit d076a5a

Please sign in to comment.