added support for Lists (like Arrays, but mutable). It has the same properties as the mutable string type, formerly known as Word Type

This commit is contained in:
DadaMonad 2015-01-16 21:20:47 +00:00
parent e54402e842
commit bab4bcc94b
10 changed files with 431 additions and 110 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -43,6 +43,8 @@ module.exports = (HB)->
for name, o of val
if o instanceof types.Object
json[name] = o.toJson(transform_to_value)
else if o instanceof types.Array
json[name] = o.toJson(transform_to_value)
else if transform_to_value and o instanceof types.Operation
json[name] = o.val()
else

View File

@ -104,7 +104,26 @@ module.exports = (HB)->
cleanup: ()->
super()
val: ()->
toJson: (transform_to_value = false)->
val = @val()
for i, o in val
if o instanceof types.Object
o.toJson(transform_to_value)
else if o instanceof types.Array
o.toJson(transform_to_value)
else if transform_to_value and o instanceof types.Operation
o.val()
else
o
val: (pos)->
if pos?
o = @getOperationByPosition(pos+1)
if not (o instanceof types.Delimiter)
o.val()
else
throw new Error "this position does not exist"
else
o = @beginning.next_cl
result = []
while o isnt @end
@ -115,16 +134,27 @@ module.exports = (HB)->
push: (content)->
@insertAfter @end.prev_cl, content
insertAfter: (left, content)->
insertAfter: (left, content, options)->
createContent = (content, options)->
if content? and content.constructor?
type = types[content.constructor.name]
if type? and type.create?
type.create content, options
else
throw new Error "The #{content.constructor.name}-type is not (yet) supported in Yatta."
else
content
right = left.next_cl
while right.isDeleted()
right = right.next_cl # find the first character to the right, that is not deleted. In the case that position is 0, its the Delimiter.
left = right.prev_cl
if content.type?
if content instanceof types.Operation
(new types.TextInsert content, undefined, left, right).execute()
else
for c in content
tmp = (new types.TextInsert c, undefined, left, right).execute()
tmp = (new types.TextInsert createContent(c, options), undefined, left, right).execute()
left = tmp
@
@ -133,11 +163,11 @@ module.exports = (HB)->
#
# @return {Array Type} This String object.
#
insert: (position, content)->
insert: (position, content, options)->
ith = @getOperationByPosition position
# the (i-1)th character. e.g. "abc" the 1th character is "a"
# the 0th character is the left Delimiter
@insertAfter ith, content
@insertAfter ith, content, options
#
# Deletes a part of the word.
@ -230,6 +260,10 @@ module.exports = (HB)->
toString: ()->
@val()
# String must not set options! (the third parameter)
insert: (position, content)->
super position, content
#
# Bind this String to a textfield or input field.
#

View File

@ -25,17 +25,25 @@ class JsonTest extends Test
if _.random(0,1) is 1 # take root
root
else # take child
properties =
elems = null
if root.type is "Object"
elems =
for oname,val of root.val()
oname
properties.filter (oname)->
root[oname] instanceof types.Operation
if properties.length is 0
val
else if root.type is "Array"
elems = root.val()
else
return root
elems = elems.filter (elem)->
(elem.type is "Array") or (elem.type is "Object")
if elems.length is 0
root
else
p = root[properties[_.random(0, properties.length-1)]]
p = elems[_.random(0, elems.length-1)]
@getRandomRoot user_num, p
getContent: (user_num)->
@users[user_num].toJson(true)
@ -43,15 +51,33 @@ class JsonTest extends Test
types = @users[user_num].types
super(user_num).concat [
f : (y)=> # SET PROPERTY
y.val(@getRandomKey(), @getRandomText(), 'immutable')
l = y.val().length
y.val(_.random(0, l-1), @getRandomText(), 'immutable')
null
types : [types.Object]
,
f : (y)=> # SET Object Property 1)
y.val(@getRandomObject())
types : [types.Array]
, f : (y)=> # Delete Array Element
list = y.val()
if list.length > 0
key = list[_random(0,list.length-1)]
y.delete(key)
types: [types.Array]
, f : (y)=> # insert TEXT mutable
l = y.val().length
y.val(_.random(0, l-1), @getRamdomObject())
types: [types.Array]
, f : (y)=> # insert string
l = y.val().length
y.val(_.random(0, l-1), @getRandomText(), 'immutable')
null
types : [types.Array]
, f : (y)=> # Delete Object Property
list = for name, o of y.val()
name
if list.length > 0
key = list[_random(0,list.length-1)]
y.delete(key)
types: [types.Object]
,
f : (y)=> # SET Object Property 2)
, f : (y)=> # SET Object Property
y.val(@getRandomKey(), @getRandomObject())
types: [types.Object]
,
@ -116,7 +142,9 @@ describe "JsonFramework", ->
@yTest.users[1].val('l', [4,5,6], "mutable")
@yTest.compareAll()
@yTest.users[2].val('l').insert(0,'A')
@yTest.users[1].val('l').insert(0,'B')
w = @yTest.users[1].val('l').insert(0,'B', "mutable").val(0)
w.insert 1, "C"
expect(w.val()).to.equal("BC")
@yTest.compareAll()
it "handles immutables and primitive data types", ->

View File

@ -100,6 +100,7 @@ module.exports = class Test
y instanceof type
if choices.length is 0
console.dir(y)
throw new Error "You forgot to specify a test generation methot for this Operation! (#{y.type})"
i = _.random 0, (choices.length-1)
choices[i].f y

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long