Skip to content

Commit

Permalink
[knf] Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Apr 29, 2024
1 parent ee80744 commit 2b1d76a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `[knf]` Added `Alias` method
- `[knf]` Added property name validation for all getters
- `[knf]` Code refactoring
- `[knf]` Added more tests

### 12.119.0

Expand Down
4 changes: 4 additions & 0 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,10 @@ func (c *Config) Is(name string, value any) bool {
return c.GetM(name) == t
case time.Duration:
return c.GetD(name, Second) == t
case time.Time:
return c.GetTS(name).Unix() == t.Unix()
case *time.Location:
return fmt.Sprint(c.GetTZ(name)) == fmt.Sprint(t)
}

return false
Expand Down
37 changes: 37 additions & 0 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,39 @@ func (s *KNFSuite) TestTimeDuration(c *check.C) {
c.Assert(GetTD("time-duration:test5"), check.Equals, time.Duration(0))
}

func (s *KNFSuite) TestTimestamp(c *check.C) {
err := Global(s.ConfigPath)

c.Assert(global, check.NotNil)
c.Assert(err, check.IsNil)

c.Assert(GetTS("timestamp:test1"), check.DeepEquals, time.Unix(0, 0))
c.Assert(GetTS("timestamp:test2"), check.DeepEquals, time.Unix(1709629048, 0))
c.Assert(GetTS("timestamp:test3"), check.DeepEquals, time.Time{})
}

func (s *KNFSuite) TestTimezone(c *check.C) {
err := Global(s.ConfigPath)

c.Assert(global, check.NotNil)
c.Assert(err, check.IsNil)

l, _ := time.LoadLocation("Europe/Zurich")

c.Assert(GetTZ("timezone:test1"), check.DeepEquals, l)
c.Assert(GetTZ("timezone:test2"), check.IsNil)
}

func (s *KNFSuite) TestList(c *check.C) {
err := Global(s.ConfigPath)

c.Assert(global, check.NotNil)
c.Assert(err, check.IsNil)

c.Assert(GetL("list:test1"), check.HasLen, 0)
c.Assert(GetL("list:test2"), check.HasLen, 2)
}

func (s *KNFSuite) TestIs(c *check.C) {
err := Global(s.ConfigPath)

Expand All @@ -501,6 +534,10 @@ func (s *KNFSuite) TestIs(c *check.C) {
c.Assert(Is("integer:test1", int64(1)), check.Equals, true)
c.Assert(Is("file-mode:test1", os.FileMode(0644)), check.Equals, true)
c.Assert(Is("duration:test2", time.Minute), check.Equals, true)
c.Assert(Is("timestamp:test2", time.Unix(1709629048, 0)), check.Equals, true)

l, _ := time.LoadLocation("Europe/Zurich")
c.Assert(Is("timezone:test1", l), check.Equals, true)

c.Assert(Is("integer:test1", nil), check.Equals, false)
}
Expand Down

0 comments on commit 2b1d76a

Please sign in to comment.