diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-29 18:40:22 +0000 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-29 18:40:22 +0000 |
commit | b6c8304364d1457fa7454da257f7ad3d71131b91 (patch) | |
tree | de85e19971aa6ac127abf35b80421ff8f6907727 /scfg/reader_test.go | |
parent | Initial commit (diff) | |
download | go-lindenii-common-b6c8304364d1457fa7454da257f7ad3d71131b91.tar.gz go-lindenii-common-b6c8304364d1457fa7454da257f7ad3d71131b91.tar.zst go-lindenii-common-b6c8304364d1457fa7454da257f7ad3d71131b91.zip |
Add ~emersion/go-scfg as a subdirectory
Diffstat (limited to 'scfg/reader_test.go')
-rw-r--r-- | scfg/reader_test.go | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/scfg/reader_test.go b/scfg/reader_test.go new file mode 100644 index 0000000..5a4af28 --- /dev/null +++ b/scfg/reader_test.go @@ -0,0 +1,174 @@ +package scfg + +import ( + "fmt" + "reflect" + "strings" + "testing" +) + +var readTests = []struct { + name string + src string + want Block +}{ + { + name: "flat", + src: `dir1 param1 param2 "" param3 +dir2 +dir3 param1 + +# comment +dir4 "param 1" 'param 2'`, + want: Block{ + {Name: "dir1", Params: []string{"param1", "param2", "", "param3"}}, + {Name: "dir2", Params: []string{}}, + {Name: "dir3", Params: []string{"param1"}}, + {Name: "dir4", Params: []string{"param 1", "param 2"}}, + }, + }, + { + name: "simpleBlocks", + src: `block1 { + dir1 param1 param2 + dir2 param1 +} + +block2 { +} + +block3 { + # comment +} + +block4 param1 "param2" { + dir1 +}`, + want: Block{ + { + Name: "block1", + Params: []string{}, + Children: Block{ + {Name: "dir1", Params: []string{"param1", "param2"}}, + {Name: "dir2", Params: []string{"param1"}}, + }, + }, + {Name: "block2", Params: []string{}, Children: Block{}}, + {Name: "block3", Params: []string{}, Children: Block{}}, + { + Name: "block4", + Params: []string{"param1", "param2"}, + Children: Block{ + {Name: "dir1", Params: []string{}}, + }, + }, + }, + }, + { + name: "nested", + src: `block1 { + block2 { + dir1 param1 + } + + block3 { + } +} + +block4 { + block5 { + block6 param1 { + dir1 + } + } + + dir1 +}`, + want: Block{ + { + Name: "block1", + Params: []string{}, + Children: Block{ + { + Name: "block2", + Params: []string{}, + Children: Block{ + {Name: "dir1", Params: []string{"param1"}}, + }, + }, + { + Name: "block3", + Params: []string{}, + Children: Block{}, + }, + }, + }, + { + Name: "block4", + Params: []string{}, + Children: Block{ + { + Name: "block5", + Params: []string{}, + Children: Block{{ + Name: "block6", + Params: []string{"param1"}, + Children: Block{{ + Name: "dir1", + Params: []string{}, + }}, + }}, + }, + { + Name: "dir1", + Params: []string{}, + }, + }, + }, + }, + }, + { + name: "quotes", + src: `"a \b ' \" c" 'd \e \' " f' a\"b`, + want: Block{ + {Name: "a b ' \" c", Params: []string{"d e ' \" f", "a\"b"}}, + }, + }, + { + name: "quotes-2", + src: `dir arg1 "arg2" ` + `\"\"`, + want: Block{ + {Name: "dir", Params: []string{"arg1", "arg2", "\"\""}}, + }, + }, + { + name: "quotes-3", + src: `dir arg1 "\"\"\"\"" arg3`, + want: Block{ + {Name: "dir", Params: []string{"arg1", `"` + "\"\"" + `"`, "arg3"}}, + }, + }, +} + +func TestRead(t *testing.T) { + for _, test := range readTests { + t.Run(test.name, func(t *testing.T) { + r := strings.NewReader(test.src) + got, err := Read(r) + if err != nil { + t.Fatalf("Read() = %v", err) + } + stripLineno(got) + if !reflect.DeepEqual(got, test.want) { + t.Error(fmt.Sprintf("Read() = %#v but want %#v", got, test.want)) + } + }) + } +} + +func stripLineno(block Block) { + for _, dir := range block { + dir.lineno = 0 + stripLineno(dir.Children) + } +} |