Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
refactoring. Adding github action.
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryBrownEEngr committed Oct 27, 2023
1 parent 53c4621 commit b83b5d9
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 5 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go CI

on:
push:

jobs:
build:
name: Go CI
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3.1.0

- name: Set up Go
uses: actions/setup-go@v3.4.0
with:
go-version: 1.21.2
cache: true
cache-dependency-path: go.sum

- name: Install dependencies
run: |
sudo apt install libc6-dev libgl1-mesa-dev libxcursor-dev libxi-dev libxinerama-dev libxrandr-dev libxxf86vm-dev libasound2-dev pkg-config
# Check if running "go mod tidy" changes anything. If so, the commit is dirty and needs fixed.
- name: Tidy Check
run: |
go mod tidy
git diff --exit-code -- go.mod go.sum
- name: Build
run: go build -v ./...

- name: Vet
run: go vet ./...

- name: Test
run: |
go test -v -short ./... -coverprofile coverage.out
go tool cover -html=coverage.out -o coverage.html
# save to artifact
- name: Upload Coverage
uses: actions/upload-artifact@v3.1.1
with:
name: GolangCoverage
path: coverage.html
retention-days: 21

# https://github.com/lluuiissoo/go-testcoverage
- name: Check Coverage Threshold
env:
COVERAGE_THRESHOLD_PERCENT: 1
run: |
echo "Quality Gate: checking test coverage is above threshold ..."
echo "Threshold : $COVERAGE_THRESHOLD_PERCENT %"
totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
echo "Current test coverage : $totalCoverage %"
if (( $(echo "$totalCoverage $COVERAGE_THRESHOLD_PERCENT" | awk '{print ($1 >= $2)}') )); then
echo "OK"
else
echo "Current test coverage is below threshold. Please add more unit tests or adjust threshold to a lower value."
echo "Failed"
exit 1
fi
4 changes: 2 additions & 2 deletions examples/play1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"time"

"github.com/GaryBrownEEngr/scratch"
"github.com/GaryBrownEEngr/scratch/game"
"github.com/GaryBrownEEngr/scratch/models"
"github.com/GaryBrownEEngr/scratch/sprite"
"github.com/GaryBrownEEngr/scratch/tools"
)

Expand All @@ -28,7 +28,7 @@ func fileExists(path string) bool {
}

func simStartFunc(sim models.Scratch) {
sim.AddCostume(game.DecodeCodedSprite(game.TurtleImage), "t1")
sim.AddCostume(sprite.DecodeCodedSprite(sprite.TurtleImage), "t1")

if fileExists("jab.wav") {
sim.AddSound("jab.wav", "jab")
Expand Down
4 changes: 2 additions & 2 deletions examples/play2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"time"

"github.com/GaryBrownEEngr/scratch"
"github.com/GaryBrownEEngr/scratch/game"
"github.com/GaryBrownEEngr/scratch/models"
"github.com/GaryBrownEEngr/scratch/sprite"
)

func main() {
Expand All @@ -14,7 +14,7 @@ func main() {
}

func simStartFunc(sim models.Scratch) {
sim.AddCostume(game.DecodeCodedSprite(game.TurtleImage), "t1")
sim.AddCostume(sprite.DecodeCodedSprite(sprite.TurtleImage), "t1")

a := 0.0
s := sim.AddSprite("mainTurtle")
Expand Down
15 changes: 15 additions & 0 deletions models/mocks/mocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Mockery Mocks

[Mockery examples](https://vektra.github.io/mockery/latest/examples/)

## Install Mockery

```bash
go install github.com/vektra/mockery/v2@v2.32.0
```

## Build for models directory

```bash
mockery --all --dir="./models" --output="./models/mocks"
```
2 changes: 1 addition & 1 deletion game/spriteDefaults.go → sprite/spriteDefaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package game
package sprite

import (
"bytes"
Expand Down
43 changes: 43 additions & 0 deletions tools/colorUtil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tools

import "image/color"

// The ratio is capped between 0 and 1
func Lerp(a, b, ratio float64) float64 {
if ratio > 1 {
ratio = 1
} else if ratio < 0 {
ratio = 0
}
y := a + (b-a)*ratio
return y
}

// The ratio is capped between 0 and 1
// Currently the function floors the number instead of rounding to nearest.
func LerpUint8(a, b uint8, ratio float64) uint8 {
if ratio > 1 {
ratio = 1
} else if ratio < 0 {
ratio = 0
}

aF := float64(a)
bF := float64(b)
y := aF + (bF-aF)*ratio
return uint8(y)
}

// Creates a color between the given a and b. 0 means a is given, 1 means b is given, .5 is a color half way between.
// The ratio is capped between 0 and 1
// Currently the function floors the number instead of rounding to nearest.
func LerpColor(a, b color.RGBA, ratio float64) color.RGBA {
ret := color.RGBA{
R: LerpUint8(a.R, b.R, ratio),
G: LerpUint8(a.G, b.G, ratio),
B: LerpUint8(a.B, b.B, ratio),
A: LerpUint8(a.A, b.A, ratio),
}

return ret
}
103 changes: 103 additions & 0 deletions tools/colorUtil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package tools

import (
"image/color"
"reflect"
"testing"
)

func TestLerp(t *testing.T) {
type args struct {
a float64
b float64
ratio float64
}
tests := []struct {
name string
args args
want float64
}{
// TODO: Add test cases.
{name: "ratio 0", args: args{a: 0, b: 100, ratio: 0}, want: 0},
{name: "ratio .5", args: args{a: 0, b: 100, ratio: .5}, want: 50},
{name: "ratio 1", args: args{a: 0, b: 100, ratio: 1}, want: 100},
// Test negative direction
{name: "ratio 0", args: args{a: 100, b: -100, ratio: 0}, want: 100},
{name: "ratio .5", args: args{a: 100, b: -100, ratio: .5}, want: 0},
{name: "ratio 1", args: args{a: 100, b: -100, ratio: 1}, want: -100},
// Now test out of bounds behaviour
{name: "ratio 3", args: args{a: 0, b: 100, ratio: 3}, want: 100},
{name: "ratio -1", args: args{a: 0, b: 100, ratio: -1}, want: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Lerp(tt.args.a, tt.args.b, tt.args.ratio); got != tt.want {
t.Errorf("Lerp() = %v, want %v", got, tt.want)
}
})
}
}

func TestLerpUint8(t *testing.T) {
type args struct {
a uint8
b uint8
ratio float64
}
tests := []struct {
name string
args args
want uint8
}{
// TODO: Add test cases.
{name: "ratio 0", args: args{a: 0, b: 255, ratio: 0}, want: 0},
{name: "ratio 0", args: args{a: 0, b: 255, ratio: .5}, want: 127},
{name: "ratio 0", args: args{a: 0, b: 255, ratio: 1}, want: 255},
{name: "ratio 0", args: args{a: 255, b: 128, ratio: 0}, want: 255},
{name: "ratio 0", args: args{a: 255, b: 128, ratio: .5}, want: 191},
{name: "ratio 0", args: args{a: 255, b: 128, ratio: 1}, want: 128},
// Now test out of bounds behaviour
{name: "ratio 0", args: args{a: 0, b: 255, ratio: 2.5}, want: 255},
{name: "ratio 0", args: args{a: 0, b: 255, ratio: -1}, want: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LerpUint8(tt.args.a, tt.args.b, tt.args.ratio); got != tt.want {
t.Errorf("LerpUint8() = %v, want %v", got, tt.want)
}
})
}
}

func TestLerpColor(t *testing.T) {
type args struct {
a color.RGBA
b color.RGBA
ratio float64
}
tests := []struct {
name string
args args
want color.RGBA
}{
// TODO: Add test cases.
{name: "ratio 0", args: args{a: Black, b: White, ratio: 0}, want: Black},
{name: "ratio 1", args: args{a: Black, b: White, ratio: 1}, want: White},
{
name: "ratio 1",
args: args{
a: color.RGBA{0x00, 0x00, 0x00, 0xFF},
b: color.RGBA{0x10, 0x10, 0x10, 0x00},
ratio: .5,
},
want: color.RGBA{0x08, 0x08, 0x08, 0x7F},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LerpColor(tt.args.a, tt.args.b, tt.args.ratio); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LerpColor() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit b83b5d9

Please sign in to comment.