This commit is contained in:
28
parsing/cliargs.go
Normal file
28
parsing/cliargs.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package parsing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Cliargs struct {
|
||||
value *big.Int
|
||||
}
|
||||
|
||||
func (arg *Cliargs) Parse() (*big.Int, error) {
|
||||
args := os.Args[1:]
|
||||
if len(args) != 1 {
|
||||
return nil, errors.New("only expected 1 arg")
|
||||
}
|
||||
num, success := new(big.Int).SetString(args[0], 10)
|
||||
if !success {
|
||||
return nil, errors.New("argument is not an integer")
|
||||
}
|
||||
arg.value = num
|
||||
return num, nil
|
||||
}
|
||||
|
||||
func (arg *Cliargs) GetNumber() *big.Int {
|
||||
return arg.value
|
||||
}
|
||||
44
parsing/cliargs_test.go
Normal file
44
parsing/cliargs_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package parsing
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCliargs_Parse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
want *big.Int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test with valid integer",
|
||||
args: []string{"", "10"}, // the first argument is the program name
|
||||
want: big.NewInt(10),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test with non-integer",
|
||||
args: []string{"", "abc"}, // the first argument is the program name
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
os.Args = tt.args
|
||||
parser := &Cliargs{}
|
||||
got, err := parser.Parse()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && got.Cmp(tt.want) != 0 {
|
||||
t.Errorf("Parse() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
7
parsing/interface.go
Normal file
7
parsing/interface.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package parsing
|
||||
|
||||
import "math/big"
|
||||
|
||||
type Parser interface {
|
||||
Parse() (*big.Int, error)
|
||||
}
|
||||
Reference in New Issue
Block a user