diff --git a/pkg/markdown/base.go b/pkg/markdown/base.go index 23597c2..ffd41af 100644 --- a/pkg/markdown/base.go +++ b/pkg/markdown/base.go @@ -15,7 +15,7 @@ const ( // Base is single markdown syntax representation // Example: header, list, ... type Base interface { - ToString() string + String() string } type header struct { @@ -23,7 +23,14 @@ type header struct { text string } -func (h header) ToString() string { +func NewHeader(level int, text string) Base { + return header{ + level: level, + text: text, + } +} + +func (h header) String() string { var builder strings.Builder for i := 0; i < h.level; i++ { @@ -42,7 +49,13 @@ type listItem struct { text string } -func (i listItem) ToString() string { +func NewListItem(text string) Base { + return listItem{ + text: text, + } +} + +func (i listItem) String() string { text := strings.TrimSpace(i.text) return string(defaultListToken) + string(spaceToken) + text diff --git a/pkg/markdown/base_test.go b/pkg/markdown/base_test.go index 953f8af..adeefc4 100644 --- a/pkg/markdown/base_test.go +++ b/pkg/markdown/base_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestHeaderToString(t *testing.T) { +func TestHeaderString(t *testing.T) { tests := []struct { name string header header @@ -32,13 +32,13 @@ func TestHeaderToString(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - got := tc.header.ToString() + got := tc.header.String() assert.Equal(t, tc.want, got) }) } } -func TestListItemToString(t *testing.T) { +func TestListItemString(t *testing.T) { tests := []struct { name string listItem listItem @@ -55,7 +55,7 @@ func TestListItemToString(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - got := tc.listItem.ToString() + got := tc.listItem.String() assert.Equal(t, tc.want, got) }) } diff --git a/pkg/markdown/generate.go b/pkg/markdown/generate.go new file mode 100644 index 0000000..30f88ae --- /dev/null +++ b/pkg/markdown/generate.go @@ -0,0 +1,13 @@ +package markdown + +import "strings" + +func Generate(bases []Base) string { + lines := make([]string, len(bases)) + + for i, base := range bases { + lines[i] = base.String() + } + + return strings.Join(lines, string(newlineToken)) +} diff --git a/pkg/markdown/generate_test.go b/pkg/markdown/generate_test.go new file mode 100644 index 0000000..7b8b63c --- /dev/null +++ b/pkg/markdown/generate_test.go @@ -0,0 +1,39 @@ +package markdown + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGenerate(t *testing.T) { + tests := []struct { + name string + bases []Base + want string + }{ + { + name: "normal", + bases: []Base{ + header{ + level: 1, + text: "header", + }, + listItem{ + text: "item 1", + }, + listItem{ + text: "item 2", + }, + }, + want: "# header\n- item 1\n- item 2", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := Generate(tc.bases) + assert.Equal(t, tc.want, got) + }) + } +} diff --git a/pkg/markdown/parser_test.go b/pkg/markdown/parser_test.go index 1879e34..893c53a 100644 --- a/pkg/markdown/parser_test.go +++ b/pkg/markdown/parser_test.go @@ -36,7 +36,7 @@ func TestParse(t *testing.T) { }, want: []Base{ header{ - level: 1, + level: 3, text: "xyz", }, listItem{ @@ -45,6 +45,13 @@ func TestParse(t *testing.T) { }, }, } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := Parse(tc.lines) + assert.Equal(t, tc.want, got) + }) + } } func TestParseHeader(t *testing.T) {