Skip to content

Commit

Permalink
Improved test coverage for symbol/string keys.
Browse files Browse the repository at this point in the history
Improved README describing symbols/strings are used to access the values but internally everything is stored as a symbol keyed hash.
  • Loading branch information
tnarik committed Mar 27, 2014
1 parent fa15de0 commit 820925c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ You can load a valid Java properties file from the file system using a path:
```ruby
properties = ProperProperties.load("path/to/my.properties")
properties[:foo] # => "bar"
properties['foo'] # => "bar"
```

If have already the content of the properties file at hand than parse the content as:

```ruby
properties = ProperProperties.load("foo=bar")
properties[:foo] # => "bar"
properties['foo'] # => "bar"
```

## Writing files

You can write any Hash-like structure as a properties file:
You can write any Hash-like structure (with symbol or string keys) as a properties file:

```ruby
hash = {:foo => "bar"}
hash = {:foo => "bar", "foobar" => "barfoo"}
ProperProperties.write(hash, "path/to/my.properties")
```

Expand Down Expand Up @@ -95,7 +97,7 @@ In the opposite direction line breaks will be correctly escaped but the generato
## Author

- Tnarik Innael (@tnarik)
- Jonas Thiel (@jonasthiel) : [original project](https://github.com/jnbt/java-properties) from commit [1f2c4b008d69d0eae1084b1adfdea814e2b29899]
- Jonas Thiel (@jonasthiel) : [original project](https://github.com/jnbt/java-properties) upto [1f2c4b008d69d0eae1084b1adfdea814e2b29899](https://github.com/tnarik/proper_properties/commit/1f2c4b008d69d0eae1084b1adfdea814e2b29899)

## References

Expand Down
2 changes: 1 addition & 1 deletion lib/proper_properties/properties.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ProperProperties
# Simple representation of a properties file content
# by a simple ruby hash object
# by an extension of a Hash object a la ActiveSupport::HashWithIndifferentAccess, with underlying symbol keys
class Properties < Hash

# Assigns a new value to the hash:
Expand Down
50 changes: 46 additions & 4 deletions spec/proper_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,71 @@
describe ProperProperties do
subject{ ProperProperties }

it "parses from string" do
it "parses from string to symbol hash" do
subject.parse("item1=item1").must_equal({:item1 => "item1"})
end

it "generates from hash" do
subject.generate({:item1 => "item1"}).must_equal("item1=item1")
subject.generate({"item2" => "item2"}).must_equal("item2=item2")
end

it "loads from file" do
it "loads from file and create a hash" do
with_temp_file do |file|
file << "item1=item1"
file.flush

subject.load(file.path).must_equal({:item1 => "item1"})

end
end

it "loads from file and allows symbol/string access" do
with_temp_file do |file|
file << "item1=item1"
file.flush

properties = subject.load(file.path)
properties[:item1].must_equal("item1")
properties['item1'].must_equal("item1")
end
end

it "loads from file and allows manipulation" do
with_temp_file do |file|
file << "item1=item1\n"
file << "item2=item2\n"
file << "item3=item3\n"
file << "item4=item4"
file.flush

properties = subject.load(file.path)

properties.values_at(:item1, "item2", 'item3').must_equal(["item1", "item2", "item3"])


properties[:item2].must_equal("item2")
properties.delete(:item2).must_equal("item2")
proc{ properties[:item2] }.must_raise(KeyError)

properties['item3'].must_equal("item3")
properties.delete('item3').must_equal("item3")
proc{ properties['item3'] }.must_raise(KeyError)

properties.key?(:item4).must_equal(true)
properties.key?('item4').must_equal(true)

properties.key?(:item5).must_equal(false)
properties.key?('item5').must_equal(false)
end
end

it "writes to file" do
with_temp_file do |file|
subject.write({:item1 => "item1"}, file.path)
subject.write({:item1 => "item1", "item2" => "item2"}, file.path)

file.rewind
file.read.must_equal "item1=item1"
file.read.must_equal "item1=item1\nitem2=item2"
end
end

Expand Down

0 comments on commit 820925c

Please sign in to comment.