Added unit tests and workflow call for same
This commit is contained in:
@@ -21,6 +21,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
go-version: '1.24.4'
|
go-version: '1.24.4'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
- name: Build the fib app
|
- name: Test build
|
||||||
run: go build
|
run: go build
|
||||||
- run: echo "🍏 This job's status is ${{ job.status }}."
|
- name: Test functions module
|
||||||
|
run: go test ./functions -v
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,6 +7,7 @@
|
|||||||
# Ignore everything
|
# Ignore everything
|
||||||
*
|
*
|
||||||
|
|
||||||
|
.env
|
||||||
# But not these files...
|
# But not these files...
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
package functions
|
package functions
|
||||||
|
|
||||||
func MyFibonacci(count int) (fibonaccivalue int) {
|
import(
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MyFibonacci(count int) (fibonaccivalue int, error error) {
|
||||||
fibonaccivalue = 1
|
fibonaccivalue = 1
|
||||||
|
error = nil
|
||||||
var prev_fibonacci int = 0
|
var prev_fibonacci int = 0
|
||||||
|
|
||||||
|
if count < 1 {
|
||||||
|
return -1, errors.New(fmt.Sprintf("%d is not a positive integer.",count))
|
||||||
|
}
|
||||||
for i := 1; i < count; i++ {
|
for i := 1; i < count; i++ {
|
||||||
fibonaccivalue += prev_fibonacci
|
fibonaccivalue += prev_fibonacci
|
||||||
prev_fibonacci = fibonaccivalue - prev_fibonacci
|
prev_fibonacci = fibonaccivalue - prev_fibonacci
|
||||||
|
|||||||
15
functions/flagcheck.go
Normal file
15
functions/flagcheck.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package functions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FlagSet(name string) (found bool) {
|
||||||
|
found = false
|
||||||
|
flag.Visit(func (f *flag.Flag) {
|
||||||
|
if f.Name == name {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
52
functions/functions_test.go
Normal file
52
functions/functions_test.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package functions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Test fibonacci function
|
||||||
|
|
||||||
|
func TestFibonacci(t *testing.T) {
|
||||||
|
var sequence = [4]int{1, 3, 5, 9}
|
||||||
|
var values = [4]int{1, 2, 5, 34}
|
||||||
|
for i := 0; i < len(sequence); i++ {
|
||||||
|
var seq, want = sequence[i], values[i]
|
||||||
|
outcome, error := MyFibonacci(seq)
|
||||||
|
if error != nil {
|
||||||
|
t.Errorf("Error trying to Fibonacci %d: %s", seq, error)
|
||||||
|
}
|
||||||
|
if outcome != want {
|
||||||
|
t.Errorf("Invalid outcome: Fibonacci sequence %d should be %d, got %d",
|
||||||
|
seq, want, outcome)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFibBadNumber(t *testing.T) {
|
||||||
|
outcome,error := MyFibonacci(-4)
|
||||||
|
if error == nil {
|
||||||
|
t.Errorf("This should be an error, not a %d",outcome)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSums(t *testing.T) {
|
||||||
|
var tests = []struct{
|
||||||
|
a, b int
|
||||||
|
want int
|
||||||
|
} {
|
||||||
|
{1, 2, 3},
|
||||||
|
{5, 3, 8},
|
||||||
|
{9, 4, 13},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("%d+%d",tt.a, tt.b)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
result := sum(tt.a, tt.b)
|
||||||
|
if result != tt.want {
|
||||||
|
t.Errorf("Expected %d, got %d", tt.want,result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,8 +46,14 @@ func HttFibonacci(w http.ResponseWriter, request *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Printf("Received request for Fibonacci Sequence Number %d\n",
|
fmt.Printf("Received request for Fibonacci Sequence Number %d\n",
|
||||||
seq)
|
seq)
|
||||||
|
value, error := MyFibonacci(seq)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Printf("Error trying to Fibonacci %d: %s", seq, error)
|
||||||
|
fmt.Fprintf(w, "Error trying to Fibonacci %d: %s", seq, error)
|
||||||
|
} else {
|
||||||
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
|
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
|
||||||
seq, MyFibonacci(seq))
|
seq, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
43
main.go
43
main.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"flag"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ func main() {
|
|||||||
var port_number int = 8098 // hard default
|
var port_number int = 8098 // hard default
|
||||||
var error error
|
var error error
|
||||||
var bind_socket string
|
var bind_socket string
|
||||||
fmt.Println("Hello, world. I'm teaching myself Go.")
|
var seq, port int
|
||||||
|
|
||||||
error = godotenv.Load() // Load .env file
|
error = godotenv.Load() // Load .env file
|
||||||
if error != nil {
|
if error != nil {
|
||||||
@@ -23,16 +24,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
port_arg := os.Getenv("FIB_PORT")
|
port_arg := os.Getenv("FIB_PORT")
|
||||||
if port_arg != "" {
|
if port_arg != "" {
|
||||||
fmt.Printf("Loaded port value '%s' from environment.\n",
|
fmt.Printf("Loaded port value '%s' from environment.\n",port_arg)
|
||||||
port_arg)
|
|
||||||
}
|
|
||||||
if len(os.Args) > 1 { // commandline overrules env
|
|
||||||
port_arg = os.Args[1]
|
|
||||||
fmt.Printf("Loading port value '%s' from command line.\n",
|
|
||||||
port_arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if port_arg != "" {
|
|
||||||
port_number, error = strconv.Atoi(port_arg)
|
port_number, error = strconv.Atoi(port_arg)
|
||||||
if error != nil {
|
if error != nil {
|
||||||
fmt.Printf("I had an error trying to parse '%s' into a number: %s\n",
|
fmt.Printf("I had an error trying to parse '%s' into a number: %s\n",
|
||||||
@@ -41,9 +33,28 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bind_socket = fmt.Sprintf(":%d", port_number)
|
flag.IntVar(&port, "port", 8098,
|
||||||
http.HandleFunc("/fibonacci", functions.HttFibonacci)
|
fmt.Sprintf("Port number to bind to, default %d", port_number))
|
||||||
http.HandleFunc("/sum", functions.HttSum)
|
flag.IntVar(&seq, "number", 0,
|
||||||
fmt.Printf("Listening on port %d...\n", port_number)
|
"Fibonacci sequence value to calculate")
|
||||||
http.ListenAndServe(bind_socket, nil)
|
flag.Parse()
|
||||||
|
|
||||||
|
if functions.FlagSet("port") { // Command line overrides env
|
||||||
|
port_number = port
|
||||||
|
}
|
||||||
|
|
||||||
|
if !functions.FlagSet("number") {
|
||||||
|
fmt.Printf("Listening on port %d...\n", port_number)
|
||||||
|
bind_socket = fmt.Sprintf(":%d", port_number)
|
||||||
|
http.HandleFunc("/fibonacci", functions.HttFibonacci)
|
||||||
|
http.HandleFunc("/sum", functions.HttSum)
|
||||||
|
http.ListenAndServe(bind_socket, nil)
|
||||||
|
} else {
|
||||||
|
value, error := functions.MyFibonacci(seq)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Printf("Failed to Fibonacci %d: %s", seq, error)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%d", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user