Coder Social home page Coder Social logo

typedjs's Introduction

TypedJS

Basic, automatic JavaScript testing. This is just a start. TypedJS will soon provide more complex forms of program specification, and analysis. The goal: safe, correct JavaScript.

(Warning: In need of some refactoring).

Usage

First, annotate your javascript functions with Haskell-like type signatures:

//+ str_first :: String -> String -> String

function str_first(c1,c2){
  return c1;
}

//+ my_prop :: {name:String, valid:Boolean} -> Boolean

function my_prop(obj){
  if(obj.valid === true){
    return "true"; // Error, we are 
  }                // returning a string here
  else{
    return obj.valid;
  }
};

// We can do objects, too:

MyObj = {
  //+ MyObj.test_fun :: Number -> Number -> Number
  test_fun:function(num1, num2){
    return num1 + num2;
  }
}

Run in the Browser

Load your JavaScript file in a browser window and run:

TypedJS.run_tests()

If you want to instrument your annotated functions to dynamically detect type violations, run:

TypedJS.run_tests(true)

Node.js support

Add tests manually:

    var TypedJS = require('../typed');
    typedjs_parser = require('../typedjs_parser');


    // Example function to test...
    function concat(a, b) {
         return a + b;
    }

    // You can manually add tests to TypedJS
    // The first parameter is the type signature
    // The second parameter is the actual function we'll be testing
    var test = TypedJS.addTest('concat :: String -> String -> String', concat);

    // Call 'go' to execute the automated tests
    // go requires one parameter and it's an Array of the tests to run.
    TypedJS.go([test]);

Or load a file:

    var TypedJS = require('../typed');
    typedjs_parser = require('../typedjs_parser');

    // A vm is necessary so you can bind all the global functions to 'window'
    var fs = require('fs');
    var vm = require('vm');

    // Extract the data from the file
    var fileData = fs.readFileSync('examples/test.js', 'utf-8');

    // Pull in all the global functions into window
    window = {};
    vm.runInNewContext(fileData, window);


    // Run TypedJS on the fileData String.
    // TypedJS will parse all your type signatures
    // read the functions from 'window' and execute
    // the automated tests.
    TypedJS.run_tests_on_string(fileData);

Types

Currently available primitives:

Number
String
Boolean

And ORs of primitives:

Number | String   // Number or String
Boolean | Number  // Boolean or Number
...

And tuples (T1, T2, ... TN). For instance:

(Number, String, Boolean, Boolean)  // An example instance => [4,"hello",true,true]
(Boolean, Boolean)                  // An example instance => [true, false]

And arrays:

[Number]            // Array of Numbers. Example instances => [3,4,5] or [45,62,34,78,23]
[Number | String]   // Array of Numbers or Strings. Example instance => [3,"s",5,6,"h"]

And objects, which can be nested and mixed with other types. For instance:

{key1: String, key2:[Number], key3:{subkey1:String, subkey2: Number}}

MIT License

Copyright (C) 2012 Ethan Fast & Taazr Corporation

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.

typedjs's People

Contributors

ahamid avatar dazwilkin avatar ejhfast avatar goatslacker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

typedjs's Issues

Char isn't a JavaScript type

It's useful but it really isn't a type in JS. There's just String, IMHO this feature should be opt-in/opt-out.

Also, would be nice to add 'RegExp', 'Date', and 'isNaN'

Pull request coming at some point when GH-6 is fixed.

Contracts rather than Types

Rather than trying to stick to a type signature, why not expand the idea to do something similar to what Mozilla is doing with contracts.coffee and leverage the type signatures plus other options as a form of contracts? I'm sure this is similar to what is already done; the point is more to not limit only to mimicking a type system since you don't have to stop there.

Issue in test.js - add_all()

The main web site indicates (as does the function name) that add_all() will return the sum of all numbers in an array. It only returns the value of the last item in the array.

The code expresses "count" as the value being modified -- but it's not a "count" per se, its a value of the last item in the array.

License choice

This looks interesting! But I think you might want to think more about your license choice; are you aware that the CC noncommercial license means that no-one at any company can use this code, even if they give back all of the changes they make to it? CC licenses are not usually used or recommended for source code.

(I'd recommend a BSD or AGPL license instead.)

Thanks.

Confusing syntax: addTest/go

I find this syntax (from the readme) confusing:

var test = TypedJS.addTest('concat :: String -> String -> String', concat);
TypedJS.go([test]);

The "addTest" function name suggests (to me) that TypedJS is maintaining an internal collection of the tests for which I'd expect to be able to call 'go' to run the current tests:

TypedJS.addTest('concat :: String -> String -> String', concat);
TypedJS.go();

Perhaps it would be more appropriate to rename "addTest" to "createTest" and "go" to "runTests"?

var tests = $.map([{
    name: concat,
    type: "concat :: String, String -> Boolean"},
{
    name: add,
    type: "add :: Number, Number -> Number"}], function(func) {
    return TypedJS.createTest(func.type, func.name);
});
TypedJS.runTests(tests);โ€‹

Issue in test.js - function name

The last function in the test is documented as maybe_char, but the last function is actually char_first -- which is the same function name as the previous.

use java doc syntax

Any chance that we could use java docs - much more widely used IMHO than haskell-like
also means anyone who marked up fro google closure compiler can use this out the box

jQuery required?

This is a very useful project, and I hope my thought here is taken in the context of my appreciation for what you've begun.

Do you intend to drop the jQuery dependency at some future point so that it can be used as part of a browserless development cycle? For example, I would like to be able to use TypedJS as a quick check inside a text editor/IDE to confirm that things check out, before heading to the browser. It would also be tremendously helpful to be able to do this for NodeJS code.

object names that include numbers generate parse errors

For example:

test1a = function (a) { return true; }

tested with

test = TypedJS.addTest("test1a :: Number -> Boolean", test1a);
TypedJS.go([test]);

fails with:

Uncaught Error: Parse error on line 1:
//+test1a :: Number -> Numb
-------^
Expecting '::'

Objects: Enforce types on properties mentioned

It's a little late for me so I don't have a good code sample. I'll get one tomorrow.

In the meantime this seems to be failing:

//+ foo: { hello: String } -> String
function foo(o) {
    return o.hello;
}

foo({ hello: 'foo', world: 'bar' }); // will fail because world is not typed.

You could make an argument that it should throw a TypeError but I'd like for there to be an option.

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.