-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4: Add AssertFloat64 and AssertInt64 r=Jaskaranbir a=Jaskaranbir Co-authored-by: Jaskaranbir <jaskaranbir.dhillon@gmail.com>
- Loading branch information
Showing
6 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package commonutil | ||
|
||
import "errors" | ||
|
||
// AssertFloat64 attempts to type-assert the provided interface-argument to int64. | ||
// The provided interface-argument must be a numeric-type or an error is thrown. | ||
func AssertFloat64(num interface{}) (float64, error) { | ||
switch num.(type) { | ||
case int32: | ||
v := num.(int32) | ||
return float64(v), nil | ||
case int64: | ||
v := num.(int64) | ||
return float64(v), nil | ||
case float32: | ||
v := num.(float32) | ||
return float64(v), nil | ||
case float64: | ||
return num.(float64), nil | ||
default: | ||
return 0, errors.New("assertFloat64: Unexpected data-type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package commonutil | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Float64Util", func() { | ||
Describe("AssertFloat64", func() { | ||
Context("assert given numeric-interface to float64", func() { | ||
Specify("int32", func() { | ||
var v int32 = 4 | ||
var v64 float64 = float64(v) | ||
float64Val, err := AssertFloat64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(float64Val).To(Equal(v64)) | ||
}) | ||
|
||
Specify("int64", func() { | ||
var v int64 = 78 | ||
var v64 float64 = float64(v) | ||
float64Val, err := AssertFloat64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(float64Val).To(Equal(v64)) | ||
}) | ||
|
||
Specify("float32", func() { | ||
var v float32 = 234 | ||
var v64 float64 = float64(v) | ||
float64Val, err := AssertFloat64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(float64Val).To(Equal(v64)) | ||
}) | ||
|
||
Specify("float64", func() { | ||
var v float64 = 457 | ||
float64Val, err := AssertFloat64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(float64Val).To(Equal(v)) | ||
}) | ||
}) | ||
|
||
Context("pass non-numeric interface", func() { | ||
It("should return an error", func() { | ||
v := "invalid" | ||
float64Val, err := AssertFloat64(v) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(float64Val).To(Equal(float64(0))) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package commonutil | ||
|
||
import "errors" | ||
|
||
// AssertInt64 attempts to type-assert the provided interface-argument to int64. | ||
// The provided interface-argument must be a numeric-type or an error is thrown. | ||
func AssertInt64(num interface{}) (int64, error) { | ||
switch num.(type) { | ||
case int32: | ||
v := num.(int32) | ||
return int64(v), nil | ||
case int64: | ||
return num.(int64), nil | ||
case float32: | ||
v := num.(float32) | ||
return int64(v), nil | ||
case float64: | ||
v := num.(float64) | ||
return int64(v), nil | ||
default: | ||
return 0, errors.New("assertInt64: Unexpected data-type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package commonutil | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Int64Util", func() { | ||
Describe("AssertInt64", func() { | ||
Context("assert given numeric-interface to int64", func() { | ||
Specify("int32", func() { | ||
var v int32 = 4 | ||
var v64 int64 = int64(v) | ||
int64Val, err := AssertInt64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(int64Val).To(Equal(v64)) | ||
}) | ||
|
||
Specify("int64", func() { | ||
var v int64 = 78 | ||
int64Val, err := AssertInt64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(int64Val).To(Equal(v)) | ||
}) | ||
|
||
Specify("float32", func() { | ||
var v float32 = 234 | ||
var v64 int64 = int64(v) | ||
int64Val, err := AssertInt64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(int64Val).To(Equal(v64)) | ||
}) | ||
|
||
Specify("float64", func() { | ||
var v float64 = 457 | ||
var v64 int64 = int64(v) | ||
int64Val, err := AssertInt64(v) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(int64Val).To(Equal(v64)) | ||
}) | ||
}) | ||
|
||
Context("pass non-numeric interface", func() { | ||
It("should return an error", func() { | ||
v := "invalid" | ||
int64Val, err := AssertInt64(v) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(int64Val).To(Equal(int64(0))) | ||
}) | ||
}) | ||
}) | ||
}) |