step/meta_test.go
Sasha Koshka 43084fb5bb Add ability to parse quoted strings in meta
This should actually make migration from Caddy easier, because
Caddy's YAML front matter seems to support quoted strings? IDK,
the current Holanet has them. Closes #11
2024-12-09 12:56:16 -05:00

112 lines
3.3 KiB
Go

package step
import "strings"
import "testing"
const quickBrownFox = "The quick brown fox jumped over the lazy dog."
func TestSplitMetaLF (test *testing.T) {
correctBody :=
`this is some sample text
---
theres another hr that shouldnt
break anything
break: anything
`
frontMatter, body, err := SplitMeta(
`---
FOO: baR
fOoo: Bar
Shouldn't break anything: ---
this : that
Sentence: ` + quickBrownFox + `
---
` + correctBody)
if err != nil {
test.Fatal(err)
}
test.Log("BODY:")
test.Log(body)
if body != correctBody {
test.Fatal("body is not correct")
}
test.Log("META:")
test.Log(frontMatter)
value0, ok := frontMatter["foo"]
if !ok { test.Fatal("missing key") }
value1, ok := frontMatter["fooo"]
if !ok { test.Fatal("missing key") }
value2, ok := frontMatter["shouldn't break anything"]
if !ok { test.Fatal("missing key") }
value3, ok := frontMatter["this"]
if !ok { test.Fatal("missing key") }
value4, ok := frontMatter["sentence"]
if !ok { test.Fatal("missing key") }
if value0 != "baR" { test.Fatal("value is not correct") }
if value1 != "Bar" { test.Fatal("value is not correct") }
if value2 != "---" { test.Fatal("value is not correct") }
if value3 != "that" { test.Fatal("value is not correct") }
if value4 != quickBrownFox { test.Fatal("value is not correct") }
}
func TestSplitMetaCRLF (test *testing.T) {
correctBody := "this is some sample text\r\n---\r\ntheres another hr that shouldnt\r\nbreak anything\r\nbreak: anything\r\n"
frontMatter, body, err := SplitMeta(
"---\r\nFOO: baR\r\n fOoo: Bar\r\nShouldn't break anything: ---\r\n\r\nthis : that\r\nSentence: " + quickBrownFox + "\r\n---\r\n" + correctBody)
if err != nil {
test.Fatal(err)
}
test.Log("BODY:")
test.Log(body)
correctBody = strings.ReplaceAll(correctBody, "\r\n", "\n")
if body != correctBody {
test.Fatal("body is not correct")
}
test.Log("META:")
test.Log(frontMatter)
value0, ok := frontMatter["foo"]
if !ok { test.Fatal("missing key") }
value1, ok := frontMatter["fooo"]
if !ok { test.Fatal("missing key") }
value2, ok := frontMatter["shouldn't break anything"]
if !ok { test.Fatal("missing key") }
value3, ok := frontMatter["this"]
if !ok { test.Fatal("missing key") }
value4, ok := frontMatter["sentence"]
if !ok { test.Fatal("missing key") }
if value0 != "baR" { test.Fatal("value is not correct") }
if value1 != "Bar" { test.Fatal("value is not correct") }
if value2 != "---" { test.Fatal("value is not correct") }
if value3 != "that" { test.Fatal("value is not correct") }
if value4 != quickBrownFox { test.Fatal("value is not correct") }
}
func TestParseMeta (test *testing.T) {
meta, err := ParseMeta(
`thing: " Quoted string!!!!! "
Other-Thing: askdjlksajd
number: 3849
`)
if err != nil {
test.Fatal(err)
}
test.Log("META:")
test.Log(meta)
value0, ok := meta["thing"]
if !ok { test.Fatal("missing key") }
value1, ok := meta["other-thing"]
if !ok { test.Fatal("missing key") }
value2, ok := meta["number"]
if !ok { test.Fatal("missing key") }
if value0 != "\tQuoted string!!!!!m " { test.Fatal("value is not correct") }
if value1 != "askdjlksajd" { test.Fatal("value is not correct") }
if value2 != "3849" { test.Fatal("value is not correct") }
}