]> git.proxmox.com Git - rustc.git/blame - vendor/idna/tests/punycode.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / vendor / idna / tests / punycode.rs
CommitLineData
e74abb32
XL
1// Copyright 2013 The rust-url developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
6a06907d 9use crate::test::TestFn;
e74abb32 10use idna::punycode::{decode, encode_str};
e74abb32 11use serde_json::map::Map;
6a06907d 12use serde_json::Value;
4b012472 13use std::panic::catch_unwind;
e74abb32 14use std::str::FromStr;
e74abb32
XL
15
16fn one_test(decoded: &str, encoded: &str) {
17 match decode(encoded) {
18 None => panic!("Decoding {} failed.", encoded),
19 Some(result) => {
20 let result = result.into_iter().collect::<String>();
21 assert!(
22 result == decoded,
6a06907d
XL
23 "Incorrect decoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
24 encoded,
25 result,
26 decoded
e74abb32
XL
27 )
28 }
29 }
30
31 match encode_str(decoded) {
32 None => panic!("Encoding {} failed.", decoded),
33 Some(result) => assert!(
34 result == encoded,
6a06907d
XL
35 "Incorrect encoding of \"{}\":\n \"{}\"\n!= \"{}\"\n",
36 decoded,
37 result,
38 encoded
e74abb32
XL
39 ),
40 }
41}
42
4b012472
FG
43fn one_bad_test(encode: &str) {
44 let result = catch_unwind(|| encode_str(encode));
45 assert!(
46 matches!(&result, Ok(None)),
47 "Should neither panic nor return Some result, but got {:?}",
48 result
49 )
50}
51
e74abb32
XL
52fn get_string<'a>(map: &'a Map<String, Value>, key: &str) -> &'a str {
53 match map.get(&key.to_string()) {
fe692bf9 54 Some(Value::String(s)) => s,
e74abb32
XL
55 None => "",
56 _ => panic!(),
57 }
58}
59
60pub fn collect_tests<F: FnMut(String, TestFn)>(add_test: &mut F) {
61 match Value::from_str(include_str!("punycode_tests.json")) {
62 Ok(Value::Array(tests)) => {
63 for (i, test) in tests.into_iter().enumerate() {
64 match test {
65 Value::Object(o) => {
66 let test_name = {
67 let desc = get_string(&o, "description");
68 if desc.is_empty() {
69 format!("Punycode {}", i + 1)
70 } else {
71 format!("Punycode {}: {}", i + 1, desc)
72 }
73 };
74 add_test(
75 test_name,
2b03887a 76 TestFn::DynTestFn(Box::new(move || {
e74abb32 77 one_test(get_string(&o, "decoded"), get_string(&o, "encoded"))
2b03887a 78 })),
e74abb32
XL
79 )
80 }
81 _ => panic!(),
82 }
83 }
84 }
85 other => panic!("{:?}", other),
86 }
4b012472
FG
87
88 match Value::from_str(include_str!("bad_punycode_tests.json")) {
89 Ok(Value::Array(tests)) => {
90 for (i, test) in tests.into_iter().enumerate() {
91 match test {
92 Value::Object(o) => {
93 let test_name = {
94 let desc = get_string(&o, "description");
95 if desc.is_empty() {
96 format!("Bad Punycode {}", i + 1)
97 } else {
98 format!("Bad Punycode {}: {}", i + 1, desc)
99 }
100 };
101 add_test(
102 test_name,
103 TestFn::DynTestFn(Box::new(move || {
104 one_bad_test(get_string(&o, "decoded"))
105 })),
106 )
107 }
108 _ => panic!(),
109 }
110 }
111 }
112 other => panic!("{:?}", other),
113 }
e74abb32 114}