diff options
| author | mo khan <mo.khan@gmail.com> | 2019-11-02 09:16:26 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2019-11-02 09:16:26 -0600 |
| commit | fadc9064e56cafcbda1597e2473b66c5a105b081 (patch) | |
| tree | 000c6ea37dae54e84ebc9b25a218c6534b3704b1 | |
| parent | 727fcdf3e8c5300880358d591c772f542b74fdd6 (diff) | |
Play with pointers
| -rwxr-xr-x | bin/lint | 5 | ||||
| -rwxr-xr-x | bin/setup | 3 | ||||
| -rw-r--r-- | wallet.go | 37 | ||||
| -rw-r--r-- | wallet_test.go | 54 |
4 files changed, 99 insertions, 0 deletions
diff --git a/bin/lint b/bin/lint new file mode 100755 index 0000000..d579ece --- /dev/null +++ b/bin/lint @@ -0,0 +1,5 @@ +#!/bin/sh + +./bin/setup + +errcheck . diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..37ebb32 --- /dev/null +++ b/bin/setup @@ -0,0 +1,3 @@ +#!/bin/sh + +go get -u github.com/kisielk/errcheck diff --git a/wallet.go b/wallet.go new file mode 100644 index 0000000..48c83dc --- /dev/null +++ b/wallet.go @@ -0,0 +1,37 @@ +package main + +import ( + "errors" + "fmt" +) + +type Bitcoin int + +func (b Bitcoin) String() string { + return fmt.Sprintf("%d BTC", b) +} + +type Wallet struct { + balance Bitcoin +} + +var InsufficientFundsError = errors.New("cannot withdraw, insufficient funds") + +func (w *Wallet) Deposit(amount Bitcoin) { + fmt.Printf("address of balance in test is %v \n", &w.balance) + w.balance += amount +} + +func (w *Wallet) Balance() Bitcoin { + fmt.Printf("address of balance in test is %v \n", &w.balance) + return w.balance +} + +func (w *Wallet) Withdraw(amount Bitcoin) error { + if amount > w.balance { + return InsufficientFundsError + } + + w.balance -= amount + return nil +} diff --git a/wallet_test.go b/wallet_test.go new file mode 100644 index 0000000..99dacf1 --- /dev/null +++ b/wallet_test.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "testing" +) + +func TestWallet(t *testing.T) { + t.Run("Deposit", func(t *testing.T) { + wallet := Wallet{} + wallet.Deposit(Bitcoin(10)) + fmt.Printf("address of balance in test is %v \n", &wallet.balance) + assertBalance(t, wallet, Bitcoin(10)) + }) + + t.Run("Withdraw with funds", func(t *testing.T) { + wallet := Wallet{balance: Bitcoin(20)} + err := wallet.Withdraw(Bitcoin(10)) + assertBalance(t, wallet, Bitcoin(10)) + assertNoError(t, err) + }) + + t.Run("Withdraw insufficient funds", func(t *testing.T) { + wallet := Wallet{balance: Bitcoin(20)} + err := wallet.Withdraw(Bitcoin(100)) + assertBalance(t, wallet, Bitcoin(20)) + assertError(t, err, InsufficientFundsError) + }) +} + +func assertBalance(t *testing.T, wallet Wallet, want Bitcoin) { + t.Helper() + got := wallet.Balance() + if got != want { + t.Errorf("got %s want %s", got, want) + } +} + +func assertError(t *testing.T, got error, want error) { + t.Helper() + if got == nil { + t.Fatal("wanted an error but didn't get one") + } + if got != want { + t.Errorf("got %q, want %q", got, want) + } +} + +func assertNoError(t *testing.T, got error) { + t.Helper() + if got != nil { + t.Fatal("wanted an error but didn't get one") + } +} |
