Skip to content

Commit b1f64d9

Browse files
committed
Add new test group for "null and undefined"
* move the two existing tests to there * Also add a new test to the "Basic" group for a literal undefined (which should and we already do, return undefined for that, just adding here to make sure we keep watching this)
1 parent c602982 commit b1f64d9

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/jquery.json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
} else if (type === 'string') {
113113
name = $.quoteString(k);
114114
} else {
115-
//skip non-string or number keys
115+
// Keys must be numerical or string. Skip others
116116
continue;
117117
}
118118

test/jquery.json.test.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ test( 'Basic toJSON usage', function(){
3232
'{"apple":7,"pear":5}',
3333
'Objects with multiple members should be separated by a comma'
3434
);
35-
equalJSON(
36-
{ apple: undefined, pear: 5 },
37-
'{"pear":5}',
38-
'Objects with a member with value of type undefined should be removed'
39-
);
4035
equalJSON(
4136
2.5,
4237
'2.5',
@@ -77,11 +72,6 @@ test( 'Basic toJSON usage', function(){
7772
'{"dDefault":"1.84467440737E+19"}',
7873
'Object with lower case key and value that should not be touched'
7974
);
80-
equalJSON(
81-
[undefined, undefined, 1, 2],
82-
'[null,null,1,2]',
83-
'Resolve undefined to null when encountered as object member values'
84-
);
8575
equalJSON(
8676
[0, false, function() {}],
8777
'[0,false,null]',
@@ -102,6 +92,11 @@ test( 'Basic toJSON usage', function(){
10292
'null',
10393
'null is null'
10494
);
95+
equalJSON(
96+
undefined,
97+
undefined,
98+
'undefined is not valid and should not return a string "undefined" but literally undefined'
99+
);
105100

106101
});
107102

@@ -160,4 +155,21 @@ test( 'Function arguments object', function(){
160155

161156
argTest( 'foo', 'bar', 'baz' );
162157

163-
})
158+
});
159+
160+
test( 'Undefined and null', function(){
161+
162+
163+
equalJSON(
164+
{ apple: undefined, pear: 5 },
165+
'{"pear":5}',
166+
'Objects with a member with value of type undefined should be removed'
167+
);
168+
equalJSON(
169+
[undefined, undefined, 1, 2],
170+
'[null,null,1,2]',
171+
'Resolve undefined to null when encountered as object member values'
172+
);
173+
174+
175+
});

0 commit comments

Comments
 (0)