Skip to content

Commit

Permalink
changing dependent contract syntax #8
Browse files Browse the repository at this point in the history
  • Loading branch information
disnet committed Mar 13, 2012
1 parent 67519a2 commit 4e127ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,16 @@ exports.FunctionContract = class FunctionContract extends Base
compileNode: (o) ->
params = @dom.compile o
range = @rng.compile o
if @rng instanceof WrapContract # dependent function contract case
depargs = ("$#{n}" for n in [1..@dom.objects.length]).join(", ")
range = "function(#{depargs}) { return #{range}; }"
if @rng instanceof WrapContract and @rng.contract instanceof Code # dependent function contract case
if @rng?.contract?.params[1]?
paramsName = @rng.contract.params[1].compile o
delete @rng.contract.params[1]
@rng.contract.params.length = 1
else
paramsName = ""
# recompile since we modified @rng
range = @rng.compile o
range = "function(#{paramsName}) { return #{range}; }"
if @tags is 'callOnly'
@options.properties.push(makeObjectProp "callOnly", "true")
else if @tags is 'newOnly'
Expand Down
8 changes: 4 additions & 4 deletions test/contracts.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,22 @@ test "function, dependent", ->

# inc :: (Num) -> !gt

inc :: (Num) -> !(result) -> result > $1
inc :: (Num) -> !(result, params) -> result > params[0]
inc = (x) -> x + 1

eq (inc 42), 43, "abides by contract"

bad_inc :: (Num) -> !(result) -> result > $1
bad_inc :: (Num) -> !(result, params) -> result > params[0]
bad_inc = (x) -> x - 1

throws (-> bad_inc 42), "violates dependent contract"

bad_inc :: (Str, Num) -> !(result) -> result > $2
bad_inc :: (Str, Num) -> !(result, params) -> result > params[1]
bad_inc = (x, y) -> y - 1

throws (-> bad_inc "foo", 42), "violates multi arg dependent contract"

neg :: (Bool or Num) -> !(r) -> typeof r is typeof $1
neg :: (Bool or Num) -> !(r, p) -> typeof r is typeof p[0]
neg = (x) ->
if typeof x is 'number'
0 - x
Expand Down

0 comments on commit 4e127ca

Please sign in to comment.