-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the external API of the PropertyBag #1669
Changes from 4 commits
f451256
eb143a4
5954157
cd54a3a
1ed64b8
2523ed7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,23 +6,113 @@ | |
package genruntime | ||
|
||
import ( | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
/* | ||
* NewPropertyBag() Tests | ||
*/ | ||
|
||
func TestPropertyBag_NewPropertyBag_WithNoParameters_ReturnsEmptyBag(t *testing.T) { | ||
g := NewWithT(t) | ||
bag := NewPropertyBag() | ||
g.Expect(bag).To(BeEmpty()) | ||
} | ||
|
||
func TestPropertyBag_NewPropertyBag_WithZeroBag_ReturnsEmptyBag(t *testing.T) { | ||
g := NewWithT(t) | ||
var zeroBag PropertyBag | ||
bag := NewPropertyBag(zeroBag) | ||
g.Expect(bag).To(BeEmpty()) | ||
} | ||
|
||
func TestPropertyBag_NewPropertyBag_WithEmptyBag_ReturnsEmptyBag(t *testing.T) { | ||
g := NewWithT(t) | ||
emptyBag := NewPropertyBag() | ||
bag := NewPropertyBag(emptyBag) | ||
g.Expect(bag).To(BeEmpty()) | ||
} | ||
|
||
func TestPropertyBag_NewPropertyBag_WithSingleItemBag_ReturnsBagWithExpectedLength(t *testing.T) { | ||
g := NewWithT(t) | ||
original := NewPropertyBag() | ||
g.Expect(original.Add("Answer", 42)).To(Succeed()) | ||
|
||
bag := NewPropertyBag(original) | ||
g.Expect(bag).To(HaveLen(1)) | ||
} | ||
|
||
func TestPropertyBag_NewPropertyBag_WithMultipleItemBag_ReturnsBagWithExpectedContent(t *testing.T) { | ||
g := NewWithT(t) | ||
original := NewPropertyBag() | ||
g.Expect(original.Add("Answer", 42)).To(Succeed()) | ||
g.Expect(original.Add("Halloween", "31OCT")).To(Succeed()) | ||
g.Expect(original.Add("Christmas", "25DEC")).To(Succeed()) | ||
|
||
bag := NewPropertyBag(original) | ||
g.Expect(bag).To(HaveKey("Answer")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You aren't asserting that the actual content is correct here, just that it has the key? Same comment applies below in a couple other tests, but won't repeat. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I see in the other tests (that you already had) that you may have those cases covered already. Maybe this just becomes minor: rather than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other tests check that the values stored can be recalled cleanly, so I'm only worried in this test whether the expected keys are put into the bag. I'll rename the tests as you suggest. |
||
g.Expect(bag).To(HaveKey("Halloween")) | ||
g.Expect(bag).To(HaveKey("Christmas")) | ||
g.Expect(bag).To(HaveLen(3)) | ||
} | ||
|
||
func TestPropertyBag_NewPropertyBag_WithMultipleSingleItemBags_ReturnsBagWithExpectedContent(t *testing.T) { | ||
g := NewWithT(t) | ||
first := NewPropertyBag() | ||
g.Expect(first.Add("Answer", 42)).To(Succeed()) | ||
|
||
second := NewPropertyBag() | ||
g.Expect(second.Add("Halloween", "31OCT")).To(Succeed()) | ||
g.Expect(second.Add("Christmas", "25DEC")).To(Succeed()) | ||
|
||
bag := NewPropertyBag(first, second) | ||
g.Expect(bag).To(HaveKey("Answer")) | ||
g.Expect(bag).To(HaveKey("Halloween")) | ||
g.Expect(bag).To(HaveKey("Christmas")) | ||
g.Expect(bag).To(HaveLen(3)) | ||
} | ||
|
||
func TestPropertyBag_NewPropertyBag_WithOverlappingBags_ReturnsBagWithExpectedContent(t *testing.T) { | ||
g := NewWithT(t) | ||
first := NewPropertyBag() | ||
g.Expect(first.Add("Answer", 42)).To(Succeed()) | ||
g.Expect(first.Add("Gift", "Skull")).To(Succeed()) | ||
|
||
second := NewPropertyBag() | ||
g.Expect(second.Add("Halloween", "31OCT")).To(Succeed()) | ||
g.Expect(second.Add("Christmas", "25DEC")).To(Succeed()) | ||
g.Expect(second.Add("Gift", "Gold")).To(Succeed()) | ||
|
||
bag := NewPropertyBag(first, second) | ||
g.Expect(bag).To(HaveKey("Answer")) | ||
g.Expect(bag).To(HaveKey("Halloween")) | ||
g.Expect(bag).To(HaveKey("Christmas")) | ||
g.Expect(bag).To(HaveKey("Gift")) | ||
g.Expect(bag).To(HaveLen(4)) | ||
|
||
var gift string | ||
err := bag.Pull("Gift", &gift) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(gift).To(Equal("Gold")) | ||
} | ||
|
||
/* | ||
* Roundtrip Tests | ||
*/ | ||
|
||
func TestPropertyBag_CorrectlyRoundTripsIntegers(t *testing.T) { | ||
g := NewWithT(t) | ||
var original int = 42 | ||
var actual int | ||
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(actual).To(Equal(original)) | ||
} | ||
|
||
|
@@ -33,11 +123,10 @@ func TestPropertyBag_CorrectlyRoundTrips64bitIntegers(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(actual).To(Equal(original)) | ||
} | ||
|
||
|
@@ -48,11 +137,10 @@ func TestPropertyBag_CorrectlyRoundTripsStrings(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(actual).To(Equal(original)) | ||
} | ||
|
||
|
@@ -63,11 +151,10 @@ func TestPropertyBag_CorrectlyRoundTripsBooleans(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(actual).To(Equal(original)) | ||
} | ||
|
||
|
@@ -78,11 +165,10 @@ func TestPropertyBag_CorrectlyRoundTripsFloats(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(actual).To(Equal(original)) | ||
} | ||
|
||
|
@@ -105,11 +191,10 @@ func TestPropertyBag_CorrectlyRoundTripsStructs(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).To(Succeed()) | ||
g.Expect(actual).To(Equal(original)) | ||
} | ||
|
||
|
@@ -120,11 +205,10 @@ func TestPropertyBag_WhenPropertyNotPresent(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("otherProp", &actual) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(found).To(BeFalse()) | ||
err = bag.Pull("otherProp", &actual) | ||
g.Expect(err).NotTo(Succeed()) | ||
} | ||
|
||
func TestPropertyBag_WhenPropertyWrongType(t *testing.T) { | ||
|
@@ -134,9 +218,8 @@ func TestPropertyBag_WhenPropertyWrongType(t *testing.T) { | |
|
||
bag := make(PropertyBag) | ||
err := bag.Add("prop", original) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(err).To(Succeed()) | ||
|
||
found, err := bag.Pull("prop", &actual) | ||
g.Expect(err).NotTo(BeNil()) | ||
g.Expect(found).To(BeTrue()) | ||
err = bag.Pull("prop", &actual) | ||
g.Expect(err).NotTo(Succeed()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: This isn't in the code you actually changed, but I'm curious why
string
is called out here but not other primitive types?Feels like you could probably get away without any special casing and just always use
json.Marshal
? I am assuming thatjson.Marshal(str)
just returnsstr
but I don't know for sure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall - might have been to avoid the overhead of calling
json.Marshal
when not required, or it may have been to handle round trip formatting issues. I do remember it was deliberate though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth leaving a comment there as to the reason? Especially if there was some problem if it wasn't handled specially?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a laundry list of minor code-gardening things to do post
codegen-alpha-0
, I'll add this to that.