1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json;
18
19 import net.sf.json.test.JSONAssert;
20 import junit.framework.TestCase;
21
22
23
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 }