Skip to content

Commit

Permalink
Merge pull request googlecodelabs#625 from cassierecher/mutatetype
Browse files Browse the repository at this point in the history
Refactor MutateType.
  • Loading branch information
cassierecher authored Aug 19, 2021
2 parents ad98bab + dab09d5 commit 1709245
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
7 changes: 7 additions & 0 deletions claat/nodes/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ func (hn *HeaderNode) Empty() bool {
func IsHeader(t NodeType) bool {
return t&(NodeHeader|NodeHeaderCheck|NodeHeaderFAQ) != 0
}

// MutateType sets the header's node type if the given type is a header type.
func (hn *HeaderNode) MutateType(t NodeType) {
if IsHeader(t) {
hn.typ = t
}
}
34 changes: 34 additions & 0 deletions claat/nodes/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,37 @@ func TestHeaderNodeEmpty(t *testing.T) {
})
}
}

func TestHeaderMutateType(t *testing.T) {
tests := []struct {
name string
inType NodeType
out NodeType
}{
{
name: "Header",
inType: NodeHeader,
out: NodeHeader,
},
{
name: "AlternateHeaderType",
inType: NodeHeaderFAQ,
out: NodeHeaderFAQ,
},
{
name: "NotAHeader",
inType: NodeButton,
out: NodeHeader,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
n := NewHeaderNode(1) // 1 chosen arbitrarily.
n.MutateType(tc.inType)
if n.typ != tc.out {
t.Errorf("HeaderNode.typ after MutateType = %v, want %v", n.typ, tc.out)
return
}
})
}
}
12 changes: 12 additions & 0 deletions claat/nodes/itemslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ func (il *ItemsListNode) NewItem(nodes ...Node) *ListNode {
il.Items = append(il.Items, n)
return n
}

// IsItemsList returns true if t is one of ItemsListNode types.
func IsItemsList(t NodeType) bool {
return t&(NodeItemsList|NodeItemsCheck|NodeItemsFAQ) != 0
}

// MutateType sets the items list's node type if the given type is an items list type.
func (il *ItemsListNode) MutateType(t NodeType) {
if IsItemsList(t) {
il.typ = t
}
}
34 changes: 34 additions & 0 deletions claat/nodes/itemslist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,37 @@ func TestItemsListNewItem(t *testing.T) {
t.Errorf("ItemsListNode after NewItem got diff ((-want +got): %s", diff)
}
}

func TestItemsListMutateType(t *testing.T) {
tests := []struct {
name string
inType NodeType
out NodeType
}{
{
name: "ItemsList",
inType: NodeItemsList,
out: NodeItemsList,
},
{
name: "AlternateItemsListType",
inType: NodeItemsCheck,
out: NodeItemsCheck,
},
{
name: "NotAItemsList",
inType: NodeButton,
out: NodeItemsList,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
n := NewItemsListNode("foobar", 0) // Args chosen arbitrarily.
n.MutateType(tc.inType)
if n.typ != tc.out {
t.Errorf("ItemsListNode.typ after MutateType = %v, want %v", n.typ, tc.out)
return
}
})
}
}
11 changes: 2 additions & 9 deletions claat/nodes/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ type Node interface {
MutateEnv(env []string)
}

// IsItemsList returns true if t is one of ItemsListNode types.
func IsItemsList(t NodeType) bool {
return t&(NodeItemsList|NodeItemsCheck|NodeItemsFAQ) != 0
}

// IsInline returns true if t is an inline node type.
func IsInline(t NodeType) bool {
return t&(NodeText|NodeURL|NodeImage|NodeButton) != 0
Expand All @@ -95,11 +90,9 @@ func (b *node) Type() NodeType {
return b.typ
}

// TODO test
// Default implementation is a no op.
func (b *node) MutateType(t NodeType) {
if IsItemsList(b.typ) && IsItemsList(t) || IsHeader(b.typ) && IsHeader(t) {
b.typ = t
}
return
}

func (b *node) Block() interface{} {
Expand Down

0 comments on commit 1709245

Please sign in to comment.