Skip to content

Commit

Permalink
More cleanups, test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed Jul 14, 2023
1 parent 5268b3f commit 5a685c0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- run: go vet ./...
- run: go test -v ./...
- run: make
env:
URFAVE_ARGH_TRACING: on
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
BENCHTIME ?= 10s
STRINGER := .local/bin/stringer
URFAVE_ARGH_TRACING ?= off

export URFAVE_ARGH_TRACING

.PHONY: all
all: generate test
Expand Down
40 changes: 23 additions & 17 deletions argh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,41 @@ package argh
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)

var (
isTracingOn = os.Getenv("ARGH_TRACING") == "on"
traceLogger *log.Logger
isTracingOn = os.Getenv("URFAVE_ARGH_TRACING") == "on"

Err = errors.New("argh error")
Err = errors.New("urfave/argh error")
)

func init() {
func tracef(format string, a ...any) {
if !isTracingOn {
return
}

traceLogger = log.New(os.Stderr, "## ARGH TRACE ", 0)
}

func tracef(format string, v ...any) {
if !isTracingOn {
return
}

if _, file, line, ok := runtime.Caller(1); ok {
format = fmt.Sprintf("%v:%v ", filepath.Base(file), line) + format
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}

traceLogger.Printf(format, v...)
pc, file, line, _ := runtime.Caller(1)
cf := runtime.FuncForPC(pc)

fmt.Fprintf(
os.Stderr,
strings.Join([]string{
"## URFAVE ARGH TRACE ",
file,
":",
fmt.Sprintf("%v", line),
" ",
fmt.Sprintf("(%s)", cf.Name()),
" ",
format,
}, ""),
a...,
)
}
28 changes: 28 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package argh

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestCommandError(t *testing.T) {
err := &CommandError{
Pos: Position{Column: 42},
Node: &StopFlag{},
Msg: "unable to stop at this time",
}

require.Equal(t, "unable to stop at this time", fmt.Sprintf("%[1]v", err))
}

func TestFlagError(t *testing.T) {
err := &FlagError{
Pos: Position{Column: 42},
Node: &StdinFlag{},
Msg: "am just not that into you",
}

require.Equal(t, "am just not that into you", fmt.Sprintf("%[1]v", err))
}
7 changes: 7 additions & 0 deletions nvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var (

type NValue int

// Required returns whether the NValue represents a number that
// must be provided in order to be considered valid, which will
// always be true for OneOrMoreValue and will always be false for
// ZeroOrMoreValue.
func (nv NValue) Required() bool {
if nv == OneOrMoreValue {
return true
Expand All @@ -24,6 +28,9 @@ func (nv NValue) Required() bool {
return int(nv) >= 1
}

// Contains returns whether the given *index* is within the range
// of the NValue, which will always be false for negative integers
// and will always be true for OneOrMoreValue or ZeroOrMoreValue.
func (nv NValue) Contains(i int) bool {
tracef("NValue.Contains(%v)", i)

Expand Down
42 changes: 42 additions & 0 deletions nvalue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package argh

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNValue(t *testing.T) {
t.Run("String", func(t *testing.T) {
r := require.New(t)
r.Equal("OneOrMoreValue", OneOrMoreValue.String())
r.Equal("NValue(42)", NValue(42).String())
})

t.Run("Required", func(t *testing.T) {
r := require.New(t)
r.True(OneOrMoreValue.Required())
r.True(NValue(42).Required())
r.False(ZeroOrMoreValue.Required())
r.False(ZeroValue.Required())
})

t.Run("Contains", func(t *testing.T) {
r := require.New(t)
r.False(OneOrMoreValue.Contains(-1))
r.True(OneOrMoreValue.Contains(0))
r.True(OneOrMoreValue.Contains(1))
r.True(OneOrMoreValue.Contains(2))
r.True(OneOrMoreValue.Contains(42))

r.False(ZeroOrMoreValue.Contains(-1))
r.True(ZeroOrMoreValue.Contains(0))
r.True(ZeroOrMoreValue.Contains(1))
r.True(ZeroOrMoreValue.Contains(2))
r.True(ZeroOrMoreValue.Contains(42))

r.True(NValue(42).Contains(0))
r.True(NValue(42).Contains(41))
r.False(NValue(42).Contains(-1))
})
}
12 changes: 12 additions & 0 deletions token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package argh

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestTokenString(t *testing.T) {
require.Equal(t, "ILLEGAL", ILLEGAL.String())
require.Equal(t, "Token(42)", Token(42).String())
}

0 comments on commit 5a685c0

Please sign in to comment.