-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(compiler-base-session): Add some method for 'Session'. (#245)
* Feat(compiler-base-session): Add some method for 'Session'. add constructor `new_with_src_code()` to 'Session' for constructing by source code. add method 'emit_err()' to 'Session' for displaying error diagnostic. issue #115 * update compiler-base-error version * refactor method `Session.emit_err`
- Loading branch information
Showing
4 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
mod test_session { | ||
use crate::{Session, SessionDiagnostic}; | ||
use anyhow::Result; | ||
use compiler_base_error::{components::Label, Diagnostic, DiagnosticStyle}; | ||
// 1. Create your own error type. | ||
struct MyError; | ||
|
||
// 2. Implement trait `SessionDiagnostic` manually. | ||
impl SessionDiagnostic for MyError { | ||
fn into_diagnostic(self, _: &Session) -> Result<Diagnostic<DiagnosticStyle>> { | ||
let mut diag = Diagnostic::<DiagnosticStyle>::new(); | ||
// Label Component | ||
let label_component = Box::new(Label::Error("error".to_string())); | ||
diag.append_component(label_component); | ||
Ok(diag) | ||
} | ||
} | ||
#[test] | ||
fn test_session_emit_err() { | ||
let prev_hook = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(|_| {})); | ||
let result = std::panic::catch_unwind(|| { | ||
// 3. Create a Session. | ||
let sess = Session::new_with_src_code("test code").unwrap(); | ||
// 4. Emit the error diagnostic. | ||
sess.emit_err(MyError {}).unwrap(); | ||
}); | ||
assert!(result.is_err()); | ||
std::panic::set_hook(prev_hook); | ||
} | ||
} |