parse_test.go (1951B)
1 package gemtextparser 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 ) 8 9 var DATA_DIR = "test_data/" 10 var URL = "gemini://laack.co" 11 12 func hasSpaceOrTab(text string) bool { 13 return strings.Contains(text, " ") || strings.Contains(text, "\t") 14 } 15 16 func readGemtext(path string, t *testing.T) string { 17 18 file, err := os.ReadFile(path) 19 20 if err != nil { 21 t.Errorf("Unable to read %s", path) 22 return "" 23 } 24 25 return string(file) 26 } 27 28 func TestAbsoluteRelativeParsingMatch(t *testing.T) { 29 30 absBody := readGemtext(DATA_DIR+"absoluteLinks.gmi", t) 31 absLinks := ParseLinks(absBody, URL) 32 33 if len(absLinks) != 5 { 34 t.Errorf("Unexpected number of absolute links") 35 } 36 37 relBody := readGemtext(DATA_DIR+"relativeLinks.gmi", t) 38 relLinks := ParseLinks(relBody, URL) 39 40 if len(relLinks) != 5 { 41 t.Errorf("Unexpected number of relative links") 42 } 43 44 for index, link := range relLinks { 45 if strings.Compare(link, absLinks[index]) != 0 { 46 t.Errorf("Links don't match: %s => %s", link, absLinks[index]) 47 } 48 } 49 } 50 51 func TestParseNonStandardLinks(t *testing.T) { 52 53 body := readGemtext(DATA_DIR+"nonStandard.gmi", t) 54 links := ParseLinks(body, URL) 55 56 if len(links) != 12 { 57 t.Errorf("Unexpected link count: %v", links) 58 } 59 60 for _, link := range links { 61 if hasSpaceOrTab(link) { 62 63 // this seeems like the best place to do this check because these nonstandard links 64 // test all variations of whitespace that can exist 65 66 t.Errorf("Link contains a space or tab, improper whitespace handling: %s", link) 67 } 68 } 69 70 } 71 72 func TestParsePreformattedLinks(t *testing.T) { 73 74 body := readGemtext(DATA_DIR+"preformatedLinks.gmi", t) 75 links := ParseLinks(body, URL) 76 77 if len(links) != 2 { 78 t.Errorf("Unexpected link count: %v", links) 79 } 80 81 } 82 83 func TestParseWords(t *testing.T) { 84 body := readGemtext(DATA_DIR+"wordParsing.gmi", t) 85 results := ParseWords(body) 86 for _, res := range results { 87 if strings.Compare(res, "FINE") != 0 { 88 t.Errorf("Unexpected words parsed: %v", results) 89 } 90 } 91 }