Commit 29d5cb80 authored by Ma_124's avatar Ma_124

improved coverage

parent ea8f53e9
Pipeline #46721362 failed with stages
in 1 minute and 51 seconds
package awesomefw
import (
"errors"
"fmt"
"github.com/kballard/go-shellquote"
"io/ioutil"
......@@ -14,6 +13,10 @@ func IFFByName(name string) (*InputFileFormat, error) {
return nil, err
}
if err := processArgs(name, args); err != nil {
return nil, err
}
switch args[0] {
case "md:list":
if len(args) == 1 {
......@@ -69,17 +72,17 @@ func OFFByName(name string) (*OutputFileFormat, error) {
}
return OFFHtml(), nil
default:
return nil, errors.New("unknown off name")
return nil, fmt.Errorf("%q: unknown off name", name)
}
}
func processArgs(name string, args []string) error {
for i := range args {
if args[i][0] == '\\' || args[i][1] == '\\' {
if len(args[i]) > 1 && (args[i][0] == '\\' || args[i][1] == '\\') {
args[i] = args[i][1:]
} else if args[i][0] == '\\' || args[i][1] == '@' {
} else if len(args[i]) > 1 && (args[i][0] == '\\' || args[i][1] == '@') {
args[i] = args[i][1:]
} else if args[i][0] == '@' {
} else if len(args[i]) > 0 && (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:])
......
......@@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
"io/ioutil"
)
func TestIFFByName_Unknown(t *testing.T) {
......@@ -55,8 +56,121 @@ func TestIFFByName_TabSepDefArgs(t *testing.T) {
assert.Equal(t, "a\tb\tc\n", buf.String())
}
func TestOFFByName_MdTable(t *testing.T) {
off, err := OFFByName("md:table")
assert.Equal(t, nil, err)
buf := &strings.Builder{}
off.Item(buf, &Item{
"a",
"b",
"",
"c",
}, NewDebugCtx())
assert.Equal(t, "\n| Name | Desc |\n|------|------|\n| [a](b) | c |\n", buf.String())
}
func TestOFFByName_MdTableUnknownNArgs(t *testing.T) {
off, err := OFFByName("md:table 'a'")
assert.Equal(t, "\"md:table 'a'\": wrong number of arguments (usage: md:table)", err.Error())
assert.Equal(t, (*OutputFileFormat)(nil), off)
}
func TestOFFByName_MdTableMeta(t *testing.T) {
off, err := OFFByName("md:table+meta")
assert.Equal(t, nil, err)
buf := &strings.Builder{}
off.Item(buf, &Item{
"a",
"b",
"",
"c",
}, NewDebugCtx())
assert.Equal(t, "\n| Stars | Forks | Issues | Last Commit | Name | Desc |\n|-------|-------|--------|-------------|------|------|\n| N/A | N/A | N/A | N/A | [a](b) | c |\n", buf.String())
}
func TestOFFByName_MdTableMetaUnknownNArgs(t *testing.T) {
off, err := OFFByName("md:table+meta 'a'")
assert.Equal(t, "\"md:table+meta 'a'\": wrong number of arguments (usage: md:table+meta)", err.Error())
assert.Equal(t, (*OutputFileFormat)(nil), off)
}
func TestOFFByName_Html(t *testing.T) {
off, err := OFFByName("html")
assert.Equal(t, nil, err)
buf := &strings.Builder{}
off.Item(buf, &Item{
"a",
"b",
"",
"c",
}, NewDebugCtx())
assert.Equal(t, "<table><thead><tr><th>Stars</th><th>Forks</th><th>Issues</th><th>Last Commit</th><th>Name</th><th>Desc</th></tr></thead><tbody>\n<tr><td>N/A</td><td>N/A</td><td>N/A</td><td>N/A</td><td><a href=\"b\">a</a></td><td>c</td></tr>\n", buf.String())
}
func TestOFFByName_HtmlNArgs(t *testing.T) {
off, err := OFFByName("html 'a'")
assert.Equal(t, "\"html 'a'\": wrong number of arguments (usage: html)", err.Error())
assert.Equal(t, (*OutputFileFormat)(nil), off)
}
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)
}
func TestOFFByName_Unknown(t *testing.T) {
off, err := OFFByName("unknown_ ' - '")
assert.Equal(t, "\"unknown_ ' - '\": unknown off name", err.Error())
assert.Equal(t, (*OutputFileFormat)(nil), off)
}
func TestIorOFFByName_ProcessEscEscArgs(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_ProcessEscAtArgs(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_ProcessAtArgs(t *testing.T) {
f, err := ioutil.TempFile("", "awesome-framework-test-process-at-args-*.txt")
if err != nil {
panic(err)
}
defer f.Close()
f.Write([]byte("||"))
iff, err := IFFByName("md:list '@" + f.Name() + "'")
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_ProcessAtArgsUnknownFile(t *testing.T) {
iff, err := IFFByName("md:list '@/unknown'")
assert.Equal(t, (*InputFileFormat)(nil), iff)
assert.Equal(t, `"md:list '@/unknown'": cannot open file "/unknown"`, err.Error())
}
......@@ -26,9 +26,9 @@ func writeLiteralHtml(out io.Writer, l string, ctx *Ctx) {
}
func writeItemHtml(out io.Writer, item *Item, ctx *Ctx) {
if ctx.OutFFCtx["writeTblHead"] == true {
if ctx.OutFFCtx["writeTblHead"] == true || ctx.OutFFCtx["writeTblHead"] == nil {
ctx.OutFFCtx["writeTblHead"] = false
out.Write([]byte(`<table><thead><tr><th>Stars</th><th>Forks</th><th>Issues</th><th>Last Commit</th><th>Name</th><th>Desc</th></tr></thead><tbody>` + "\n"))
out.Write([]byte("<table><thead><tr><th>Stars</th><th>Forks</th><th>Issues</th><th>Last Commit</th><th>Name</th><th>Desc</th></tr></thead><tbody>\n"))
}
meta := FetchMeta(item.Url, ctx)
......
......@@ -64,7 +64,7 @@ func writeLiteralMd(out io.Writer, l string, ctx *Ctx) {
}
func writeSimpleItemMd(out io.Writer, item *Item, ctx *Ctx) {
if ctx.OutFFCtx["writeTblHead"] == true {
if ctx.OutFFCtx["writeTblHead"] == true || ctx.OutFFCtx["writeTblHead"] == nil {
ctx.OutFFCtx["writeTblHead"] = false
out.Write([]byte("\n| Name | Desc |\n"))
out.Write([]byte("|------|------|\n"))
......@@ -80,7 +80,7 @@ func writeSimpleItemMd(out io.Writer, item *Item, ctx *Ctx) {
}
func writeItemMd(out io.Writer, item *Item, ctx *Ctx) {
if ctx.OutFFCtx["writeTblHead"] == true {
if ctx.OutFFCtx["writeTblHead"] == true || ctx.OutFFCtx["writeTblHead"] == nil {
ctx.OutFFCtx["writeTblHead"] = false
out.Write([]byte("\n| Stars | Forks | Issues | Last Commit | Name | Desc |\n"))
out.Write([]byte("|-------|-------|--------|-------------|------|------|\n"))
......
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