2020-11-12 10:22:43 +00:00
package gemtext
2020-11-12 13:18:44 +00:00
import (
"testing"
)
2020-11-12 10:22:43 +00:00
type LineCase struct {
Str string
Preformatted bool
Want GemtextObject
}
2020-11-12 13:18:44 +00:00
2020-11-14 19:29:55 +00:00
var testdocument string = "# Document\nThis is paragraph text & It exists\n=> https://example.com example.com\n* List Item 1\n* List Item 2\n```preformatted stuff\nthis should be preformatted.\n=> https://example.com Not a link\n```\n* A single list item\n```\none line of pre\n```\n```\n```\n>Something\n>- someone"
var testdocumenttree GemtextPage = GemtextPage {
GemtextObject { Type : HEADING , Literal : "# Document" , Level : 1 , Text : "Document" } , GemtextObject { Type : TEXT , Literal : "This is paragraph text & It exists" , Text : "This is paragraph text & It exists" } ,
GemtextObject { Type : LINK , Literal : "=> https://example.com example.com" , Text : "example.com" , Path : "https://example.com" } ,
GemtextObject { Type : LIST , Literal : "* List Item 1" , Text : "List Item 1" } ,
GemtextObject { Type : LIST , Literal : "* List Item 2" , Text : "List Item 2" } ,
GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```preformatted stuff" , Text : "preformatted stuff" } ,
GemtextObject { Type : PREFORMATTED_TEXT , Literal : "this should be preformatted." , Text : "this should be preformatted." } ,
GemtextObject { Type : PREFORMATTED_TEXT , Literal : "=> https://example.com Not a link" , Text : "=> https://example.com Not a link" } ,
GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```" } ,
GemtextObject { Type : LIST , Literal : "* A single list item" , Text : "A single list item" } ,
GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```" } ,
GemtextObject { Type : PREFORMATTED_TEXT , Literal : "one line of pre" , Text : "one line of pre" } ,
GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```" } ,
GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```" } ,
GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```" } ,
GemtextObject { Type : QUOTE , Text : "Something" , Literal : ">Something" } ,
GemtextObject { Type : QUOTE , Text : "- someone" , Literal : ">- someone" } ,
}
2020-11-12 10:55:29 +00:00
// These tests suck.
2020-11-12 10:22:43 +00:00
func TestParseLine ( t * testing . T ) {
cases := [ ] LineCase {
LineCase { Str : "```" , Preformatted : false , Want : GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```" } } ,
2020-11-12 11:08:41 +00:00
LineCase { Str : "```alt" , Preformatted : false , Want : GemtextObject { Type : PREFORMATTED_TOGGLE , Literal : "```alt" , Text : "alt" } } ,
2020-11-12 10:22:43 +00:00
LineCase {
Str : "This is a test of a normal text line." ,
Preformatted : false ,
Want : GemtextObject {
Type : TEXT ,
Literal : "This is a test of a normal text line." ,
Text : "This is a test of a normal text line." } ,
} ,
LineCase {
Str : "This is a test of a normal text line." ,
Preformatted : true ,
Want : GemtextObject {
Type : PREFORMATTED_TEXT ,
Literal : "This is a test of a normal text line." ,
Text : "This is a test of a normal text line." } ,
} ,
LineCase {
Str : "# heading 1" ,
Preformatted : false ,
Want : GemtextObject {
Type : HEADING ,
Literal : "# heading 1" ,
Text : "heading 1" ,
Level : 1 } ,
} ,
LineCase {
Str : "## heading 2" ,
Preformatted : false ,
Want : GemtextObject {
Type : HEADING ,
Literal : "## heading 2" ,
Text : "heading 2" ,
Level : 2 } ,
} ,
LineCase {
Str : "### heading 3" ,
Preformatted : false ,
Want : GemtextObject {
Type : HEADING ,
Literal : "### heading 3" ,
Text : "heading 3" ,
Level : 3 } ,
} ,
2020-11-12 10:55:29 +00:00
LineCase {
Str : "###heading 3" ,
Preformatted : false ,
Want : GemtextObject {
Type : HEADING ,
Literal : "###heading 3" ,
Text : "heading 3" ,
Level : 3 } ,
} ,
LineCase {
Str : "### heading 3" ,
Preformatted : false ,
Want : GemtextObject {
Type : HEADING ,
Literal : "### heading 3" ,
Text : "heading 3" ,
Level : 3 } ,
} ,
2020-11-12 10:22:43 +00:00
LineCase {
Str : "#### heading 4" ,
Preformatted : false ,
Want : GemtextObject {
Type : TEXT ,
Literal : "#### heading 4" ,
Text : "#### heading 4" } ,
} ,
LineCase {
Str : "*list item" ,
Preformatted : false ,
Want : GemtextObject {
2020-11-12 11:08:41 +00:00
Type : TEXT ,
2020-11-12 10:22:43 +00:00
Literal : "*list item" ,
2020-11-12 11:08:41 +00:00
Text : "*list item" } ,
2020-11-12 10:22:43 +00:00
} ,
LineCase {
Str : "* list item" ,
Preformatted : false ,
Want : GemtextObject {
Type : LIST ,
Literal : "* list item" ,
Text : "list item" } ,
} ,
LineCase {
Str : ">quote" ,
Preformatted : false ,
Want : GemtextObject {
Type : QUOTE ,
Literal : ">quote" ,
Text : "quote" } ,
} ,
LineCase {
Str : "> quote" ,
Preformatted : false ,
Want : GemtextObject {
Type : QUOTE ,
Literal : "> quote" ,
Text : "quote" } ,
} ,
2020-11-12 10:55:29 +00:00
LineCase {
Str : "> quote" ,
Preformatted : false ,
Want : GemtextObject {
Type : QUOTE ,
Literal : "> quote" ,
Text : "quote" } ,
} ,
LineCase {
Str : "> quote" ,
Preformatted : true ,
Want : GemtextObject {
Type : PREFORMATTED_TEXT ,
Literal : "> quote" ,
Text : "> quote" } ,
} ,
LineCase {
Str : "=> https://example.com link" ,
Preformatted : true ,
Want : GemtextObject {
Type : PREFORMATTED_TEXT ,
Literal : "=> https://example.com link" ,
Text : "=> https://example.com link" } ,
} ,
LineCase {
Str : "=> https://example.com link" ,
Preformatted : false ,
Want : GemtextObject {
Type : LINK ,
Literal : "=> https://example.com link" ,
Text : "link" ,
Path : "https://example.com" } ,
} ,
2020-11-12 13:18:44 +00:00
LineCase {
Str : "\t" ,
Preformatted : true ,
Want : GemtextObject {
Type : PREFORMATTED_TEXT ,
Literal : "\t" ,
Text : "\t" } ,
} ,
2020-11-12 10:55:29 +00:00
LineCase {
Str : "=>https://example.com link" ,
Preformatted : false ,
Want : GemtextObject {
Type : LINK ,
Literal : "=>https://example.com link" ,
Text : "link" ,
Path : "https://example.com" } ,
} ,
LineCase {
Str : "\n" ,
Preformatted : false ,
Want : GemtextObject {
Type : TEXT ,
Literal : "\n" ,
Text : "\n" } ,
} ,
2020-11-12 11:00:34 +00:00
LineCase {
Str : "" ,
Preformatted : false ,
Want : GemtextObject {
Type : TEXT ,
Literal : "" ,
Text : "" } ,
} ,
2020-11-12 10:22:43 +00:00
}
for _ , c := range cases {
got , _ := ParseLine ( c . Str , c . Preformatted )
if got != c . Want {
t . Errorf ( "case %#v, got %#v" , c , got )
}
}
}
2020-11-12 13:18:44 +00:00
// We test each line in ParseLine()
// so all we have to test for ParsePage() is blank lines and preformatting.
func TestParsePage ( t * testing . T ) {
2020-11-14 19:29:55 +00:00
input := testdocument
want := testdocumenttree
2020-11-12 13:18:44 +00:00
output , _ := ParsePage ( input )
for i , _ := range output {
if output [ i ] != want [ i ] {
2020-11-12 14:53:35 +00:00
t . Errorf ( "Wanted: %#v, got: %#v at index %d" , want [ i ] , output [ i ] , i )
2020-11-12 13:18:44 +00:00
}
}
}