assert_json 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Gemfile +2 -0
  2. data/MIT-LICENSE +19 -0
  3. data/README.md +70 -0
  4. data/Rakefile +13 -0
  5. data/lib/assert_json.rb +60 -0
  6. metadata +81 -0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009-2011 XING AG (http://www.xing.com/)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ AssertJson
2
+ ==========
3
+
4
+ A gem to test JSON strings.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ cd path/to/your/rails-project
10
+ ./script/plugin install git://github.com/xing/assert_json.git
11
+
12
+
13
+ Usage
14
+ -----
15
+
16
+ ``` ruby
17
+ class MyActionTest < ActionController::TestCase
18
+ include AssertJson
19
+
20
+ def test_my_action
21
+ get :my_action, :format => 'json'
22
+ # => @response.body= '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}'
23
+
24
+ assert_json(@response.body) do |json|
25
+ json.element 'key' do
26
+ json.element 'inner_key1', 'value1'
27
+ json.element 'inner_key2', 'value2'
28
+ end
29
+ json.not_element 'key_not_included'
30
+ end
31
+ end
32
+
33
+ end
34
+ ```
35
+
36
+ Authors
37
+ -------
38
+
39
+ [Thorsten Böttger](http://github.com/alto),
40
+ [Andree Wille](http://github.com/dreewill),
41
+ [Ralph von der Heyden](http://github.com/ralph)
42
+
43
+ Please find out more about our work in our
44
+ [Xing Dev Blog](http://devblog.xing.com/).
45
+
46
+
47
+ License
48
+ -------
49
+
50
+ The MIT License
51
+
52
+ Copyright (c) 2009-2011 [XING AG](http://www.xing.com/)
53
+
54
+ Permission is hereby granted, free of charge, to any person obtaining a copy
55
+ of this software and associated documentation files (the "Software"), to deal
56
+ in the Software without restriction, including without limitation the rights
57
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
58
+ copies of the Software, and to permit persons to whom the Software is
59
+ furnished to do so, subject to the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be included in
62
+ all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
65
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
66
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
67
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
68
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
69
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
70
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the assert_json plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
@@ -0,0 +1,60 @@
1
+ module AssertJson
2
+
3
+ def assert_json(json_string, &block)
4
+ if block_given?
5
+ yield AssertJson::Json.new(json_string)
6
+ end
7
+ end
8
+
9
+ class Json
10
+
11
+ def initialize(json_string)
12
+ @decoded_json = ActiveSupport::JSON.decode(json_string)
13
+ end
14
+
15
+ def element(*args, &block)
16
+ arg = args.shift
17
+ token = @decoded_json.is_a?(Array) ? @decoded_json.shift : @decoded_json
18
+ case token
19
+ when Hash
20
+ raise_error("element #{arg} not found") unless token.keys.include?(arg)
21
+ if second_arg = args.shift
22
+ raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg != token[arg]
23
+ end
24
+ when String, Array
25
+ raise_error("element #{arg} not found") if token != arg
26
+ when NilClass
27
+ raise_error("no element left")
28
+ else
29
+ flunk
30
+ end
31
+
32
+ if block_given?
33
+ begin
34
+ in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token
35
+ yield
36
+ ensure
37
+ @decoded_json = in_scope
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ def not_element(*args, &block)
44
+ arg = args.shift
45
+ token = @decoded_json
46
+ raise_error("element #{arg} found, but not expected") if token.keys.include?(arg)
47
+ end
48
+
49
+ private
50
+
51
+ def raise_error(message)
52
+ if Object.const_defined?(:MiniTest)
53
+ raise MiniTest::Assertion.new(message)
54
+ else
55
+ raise Test::Unit::AssertionFailedError.new(message)
56
+ end
57
+ end
58
+
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assert_json
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors: []
8
+
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-05 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activesupport
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: A gem to test JSON strings.
39
+ email:
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - lib/assert_json.rb
48
+ - MIT-LICENSE
49
+ - Rakefile
50
+ - Gemfile
51
+ - README.md
52
+ has_rdoc: true
53
+ homepage:
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.6.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A gem to test JSON strings.
80
+ test_files: []
81
+