Skip to content

Instantly share code, notes, and snippets.

@pei0804
Created June 7, 2018 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pei0804/ff0408550fd14de710708e8ea62a0e2a to your computer and use it in GitHub Desktop.
Save pei0804/ff0408550fd14de710708e8ea62a0e2a to your computer and use it in GitHub Desktop.
package tdd
import (
"fmt"
"reflect"
)
// Semver a
type Semver struct {
major uint
minor uint
patch uint
}
// NewSemver a
func NewSemver(major uint, minor uint, patch uint) *Semver {
return &Semver{
major: major,
minor: minor,
patch: patch,
}
}
// GetVersionString a
func (s *Semver) GetVersionString() string {
return fmt.Sprintf("%v", s)
}
func (s *Semver) String() string {
return fmt.Sprintf("%d.%d.%d", s.major, s.minor, s.patch)
}
// GetMajor a
func (s *Semver) GetMajor() uint {
return s.major
}
// GetMinor a
func (s *Semver) GetMinor() uint {
return s.minor
}
// GetPatch a
func (s *Semver) GetPatch() uint {
return s.patch
}
// IsEqualSemver a
func IsEqualSemver(s1 *Semver, s2 *Semver) bool {
return reflect.DeepEqual(s1, s2)
}
// IsBiggerThan a
func (s *Semver) IsBiggerThan(comparison *Semver) bool {
if s.GetMajor() < comparison.GetMajor() {
return false
} else if s.GetMajor() > comparison.GetMajor() {
return true
}
if s.GetMinor() < comparison.GetMinor() {
return false
} else if s.GetMinor() > comparison.GetMinor() {
return true
}
if s.GetPatch() < comparison.GetPatch() {
return false
} else if s.GetPatch() > comparison.GetPatch() {
return true
}
return false
}
package tdd
import (
"testing"
)
func TestSemverのバージョン番号を文字列に変換する(t *testing.T) {
s := NewSemver(1, 4, 2)
actual := s.GetVersionString()
expected := "1.4.2"
if actual != expected {
t.Errorf("actual:%s expected:%s", actual, expected)
}
}
func TestSemver1_4_2のメジャーバージョン番号はintの1である(t *testing.T) {
s := NewSemver(1, 4, 2)
actual := s.GetMajor()
expected := uint(1)
if actual != expected {
t.Errorf("actual:%d expected:%d", actual, expected)
}
}
func TestSemver1_4_2のマイナーバージョン番号はintの4である(t *testing.T) {
s := NewSemver(1, 4, 2)
actual := s.GetMinor()
expected := uint(4)
if actual != expected {
t.Errorf("actual:%d expected:%d", actual, expected)
}
}
func TestSemver1_4_2のパッチバージョン番号はintの2である(t *testing.T) {
s := NewSemver(1, 4, 2)
actual := s.GetPatch()
expected := uint(2)
if actual != expected {
t.Errorf("actual:%d expected:%d", actual, expected)
}
}
func TestSemver1_4_2はSemver2_1_0と等しくない(t *testing.T) {
s := NewSemver(1, 4, 2)
s2 := NewSemver(2, 1, 0)
actual := IsEqualSemver(s, s2)
expected := false
if actual != expected {
t.Errorf("actual:%t expected:%t", actual, expected)
}
}
func TestSemver1_4_2はSemver1_4_2と等しい(t *testing.T) {
s := NewSemver(1, 4, 2)
s2 := NewSemver(1, 4, 2)
actual := IsEqualSemver(s, s2)
expected := true
if actual != expected {
t.Errorf("actual:%t expected:%t", actual, expected)
}
}
func TestSemver1_3_9はSemver1_4_2よりちいさい(t *testing.T) {
s := NewSemver(1, 4, 2)
s2 := NewSemver(1, 3, 9)
actual := s2.IsBiggerThan(s)
expected := false
if actual != expected {
t.Errorf("%v > %v; %t, actual: %t", s2, s, expected, actual)
}
}
func TestSemver10_3_5はSemver2_23_1よりおっきぃ(t *testing.T) {
s := NewSemver(10, 3, 5)
s2 := NewSemver(2, 23, 1)
actual := s.IsBiggerThan(s2)
expected := true
if actual != expected {
t.Errorf("%v > %v; %t, actual: %t", s, s2, expected, actual)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment