Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/dreamchecker/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::{run_inner};

pub const NO_ERRORS: &[(u32, u16, &str)] = &[];

pub fn parse_a_file_for_test<S: Into<Cow<'static, str>>>(buffer: S) -> Context {
pub fn parse_a_file_for_test<S: Into<Cow<'static, str>>>(buffer: S, config: String) -> Context {
let context = Context::default();

context.set_config(config);

let pp = dm::preprocessor::Preprocessor::from_buffer(&context, "unit_tests.rs".into(), buffer.into());

let indents = dm::indents::IndentProcessor::new(&context, pp);
Expand All @@ -22,7 +24,11 @@ pub fn parse_a_file_for_test<S: Into<Cow<'static, str>>>(buffer: S) -> Context {
}

pub fn check_errors_match<S: Into<Cow<'static, str>>>(buffer: S, errorlist: &[(u32, u16, &str)]) {
let context = parse_a_file_for_test(buffer);
check_errors_match_with_config(buffer, errorlist, String::new());
}

pub fn check_errors_match_with_config<S: Into<Cow<'static, str>>>(buffer: S, errorlist: &[(u32, u16, &str)], config: String) {
let context = parse_a_file_for_test(buffer, config);
let errors = context.errors();
let mut iter = errors.iter();
for (line, column, desc) in errorlist {
Expand Down
27 changes: 27 additions & 0 deletions crates/dreamchecker/tests/turf_iteration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

extern crate dreamchecker as dc;

use dc::test_helpers::*;

pub const CONST_EVAL_ERRORS: &[(u32, u16, &str)] = &[
(3, 18, "iterating over turf contents"),
(5, 18, "iterating over turf contents"),
];

#[test]
fn const_eval() {
let code = r##"
/proc/test()
var/turf/T = new /turf
for(var/a in T)
world.log << a
for(var/a in T.contents)
world.log << a
"##.trim();
let config = r##"
[code_standards]
disallow_turf_contents_iteration = true
"##.trim();
check_errors_match_with_config(code, CONST_EVAL_ERRORS, config.to_string());
}

4 changes: 4 additions & 0 deletions crates/dreammaker/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl Config {
Ok(toml::from_str(&config_toml)?)
}

pub fn read_toml_string(toml: String) -> Result<Config, Error> {
Ok(toml::from_str(&toml)?)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this takes &str, presumably so could the chain that calls it, rather than requiring config.to_string() in the tests

}

fn config_warninglevel(&self, error: &DMError) -> Option<&WarningLevel> {
if let Some(errortype) = error.errortype() {
return self.diagnostics.get(errortype)
Expand Down
4 changes: 4 additions & 0 deletions crates/dreammaker/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl Context {
// ------------------------------------------------------------------------
// Configuration

pub fn set_config(&self, config: String) {
*self.config.borrow_mut() = Config::read_toml_string(config).unwrap();
}

pub fn force_config(&self, toml: &Path) {
match Config::read_toml(toml) {
Ok(config) => *self.config.borrow_mut() = config,
Expand Down