Skip to content

Commit

Permalink
bring back examples, simplify, port 2 more tests
Browse files Browse the repository at this point in the history
This change brings back the two examples so they continue
to show up the package documentation.

It also removes a lot of functionality that is not used and not
justified at this time, given the basic testing requirements of
markdownfmt (i.e., no need to support different options).
It simplifies the code to a bare minimum (to make future
maintenance easier) and more consistent with my other code.

It ports two more tests, TestLineBreak and TestDoubleSpacedListEnd,
to use the new file-based test suite.
  • Loading branch information
dmitshur committed Mar 11, 2019
1 parent 34a8023 commit 8585be1
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 173 deletions.
182 changes: 82 additions & 100 deletions markdown/main_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package markdown_test


import ( import (
"bytes" "bytes"
"encoding/json"
"flag" "flag"
"io/ioutil" "io/ioutil"
"log" "log"
Expand All @@ -15,128 +14,111 @@ import (
"github.com/shurcooL/markdownfmt/markdown" "github.com/shurcooL/markdownfmt/markdown"
) )


var ( func Example() {
updateGoldens = flag.Bool("update_goldens", false, "Set to true to update the goldens that are stored on disk.") input := []byte(`Title
) =

func Test(t *testing.T) {
testModes, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatalf("Error enumerating test modes: %s", err)
}
for _, testMode := range testModes { This is a new paragraph. I wonder if I have too many spaces.
if !testMode.IsDir() { What about new paragraph.
continue But the next one...
}
t.Run(testMode.Name(), func(t *testing.T) {
testFiles, err := ioutil.ReadDir(filepath.Join("testdata/", testMode.Name()))
if err != nil {
t.Fatalf("Error enumerating test cases for mode %s: %s", testMode.Name(), err)
}
optionsFile := filepath.Join("testdata", testMode.Name(), "options.json") Is really new.
// This is the default set of options
options := &markdown.Options{}
optionJson, err := ioutil.ReadFile(optionsFile)
if err == nil {
if err := json.Unmarshal(optionJson, options); err != nil {
t.Errorf("Unable to unmarshal %s.\nGot: %q\nError: %v", optionsFile, optionJson, err)
}
} else if os.IsNotExist(err) {
// Do nothing and use the default options.
} else {
t.Errorf("Error opening or reading `%s`'s options.json file: %v", optionsFile, err)
}
for _, testFile := range testFiles { 1. Item one.
// Only test on input files. 1. Item TWO.
if !strings.HasSuffix(testFile.Name(), ".in.md") {
continue
}
testName := testFile.Name()[0 : len(testFile.Name())-len(".in.md")]

t.Run(testName, func(t *testing.T) {
in, err := ioutil.ReadFile(filepath.Join("testdata", testMode.Name(), testName+".in.md"))
if err != nil {
t.Errorf("Unable to open input file %s.in.md: %v", testName, err)
}

got, err := markdown.Process("", in, options)
if err != nil {
t.Errorf("Error processing input: %s", err)
}

wantFile := filepath.Join("testdata", testMode.Name(), testName+".out.md")
want, err := ioutil.ReadFile(wantFile)
if err != nil {
t.Errorf("Unable to open input file %s.in.md: %v", in, err)
}

d, err := diff(got, want)
if err != nil {
t.Errorf("Unable to diff output against golden data: %v", err)
}

if len(d) != 0 {
t.Errorf("Difference of %d lines:\n%s", bytes.Count(d, []byte("\n")), string(d))
}

if *updateGoldens && len(d) > 0 {
err := ioutil.WriteFile(wantFile, got, 0644)
if err != nil {
t.Errorf("Unable to write golden output file %s.out.md: %v", testName, err)
}
t.Logf("Wrote updated golden file as %s", wantFile)
}
})
}
})
}
}
func TestLineBreak(t *testing.T) { Final paragraph.
input := []byte("Some text with two trailing spaces for linebreak. \nMore spaced **text** *immediately* after that. \nMore than two spaces become two.\n") `)
expected := []byte("Some text with two trailing spaces for linebreak. \nMore spaced **text** *immediately* after that. \nMore than two spaces become two.\n")


output, err := markdown.Process("", input, nil) output, err := markdown.Process("", input, nil)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }


diff, err := diff(expected, output) os.Stdout.Write(output)
if err != nil {
log.Fatalln(err) // Output:
} // Title

// =====
if len(diff) != 0 { //
t.Errorf("Difference of %d lines:\n%s", bytes.Count(diff, []byte("\n")), string(diff)) // This is a new paragraph. I wonder if I have too many spaces. What about new paragraph. But the next one...
} //
// Is really new.
//
// 1. Item one.
// 2. Item TWO.
//
// Final paragraph.
//
} }


// TestDoubleSpacedListEnd tests that when the document ends with a double spaced list, func Example_two() {
// an extra blank line isn't appended. See issue #30. input := []byte(`Title
func TestDoubleSpacedListEnd(t *testing.T) { ==
const reference = `- An item.
Subtitle
---
- Another time with a blank line in between. How about ` + "`this`" + ` and other stuff like *italic*, **bold** and ***super extra***.
` `)
input := []byte(reference)
expected := []byte(reference)


output, err := markdown.Process("", input, nil) output, err := markdown.Process("", input, nil)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }


diff, err := diff(expected, output) os.Stdout.Write(output)

// Output:
// Title
// =====
//
// Subtitle
// --------
//
// How about `this` and other stuff like *italic*, **bold** and ***super extra***.
//
}

var updateFlag = flag.Bool("update", false, "Update golden files.")

func Test(t *testing.T) {
fis, err := ioutil.ReadDir("testdata")
if err != nil { if err != nil {
log.Fatalln(err) t.Fatal(err)
} }
for _, fi := range fis {
if !strings.HasSuffix(fi.Name(), ".in.md") {
continue
}
name := strings.TrimSuffix(fi.Name(), ".in.md")
t.Run(name, func(t *testing.T) {
got, err := markdown.Process(filepath.Join("testdata", fi.Name()), nil, nil)
if err != nil {
t.Fatal("markdown.Process:", err)
}
if *updateFlag {
err := ioutil.WriteFile(filepath.Join("testdata", name+".golden.md"), got, 0644)
if err != nil {
t.Fatal(err)
}
return
}

want, err := ioutil.ReadFile(filepath.Join("testdata", name+".golden.md"))
if err != nil {
t.Fatal(err)
}


if len(diff) != 0 { diff, err := diff(got, want)
t.Errorf("Difference of %d lines:\n%s", bytes.Count(diff, []byte("\n")), string(diff)) if err != nil {
t.Fatal(err)
}
if len(diff) != 0 {
t.Errorf("difference of %d lines:\n%s", bytes.Count(diff, []byte("\n")), string(diff))
}
})
} }
} }


Expand Down
26 changes: 0 additions & 26 deletions markdown/testdata/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions markdown/testdata/default/example.in.md

This file was deleted.

11 changes: 0 additions & 11 deletions markdown/testdata/default/example.out.md

This file was deleted.

7 changes: 0 additions & 7 deletions markdown/testdata/default/example2.in.md

This file was deleted.

7 changes: 0 additions & 7 deletions markdown/testdata/default/example2.out.md

This file was deleted.

1 change: 0 additions & 1 deletion markdown/testdata/default/options.json

This file was deleted.

Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,7 @@
Demonstrates https://github.com/shurcooL/markdownfmt/issues/35. Test for issue https://github.com/shurcooL/markdownfmt/issues/35.


[Link](path\\to\\page) [Link](path\\to\\page)


![Image](path\\to\\image) ![Image](path\\to\\image)


https://path\\to\\page https://path\\to\\page

Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
Demonstrates https://github.com/shurcooL/markdownfmt/issues/35. Test for issue https://github.com/shurcooL/markdownfmt/issues/35.


[Link](path\\to\\page) [Link](path\\to\\page)


Expand Down
3 changes: 3 additions & 0 deletions markdown/testdata/linebreak.golden.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
Some text with two trailing spaces for linebreak.
More spaced **text** *immediately* after that.
More than two spaces become two.
3 changes: 3 additions & 0 deletions markdown/testdata/linebreak.in.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
Some text with two trailing spaces for linebreak.
More spaced **text** *immediately* after that.
More than two spaces become two.
5 changes: 5 additions & 0 deletions markdown/testdata/listend.golden.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
Test that when the document ends with a double spaced list, an extra blank line isn't appended. See issue #30.

- An item.

- Another time with a blank line in between.
5 changes: 5 additions & 0 deletions markdown/testdata/listend.in.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
Test that when the document ends with a double spaced list, an extra blank line isn't appended. See issue #30.

- An item.

- Another time with a blank line in between.
File renamed without changes.
File renamed without changes.
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
Demonstrates https://github.com/shurcooL/markdownfmt/issues/20 Test for issue https://github.com/shurcooL/markdownfmt/issues/20.


text text text text


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
Demonstrates https://github.com/shurcooL/markdownfmt/issues/20 Test for issue https://github.com/shurcooL/markdownfmt/issues/20.


text text
text text
Expand Down
File renamed without changes.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@


aaa/あああ aaa/あああ
---------- ----------

0 comments on commit 8585be1

Please sign in to comment.