• Stars
    star
    224
  • Rank 176,813 (Top 4 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated over 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight program specifications for testing JavaScript

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.

More Repositories

1

empath-client

analyze text with empath
Python
309
star
2

iris-agent

A extensible conversational agent for data science tasks
TeX
123
star
3

Gajure

A framework for implementing genetic algorithms in Clojure.
Clojure
63
star
4

ProofFrontend

JavaScript
12
star
5

Proof-Search

Haskell
11
star
6

empath-outofdate

HTML
10
star
7

Clogger

A basic blog framework designed around Compojure
Clojure
8
star
8

mobile-for-compojure

Middleware for handling mobile devices in compojure applications
Clojure
7
star
9

codex

Collecting and aggregating information about Ruby ASTs
Ruby
5
star
10

Atom-Feed-for-Clojure-and-Compojure

A simple template for creating atom feeds with Clojure and Compojure
Clojure
4
star
11

meta

a community-aware domain specific language for Python
Python
4
star
12

code_extraction

Python
4
star
13

cs1120

An example Django app for UVa's CS1120
Python
3
star
14

augur-nlp-mining

Python
3
star
15

wordy

analyze text patterns in webpages
Clojure
3
star
16

ejhfast.github.com

Blog
HTML
3
star
17

CS376

Arduino interface code
JavaScript
3
star
18

5k

Ruby
2
star
19

UXMockups

Ruby
2
star
20

BarefootCS

Ruby
2
star
21

faml

Finite Automata for Ocaml
OCaml
2
star
22

OurApp

An application
JavaScript
2
star
23

HackLikeMe

Connecting developers and designers
Ruby
2
star
24

integratedCS

Objective-C
2
star
25

CustomerManagmentApp

TBD
Ruby
2
star
26

iphoneCS

iphone project for sherriff's class
Objective-C
2
star
27

acl2-backend

Backend proof checker for education project
Common Lisp
2
star
28

fa-ruby

Build Finite Automata in Ruby
Ruby
1
star
29

MotivateCSS

Design for motivation site
Ruby
1
star
30

oldblog

placeholder for old blog
Ruby
1
star
31

NLP-Challenge

Solution to Joseph Turian's NLP problem
Ruby
1
star
32

fiction-bias

HTML
1
star
33

fahs

Learning Haskell with Finite Automata
Haskell
1
star
34

node-proof-server

Nodejs wrapper for acl2 proof checker
Common Lisp
1
star
35

virtual_assistant

natural language assistant for OSX
Python
1
star
36

maria-web

web interface for deep learning peptide presentation
JavaScript
1
star
37

Clustering-in-Haskell

A simple clustering implementation
Haskell
1
star
38

Fortune

A toy Sinatra app that generates unix fortunes
Ruby
1
star
39

Jokes

Agent Based Joke Simulation
Java
1
star
40

Bite

Minimalist Web Framework for Clojure
Clojure
1
star
41

Markov

Markov text creation
Ruby
1
star
42

elrond-rust

Key creation and transaction signing for the Elrond network in pure Rust
Rust
1
star
43

Narcissist

Track a user's twitter-based narcissism quotient
Ruby
1
star