class - How to compare two different javascript objects for same attributes (without comparing the instance function) - Stack Ov

How do I pare the tree structure inside an object task with the obj_after tree, without having to actua

How do I pare the tree structure inside an object task with the obj_after tree, without having to actually pare individual nodes in the tree. (one is an instance of a class i.e. with instance methods and the other has no functions just data).

Please read below to understand my exact problem (not so clear from the above).

I am writing a script that builds a GUI based on a js object tree. I am using coffeescript for this.

I would like to create a class that takes a json, and build the tree.

window.GuiTree = class GuiTree
    constructor:(json_GUI_tree)->

But the problem I am having is in testing this. I would like to add methods to this class like 'add_node'. That adds a new node to the GuiTree.

So I try this in Jasmine.

obj_before =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   


obj_after =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   
        5:
          type:"text" 
          name:"subtask"
          label:"Subtask"

Where I have added the task 5 to the Gui tree

And I would like to test it like this

task = new GuiTree(obj_before)
expect(task.add(3, node_5)).givesNewTree(obj_after)

The matcher 'givesNewTree' is a custom matcher.

The problem is here !! how do I pare the tree structure inside the object task with the obj_after tree, without having to actually pare individual nodes in the tree. (one is )

To me it looks like lots of error prone code to write to pare the trees. So I may need to write tests to test. Is there a smarter way.

How do I pare the tree structure inside an object task with the obj_after tree, without having to actually pare individual nodes in the tree. (one is an instance of a class i.e. with instance methods and the other has no functions just data).

Please read below to understand my exact problem (not so clear from the above).

I am writing a script that builds a GUI based on a js object tree. I am using coffeescript for this.

I would like to create a class that takes a json, and build the tree.

window.GuiTree = class GuiTree
    constructor:(json_GUI_tree)->

But the problem I am having is in testing this. I would like to add methods to this class like 'add_node'. That adds a new node to the GuiTree.

So I try this in Jasmine.

obj_before =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   


obj_after =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   
        5:
          type:"text" 
          name:"subtask"
          label:"Subtask"

Where I have added the task 5 to the Gui tree

And I would like to test it like this

task = new GuiTree(obj_before)
expect(task.add(3, node_5)).givesNewTree(obj_after)

The matcher 'givesNewTree' is a custom matcher.

The problem is here !! how do I pare the tree structure inside the object task with the obj_after tree, without having to actually pare individual nodes in the tree. (one is )

To me it looks like lots of error prone code to write to pare the trees. So I may need to write tests to test. Is there a smarter way.

Share Improve this question asked Oct 5, 2011 at 9:30 naruvimamanaruvimama 1091 silver badge8 bronze badges 1
  • Note that you can write: class window.GuiTree – tokland Commented Oct 5, 2011 at 20:35
Add a ment  | 

2 Answers 2

Reset to default 4

What you're looking for is an implementation of deep equivalence. They're not necessarily error prone, in fact there are several known implementation, the most well known is QUnit's deepEqual implementation. Here's another. Granted it's a quite plicated algorithm.

Your could of course also define a .equals (or similarly named) method in your GuiTree class that pares two instances in a simpler way based on what you already know is significant about them, but I would try existing deep equal algorithms first since you might be able to import previously written code that you already know is correct.

A hackish alternative is to do JSON.stringify() on both objects and expect the resulting JSON encoded strings to be equal, but I suspect that could lead to various problems with circular references and the like, but it might get you started.

Just today, Underscore.js 1.2.0 came out, with a new _.isEqual function that can handle cycles. Underscore is a robust, mature, and reasonably small library that works in both client-side and server-side JavaScript.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745328172a4622753.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信