|
| 1 | +// |
| 2 | +// Copyright (c) 2024 Hemi Labs, Inc. |
| 3 | +// |
| 4 | +// This file is part of the posixutils-rs project covered under |
| 5 | +// the MIT License. For the full license text, please see the LICENSE |
| 6 | +// file in the root directory of this project. |
| 7 | +// SPDX-License-Identifier: MIT |
| 8 | +// |
| 9 | + |
| 10 | +use plib::testing::{run_test, TestPlan}; |
| 11 | + |
| 12 | +fn asa_test(test_data: &str, expected_output: &str) { |
| 13 | + run_test(TestPlan { |
| 14 | + cmd: String::from("asa"), |
| 15 | + args: vec![], |
| 16 | + stdin_data: String::from(test_data), |
| 17 | + expected_out: String::from(expected_output), |
| 18 | + expected_err: String::from(""), |
| 19 | + expected_exit_code: 0, |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +// Test empty input |
| 24 | +#[test] |
| 25 | +fn asa_empty() { |
| 26 | + asa_test("", ""); |
| 27 | +} |
| 28 | + |
| 29 | +// Test basic space control character (normal single-spacing) |
| 30 | +#[test] |
| 31 | +fn asa_space_single_line() { |
| 32 | + asa_test(" hello\n", "hello\n"); |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn asa_space_multiple_lines() { |
| 37 | + asa_test(" line1\n line2\n line3\n", "line1\nline2\nline3\n"); |
| 38 | +} |
| 39 | + |
| 40 | +// Test '0' control character (double-spacing - blank line before) |
| 41 | +#[test] |
| 42 | +fn asa_zero_first_line() { |
| 43 | + // '0' as first line: outputs blank line, then content |
| 44 | + asa_test("0hello\n", "\nhello\n"); |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn asa_zero_second_line() { |
| 49 | + // '0' on second line: previous line, blank line, then content |
| 50 | + asa_test(" line1\n0line2\n", "line1\n\nline2\n"); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn asa_zero_multiple() { |
| 55 | + asa_test("0first\n0second\n", "\nfirst\n\nsecond\n"); |
| 56 | +} |
| 57 | + |
| 58 | +// Test '1' control character (form-feed/new page) |
| 59 | +#[test] |
| 60 | +fn asa_one_first_line() { |
| 61 | + // '1' as first line: form-feed, then content |
| 62 | + asa_test("1page1\n", "\x0cpage1\n"); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn asa_one_second_line() { |
| 67 | + // '1' on second line: previous line ends, form-feed, then content |
| 68 | + asa_test(" line1\n1page2\n", "line1\n\x0cpage2\n"); |
| 69 | +} |
| 70 | + |
| 71 | +// Test '+' control character (overprint - carriage return) |
| 72 | +#[test] |
| 73 | +fn asa_plus_overprint() { |
| 74 | + // '+' causes overprint: carriage return instead of newline |
| 75 | + asa_test(" line1\n+over\n", "line1\rover\n"); |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn asa_plus_multiple_overprint() { |
| 80 | + // Multiple overprints on same logical line |
| 81 | + asa_test(" base\n+mid\n+top\n", "base\rmid\rtop\n"); |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +fn asa_plus_first_line() { |
| 86 | + // POSIX: '+' as first character in input is equivalent to space |
| 87 | + asa_test("+first\n", "first\n"); |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn asa_plus_first_line_then_normal() { |
| 92 | + // '+' as first, then normal lines |
| 93 | + asa_test("+first\n line2\n", "first\nline2\n"); |
| 94 | +} |
| 95 | + |
| 96 | +// Test '-' control character (triple-spacing - non-POSIX extension) |
| 97 | +#[test] |
| 98 | +fn asa_dash_first_line() { |
| 99 | + asa_test("-content\n", "\n\ncontent\n"); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn asa_dash_second_line() { |
| 104 | + asa_test(" line1\n-line2\n", "line1\n\n\nline2\n"); |
| 105 | +} |
| 106 | + |
| 107 | +// Test other/unknown control characters (treated as space) |
| 108 | +#[test] |
| 109 | +fn asa_other_char() { |
| 110 | + // Unknown control chars treated as space (normal single-spacing) |
| 111 | + asa_test("Xhello\n", "hello\n"); |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +fn asa_digit_as_control() { |
| 116 | + // '2' is not a special control, treated as space |
| 117 | + asa_test("2hello\n", "hello\n"); |
| 118 | +} |
| 119 | + |
| 120 | +// Test empty content (control char only) |
| 121 | +#[test] |
| 122 | +fn asa_space_empty_content() { |
| 123 | + asa_test(" \n", "\n"); |
| 124 | +} |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn asa_zero_empty_content() { |
| 128 | + asa_test("0\n", "\n\n"); |
| 129 | +} |
| 130 | + |
| 131 | +// Test mixed control characters |
| 132 | +#[test] |
| 133 | +fn asa_mixed_controls() { |
| 134 | + asa_test( |
| 135 | + " line1\n0double\n1newpage\n+over\n line2\n", |
| 136 | + "line1\n\ndouble\n\x0cnewpage\rover\nline2\n", |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +// Test content without trailing newline (EOF without newline) |
| 141 | +#[test] |
| 142 | +fn asa_no_trailing_newline() { |
| 143 | + asa_test(" hello", "hello\n"); |
| 144 | +} |
| 145 | + |
| 146 | +#[test] |
| 147 | +fn asa_plus_no_trailing_newline() { |
| 148 | + asa_test(" line1\n+over", "line1\rover\n"); |
| 149 | +} |
| 150 | + |
| 151 | +// Test lines with only control character (no content, no newline) |
| 152 | +#[test] |
| 153 | +fn asa_control_only_no_newline() { |
| 154 | + asa_test(" ", "\n"); |
| 155 | +} |
| 156 | + |
| 157 | +// Test complex FORTRAN-style output simulation |
| 158 | +#[test] |
| 159 | +fn asa_fortran_style_report() { |
| 160 | + // Simulate a simple FORTRAN report with page header and data |
| 161 | + let input = "1REPORT TITLE\n \n DATA LINE 1\n DATA LINE 2\n0SECTION 2\n DATA LINE 3\n"; |
| 162 | + let expected = "\x0cREPORT TITLE\n\nDATA LINE 1\nDATA LINE 2\n\nSECTION 2\nDATA LINE 3\n"; |
| 163 | + asa_test(input, expected); |
| 164 | +} |
0 commit comments