1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json;
18  
19  import net.sf.json.test.JSONAssert;
20  import junit.framework.TestCase;
21  
22  /**
23   * @author Andres Almiray <aalmiray@users.sourceforge.net>
24   */
25  public class TestJSONFunction extends TestCase {
26     public static void main( String[] args ) {
27        junit.textui.TestRunner.run( TestJSONFunction.class );
28     }
29  
30     public TestJSONFunction( String name ) {
31        super( name );
32     }
33  
34     public void testEquals() {
35        JSONFunction expected = new JSONFunction( new String[] { "a" }, "return a;" );
36        assertFalse( expected.equals( null ) );
37        assertFalse( expected.equals( new Object() ) );
38        assertFalse( expected.equals( "" ) );
39        assertFalse( expected.equals( new JSONFunction( "return a;" ) ) );
40        assertFalse( expected.equals( new JSONFunction( new String[] { "a" }, "return b;" ) ) );
41        assertTrue( expected.equals( new JSONFunction( new String[] { "a" }, "return a;" ) ) );
42     }
43  
44     public void testHashCode() {
45        JSONFunction a = new JSONFunction( new String[] { "a,b" }, "return a+b;" );
46        JSONFunction b = new JSONFunction( new String[] { "a,b" }, "return a+b;" );
47        assertTrue( a.hashCode() == b.hashCode() );
48     }
49  
50     public void testParse_String() {
51        assertEquals( "function(){ return a; }", JSONFunction.parse( "function(){ return a; }" ).toString() );
52     }
53  
54     public void testParse_String_withWhiteSpacechars() {
55        assertEquals( "function(){ return a; }", JSONFunction.parse( "function() { return a; }" ).toString() );
56        assertEquals( "function(){ return a; }", JSONFunction.parse( "function()  { return a; }" ).toString() );
57        assertEquals( "function(){ return a; }", JSONFunction.parse( "function()\n{ return a; }" ).toString() );
58        assertEquals( "function(){ return a; }", JSONFunction.parse( "function()\t{ return a; }" ).toString() );
59     }
60     
61     public void testParse_withSingleArg(){
62        JSONFunction expected = new JSONFunction(new String[]{"a"},"return this");
63        JSONFunction actual = JSONFunction.parse( "function(a){ return this}" );
64        JSONAssert.assertEquals( expected, actual );
65     }
66    
67     public void testParse_withMultipleArgs(){
68        JSONFunction expected = new JSONFunction(new String[]{"a","b"},"return this");
69        JSONAssert.assertEquals( expected, JSONFunction.parse( "function(a,b){ return this }" ) );
70        JSONAssert.assertEquals( expected, JSONFunction.parse( "function( a,b ){ return this }" ) );
71        JSONAssert.assertEquals( expected, JSONFunction.parse( "function( a,b){ return this }" ) );
72        JSONAssert.assertEquals( expected, JSONFunction.parse( "function(a, b ){ return this }" ) );
73        JSONAssert.assertEquals( expected, JSONFunction.parse( "function( a, b ){ return this }" ) );
74        JSONAssert.assertEquals( expected, JSONFunction.parse( "function ( a, b ){ return this }" ) );
75        JSONAssert.assertEquals( expected, JSONFunction.parse( "function ( a, b ) { return this }" ) );
76     }
77  }