Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szktkfm committed Mar 23, 2024
1 parent a82a266 commit ee9e8e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
53 changes: 35 additions & 18 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package mdtt

import (
"bytes"
"fmt"
"io"
"os"
"testing"

"github.com/charmbracelet/log"
"github.com/google/go-cmp/cmp"
)

Expand All @@ -31,29 +32,45 @@ func TestFindSegment(t *testing.T) {
}

func TestReplaceTable(t *testing.T) {
testCases := []struct {
name string
src string
wnt string
}{
{
name: "Test Case 1",
src: "testdata/replace01.md",
wnt: "testdata/replace_want01.md",
},
// Add more test cases here
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
want, got := testUtilReplaceTable(tc.src, tc.wnt)

if diff := cmp.Diff(string(want), string(got)); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
})
}
}

func testUtilReplaceTable(src, wnt string) ([]byte, []byte) {
tw := TableWriter{}
fp, _ := os.Open("testdata/replace01.md")
fp, _ := os.Open(src)
defer fp.Close()

m := NewRoot(
WithMDFile("testdata/replace_want01.md"),
WithMDFile(wnt),
)

tw.render(m.table)
b := tw.replaceTable(fp)

fmt.Println(string(b))
// fp2, _ := os.Open("testdata/replace_want01.md")
// defer fp2.Close()
// want, _ := io.ReadAll(fp2)
want := `# Title
| foo2 | bar |
| -----------------------------------| ----------------------------------- |
| bazaaaa | bim |
`

if diff := cmp.Diff(string(want), string(b)); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
got := tw.replaceTable(fp)

log.Debug(string(got))
fp2, _ := os.Open(wnt)
defer fp2.Close()
want, _ := io.ReadAll(fp2)
return got, want
}
2 changes: 1 addition & 1 deletion testdata/replace_want01.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Title
| foo2 | bar |
| -----------------------------------| ----------------------------------- |
| bazaaaa | bim |
| bazaaaa | bim |

0 comments on commit ee9e8e9

Please sign in to comment.