Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package json_markd

import (
"fmt"
"os"
)

// Example usage for json_markd
func main() {
SetTabSpaceValue(2)
SetupLogger()
result, err := ParseMarkdown("data/sample_api.md")
f, err := os.Open("data/sample_api.md")
if err != nil {
fmt.Println(err)
return
}
result, err := ParseMarkdown(f)
if err != nil {
fmt.Println(err)
return
Expand Down
5 changes: 3 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package json_markd

import (
"fmt"
"io"
"io/ioutil"
"strings"
)
Expand All @@ -29,9 +30,9 @@ func SetTabSpaceValue(val int) {
}

// ParseMarkdown accepts a filepath to a markdown file and return JSON string for same
func ParseMarkdown(filepath string) (string, error) {
func ParseMarkdown(src io.Reader) (string, error) {
SetupLogger()
markdownData, err := ioutil.ReadFile(filepath)
markdownData, err := ioutil.ReadAll(src)
if err != nil {
fmt.Println("File reading error", err)
return "", err
Expand Down
20 changes: 17 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package json_markd

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -40,23 +41,36 @@ func TestParseMarkdown(t *testing.T) {
t.Run("when correct file is passed", func(t *testing.T) {
t.Run("it should return correct response", func(t *testing.T) {
expectedResponse := "{\n \"data\" : {\n \"name\" : \"random string\",\n \"age\" : 0,\n \"income\" : 0.0,\n \"vehicles\" : [\n {\n \"name\" : \"random string\",\n \"price\" : 0.0\n }\n ],\n \"apps\" : [\n [\n \"random string\",\n \"random string\"\n ],\n [\n \"random string\",\n \"random string\"\n ]\n ]\n },\n \"errors\" : {\n \"type\" : \"random string\"\n }\n}"
result, _ := ParseMarkdown("./test_data/sample.md")
const filePath = "./test_data/sample.md"
f, err := os.Open(filePath)
assert.NoError(t, err, "failed to open file: %s", filePath)
result, _ := ParseMarkdown(f)
assert.Equal(t, expectedResponse, result)
})
})

t.Run("when incorrect file is passed", func(t *testing.T) {
t.Run("it should return correct response", func(t *testing.T) {
expectedResponse := "open ./test_data/sample_1.md: no such file or directory"
_, err := ParseMarkdown("./test_data/sample_1.md")
const filePath = "./test_data/sample_1.md"
f, err := os.Open(filePath)
assert.Equal(t, expectedResponse, err.Error())
if err != nil {
return
}
expectedResponse = "invalid argument"
_, err = ParseMarkdown(f)
assert.Equal(t, expectedResponse, err.Error())
})
})

t.Run("when file data is invalid", func(t *testing.T) {
t.Run("it should return correct response", func(t *testing.T) {
expectedResponse := ".errors.invalid_markdown_list_format"
_, err := ParseMarkdown("./test_data/sample_invalid.md")
const filePath = "./test_data/sample_invalid.md"
f, err := os.Open(filePath)
assert.NoError(t, err, "failed to open file: %s", filePath)
_, err = ParseMarkdown(f)
assert.Equal(t, expectedResponse, err.Error())
})
})
Expand Down