Coder Social home page Coder Social logo

jcon's Introduction

JCon – JavaScript Type Conformance Checking

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

JCON also defines an RSpec matcher, conforms_to_js.

Use JCON together with the JavaScript Fu Rails plugin to test the argument values in generated JavaScript function calls.

Install

gem install rcon

Usage

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

type = JCON::parse "type S = (string, int); {a: [S], b: int}"
type.contains?({:a => [1, 'b'], :b => 2}) # => true

RSpec Matcher

[1, 'xyzzy'].should conform_to_js('[int, string]')
[1, 2, 'xyzzy'].should_not conform_to_js('[int, string]')
{:x => 1}.should conform_to_js('{x: int}')

Use this with the JavaScript Fu Rails plugin to test JSON arguments:

'<script>fn("id", {x:1, y:2}, true)</script>'.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

License

Copyright 2008 by Oliver Steele. All rights reserved. Released under the MIT License.

jcon's People

Contributors

osteele avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.