Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 8340dad7626b38e71fd1b8adf1f393eb1b9d2792 @jsl committed Sep 18, 2009
Showing with 130 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +30 −0 README.rdoc
  3. +21 −0 Rakefile
  4. +38 −0 lib/be_valid_json.rb
  5. +11 −0 spec/be_valid_json_spec.rb
  6. +10 −0 spec/spec_helper.rb
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Justin S. Leitgeb
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
+= be_valid_json
+
+Makes available method be_valid_json to check to see if a string is valid JSON or not
+without clouding specs with exception handling code.
+
+== Installation
+
+Just copy be_valid_json.rb under the 'lib' folder to your rspec matchers folder, and include it
+in your spec_helper. Some Rails programmers follow a convention where custom matchers are put
+in a folder under the project root called 'spec/support/matchers', and be_valid_json.rb could
+be put in that folder.
+
+In order to make the methods provided available in your specs, include the matcher library
+with something like the following line (which pulls in all code under 'support/matchers'):
+
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
+
+== Basic Usage
+
+ it "should detect valid json" do
+ "foo".should_not be_valid_json
+ end
+
+ it "should detect invalid json" do
+ {:a => :b}.to_json.should be_valid_json
+ end
+
+== Authors
+
+Justin Leitgeb
@@ -0,0 +1,21 @@
+require 'rake'
+require 'rake/rdoctask'
+require 'spec/rake/spectask'
+
+desc 'Test the plugin.'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.libs << 'lib'
+ t.verbose = true
+end
+
+desc "Run all the tests"
+task :default => :spec
+
+desc 'Generate documentation for the be_valid_json plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Enum For'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,38 @@
+module CustomJsonMatchers
+
+ class BeValidJson
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def matches?(target)
+ @target = target
+
+ res = true
+
+ begin
+ JSON.parse(target)
+ rescue JSON::ParserError => e
+ res = false
+ end
+
+ res
+ end
+
+ def failure_message_for_should
+ "expected #{@target.inspect} to be valid JSON"
+ end
+
+ def failure_message_for_should_not
+ "expected #{@target.inspect} not to be valid json"
+ end
+ end
+
+ def be_valid_json
+ BeValidJson.new(@expected)
+ end
+end
+
+Spec::Runner.configure do |config|
+ config.include(CustomJsonMatchers)
+end
@@ -0,0 +1,11 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe CustomJsonMatchers::BeValidJson do
+ it "should detect valid json" do
+ "foo".should_not be_valid_json
+ end
+
+ it "should detect invalid json" do
+ {:a => :b}.to_json.should be_valid_json
+ end
+end
@@ -0,0 +1,10 @@
+require 'rubygems'
+require 'mocha'
+require 'spec'
+require 'json'
+
+require File.join(File.dirname(__FILE__), %w[.. lib be_valid_json])
+
+Spec::Runner.configure do |config|
+ config.mock_with(:mocha)
+end

0 comments on commit 8340dad

Please sign in to comment.