Skip to content

Commit 9001b7f

Browse files
ref(tests): Move tests under cfg(test)
This way, tests are not compiled into binaries except when testing.
1 parent c9ecbf1 commit 9001b7f

File tree

7 files changed

+344
-308
lines changed

7 files changed

+344
-308
lines changed

src/decoder.rs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -338,49 +338,53 @@ pub fn decode_data_url(url: &str) -> Result<DecodedMap> {
338338
decode_slice(&data[..])
339339
}
340340

341-
#[test]
342-
fn test_strip_header() {
343-
use std::io::BufRead;
344-
let input: &[_] = b")]}garbage\r\n[1, 2, 3]";
345-
let mut reader = io::BufReader::new(StripHeaderReader::new(input));
346-
let mut text = String::new();
347-
reader.read_line(&mut text).ok();
348-
assert_eq!(text, "[1, 2, 3]");
349-
}
341+
#[cfg(test)]
342+
mod tests {
343+
use super::*;
344+
use std::io::{self, BufRead};
345+
346+
#[test]
347+
fn test_strip_header() {
348+
let input: &[_] = b")]}garbage\r\n[1, 2, 3]";
349+
let mut reader = io::BufReader::new(StripHeaderReader::new(input));
350+
let mut text = String::new();
351+
reader.read_line(&mut text).ok();
352+
assert_eq!(text, "[1, 2, 3]");
353+
}
350354

351-
#[test]
352-
fn test_bad_newline() {
353-
use std::io::BufRead;
354-
let input: &[_] = b")]}'\r[1, 2, 3]";
355-
let mut reader = io::BufReader::new(StripHeaderReader::new(input));
356-
let mut text = String::new();
357-
match reader.read_line(&mut text) {
358-
Err(err) => {
359-
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
360-
}
361-
Ok(_) => {
362-
panic!("Expected failure");
355+
#[test]
356+
fn test_bad_newline() {
357+
let input: &[_] = b")]}'\r[1, 2, 3]";
358+
let mut reader = io::BufReader::new(StripHeaderReader::new(input));
359+
let mut text = String::new();
360+
match reader.read_line(&mut text) {
361+
Err(err) => {
362+
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
363+
}
364+
Ok(_) => {
365+
panic!("Expected failure");
366+
}
363367
}
364368
}
365-
}
366369

367-
#[test]
368-
fn test_decode_rmi() {
369-
fn decode(rmi_str: &str) -> Vec<usize> {
370-
let mut out = bitvec::bitvec![u8, Lsb0; 0; 0];
371-
decode_rmi(rmi_str, &mut out).expect("failed to decode");
370+
#[test]
371+
fn test_decode_rmi() {
372+
fn decode(rmi_str: &str) -> Vec<usize> {
373+
let mut out = bitvec::bitvec![u8, Lsb0; 0; 0];
374+
decode_rmi(rmi_str, &mut out).expect("failed to decode");
372375

373-
let mut res = vec![];
374-
for (idx, bit) in out.iter().enumerate() {
375-
if *bit {
376-
res.push(idx);
376+
let mut res = vec![];
377+
for (idx, bit) in out.iter().enumerate() {
378+
if *bit {
379+
res.push(idx);
380+
}
377381
}
382+
res
378383
}
379-
res
380-
}
381384

382-
// This is 0-based index of the bits
383-
assert_eq!(decode("AAB"), vec![12]);
384-
assert_eq!(decode("g"), vec![5]);
385-
assert_eq!(decode("Bg"), vec![0, 11]);
385+
// This is 0-based index of the bits
386+
assert_eq!(decode("AAB"), vec![12]);
387+
assert_eq!(decode("g"), vec![5]);
388+
assert_eq!(decode("Bg"), vec![0, 11]);
389+
}
386390
}

src/encoder.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,30 @@ impl Encodable for DecodedMap {
229229
}
230230
}
231231

232-
#[test]
233-
fn test_encode_rmi() {
234-
fn encode(indices: &[usize]) -> String {
235-
let mut out = vec![];
232+
#[cfg(test)]
233+
mod tests {
234+
use super::*;
236235

237-
// Fill with zeros while testing
238-
let mut data = vec![0; 256];
236+
#[test]
237+
fn test_encode_rmi() {
238+
fn encode(indices: &[usize]) -> String {
239+
let mut out = vec![];
239240

240-
let bits = data.view_bits_mut::<Lsb0>();
241-
for &i in indices {
242-
bits.set(i, true);
241+
// Fill with zeros while testing
242+
let mut data = vec![0; 256];
243+
244+
let bits = data.view_bits_mut::<Lsb0>();
245+
for &i in indices {
246+
bits.set(i, true);
247+
}
248+
249+
encode_rmi(&mut out, &data);
250+
String::from_utf8(out).unwrap()
243251
}
244252

245-
encode_rmi(&mut out, &data);
246-
String::from_utf8(out).unwrap()
253+
// This is 0-based index
254+
assert_eq!(encode(&[12]), "AAB");
255+
assert_eq!(encode(&[5]), "g");
256+
assert_eq!(encode(&[0, 11]), "Bg");
247257
}
248-
249-
// This is 0-based index
250-
assert_eq!(encode(&[12]), "AAB");
251-
assert_eq!(encode(&[5]), "g");
252-
assert_eq!(encode(&[0, 11]), "Bg");
253258
}

src/js_identifiers.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,23 @@ pub fn get_javascript_token(source_line: &str) -> Option<&str> {
6363
}
6464
}
6565

66-
#[test]
67-
fn test_is_valid_javascript_identifier() {
68-
// assert_eq!(is_valid_javascript_identifier("foo 123"));
69-
assert!(is_valid_javascript_identifier("foo_$123"));
70-
assert!(!is_valid_javascript_identifier(" foo"));
71-
assert!(!is_valid_javascript_identifier("foo "));
72-
assert!(!is_valid_javascript_identifier("[123]"));
73-
assert!(!is_valid_javascript_identifier("foo.bar"));
74-
// Should these pass?
75-
// assert!(is_valid_javascript_identifier("foo [bar]"));
76-
// assert!(is_valid_javascript_identifier("foo[bar]"));
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
7769

78-
assert_eq!(get_javascript_token("foo "), Some("foo"));
79-
assert_eq!(get_javascript_token("f _hi"), Some("f"));
80-
assert_eq!(get_javascript_token("foo.bar"), Some("foo"));
81-
assert_eq!(get_javascript_token("[foo,bar]"), None);
70+
#[test]
71+
fn test_is_valid_javascript_identifier() {
72+
// assert_eq!(is_valid_javascript_identifier("foo 123"));
73+
assert!(is_valid_javascript_identifier("foo_$123"));
74+
assert!(!is_valid_javascript_identifier(" foo"));
75+
assert!(!is_valid_javascript_identifier("foo "));
76+
assert!(!is_valid_javascript_identifier("[123]"));
77+
assert!(!is_valid_javascript_identifier("foo.bar"));
78+
// Should these pass?
79+
// assert!(is_valid_javascript_identifier("foo [bar]"));
80+
assert_eq!(get_javascript_token("foo "), Some("foo"));
81+
assert_eq!(get_javascript_token("f _hi"), Some("f"));
82+
assert_eq!(get_javascript_token("foo.bar"), Some("foo"));
83+
assert_eq!(get_javascript_token("[foo,bar]"), None);
84+
}
8285
}

0 commit comments

Comments
 (0)