-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de0f04f
commit f20673d
Showing
14 changed files
with
137 additions
and
61 deletions.
There are no files selected for viewing
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
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
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
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,54 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package astmodel | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
// PropertySet wraps a set of property definitions, indexed by name, along with some convenience methods | ||
type PropertySet map[PropertyName]*PropertyDefinition | ||
|
||
// NewPropertySet creates a new set of properties | ||
func NewPropertySet(properties ...*PropertyDefinition) PropertySet { | ||
result := make(PropertySet) | ||
for _, prop := range properties { | ||
result[prop.PropertyName()] = prop | ||
} | ||
|
||
return result | ||
} | ||
|
||
// AsSlice returns all the properties in a slice, sorted alphabetically by name | ||
func (p PropertySet) AsSlice() []*PropertyDefinition { | ||
var result []*PropertyDefinition | ||
for _, prop := range p { | ||
result = append(result, prop) | ||
} | ||
|
||
// Sort it so that it's always consistent | ||
sort.Slice(result, func(left int, right int) bool { | ||
return result[left].propertyName < result[right].propertyName | ||
}) | ||
|
||
return result | ||
} | ||
|
||
// Add updates the set by including the provided property | ||
// Any existing definition by that name will be overwritten if present | ||
func (p PropertySet) Add(property *PropertyDefinition) { | ||
p[property.propertyName] = property | ||
} | ||
|
||
// Copy returns a new property set with the same properties as this one | ||
func (p PropertySet) Copy() PropertySet { | ||
result := make(PropertySet, len(p)) | ||
for name, prop := range p { | ||
result[name] = prop | ||
} | ||
|
||
return result | ||
} |
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,50 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package astmodel | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestPropertySet_NewPropertySet_ReturnsEmptySet(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
set := NewPropertySet() | ||
g.Expect(set).To(HaveLen(0)) | ||
} | ||
|
||
func TestPropertySet_NewPropertySetWithProperties_ReturnsSetContainingProperties(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
set := NewPropertySet(fullName, familyName, knownAs, gender) | ||
g.Expect(set).To(ContainElements(fullName, familyName, knownAs, gender)) | ||
g.Expect(set).To(HaveLen(4)) | ||
} | ||
|
||
func TestPropertySet_AsSlice_ReturnsSliceContainingProperties(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
slice := NewPropertySet(fullName, familyName, knownAs, gender).AsSlice() | ||
g.Expect(slice).To(ContainElements(fullName, familyName, knownAs, gender)) | ||
g.Expect(slice).To(HaveLen(4)) | ||
} | ||
|
||
func TestPropertySet_Add_ReturnsExpectedSet(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
set := NewPropertySet() | ||
set.Add(fullName) | ||
set.Add(familyName) | ||
set.Add(knownAs) | ||
set.Add(gender) | ||
g.Expect(set).To(ContainElements(fullName, familyName, knownAs, gender)) | ||
g.Expect(set).To(HaveLen(4)) | ||
} | ||
|
||
func TestPropertySet_Copy_ReturnsExpectedSet(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
clone := NewPropertySet(fullName, familyName, knownAs, gender).Copy() | ||
g.Expect(clone).To(ContainElements(fullName, familyName, knownAs, gender)) | ||
g.Expect(clone).To(HaveLen(4)) | ||
} |
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
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
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