forked from woodpecker-ci/woodpecker
-
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.
- Loading branch information
1 parent
dbfaedd
commit 67fbc8f
Showing
2 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package types | ||
|
||
import "strconv" | ||
|
||
// BoolTrue is a custom Yaml boolean type that defaults to true. | ||
type BoolTrue struct { | ||
value bool | ||
} | ||
|
||
// UnmarshalYAML implements custom Yaml unmarshaling. | ||
func (b *BoolTrue) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var s string | ||
err := unmarshal(&s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b.value, err = strconv.ParseBool(s) | ||
if err != nil { | ||
b.value = true | ||
} | ||
return nil | ||
} | ||
|
||
// Bool returns the bool value. | ||
func (b BoolTrue) Bool() bool { | ||
return b.value | ||
} |
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 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/franela/goblin" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestBoolTrue(t *testing.T) { | ||
g := goblin.Goblin(t) | ||
|
||
g.Describe("Yaml bool type", func() { | ||
g.Describe("given a yaml file", func() { | ||
|
||
g.It("should unmarshal true", func() { | ||
in := []byte("true") | ||
out := BoolTrue{} | ||
err := yaml.Unmarshal(in, &out) | ||
if err != nil { | ||
g.Fail(err) | ||
} | ||
g.Assert(out.Bool()).Equal(true) | ||
}) | ||
|
||
g.It("should unmarshal false", func() { | ||
in := []byte("false") | ||
out := BoolTrue{} | ||
err := yaml.Unmarshal(in, &out) | ||
if err != nil { | ||
g.Fail(err) | ||
} | ||
g.Assert(out.Bool()).Equal(false) | ||
}) | ||
|
||
g.It("should unmarshal true when empty", func() { | ||
in := []byte("") | ||
out := BoolTrue{} | ||
err := yaml.Unmarshal(in, &out) | ||
if err != nil { | ||
g.Fail(err) | ||
} | ||
g.Assert(out.Bool()).Equal(false) | ||
}) | ||
|
||
g.It("should throw error when invalid", func() { | ||
in := []byte("{ }") // string value should fail parse | ||
out := BoolTrue{} | ||
err := yaml.Unmarshal(in, &out) | ||
g.Assert(err != nil).IsTrue("expects error") | ||
}) | ||
}) | ||
}) | ||
} |