The Artima Developer Community
Sponsored Link

Agile Buzz Forum
JCON: Ruby Gem for JSON type conformance

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Oliver Steele

Posts: 112
Nickname: ows
Registered: Aug, 2003

Oliver Steele is Chief Software Architect at Laszlo Systems, Inc.
JCON: Ruby Gem for JSON type conformance Posted: Apr 17, 2008 1:19 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Oliver Steele.
Original Post: JCON: Ruby Gem for JSON type conformance
Feed Title: Oliver Steele on Software
Feed URL: http://feeds.feedburner.com/osteele
Feed Description: Languages of the real and artificial.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Oliver Steele
Latest Posts From Oliver Steele on Software

Advertisement

JCON (the JavaScript Conformance gem) tests JSON values against ECMAScript 4.0-style type definitions
(PDF) such as string?, (int, boolean), or [string, (int, boolean), {x:double, y:double}?].

Usage

type = JCON::parse "[string, int]"
type.contains?([‘a’, 1])     # => true
type.contains?([‘a’, ‘b’])   # => false
type.contains?([‘a’, 1, 2])  # => true

JCON also defines an RSpec matcher, conforms_to_js:

[1, ‘xyzzy’].should conform_to_js(‘[int, string]’)
[1, 2, ‘xyzzy’].should_not conform_to_js(‘[int, string]’)  # 2 isn’t a string
{:x => 1}.should conform_to_js(‘{x: int}’)

Use JCON together with the JavaScript Fu Rails plugin to test the argument values to functions in generated JavaScript:

# this will succeed if e.g. response contains a script tag that includes
#   fn("id", {x:1, y:2}, true)
response.should call_js(‘fn’) do |args|
  args[0].should conform_to_js(‘string’)
  args[1].should conform_to_js(‘{x:int, y:int}’)
  args[2].should conform_to_js(‘boolean’)
  # or:
  args.should conform_to_js(‘[string, {x:int, y:int}, boolean]’)
end

Whence

Github for the sources.

Rubyforge for docs.

gem install jcon to install.

License and version

MIT License, of course.

JCON is at version 0.1 because it’s just a few days old and I had to guess about the ECMAScript 4.0 type syntax from the examples in the overview. I can’t imagine that I got everything right.

Read: JCON: Ruby Gem for JSON type conformance

Topic: Introducing Railscheck - A Q/A verification tool for Ruby On Rails Previous Topic   Next Topic Topic: JetGroovy 1.5 released

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use