Skip to content

Instantly share code, notes, and snippets.

@ridhoperdana
Created June 12, 2019 03:21
Show Gist options
  • Select an option

  • Save ridhoperdana/5d277ccacd0633d566e0778feebec91d to your computer and use it in GitHub Desktop.

Select an option

Save ridhoperdana/5d277ccacd0633d566e0778feebec91d to your computer and use it in GitHub Desktop.
Testfile for operation.go
package calculator_test
import (
"context"
"testing"
"github.com/ridhoperdana/calculator"
)
func TestSempoa_Add(t *testing.T) {
t.Run("success add number", func(t *testing.T) {
s := calculator.InitOperation()
result, err := s.Add(context.TODO(), 5)
if err != nil {
t.Fail()
}
if result != 5 {
t.Fail()
}
if s.SavedNumber != 5 {
t.Fail()
}
})
t.Run("success add negative number", func(t *testing.T) {
s := calculator.InitOperation()
result, err := s.Add(context.TODO(), -5)
if err != nil {
t.Fail()
}
if result != -5 {
t.Fail()
}
if s.SavedNumber != -5 {
t.Fail()
}
})
t.Run("success add more than one number", func(t *testing.T) {
s := calculator.InitOperation()
result, err := s.Add(context.TODO(), -5)
if err != nil {
t.Fail()
}
if result != -5 {
t.Fail()
}
result, err = s.Add(context.TODO(), 10)
if err != nil {
t.Fail()
}
if result != 5 {
t.Fail()
}
})
}
func TestSempoa_Minus(t *testing.T) {
t.Run("success minus number", func(t *testing.T) {
s := calculator.InitOperation()
result, err := s.Minus(context.TODO(), 5)
if err != nil {
t.Fail()
}
if result != -5 {
t.Fail()
}
if s.SavedNumber != -5 {
t.Fail()
}
})
t.Run("success minus negative number", func(t *testing.T) {
s := calculator.InitOperation()
result, err := s.Minus(context.TODO(), -5)
if err != nil {
t.Fail()
}
if result != 5 {
t.Fail()
}
if s.SavedNumber != 5 {
t.Fail()
}
})
t.Run("success minus more than one number", func(t *testing.T) {
s := calculator.InitOperation()
result, err := s.Minus(context.TODO(), -5)
if err != nil {
t.Fail()
}
if result != 5 {
t.Fail()
}
result, err = s.Minus(context.TODO(), 10)
if err != nil {
t.Fail()
}
if result != -5 {
t.Fail()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment