From 13a990bb4a4206b7d0556e58a8aae69a0ba9d1f7 Mon Sep 17 00:00:00 2001 From: adminoo Date: Thu, 4 Mar 2021 17:30:38 +0100 Subject: [PATCH] more tests for good boy points, split entities into files --- Makefile | 2 +- feed.go | 27 +++++++++++++++++++++ tokenizer.go => lexer.go | 51 ---------------------------------------- main.go | 2 +- main_test.go | 20 ++++++++++++++++ token.go | 34 +++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 53 deletions(-) create mode 100644 feed.go rename tokenizer.go => lexer.go (65%) create mode 100644 token.go diff --git a/Makefile b/Makefile index 43cabe6..7c3a4ca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ build: - go build -o bin/org2nb main.go + go build -o bin/org2nb main.go feed.go lexer.go token.go install: cp bin/org2nb ~/.local/bin/ diff --git a/feed.go b/feed.go new file mode 100644 index 0000000..69e99a9 --- /dev/null +++ b/feed.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "strings" +) + +type Feed struct { + URL string + Description string + Tags []string +} + +// Return the final feed string, depending on either the link has a description, tags or not +func (f Feed) String() string { + var ff string + var tags string = strings.TrimSpace(strings.Join(f.Tags, " ")) + + ff = fmt.Sprintf("%s %s", f.URL, tags) + + if f.Description != "" { + ff = fmt.Sprintf("%s # %s", strings.TrimSpace(ff), f.Description) + } + + return strings.TrimSpace(ff) +} + diff --git a/tokenizer.go b/lexer.go similarity index 65% rename from tokenizer.go rename to lexer.go index d26a5e4..7bb3298 100644 --- a/tokenizer.go +++ b/lexer.go @@ -1,41 +1,10 @@ package main import ( - "fmt" "regexp" "strings" ) -type Feed struct { - URL string - Description string - Tags []string -} - -// Return the final feed string, depending on either the link has a description, tags or not -func (f Feed) String() string { - var ff string - var tags string = strings.TrimSpace(strings.Join(f.Tags, " ")) - - ff = fmt.Sprintf("%s %s", f.URL, tags) - - if f.Description != "" { - ff = fmt.Sprintf("%s # %s", strings.TrimSpace(ff), f.Description) - } - - return strings.TrimSpace(ff) -} - - -type Token struct { - Type string - Value string -} - -func (t Token) String() string { - return fmt.Sprintf("%s : '%s'", t.Type, t.Value) -} - // Lexer embbed a temporary buffer to store "content" // (url, description, tags) and an array of tokens type Lexer struct { @@ -108,23 +77,3 @@ func (l Lexer) Process(s string) []Token { return l.Tokens } - -func Parse(t []Token) Feed { - var f Feed - for i := range t { - token := t[i] - if token.Type == "URL" { - f.URL = token.Value - } - - if token.Type == "DESC" { - f.Description = token.Value - } - - if token.Type == "TAG" { - f.Tags = append(f.Tags, token.Value) - } - } - - return f -} diff --git a/main.go b/main.go index a72c698..30372f2 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ func main() { tokens := lexer.Process(scanner.Text()) feed := Parse(tokens).String() - file.WriteString(feed) + file.WriteString(feed + "\n") } file.Close() diff --git a/main_test.go b/main_test.go index db0f685..6b7a1c0 100644 --- a/main_test.go +++ b/main_test.go @@ -45,6 +45,16 @@ func TestLinkDescTag(t *testing.T) { } } +func TestLinkDescManyTagsSpaces(t *testing.T) { + var message string = "** [[https://pleroma.social/announcements/feed.xml][Pleroma Social]] :software: :social: :cofe:" + var expected string = "https://pleroma.social/announcements/feed.xml software social cofe # Pleroma Social" + var result string = LexerTestWrapper(message, expected) + if result != expected { + LexerTestWrapperFail(expected, result) + t.Fail() + } +} + func TestLinkDescNoTag(t *testing.T) { var message string = "** [[https://pleroma.social/announcements/feed.xml][Pleroma Social]]" @@ -55,3 +65,13 @@ func TestLinkDescNoTag(t *testing.T) { t.Fail() } } + +func TestLinkDescWithSymbols(t *testing.T) { + var message string = "** [[https://pleroma.social/announcements/feed.xml][Pleroma Social [*Very Cool*]]] :software:" + var expected string = "https://pleroma.social/announcements/feed.xml software # Pleroma Social [*Very Cool*]" + var result string = LexerTestWrapper(message, expected) + if result != expected { + LexerTestWrapperFail(expected, result) + t.Fail() + } +} diff --git a/token.go b/token.go new file mode 100644 index 0000000..52a5a55 --- /dev/null +++ b/token.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" +) + +type Token struct { + Type string + Value string +} + +func (t Token) String() string { + return fmt.Sprintf("%s : '%s'", t.Type, t.Value) +} + +func Parse(t []Token) Feed { + var f Feed + for i := range t { + token := t[i] + if token.Type == "URL" { + f.URL = token.Value + } + + if token.Type == "DESC" { + f.Description = token.Value + } + + if token.Type == "TAG" { + f.Tags = append(f.Tags, token.Value) + } + } + + return f +}