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 for name, o of val
if o instanceof types.Object if o instanceof types.Object
json[name] = o.toJson(transform_to_value) 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 else if transform_to_value and o instanceof types.Operation
json[name] = o.val() json[name] = o.val()
else else

View File

@ -104,27 +104,57 @@ module.exports = (HB)->
cleanup: ()-> cleanup: ()->
super() super()
val: ()-> toJson: (transform_to_value = false)->
o = @beginning.next_cl val = @val()
result = [] for i, o in val
while o isnt @end if o instanceof types.Object
result.push o.val() o.toJson(transform_to_value)
o = o.next_cl else if o instanceof types.Array
result 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
result.push o.val()
o = o.next_cl
result
push: (content)-> push: (content)->
@insertAfter @end.prev_cl, 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 right = left.next_cl
while right.isDeleted() 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. 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 left = right.prev_cl
if content.type?
if content instanceof types.Operation
(new types.TextInsert content, undefined, left, right).execute() (new types.TextInsert content, undefined, left, right).execute()
else else
for c in content 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 left = tmp
@ @
@ -133,11 +163,11 @@ module.exports = (HB)->
# #
# @return {Array Type} This String object. # @return {Array Type} This String object.
# #
insert: (position, content)-> insert: (position, content, options)->
ith = @getOperationByPosition position ith = @getOperationByPosition position
# the (i-1)th character. e.g. "abc" the 1th character is "a" # the (i-1)th character. e.g. "abc" the 1th character is "a"
# the 0th character is the left Delimiter # the 0th character is the left Delimiter
@insertAfter ith, content @insertAfter ith, content, options
# #
# Deletes a part of the word. # Deletes a part of the word.
@ -230,6 +260,10 @@ module.exports = (HB)->
toString: ()-> toString: ()->
@val() @val()
# String must not set options! (the third parameter)
insert: (position, content)->
super position, content
# #
# Bind this String to a textfield or input field. # 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 if _.random(0,1) is 1 # take root
root root
else # take child else # take child
properties = elems = null
for oname,val of root.val() if root.type is "Object"
oname elems =
properties.filter (oname)-> for oname,val of root.val()
root[oname] instanceof types.Operation val
if properties.length is 0 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 root
else else
p = root[properties[_.random(0, properties.length-1)]] p = elems[_.random(0, elems.length-1)]
@getRandomRoot user_num, p @getRandomRoot user_num, p
getContent: (user_num)-> getContent: (user_num)->
@users[user_num].toJson(true) @users[user_num].toJson(true)
@ -43,15 +51,33 @@ class JsonTest extends Test
types = @users[user_num].types types = @users[user_num].types
super(user_num).concat [ super(user_num).concat [
f : (y)=> # SET PROPERTY f : (y)=> # SET PROPERTY
y.val(@getRandomKey(), @getRandomText(), 'immutable') l = y.val().length
y.val(_.random(0, l-1), @getRandomText(), 'immutable')
null null
types : [types.Object] types : [types.Array]
, , f : (y)=> # Delete Array Element
f : (y)=> # SET Object Property 1) list = y.val()
y.val(@getRandomObject()) 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] types: [types.Object]
, , f : (y)=> # SET Object Property
f : (y)=> # SET Object Property 2)
y.val(@getRandomKey(), @getRandomObject()) y.val(@getRandomKey(), @getRandomObject())
types: [types.Object] types: [types.Object]
, ,
@ -116,7 +142,9 @@ describe "JsonFramework", ->
@yTest.users[1].val('l', [4,5,6], "mutable") @yTest.users[1].val('l', [4,5,6], "mutable")
@yTest.compareAll() @yTest.compareAll()
@yTest.users[2].val('l').insert(0,'A') @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() @yTest.compareAll()
it "handles immutables and primitive data types", -> it "handles immutables and primitive data types", ->

View File

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