Skip to content

Commit 9bf42b1

Browse files
committed
Extended snapshot testing to typecheck
- introduced new Handler field to concatenate all errors into a string - remodified tests in typecheck.rs to work with snapshot - new snapshot tests
1 parent 0c965eb commit 9bf42b1

File tree

5 files changed

+98
-7
lines changed

5 files changed

+98
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pest_derive = "2.7.14"
1212
rustc-hash = "2.1.0"
1313
thiserror = "2.0.8"
1414
codespan-reporting = "0.11.1"
15+
strip-ansi-escapes = "0.2"
1516

1617
[dev-dependencies]
1718
insta = { version = "1.41.1", features = ["yaml"] }

src/diagnostic.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::ir::*;
1717

1818
/// Track Errors
1919
#[derive(Hash, Eq, PartialEq, Debug)]
20-
enum ErrorKey {
20+
pub enum ErrorKey {
2121
ExprKey(ExprId),
2222
StmtKey(StmtId),
2323
}
@@ -87,20 +87,26 @@ impl Diagnostic {
8787
pub struct DiagnosticHandler {
8888
files: SimpleFiles<String, String>,
8989
reported_errs: HashSet<ErrorKey>,
90+
error_string: String,
9091
}
9192

9293
impl DiagnosticHandler {
9394
pub fn new() -> Self {
9495
Self {
9596
files: SimpleFiles::new(),
9697
reported_errs: HashSet::new(),
98+
error_string: String::new(),
9799
}
98100
}
99101

100102
pub fn add_file(&mut self, name: String, content: String) -> usize {
101103
self.files.add(name, content)
102104
}
103105

106+
pub fn error_string(&self) -> &str {
107+
&self.error_string
108+
}
109+
104110
pub fn emit_diagnostic_expr(
105111
&mut self,
106112
tr: &Transaction,
@@ -128,7 +134,9 @@ impl DiagnosticHandler {
128134

129135
diagnostic.emit(buffer, &self.files);
130136

131-
print!("{}", String::from_utf8_lossy(buffer.as_slice()));
137+
let error_msg = String::from_utf8_lossy(buffer.as_slice());
138+
self.error_string.push_str(&error_msg);
139+
print!("{}", error_msg);
132140
}
133141
}
134142

@@ -159,17 +167,23 @@ impl DiagnosticHandler {
159167

160168
diagnostic.emit(buffer, &self.files);
161169

162-
print!("{}", String::from_utf8_lossy(buffer.as_slice()));
170+
let error_msg = String::from_utf8_lossy(buffer.as_slice());
171+
self.error_string.push_str(&error_msg);
172+
print!("{}", error_msg);
163173
}
164174
}
165175
}
166176

167177
#[cfg(test)]
168178
mod tests {
179+
use baa::BitVecValue;
180+
use insta::Settings;
181+
use std::path::Path;
182+
use strip_ansi_escapes::strip_str;
183+
169184
use crate::{serialize::tests::create_calyx_go_down_transaction, typecheck::*};
170185

171186
use super::*;
172-
use baa::BitVecValue;
173187

174188
#[test]
175189
fn test_emit_diagnostic() {
@@ -194,6 +208,14 @@ mod tests {
194208

195209
handler.emit_diagnostic_expr(&tr, &one_expr, "Random Warning", Level::Warning);
196210
handler.emit_diagnostic_expr(&tr, &zero_expr, "Random Error", Level::Error);
211+
212+
let content = strip_str(handler.error_string());
213+
214+
let mut settings = Settings::clone_current();
215+
settings.set_snapshot_path(Path::new("../tests/snapshots"));
216+
settings.bind(|| {
217+
insta::assert_snapshot!(content);
218+
});
197219
}
198220

199221
#[test]

src/typecheck.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn check_stmt_types(
7070
let fields = st[structid].pins();
7171
if fields
7272
.iter()
73-
.find(|field| field.dir() == Dir::Out && st[field.name()] == st[lhs])
73+
.find(|field| field.dir() == Dir::Out && field.name() == st[lhs].name())
7474
.is_some()
7575
{
7676
handler.emit_diagnostic_stmt(
@@ -189,23 +189,43 @@ pub fn type_check(tr: &Transaction, st: &SymbolTable, handler: &mut DiagnosticHa
189189

190190
#[cfg(test)]
191191
mod tests {
192+
use std::path::Path;
193+
192194
use crate::serialize::tests::{create_add_transaction, create_calyx_go_down_transaction};
193195
use baa::BitVecValue;
196+
use insta::Settings;
197+
use strip_ansi_escapes::strip_str;
194198

195199
use super::*;
196200

197201
#[test]
198-
fn typecheck_add_transaction() {
202+
fn test_add_transaction() {
199203
let mut handler = DiagnosticHandler::new();
200204
let (add, symbols) = create_add_transaction(&mut handler);
201205
type_check(&add, &symbols, &mut handler);
206+
207+
let content = strip_str(handler.error_string());
208+
209+
let mut settings = Settings::clone_current();
210+
settings.set_snapshot_path(Path::new("../tests/snapshots"));
211+
settings.bind(|| {
212+
insta::assert_snapshot!(content);
213+
});
202214
}
203215

204216
#[test]
205-
fn typecheck_calyx_go_down_transaction() {
217+
fn test_calyx_go_down_transaction() {
206218
let mut handler = DiagnosticHandler::new();
207219
let (calyx_go_done, symbols) = create_calyx_go_down_transaction(&mut handler);
208220
type_check(&calyx_go_done, &symbols, &mut handler);
221+
222+
let content = strip_str(handler.error_string());
223+
224+
let mut settings = Settings::clone_current();
225+
settings.set_snapshot_path(Path::new("../tests/snapshots"));
226+
settings.bind(|| {
227+
insta::assert_snapshot!(content);
228+
});
209229
}
210230

211231
// Specific Tests
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: src/typecheck.rs
3+
expression: content
4+
---
5+
warning: Inferred RHS type as BitVec(32) from LHS type BitVec(32).
6+
┌─ add.prot:13:3
7+
8+
13DUT.a := X;
9+
^^^^^^^^^^^ Inferred RHS type as BitVec(32) from LHS type BitVec(32).
10+
11+
warning: Inferred RHS type as BitVec(32) from LHS type BitVec(32).
12+
┌─ add.prot:14:3
13+
14+
14DUT.b := X;
15+
^^^^^^^^^^^ Inferred RHS type as BitVec(32) from LHS type BitVec(32).
16+
17+
error: Cannot assign to function argument. Try using assert_eq if you want to check the value of a transaction output.
18+
┌─ add.prot:15:3
19+
20+
15 │ s := DUT.s;
21+
^^^^^^^^^^^ Cannot assign to function argument. Try using assert_eq if you want to check the value of a transaction output.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: src/typecheck.rs
3+
expression: content
4+
---
5+
error: Type mismatch in assignment: dut.go : BitVec(32) and 1 : BitVec(1).
6+
┌─ calyx_go_done.prot:10:3
7+
8+
10dut.go := 1;
9+
^^^^^^^^^^^^ Type mismatch in assignment: dut.go : BitVec(32) and 1 : BitVec(1).
10+
11+
error: Type mismatch in assignment: dut.go : BitVec(32) and 0 : BitVec(1).
12+
┌─ calyx_go_done.prot:14:3
13+
14+
14dut.go := 0;
15+
^^^^^^^^^^^^ Type mismatch in assignment: dut.go : BitVec(32) and 0 : BitVec(1).
16+
17+
warning: Inferred RHS type as BitVec(32) from LHS type BitVec(32).
18+
┌─ calyx_go_done.prot:15:3
19+
20+
15dut.ii := X;
21+
^^^^^^^^^^^^ Inferred RHS type as BitVec(32) from LHS type BitVec(32).
22+
23+
error: Cannot assign to function argument. Try using assert_eq if you want to check the value of a transaction output.
24+
┌─ calyx_go_done.prot:16:3
25+
26+
16 │ oo := dut.oo;
27+
^^^^^^^^^^^^^^ Cannot assign to function argument. Try using assert_eq if you want to check the value of a transaction output.

0 commit comments

Comments
 (0)