7
7
// option. This file may not be copied, modified, or distributed
8
8
// except according to those terms.
9
9
10
- extern crate rustc_serialize ;
10
+ extern crate serde_json ;
11
11
extern crate rustc_test as test;
12
12
#[ macro_use]
13
13
extern crate html5ever;
14
14
15
15
mod foreach_html5lib_test;
16
16
use foreach_html5lib_test:: foreach_html5lib_test;
17
17
18
- use rustc_serialize :: json :: Json ;
18
+ use serde_json :: { Value , Map } ;
19
19
use std:: borrow:: Cow :: Borrowed ;
20
- use std:: collections:: BTreeMap ;
21
20
use std:: default:: Default ;
22
21
use std:: ffi:: OsStr ;
22
+ use std:: io:: Read ;
23
23
use std:: mem:: replace;
24
24
use std:: path:: Path ;
25
25
use std:: { char, env} ;
@@ -152,65 +152,65 @@ trait JsonExt: Sized {
152
152
fn get_tendril ( & self ) -> StrTendril ;
153
153
fn get_nullable_tendril ( & self ) -> Option < StrTendril > ;
154
154
fn get_bool ( & self ) -> bool ;
155
- fn get_obj < ' t > ( & ' t self ) -> & ' t BTreeMap < String , Self > ;
155
+ fn get_obj < ' t > ( & ' t self ) -> & ' t Map < String , Self > ;
156
156
fn get_list < ' t > ( & ' t self ) -> & ' t Vec < Self > ;
157
157
fn find < ' t > ( & ' t self , key : & str ) -> & ' t Self ;
158
158
}
159
159
160
- impl JsonExt for Json {
160
+ impl JsonExt for Value {
161
161
fn get_str ( & self ) -> String {
162
162
match * self {
163
- Json :: String ( ref s) => s. to_string ( ) ,
164
- _ => panic ! ( "Json ::get_str: not a String" ) ,
163
+ Value :: String ( ref s) => s. to_string ( ) ,
164
+ _ => panic ! ( "Value ::get_str: not a String" ) ,
165
165
}
166
166
}
167
167
168
168
fn get_tendril ( & self ) -> StrTendril {
169
169
match * self {
170
- Json :: String ( ref s) => s. to_tendril ( ) ,
171
- _ => panic ! ( "Json ::get_tendril: not a String" ) ,
170
+ Value :: String ( ref s) => s. to_tendril ( ) ,
171
+ _ => panic ! ( "Value ::get_tendril: not a String" ) ,
172
172
}
173
173
}
174
174
175
175
fn get_nullable_tendril ( & self ) -> Option < StrTendril > {
176
176
match * self {
177
- Json :: Null => None ,
178
- Json :: String ( ref s) => Some ( s. to_tendril ( ) ) ,
179
- _ => panic ! ( "Json ::get_nullable_tendril: not a String" ) ,
177
+ Value :: Null => None ,
178
+ Value :: String ( ref s) => Some ( s. to_tendril ( ) ) ,
179
+ _ => panic ! ( "Value ::get_nullable_tendril: not a String" ) ,
180
180
}
181
181
}
182
182
183
183
fn get_bool ( & self ) -> bool {
184
184
match * self {
185
- Json :: Boolean ( b) => b,
186
- _ => panic ! ( "Json ::get_bool: not a Boolean " ) ,
185
+ Value :: Bool ( b) => b,
186
+ _ => panic ! ( "Value ::get_bool: not a Bool " ) ,
187
187
}
188
188
}
189
189
190
- fn get_obj < ' t > ( & ' t self ) -> & ' t BTreeMap < String , Json > {
190
+ fn get_obj < ' t > ( & ' t self ) -> & ' t Map < String , Value > {
191
191
match * self {
192
- Json :: Object ( ref m) => & * m,
193
- _ => panic ! ( "Json ::get_obj: not an Object" ) ,
192
+ Value :: Object ( ref m) => & * m,
193
+ _ => panic ! ( "Value ::get_obj: not an Object" ) ,
194
194
}
195
195
}
196
196
197
- fn get_list < ' t > ( & ' t self ) -> & ' t Vec < Json > {
197
+ fn get_list < ' t > ( & ' t self ) -> & ' t Vec < Value > {
198
198
match * self {
199
- Json :: Array ( ref m) => m,
200
- _ => panic ! ( "Json ::get_list: not an Array" ) ,
199
+ Value :: Array ( ref m) => m,
200
+ _ => panic ! ( "Value ::get_list: not an Array" ) ,
201
201
}
202
202
}
203
203
204
- fn find < ' t > ( & ' t self , key : & str ) -> & ' t Json {
204
+ fn find < ' t > ( & ' t self , key : & str ) -> & ' t Value {
205
205
self . get_obj ( ) . get ( & key. to_string ( ) ) . unwrap ( )
206
206
}
207
207
}
208
208
209
209
// Parse a JSON object (other than "ParseError") to a token.
210
- fn json_to_token ( js : & Json ) -> Token {
210
+ fn json_to_token ( js : & Value ) -> Token {
211
211
let parts = js. get_list ( ) ;
212
212
// Collect refs here so we don't have to use "ref" in all the patterns below.
213
- let args: Vec < & Json > = parts[ 1 ..] . iter ( ) . collect ( ) ;
213
+ let args: Vec < & Value > = parts[ 1 ..] . iter ( ) . collect ( ) ;
214
214
match & * parts[ 0 ] . get_str ( ) {
215
215
"DOCTYPE" => DoctypeToken ( Doctype {
216
216
name : args[ 0 ] . get_nullable_tendril ( ) ,
@@ -254,14 +254,14 @@ fn json_to_token(js: &Json) -> Token {
254
254
}
255
255
256
256
// Parse the "output" field of the test case into a vector of tokens.
257
- fn json_to_tokens ( js : & Json , exact_errors : bool ) -> Vec < Token > {
257
+ fn json_to_tokens ( js : & Value , exact_errors : bool ) -> Vec < Token > {
258
258
// Use a TokenLogger so that we combine character tokens separated
259
259
// by an ignored error.
260
260
let mut sink = TokenLogger :: new ( exact_errors) ;
261
261
for tok in js. get_list ( ) . iter ( ) {
262
262
assert_eq ! (
263
263
match * tok {
264
- Json :: String ( ref s) if & s[ ..] == "ParseError" => {
264
+ Value :: String ( ref s) if & s[ ..] == "ParseError" => {
265
265
sink. process_token( ParseError ( Borrowed ( "" ) ) , 0 )
266
266
} ,
267
267
_ => sink. process_token( json_to_token( tok) , 0 ) ,
@@ -299,24 +299,24 @@ fn unescape(s: &str) -> Option<String> {
299
299
}
300
300
}
301
301
302
- fn unescape_json ( js : & Json ) -> Json {
302
+ fn unescape_json ( js : & Value ) -> Value {
303
303
match * js {
304
304
// unwrap is OK here because the spec'd *output* of the tokenizer never
305
305
// contains a lone surrogate.
306
- Json :: String ( ref s) => Json :: String ( unescape ( & s) . unwrap ( ) ) ,
307
- Json :: Array ( ref xs) => Json :: Array ( xs. iter ( ) . map ( unescape_json) . collect ( ) ) ,
308
- Json :: Object ( ref obj) => {
309
- let mut new_obj = BTreeMap :: new ( ) ;
306
+ Value :: String ( ref s) => Value :: String ( unescape ( & s) . unwrap ( ) ) ,
307
+ Value :: Array ( ref xs) => Value :: Array ( xs. iter ( ) . map ( unescape_json) . collect ( ) ) ,
308
+ Value :: Object ( ref obj) => {
309
+ let mut new_obj = Map :: new ( ) ;
310
310
for ( k, v) in obj. iter ( ) {
311
311
new_obj. insert ( k. clone ( ) , unescape_json ( v) ) ;
312
312
}
313
- Json :: Object ( new_obj)
313
+ Value :: Object ( new_obj)
314
314
} ,
315
315
_ => js. clone ( ) ,
316
316
}
317
317
}
318
318
319
- fn mk_test ( desc : String , input : String , expect : Json , opts : TokenizerOpts ) -> TestDescAndFn {
319
+ fn mk_test ( desc : String , input : String , expect : Value , opts : TokenizerOpts ) -> TestDescAndFn {
320
320
TestDescAndFn {
321
321
desc : TestDesc :: new ( DynTestName ( desc) ) ,
322
322
testfn : DynTestFn ( Box :: new ( move || {
@@ -340,14 +340,14 @@ fn mk_test(desc: String, input: String, expect: Json, opts: TokenizerOpts) -> Te
340
340
}
341
341
}
342
342
343
- fn mk_tests ( tests : & mut Vec < TestDescAndFn > , filename : & str , js : & Json ) {
343
+ fn mk_tests ( tests : & mut Vec < TestDescAndFn > , filename : & str , js : & Value ) {
344
344
let obj = js. get_obj ( ) ;
345
- let mut input = js. find ( "input" ) . unwrap ( ) . get_str ( ) ;
346
- let mut expect = js. find ( "output" ) . unwrap ( ) . clone ( ) ;
345
+ let mut input = js. find ( "input" ) . get_str ( ) ;
346
+ let mut expect = js. find ( "output" ) . clone ( ) ;
347
347
let desc = format ! (
348
348
"tok: {}: {}" ,
349
349
filename,
350
- js. find( "description" ) . unwrap ( ) . get_str( )
350
+ js. find( "description" ) . get_str( )
351
351
) ;
352
352
353
353
// "Double-escaped" tests require additional processing of
@@ -368,7 +368,7 @@ fn mk_tests(tests: &mut Vec<TestDescAndFn>, filename: &str, js: &Json) {
368
368
369
369
// Some tests want to start in a state other than Data.
370
370
let state_overrides = match obj. get ( & "initialStates" . to_string ( ) ) {
371
- Some ( & Json :: Array ( ref xs) ) => xs
371
+ Some ( & Value :: Array ( ref xs) ) => xs
372
372
. iter ( )
373
373
. map ( |s| {
374
374
Some ( match & s. get_str ( ) [ ..] {
@@ -423,10 +423,12 @@ fn tests(src_dir: &Path) -> Vec<TestDescAndFn> {
423
423
"tokenizer" ,
424
424
OsStr :: new ( "test" ) ,
425
425
|path, mut file| {
426
- let js = Json :: from_reader ( & mut file) . ok ( ) . expect ( "json parse error" ) ;
426
+ let mut s = String :: new ( ) ;
427
+ file. read_to_string ( & mut s) . ok ( ) . expect ( "file reading error" ) ;
428
+ let js: Value = serde_json:: from_str ( & s) . ok ( ) . expect ( "json parse error" ) ;
427
429
428
430
match js. get_obj ( ) . get ( & "tests" . to_string ( ) ) {
429
- Some ( & Json :: Array ( ref lst) ) => {
431
+ Some ( & Value :: Array ( ref lst) ) => {
430
432
for test in lst. iter ( ) {
431
433
mk_tests (
432
434
& mut tests,
0 commit comments