Skip to content

Commit

Permalink
added opc example (package)
Browse files Browse the repository at this point in the history
  • Loading branch information
kovalevsky0 committed Mar 21, 2024
1 parent 3bd5089 commit dd03646
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Grokking Go Fundamentals with Tests Video Series / Course
- [A new learning way on how to write "Hello, World!" program in Go](https://www.youtube.com/watch?v=EECXgocw4N0)
- [Source Code (helloworld)](./helloworld/)
- [Everything You Need to Know to Start Writing Tests In Go With TDD (For Go Beginners)](https://www.youtube.com/watch?v=Fr9xox_soKQ&t=794s)
- [Source Code](./calculator/)
- [Source Code (calculator)](./calculator/)
- [Let's learn variables and constants in Go with tests and TDD. Part 1](https://www.youtube.com/watch?v=9bny23btS2s&t=422s)
- [Source Code (opc)](./opc/)

[Link on YouTube Playlist](https://www.youtube.com/playlist?list=PLCqcI2Ic-eM_4ma__dXm4xX3P9fgQ2V9C)

23 changes: 23 additions & 0 deletions opc/opc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package opc

import "fmt"

const discountRate float64 = 0.9

func CalculateOrderPrice(
itemPrice float64,
quantity int,
taxRate float64,
discount bool,
) string {
totalPrice := itemPrice * float64(quantity)

if discount {
totalPrice = totalPrice * discountRate
}

taxAmount := totalPrice * taxRate
finalPrice := totalPrice + taxAmount

return fmt.Sprintf("The final price is: €%.2f", finalPrice)
}
56 changes: 56 additions & 0 deletions opc/opc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package opc_test

import (
"go-sandbox/opc"
"testing"
"fmt"
)

func TestCalculateOrderPrice(t *testing.T) {
type testCase struct {
itemPrice, taxRate float64
quantity int
discount bool
want string
}

testCases := []testCase{
{
itemPrice: 2.99,
taxRate: 0.07,
quantity: 3,
discount: true,
want: "The final price is: €8.64",
},
{
itemPrice: 2.99,
taxRate: 0.07,
quantity: 3,
discount: false,
want: "The final price is: €9.60",
},
}

for _, tc := range testCases {
tcName := fmt.Sprintf(
"itemPrice = %.2f, quantity = %d, taxRate = %.2f, discount = %t",
tc.itemPrice,
tc.quantity,
tc.taxRate,
tc.discount,
)

t.Run(tcName, func (t *testing.T) {
got := opc.CalculateOrderPrice(
tc.itemPrice,
tc.quantity,
tc.taxRate,
tc.discount,
)

if tc.want != got {
t.Errorf("Failed! Wanted %q, got %q", tc.want, got)
}
})
}
}

0 comments on commit dd03646

Please sign in to comment.