compile_inp_test.go 4.37 KB
Newer Older
Ma_124's avatar
Ma_124 committed
1 2 3
package awesomefw

import (
Ma_124's avatar
Ma_124 committed
4
	"errors"
Ma_124's avatar
Ma_124 committed
5
	"github.com/stretchr/testify/assert"
Ma_124's avatar
Ma_124 committed
6
	"io/ioutil"
Ma_124's avatar
Ma_124 committed
7 8 9
	"os"
	"path/filepath"
	"strconv"
Ma_124's avatar
Ma_124 committed
10
	"strings"
11
	"testing"
Ma_124's avatar
Ma_124 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
)

var awesomeAwesomeShortOut = `
"# Awesome Awesome
"
"A curated list of awesome curated lists of many topics.
"
"- [Awesome Awesome](#awesome-awesome)
"    - [Computer management](#computer-management)
"## Computer management
"
awesome-scalability	https://github.com/binhnguyennus/awesome-scalability	An up-to-date and curated reading list for designing high scalability, high availability, high stabilityback-end systems.
awesome-shell	https://github.com/alebcay/awesome-shell	Command-line frameworks, toolkits, guides and gizmos.
awesome-sysadmin	https://github.com/kahun/awesome-sysadmin	Backups, configuration management, DNS, IMAP/POP3, LDAP, monitoring, SSH, statistics, troubleshooting, virtualization, VPN and more.
"
"For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issues) and/or a [pull request](https://github.com/emijrp/awesome-awesome/pulls). Above there are some ideas for missing lists, be bold!
`[1:]

Ma_124's avatar
Ma_124 committed
30 31 32 33
func TestCompile_TabSepTabSepInvalid(t *testing.T) {
	defer func() {
		r := recover()

Ma_124's avatar
Ma_124 committed
34
		assert.Equal(t, "\"ghi\": expected <url>\n", r)
Ma_124's avatar
Ma_124 committed
35 36 37 38 39 40 41 42
	}()

	in := `
"Abc
d	e	f
ghi
`[1:]

Ma_124's avatar
Ma_124 committed
43
	Compile(strings.NewReader(in), len(in), &strings.Builder{}, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), NewDebugCfg())
Ma_124's avatar
Ma_124 committed
44 45
}

Ma_124's avatar
Ma_124 committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
func TestCompile_MarkdownTabSep(t *testing.T) {
	out := &strings.Builder{}
	in := `
# Awesome Awesome

A curated list of awesome curated lists of many topics.

- [Awesome Awesome](#awesome-awesome)
    - [Computer management](#computer-management)
## Computer management

* [awesome-scalability](https://github.com/binhnguyennus/awesome-scalability) - An up-to-date and curated reading list for designing high scalability, high availability, high stabilityback-end systems.
* [awesome-shell](https://github.com/alebcay/awesome-shell) - Command-line frameworks, toolkits, guides and gizmos.
* [awesome-sysadmin](https://github.com/kahun/awesome-sysadmin) - Backups, configuration management, DNS, IMAP/POP3, LDAP, monitoring, SSH, statistics, troubleshooting, virtualization, VPN and more.

For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issues) and/or a [pull request](https://github.com/emijrp/awesome-awesome/pulls). Above there are some ideas for missing lists, be bold!
`[1:]
Ma_124's avatar
Ma_124 committed
63 64
	Compile(strings.NewReader(in), len(in), out, IFFMarkdown(" - "), OFFTabSep(NameUrlDescTabSepPattern), NewDebugCfg())
NewDebugReqsApisCfg()
Ma_124's avatar
Ma_124 committed
65

Ma_124's avatar
Ma_124 committed
66
	assert.Equal(t, awesomeAwesomeShortOut, out.String())
Ma_124's avatar
Ma_124 committed
67
}
Ma_124's avatar
Ma_124 committed
68 69

func TestCompileFile_TmpFile(t *testing.T) {
Ma_124's avatar
Ma_124 committed
70
	f, err := ioutil.TempFile("", "awesome-framework-test-compile-file-*.txt")
Ma_124's avatar
Ma_124 committed
71 72 73 74 75
	if err != nil {
		panic(err)
	}
	defer f.Close()

Ma_124's avatar
Ma_124 committed
76
	_, _ = f.Write([]byte(awesomeAwesomeShortOut[:len(awesomeAwesomeShortOut)-1]))
Ma_124's avatar
Ma_124 committed
77

Ma_124's avatar
Ma_124 committed
78
	_ = f.Close()
Ma_124's avatar
Ma_124 committed
79 80

	buf := &strings.Builder{}
Ma_124's avatar
Ma_124 committed
81
	CompileFile(f.Name(), buf, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), NewDebugCfg())
Ma_124's avatar
Ma_124 committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95

	assert.Equal(t, buf.String(), awesomeAwesomeShortOut)
}

func TestCompileFile_UnknownFile(t *testing.T) {
	defer func() {
		r := recover()
		if _, ok := r.(error); ok {
			assert.Equal(t, r.(error).Error(), "stat /unknown: no such file or directory")
		} else {
			t.Fail()
		}
	}()

Ma_124's avatar
Ma_124 committed
96
	CompileFile("/unknown", nil, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), nil)
Ma_124's avatar
Ma_124 committed
97 98
}

Ma_124's avatar
Ma_124 committed
99
func TestCompileFile_NoReadPermFile(t *testing.T) {
Ma_124's avatar
Ma_124 committed
100
	fname := filepath.Join(os.TempDir(), "awesome-framework-test-no-read-perm-"+strconv.FormatInt(randSource.Int63(), 36)+".txt")
Ma_124's avatar
Ma_124 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

	defer func() {
		r := recover()
		if _, ok := r.(error); ok {
			assert.Equal(t, r.(error).Error(), "open "+fname+": permission denied")
		} else {
			t.Fail()
		}
	}()

	_, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0200)
	if err != nil {
		panic(err)
	}

Ma_124's avatar
Ma_124 committed
116
	CompileFile(fname, &strings.Builder{}, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), NewDebugCfg())
Ma_124's avatar
Ma_124 committed
117 118
}

Ma_124's avatar
Ma_124 committed
119 120 121 122 123 124 125 126 127 128 129 130 131
type testCompileFileReadErr struct{}

func (testCompileFileReadErr) Read(p []byte) (n int, err error) {
	return 0, errors.New("error by mocked reader")
}

func TestCompileFile_ReadErr(t *testing.T) {
	defer func() {
		r := recover()

		assert.Equal(t, r, errors.New("error by mocked reader"))
	}()

Ma_124's avatar
Ma_124 committed
132
	Compile(&testCompileFileReadErr{}, 0, &strings.Builder{}, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), NewDebugCfg())
Ma_124's avatar
Ma_124 committed
133
}