Skip to content

Commit

Permalink
Fixed flakey test
Browse files Browse the repository at this point in the history
  • Loading branch information
Malien committed Nov 22, 2023
1 parent 1cc5fa8 commit 402f759
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 81 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run:
cargo test --verbose &&
cargo test --verbose --test engine &&
cargo test --verbose --test reggie
run: ./test_suite.sh
env:
QUICKCHECK_GENERATOR_SIZE: 10
51 changes: 16 additions & 35 deletions ast_vm/src/stdlib/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn strsub_inner(str: String, start: isize, end: isize) -> Result<LuaValue, EvalE
return Ok(LuaValue::String(str));
}
let start = if start < 1 { 1 } else { start as usize };
let start = if start > str.len() { str.len() } else { start };
let start = if start > str.len() { str.len() + 1 } else { start };
let end = if end < start as isize { start } else { end as usize };
let end = if end > str.len() { str.len() } else { end };

Expand Down Expand Up @@ -333,27 +333,6 @@ mod test {
));
}

// #[test]
// fn format_errors_when_no_format_string_is_passed() {
// let res = format(&[]);
// assert!(res.is_err());
// }

// #[quickcheck]
// fn formatting_string_without_escapes_results_in_the_same_string(string: String) -> TestResult {
// if let Some(_) = string
// .matches("%c|%d|%E|%e|%f|%g|%i|%o|%u|%X|%x|%q|%s")
// .next()
// {
// return TestResult::discard();
// }
// let res = format(&[LuaValue::String(string.clone())]);
// assert!(res.is_ok());
// let value = res.unwrap();
// assert_eq!(value, LuaValue::String(string));
// TestResult::passed()
// }

#[quickcheck]
fn strlen_returns_the_number_of_bytes_in_a_string(str: String) {
let len = str.len();
Expand All @@ -379,23 +358,25 @@ mod test {

#[quickcheck]
fn strsub_slices_string_suffix(str: String, start: usize) {
let suffix = if str.len() < start {
Some("".to_string())
} else if str.is_char_boundary(start) {
Some(str[start..].to_string())
let suffix_start = if start <= 1 {
0
} else if start as usize >= str.len() + 1 {
str.len()
} else {
None
start as usize - 1
};

let res = strsub(&[LuaValue::String(str), LuaValue::Number(start.into())]);
if let Some(suffix) = suffix {
if let Ok(LuaValue::String(res)) = res {
assert_eq!(res, suffix);
} else {
panic!("Expected string, got {:?}", res);
}
let expected_suffix = LuaValue::string(&str[suffix_start..]);

let res = strsub(&[
LuaValue::String(str),
LuaValue::number(start),
LuaValue::Nil,
]);
if let Ok(res) = res {
assert_eq!(res, expected_suffix);
} else {
assert!(res.is_err());
panic!("Expected string, got {:?}", res);
}
}

Expand Down
54 changes: 48 additions & 6 deletions reggie/src/stdlib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use luar_error::ExpectedType;
use std::{
cmp::{max, min},
io::{self, Write}, rc::Rc,
io::{self, Write},
rc::Rc,
};

use crate::{trace_execution, EvalError, GlobalValues, LuaValue, TypeError};
Expand Down Expand Up @@ -165,14 +166,26 @@ pub fn define_stdlib(global_values: &mut GlobalValues) {
}

#[cfg(test)]
#[cfg(feature = "quickcheck")]
mod test {
use std::{io::Cursor, rc::Rc};
use std::io::Cursor;

use crate::NativeFunction;
use crate::{TableRef, TableValue};

use super::*;

#[test]
fn flooring_unconvertible_values_is_an_error() {
let unsupported = [
LuaValue::Nil,
LuaValue::string("hello"),
LuaValue::Table(TableRef::from(TableValue::new())),
LuaValue::native_function(|| ()),
];
for value in unsupported {
assert!(floor(&value).is_err())
}
}

#[test]
fn printing_with_no_args_prints_newline() {
let mut buf = Cursor::new(Vec::new());
Expand All @@ -188,6 +201,20 @@ mod test {
assert_eq!(res, "nil\n");
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn floor_floor_numbers(num: f64) {
use crate::eq_with_nan::eq_with_nan;

let res = floor(&LuaValue::Float(num)).unwrap();

assert!(eq_with_nan(res.number_as_f64().unwrap(), num.floor()));

let res = floor(&LuaValue::String(format!("{}", num))).unwrap();
assert!(eq_with_nan(res.number_as_f64().unwrap(), num.floor()));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn printing_string_prints_its_value(str: String) {
let mut buf = Cursor::new(Vec::new());
Expand All @@ -196,6 +223,7 @@ mod test {
assert_eq!(res, format!("{}\n", str));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn printing_int_prints_string_repr(num: i32) {
let mut buf = Cursor::new(Vec::new());
Expand All @@ -204,6 +232,7 @@ mod test {
assert_eq!(res, format!("{}\n", num));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn printing_float_prints_string_repr(num: f64) {
let mut buf = Cursor::new(Vec::new());
Expand All @@ -212,8 +241,12 @@ mod test {
assert_eq!(res, format!("{}\n", num));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn printing_function_prints_it_address() {
use std::rc::Rc;
use crate::NativeFunction;

let func = NativeFunction::new(|| ());
let addr = Rc::as_ptr(&func.0);
let mut buf = Cursor::new(Vec::new());
Expand All @@ -222,6 +255,7 @@ mod test {
assert_eq!(res, format!("function: {:p}\n", addr));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn multiple_values_are_printed_separated_by_tabs(values: Vec<LuaValue>) {
let mut buf = Cursor::new(Vec::new());
Expand All @@ -235,18 +269,22 @@ mod test {
*expected_buf.get_mut().last_mut().unwrap() = b'\t';
print(&mut expected_buf, std::slice::from_ref(&value)).unwrap();
}
let expected_str = String::from_utf8(expected_buf.into_inner()).unwrap();
assert_eq!(res_str, expected_str);
} else {
assert_eq!(res_str, "\n");
}
let expected_str = String::from_utf8(expected_buf.into_inner()).unwrap();
assert_eq!(res_str, expected_str);
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn strlen_returns_the_number_of_bytes_in_a_string(str: String) {
let len = str.len();
let res = strlen(&LuaValue::String(str)).unwrap();
assert_eq!(res, LuaValue::Int(len as i32));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn strlen_returns_the_number_of_bytes_in_a_stringified_int(num: i32) {
let str = format!("{}", num);
Expand All @@ -255,6 +293,7 @@ mod test {
assert_eq!(res, LuaValue::Int(len as i32));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn strlen_returns_the_number_of_bytes_in_a_stringified_float(num: f64) {
let str = format!("{}", num);
Expand All @@ -263,6 +302,7 @@ mod test {
assert_eq!(res, LuaValue::Int(len as i32));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn strlen_on_non_stringable_errors_out(value: LuaValue) {
if value.is_string() || value.is_int() || value.is_float() {
Expand All @@ -271,6 +311,7 @@ mod test {
assert!(strlen(&value).is_err());
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn strsub_slices_string_suffix(str: String, start: i32) {
let suffix_start = if start <= 1 {
Expand All @@ -291,6 +332,7 @@ mod test {
assert_eq!(res, Ok(expected_suffix));
}

#[cfg(feature = "quickcheck")]
#[quickcheck]
fn strsub_errors_out_on_non_stringables(value: LuaValue, start: u16, end: Option<u16>) {
if value.is_string() || value.is_int() || value.is_float() {
Expand Down
18 changes: 18 additions & 0 deletions test_suite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set -e

cd lex
cargo t --features quickcheck
cd ..

cd syn
cargo t --features quickcheck
cd ..

cd reggie
cargo t --features quickcheck
cd ..

cargo test -p ast_vm --lib
cargo test --test reggie
cargo test --test engine
cargo test --test correctness
1 change: 0 additions & 1 deletion tests/reggie/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod boolean_ops;
mod comparison;
mod function;
mod local_decl;
mod stdlib;
mod table;
mod table_constructor;
mod unary_op;
Expand Down
35 changes: 0 additions & 35 deletions tests/reggie/stdlib.rs

This file was deleted.

0 comments on commit 402f759

Please sign in to comment.