• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// tests cases ported over from python standard library
2 use textwrap::{dedent, indent};
3 
4 const ROUNDTRIP_CASES: [&str; 3] = [
5     // basic test case
6     "Hi.\nThis is a test.\nTesting.",
7     // include a blank line
8     "Hi.\nThis is a test.\n\nTesting.",
9     // include leading and trailing blank lines
10     "\nHi.\nThis is a test.\nTesting.\n",
11 ];
12 
13 const WINDOWS_CASES: [&str; 2] = [
14     // use windows line endings
15     "Hi.\r\nThis is a test.\r\nTesting.",
16     // pathological case
17     "Hi.\r\nThis is a test.\n\r\nTesting.\r\n\n",
18 ];
19 
20 #[test]
test_indent_nomargin_default()21 fn test_indent_nomargin_default() {
22     // indent should do nothing if 'prefix' is empty.
23     for text in ROUNDTRIP_CASES.iter() {
24         assert_eq!(&indent(text, ""), text);
25     }
26     for text in WINDOWS_CASES.iter() {
27         assert_eq!(&indent(text, ""), text);
28     }
29 }
30 
31 #[test]
test_roundtrip_spaces()32 fn test_roundtrip_spaces() {
33     // A whitespace prefix should roundtrip with dedent
34     for text in ROUNDTRIP_CASES.iter() {
35         assert_eq!(&dedent(&indent(text, "    ")), text);
36     }
37 }
38 
39 #[test]
test_roundtrip_tabs()40 fn test_roundtrip_tabs() {
41     // A whitespace prefix should roundtrip with dedent
42     for text in ROUNDTRIP_CASES.iter() {
43         assert_eq!(&dedent(&indent(text, "\t\t")), text);
44     }
45 }
46 
47 #[test]
test_roundtrip_mixed()48 fn test_roundtrip_mixed() {
49     // A whitespace prefix should roundtrip with dedent
50     for text in ROUNDTRIP_CASES.iter() {
51         assert_eq!(&dedent(&indent(text, " \t  \t ")), text);
52     }
53 }
54 
55 #[test]
test_indent_default()56 fn test_indent_default() {
57     // Test default indenting of lines that are not whitespace only
58     let prefix = "  ";
59     let expected = [
60         // Basic test case
61         "  Hi.\n  This is a test.\n  Testing.",
62         // Include a blank line
63         "  Hi.\n  This is a test.\n\n  Testing.",
64         // Include leading and trailing blank lines
65         "\n  Hi.\n  This is a test.\n  Testing.\n",
66     ];
67     for (text, expect) in ROUNDTRIP_CASES.iter().zip(expected.iter()) {
68         assert_eq!(&indent(text, prefix), expect)
69     }
70     let expected = [
71         // Use Windows line endings
72         "  Hi.\r\n  This is a test.\r\n  Testing.",
73         // Pathological case
74         "  Hi.\r\n  This is a test.\n\r\n  Testing.\r\n\n",
75     ];
76     for (text, expect) in WINDOWS_CASES.iter().zip(expected.iter()) {
77         assert_eq!(&indent(text, prefix), expect)
78     }
79 }
80 
81 #[test]
indented_text_should_have_the_same_number_of_lines_as_the_original_text()82 fn indented_text_should_have_the_same_number_of_lines_as_the_original_text() {
83     let texts = ["foo\nbar", "foo\nbar\n", "foo\nbar\nbaz"];
84     for original in texts.iter() {
85         let indented = indent(original, "");
86         assert_eq!(&indented, original);
87     }
88 }
89