Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assert_module_ok! with trycmd tests 🧑‍💻 #91

Merged
merged 1 commit into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
724 changes: 380 additions & 344 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions crates/ditto-ast/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{graph::Scc, Expression, Kind, ModuleName, Name, ProperName, Span, Type};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A ditto module.
///
Expand Down Expand Up @@ -36,9 +36,7 @@ pub struct Module {
}

/// The type of `module.types`, for convenience.
pub type ModuleTypes = HashMap<ProperName, ModuleType>;

// REVIEW use a `HashMap` newtype to force errors/warnings when duplicates are inserted?
pub type ModuleTypes = IndexMap<ProperName, ModuleType>;

/// A type defined by a module.
#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -52,10 +50,10 @@ pub struct ModuleType {
}

/// The type of `module.constructors`, for convenience.
pub type ModuleConstructors = HashMap<ProperName, ModuleConstructor>;
pub type ModuleConstructors = IndexMap<ProperName, ModuleConstructor>;

/// The type of `module.values`, for convenience.
pub type ModuleValues = HashMap<Name, ModuleValue>;
pub type ModuleValues = IndexMap<Name, ModuleValue>;

/// A value defined by a module.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -130,7 +128,7 @@ pub struct ModuleExports {
}

/// The type of `module_exports.types`, for convenience.
pub type ModuleExportsTypes = HashMap<ProperName, ModuleExportsType>;
pub type ModuleExportsTypes = IndexMap<ProperName, ModuleExportsType>;

/// A single exposed type.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -144,7 +142,7 @@ pub struct ModuleExportsType {
}

/// The type of `module_exports.constructors`, for convenience.
pub type ModuleExportsConstructors = HashMap<ProperName, ModuleExportsConstructor>;
pub type ModuleExportsConstructors = IndexMap<ProperName, ModuleExportsConstructor>;

/// A single exposed constructor.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -162,7 +160,7 @@ pub struct ModuleExportsConstructor {
}

/// The type of `module_exports.values`, for convenience.
pub type ModuleExportsValues = HashMap<Name, ModuleExportsValue>;
pub type ModuleExportsValues = IndexMap<Name, ModuleExportsValue>;

/// A single exposed value.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions crates/ditto-checker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ miette = { version = "4.7", features = ["fancy"] }
thiserror = "1.0"
simsearch = "0.2"
indexmap = "1.8"
serde_json = "1.0"

[dev-dependencies]
snapshot-test = { path = "../snapshot-test" }
similar-asserts = "1.4"
serde_json = "1.0"
trycmd = "0.14"
108 changes: 108 additions & 0 deletions crates/ditto-checker/src/bin/ditto-checker-testbin-check-module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
fn parse_and_check_module(
source: String,
source_name: String,
everything: &ditto_checker::Everything,
) -> ditto_ast::Module {
match ditto_cst::Module::parse(&source) {
Err(err) => {
eprintln!(
"{:?}",
miette::Report::from(err.into_report(source_name, source))
);
std::process::exit(1)
}
Ok(cst_module) => match ditto_checker::check_module(everything, cst_module) {
Err(err) => {
eprintln!(
"{:?}",
miette::Report::from(err.into_report(source_name, source))
);
std::process::exit(1)
}
Ok((module, warnings)) => {
for warning in warnings {
eprintln!(
"{:?}",
miette::Report::from(warning.into_report()).with_source_code(
miette::NamedSource::new(source_name.clone(), source.clone())
)
)
}
module
}
},
}
}

fn read_module_dir<P: AsRef<std::path::Path>>(
path: P,
mut everything: ditto_checker::Everything,
) -> Vec<ditto_ast::Module> {
let mut module_paths = Vec::new();
for entry in std::fs::read_dir(path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
continue;
}
module_paths.push(path)
}
module_paths.sort(); // NOTE file order implies dependency order!
let mut modules = Vec::new();
for mut path in module_paths {
let source = std::fs::read_to_string(&path).unwrap();
let source_name = path.to_string_lossy().into_owned();
let module = parse_and_check_module(source, source_name, &everything);

path.set_extension("ast");
let ast_file = std::fs::File::create(&path).unwrap();
serde_json::to_writer_pretty(ast_file, &module).unwrap();

everything
.modules
.insert(module.module_name.clone(), module.exports.clone());
modules.push(module);
}
modules
}

fn main() {
let mut everything = ditto_checker::Everything::default();

let mut package_paths = Vec::new();
for entry in std::fs::read_dir("./").unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
package_paths.push(path)
}
}
package_paths.sort(); // NOTE directory order implies dependency order!
for path in package_paths {
let modules = read_module_dir(&path, everything.clone())
.into_iter()
.map(|module| (module.module_name, module.exports))
.collect::<std::collections::HashMap<_, _>>();
everything.packages.insert(
ditto_ast::PackageName(path.file_name().unwrap().to_string_lossy().into_owned()),
modules,
);
}

everything.modules = read_module_dir("./", everything.clone())
.into_iter()
.map(|module| (module.module_name, module.exports))
.collect::<std::collections::HashMap<_, _>>();

let source = std::io::stdin()
.lines()
.collect::<std::io::Result<Vec<_>>>()
.unwrap()
.join("\n");
let source_name = std::env::args()
.nth(1)
.unwrap_or(String::from("test.ditto"));
let module = parse_and_check_module(source, source_name, &everything);

serde_json::to_writer_pretty(std::io::stdout(), &module).unwrap();
}
3 changes: 0 additions & 3 deletions crates/ditto-checker/src/module/exports/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(test)]
mod tests;

use crate::result::{Result, TypeError, Warning, Warnings};
use ditto_ast::{
Module, ModuleExportsConstructor, ModuleExportsType, ModuleExportsValue, ModuleType,
Expand Down
146 changes: 0 additions & 146 deletions crates/ditto-checker/src/module/exports/tests/macros.rs

This file was deleted.

Loading