Commit ea8f53e9 authored by Ma_124's avatar Ma_124

added tests for IFFByName

parent 3e383a7a
Pipeline #46616290 failed with stages
in 2 minutes and 20 seconds
......@@ -30,7 +30,7 @@ func IFFByName(name string) (*InputFileFormat, error) {
// TODO rewrite tabsep
return IFFTabSep(), nil
default:
return nil, errors.New("unknown iff name")
return nil, fmt.Errorf("%q: unknown iff name", name)
}
}
......@@ -40,19 +40,8 @@ func OFFByName(name string) (*OutputFileFormat, error) {
return nil, err
}
for i := range args {
if args[i][0] == '\\' || args[i][1] == '\\' {
args[i] = args[i][1:]
} else if args[i][0] == '\\' || args[i][1] == '@' {
args[i] = args[i][1:]
} else if args[i][0] == '@' {
s, err := ioutil.ReadFile(args[i][1:])
if err != nil {
return nil, fmt.Errorf("%q: cannot open file %q", name, args[i][1:])
}
args[i] = string(s)
}
if err := processArgs(name, args); err != nil {
return nil, err
}
// TODO md>html
......@@ -83,3 +72,22 @@ func OFFByName(name string) (*OutputFileFormat, error) {
return nil, errors.New("unknown off name")
}
}
func processArgs(name string, args []string) error {
for i := range args {
if args[i][0] == '\\' || args[i][1] == '\\' {
args[i] = args[i][1:]
} else if args[i][0] == '\\' || args[i][1] == '@' {
args[i] = args[i][1:]
} else if args[i][0] == '@' {
s, err := ioutil.ReadFile(args[i][1:])
if err != nil {
return fmt.Errorf("%q: cannot open file %q", name, args[i][1:])
}
args[i] = string(s)
}
}
return nil
}
package awesomefw
import (
"github.com/kballard/go-shellquote"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestIFFByName_Unknown(t *testing.T) {
iff, err := IFFByName("unknown_ ' - '")
assert.Equal(t, "\"unknown_ ' - '\": unknown iff name", err.Error())
assert.Equal(t, (*InputFileFormat)(nil), iff)
}
func TestIFFByName_MdListArgs(t *testing.T) {
iff, err := IFFByName("md:list ': '")
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b): c\n", OFFTabSep(), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
func TestIFFByName_MdListDefArgs(t *testing.T) {
iff, err := IFFByName("md:list")
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b) - c\n", OFFTabSep(), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
func TestIFFByName_MdListUnknownNArgs(t *testing.T) {
iff, err := IFFByName("md:list 'a' ''")
assert.Equal(t, (*InputFileFormat)(nil), iff)
assert.Equal(t, "\"md:list 'a' ''\": wrong number of arguments (usage: md [sep])", err.Error())
}
func TestIFFByName_ShellquoteError(t *testing.T) {
iff, err := IFFByName("md:list '")
assert.Equal(t, err.Error(), shellquote.UnterminatedSingleQuoteError.Error())
assert.Equal(t, (*InputFileFormat)(nil), iff)
}
func TestIFFByName_TabSepDefArgs(t *testing.T) {
iff, err := IFFByName("tabsep")
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "a\tb\tc\n", OFFTabSep(), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
func TestOFFByName_ShellquoteError(t *testing.T) {
off, err := OFFByName("md:table '")
assert.Equal(t, err.Error(), shellquote.UnterminatedSingleQuoteError.Error())
assert.Equal(t, (*OutputFileFormat)(nil), off)
}
......@@ -4,6 +4,10 @@ import (
"errors"
"github.com/stretchr/testify/assert"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
......@@ -63,7 +67,7 @@ For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issu
}
func TestCompileFile_TmpFile(t *testing.T) {
f, err := ioutil.TempFile("", "awesome-framework-test-*.txt")
f, err := ioutil.TempFile("", "awesome-framework-test-compile-file-*.txt")
if err != nil {
panic(err)
}
......@@ -92,6 +96,26 @@ func TestCompileFile_UnknownFile(t *testing.T) {
CompileFile("/unknown", nil, IFFTabSep(), OFFTabSep(), nil)
}
func TestCompileFile_NoReadPermFile(t *testing.T) {
fname := filepath.Join(os.TempDir(), "awesome-framework-test-no-read-perm-"+strconv.FormatInt(rand.Int63(), 16)+".txt")
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)
}
CompileFile(fname, &strings.Builder{}, IFFTabSep(), OFFTabSep(), nil)
}
type testCompileFileReadErr struct{}
func (testCompileFileReadErr) Read(p []byte) (n int, err error) {
......
......@@ -25,7 +25,7 @@ func parseSimpleBulletMarkdownLine(sep string) func(out io.Writer, l string, out
l[starIndx+3 : afterNameI],
l[afterNameI+2 : strings.IndexByte(l[afterNameI+2:], ')')+afterNameI+2],
"",
l[strings.Index(l, sep)+3 : len(l)-1],
l[strings.Index(l, sep)+len(sep) : len(l)-1],
},
ctx)
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment