https://github.com/crystal-china/hashr
Hashr is a tiny library makes test on JSON response easier, and can also be used as a models object.
https://github.com/crystal-china/hashr
crystal rspec spec testing
Last synced: 3 months ago
JSON representation
Hashr is a tiny library makes test on JSON response easier, and can also be used as a models object.
- Host: GitHub
- URL: https://github.com/crystal-china/hashr
- Owner: crystal-china
- License: mit
- Created: 2022-08-16T08:18:23.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-02T11:45:47.000Z (over 2 years ago)
- Last Synced: 2024-10-18T19:32:28.104Z (about 1 year ago)
- Topics: crystal, rspec, spec, testing
- Language: Crystal
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - hashr - A tiny class makes test on JSON response easier (Testing)
README
# hashr
Hashr is a tiny class makes test on JSON response easier.
The name of `hashr` come from the awesome ruby gem [hashr](https://github.com/svenfuchs/hashr), though, AFAIK, use original code is not possible because Crystal very different with Ruby in some aspect.
This shard should only be used in spec.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
hashr:
github: crystal-china/hashr
```
2. Run `shards install`
## Usage
Following is a example:
```crystal
require "./src/hashr"
require "spec"
describe "daily reports" do
it "query daily report" do
json_string = "{\"data\":{\"reportQuery\":{\"target\":{\"targetTotalCount\":47,\"processedTotalCount\":44,\"qualifiedTotalCount\":40}}}}"
response_hash = Hash(String, JSON::Any).from_json(json_string)
# => Get a hash like this:
# {"data" =>
# {"reportQuery" =>
# {"target" =>
# {"targetTotalCount" => 47,
# "processedTotalCount" => 44,
# "qualifiedTotalCount" => 40}}}} (Hash(String, JSON::Any))
# Instead, verify on the entire response result, we can verify on specified field only.
parsed_response = Hashr.new(response_hash)
# Use nice dot method call nonation.
target = parsed_response.data.reportQuery.target
target.processedTotalCount.should eq 44
target.qualifiedTotalCount.should eq 40
end
end
```
You can use Hashr as model object, like this:
```crystal
it "use hashr as model object" do
contacts = [
{
id: 1,
first: "billy",
last: "zheng",
phone: "18612385678",
email: "billy@gmail.com",
},
{
id: 2,
first: "xuan",
last: "zheng",
phone: "18512345678",
email: "retired@qq.com",
},
]
models = contacts.map { |e| Hashr.new(e) }
models.each do |contact|
puts "#{contact.first}.#{contact.last}: phone: #{contact.phone}, email: #{contact.email}"
end
billy = models.first
billy.phone = "13012345678"
p billy.name # => 13012345678
```
## Limit
For verify a value is nil, you have to use `eq`, `be_nil` not work because Crystal don't allow us redefine #nil? method on any object.
```crystal
h = {"nilValue" => nil}.to_json
value = Hashr.new({"foo" => JSON.parse(h)})
value.foo.nilValue.should eq nil # => true
value.foo.nilValue.should be_nil # => false
```
## Development
TODO: Write development instructions here
## Contributing
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [Billy.Zheng](https://github.com/zw963) - creator and maintainer