Skip to content
Permalink
Browse files

bring back examples, simplify, port 2 more tests

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 8585be1e54b7c801181aa972aab97fb4c7e459d0
@@ -2,7 +2,6 @@ package markdown_test

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

var (
updateGoldens = flag.Bool("update_goldens", false, "Set to true to update the goldens that are stored on disk.")
)

func Test(t *testing.T) {
testModes, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatalf("Error enumerating test modes: %s", err)
}
func Example() {
input := []byte(`Title
=
for _, testMode := range testModes {
if !testMode.IsDir() {
continue
}
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)
}
This is a new paragraph. I wonder if I have too many spaces.
What about new paragraph.
But the next one...
optionsFile := filepath.Join("testdata", testMode.Name(), "options.json")
// 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)
}
Is really new.
for _, testFile := range testFiles {
// Only test on input files.
if !strings.HasSuffix(testFile.Name(), ".in.md") {
continue
}
1. Item one.
1. Item TWO.
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) {
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")
Final paragraph.
`)

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

diff, err := diff(expected, output)
if err != nil {
log.Fatalln(err)
}

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

// Output:
// Title
// =====
//
// 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,
// an extra blank line isn't appended. See issue #30.
func TestDoubleSpacedListEnd(t *testing.T) {
const reference = `- An item.
func Example_two() {
input := []byte(`Title
==
Subtitle
---
- Another time with a blank line in between.
`
input := []byte(reference)
expected := []byte(reference)
How about ` + "`this`" + ` and other stuff like *italic*, **bold** and ***super extra***.
`)

output, err := markdown.Process("", input, nil)
if err != nil {
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 {
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 {
t.Errorf("Difference of %d lines:\n%s", bytes.Count(diff, []byte("\n")), string(diff))
diff, err := diff(got, want)
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))
}
})
}
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -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)

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

https://path\\to\\page

@@ -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)

@@ -0,0 +1,3 @@
Some text with two trailing spaces for linebreak.
More spaced **text** *immediately* after that.
More than two spaces become two.
@@ -0,0 +1,3 @@
Some text with two trailing spaces for linebreak.
More spaced **text** *immediately* after that.
More than two spaces become two.
@@ -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.
@@ -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.
@@ -1,4 +1,4 @@
Demonstrates https://github.com/shurcooL/markdownfmt/issues/20
Test for issue https://github.com/shurcooL/markdownfmt/issues/20.

text text

@@ -1,4 +1,4 @@
Demonstrates https://github.com/shurcooL/markdownfmt/issues/20
Test for issue https://github.com/shurcooL/markdownfmt/issues/20.

text
text

0 comments on commit 8585be1

Please sign in to comment.
You can’t perform that action at this time.