Added Keys() function
diff --git a/properties.go b/properties.go index 3741842..a5aaa21 100644 --- a/properties.go +++ b/properties.go
@@ -324,6 +324,15 @@ return len(p.m) } +// Keys returns all keys. +func (p *Properties) Keys() []string { + keys := make([]string, len(p.m)) + for k, _ := range p.m { + keys = append(keys, k) + } + return keys +} + // Set sets the property key to the corresponding value. // If a value for key existed before then ok is true and prev // contains the previous value. If the value contains a
diff --git a/properties_test.go b/properties_test.go index e0e0f9b..ef5ce2c 100644 --- a/properties_test.go +++ b/properties_test.go
@@ -285,6 +285,20 @@ // ---------------------------------------------------------------------------- +type keysTest struct { + input string + keys []string +} + +var keysTests = []*keysTest{ + &keysTest{"", []string{}}, + &keysTest{"key = abc", []string{"key"}}, + &keysTest{"key = abc\nkey2=def", []string{"key", "key2"}}, + &keysTest{"key = abc\nkey=def", []string{"key"}}, +} + +// ---------------------------------------------------------------------------- + // TestBasic tests basic single key/value combinations with all possible // whitespace, delimiter and newline permutations. func (l *TestSuite) TestBasic(c *C) { @@ -456,6 +470,17 @@ c.Assert(func() { p.MustGetString("invalid") }, PanicMatches, "unknown property: invalid") } +func (l *TestSuite) TestKeys(c *C) { + for _, test := range keysTests { + p, err := parse(test.input) + c.Assert(err, IsNil) + c.Assert(p.Len(), Equals, len(test.keys)) + for _, key := range test.keys { + _, ok := p.Get(key) + c.Assert(ok, Equals, true) + } + } +} func (l *TestSuite) TestWrite(c *C) { for _, test := range writeTests { input, output, enc := test[0], test[1], test[2]