From 8c1c7fcfc3eda56daf643a8699b222a598440f92 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Fri, 27 Aug 2021 13:04:51 +0800 Subject: [PATCH] nac3core: fixed broken tests --- nac3core/src/codegen/test.rs | 47 ++++++++++++------- .../src/typecheck/type_inferencer/test.rs | 10 ++-- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/nac3core/src/codegen/test.rs b/nac3core/src/codegen/test.rs index 81156d00..e0180b5f 100644 --- a/nac3core/src/codegen/test.rs +++ b/nac3core/src/codegen/test.rs @@ -1,13 +1,19 @@ -use crate::{codegen::{CodeGenTask, WithCall, WorkerRegistry}, location::Location, symbol_resolver::{SymbolResolver, SymbolValue}, toplevel::{DefinitionId, FunInstance, TopLevelComposer, TopLevelContext, TopLevelDef}, typecheck::{ +use crate::{ + codegen::{CodeGenTask, WithCall, WorkerRegistry}, + location::Location, + symbol_resolver::{SymbolResolver, SymbolValue}, + toplevel::{DefinitionId, FunInstance, TopLevelComposer, TopLevelContext, TopLevelDef}, + typecheck::{ type_inferencer::{FunctionData, Inferencer, PrimitiveStore}, typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier}, - }}; + }, +}; use indoc::indoc; use parking_lot::RwLock; use rustpython_parser::{ast::fold::Fold, parser::parse_program}; -use std::collections::HashMap; -use std::sync::Arc; use std::cell::RefCell; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; struct Resolver { id_to_type: HashMap, @@ -77,7 +83,7 @@ fn test_primitives() { }; let mut virtual_checks = Vec::new(); let mut calls = HashMap::new(); - let mut identifiers = vec!["a".to_string(), "b".to_string()]; + let mut identifiers: HashSet<_> = ["a".to_string(), "b".to_string()].iter().cloned().collect(); let mut inferencer = Inferencer { top_level: &top_level, function_data: &mut function_data, @@ -86,7 +92,7 @@ fn test_primitives() { primitives: &primitives, virtual_checks: &mut virtual_checks, calls: &mut calls, - defined_identifiers: identifiers.clone() + defined_identifiers: identifiers.clone(), }; inferencer.variable_mapping.insert("a".into(), inferencer.primitives.int32); inferencer.variable_mapping.insert("b".into(), inferencer.primitives.int32); @@ -211,7 +217,7 @@ fn test_simple_call() { var_id: vec![], instance_to_stmt: HashMap::new(), instance_to_symbol: HashMap::new(), - resolver: None + resolver: None, }))); let resolver = Box::new(Resolver { @@ -222,7 +228,9 @@ fn test_simple_call() { resolver.add_id_def("foo".to_string(), DefinitionId(foo_id)); let resolver = Arc::new(resolver as Box); - if let TopLevelDef::Function {resolver: r, ..} = &mut *top_level.definitions.read()[foo_id].write() { + if let TopLevelDef::Function { resolver: r, .. } = + &mut *top_level.definitions.read()[foo_id].write() + { *r = Some(resolver.clone()); } else { unreachable!() @@ -236,7 +244,7 @@ fn test_simple_call() { }; let mut virtual_checks = Vec::new(); let mut calls = HashMap::new(); - let mut identifiers = vec!["a".to_string(), "foo".into()]; + let mut identifiers: HashSet<_> = ["a".to_string(), "foo".into()].iter().cloned().collect(); let mut inferencer = Inferencer { top_level: &top_level, function_data: &mut function_data, @@ -245,7 +253,7 @@ fn test_simple_call() { primitives: &primitives, virtual_checks: &mut virtual_checks, calls: &mut calls, - defined_identifiers: identifiers.clone() + defined_identifiers: identifiers.clone(), }; inferencer.variable_mapping.insert("a".into(), inferencer.primitives.int32); inferencer.variable_mapping.insert("foo".into(), fun_ty); @@ -265,13 +273,18 @@ fn test_simple_call() { .collect::, _>>() .unwrap(); - if let TopLevelDef::Function {instance_to_stmt, ..} = &mut *top_level.definitions.read()[foo_id].write() { - instance_to_stmt.insert("".to_string(), FunInstance { - body: statements_2, - calls: inferencer.calls.clone(), - subst: Default::default(), - unifier_id: 0 - }); + if let TopLevelDef::Function { instance_to_stmt, .. } = + &mut *top_level.definitions.read()[foo_id].write() + { + instance_to_stmt.insert( + "".to_string(), + FunInstance { + body: statements_2, + calls: inferencer.calls.clone(), + subst: Default::default(), + unifier_id: 0, + }, + ); } else { unreachable!() } diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index e081a633..ff141ebe 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -313,7 +313,7 @@ impl TestEnvironment { primitives: &mut self.primitives, virtual_checks: &mut self.virtual_checks, calls: &mut self.calls, - defined_identifiers: vec![] + defined_identifiers: Default::default() } } } @@ -380,8 +380,8 @@ fn test_basic(source: &str, mapping: HashMap<&str, &str>, virtuals: &[(&str, &st println!("source:\n{}", source); let mut env = TestEnvironment::new(); let id_to_name = std::mem::take(&mut env.id_to_name); - let mut defined_identifiers: Vec<_> = env.identifier_mapping.keys().cloned().collect(); - defined_identifiers.push("virtual".to_string()); + let mut defined_identifiers: HashSet<_> = env.identifier_mapping.keys().cloned().collect(); + defined_identifiers.insert("virtual".to_string()); let mut inferencer = env.get_inferencer(); inferencer.defined_identifiers = defined_identifiers.clone(); let statements = parse_program(source).unwrap(); @@ -522,8 +522,8 @@ fn test_primitive_magic_methods(source: &str, mapping: HashMap<&str, &str>) { println!("source:\n{}", source); let mut env = TestEnvironment::basic_test_env(); let id_to_name = std::mem::take(&mut env.id_to_name); - let mut defined_identifiers: Vec<_> = env.identifier_mapping.keys().cloned().collect(); - defined_identifiers.push("virtual".to_string()); + let mut defined_identifiers: HashSet<_> = env.identifier_mapping.keys().cloned().collect(); + defined_identifiers.insert("virtual".to_string()); let mut inferencer = env.get_inferencer(); inferencer.defined_identifiers = defined_identifiers.clone(); let statements = parse_program(source).unwrap();