Skip to content

Commit 4392fd9

Browse files
committed
Fix yarnspinner tests
1 parent fce40b2 commit 4392fd9

File tree

5 files changed

+50
-27
lines changed

5 files changed

+50
-27
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ jobs:
7878
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
7979
- name: Run cargo test
8080
run: cargo test --workspace
81+
working-directory: crates/yarnspinner
8182
- name: Run cargo test
8283
run: cargo test --workspace --all-features
8384
- name: Run doc tests

crates/runtime/src/dialogue.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ impl Dialogue {
116116
language_code: Default::default(),
117117
}
118118
}
119+
120+
pub fn next(
121+
&mut self,
122+
#[cfg(feature = "bevy")]
123+
world: &mut World,
124+
) -> Option<Vec<DialogueEvent>> {
125+
self.vm.next(#[cfg(feature = "bevy")] world)
126+
}
119127
}
120128

121129
fn visited(storage: Box<dyn VariableStorage>) -> yarn_fn_type! { impl Fn(String) -> bool } {
@@ -140,17 +148,6 @@ fn visited_count(storage: Box<dyn VariableStorage>) -> yarn_fn_type! { impl Fn(S
140148
}
141149
}
142150

143-
/*
144-
impl Iterator for Dialogue {
145-
type Item = Vec<DialogueEvent>;
146-
147-
/// Panicking version of [`Dialogue::continue_`].
148-
fn next(&mut self) -> Option<Self::Item> {
149-
self.vm.next()
150-
}
151-
}
152-
*/
153-
154151
// Accessors
155152
impl Dialogue {
156153
/// The [`Dialogue`]'s locale, as an IETF BCP 47 code.

crates/runtime/src/virtual_machine.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ pub(crate) struct VirtualMachine {
3333
language_code: Option<Language>,
3434
}
3535

36-
/*
37-
impl Iterator for VirtualMachine {
38-
type Item = Vec<DialogueEvent>;
39-
40-
fn next(&mut self) -> Option<Self::Item> {
41-
self.assert_can_continue().is_ok().then(||
42-
self.continue_()
43-
.unwrap_or_else(|e| panic!("Encountered error while running dialogue through its `Iterator` implementation: {e}")))
44-
}
45-
}
46-
*/
47-
4836
impl VirtualMachine {
4937
pub(crate) fn new(
5038
library: Library,
@@ -112,6 +100,12 @@ impl VirtualMachine {
112100
std::mem::take(&mut self.batched_events)
113101
}
114102

103+
pub fn next(&mut self, #[cfg(feature = "bevy")] world: &mut World) -> Option<Vec<DialogueEvent>> {
104+
self.assert_can_continue().is_ok().then(||
105+
self.continue_(#[cfg(feature = "bevy")] world)
106+
.unwrap_or_else(|e| panic!("Encountered error while running dialogue through its `Iterator` implementation: {e}")))
107+
}
108+
115109
pub(crate) fn set_node(&mut self, node_name: impl Into<String>) -> Result<()> {
116110
let node_name = node_name.into();
117111
debug!("Loading node \"{node_name}\"");
@@ -654,3 +648,19 @@ fn expand_substitutions(text: &str, substitutions: &[String]) -> String {
654648
text.replace(&format!("{{{i}}}",), substitution)
655649
})
656650
}
651+
652+
struct RunToCompletionIterator<'a> {
653+
vm: &'a mut VirtualMachine,
654+
#[cfg(feature = "bevy")]
655+
world: &'a mut World,
656+
}
657+
658+
impl <'a>Iterator for RunToCompletionIterator<'a> {
659+
type Item = Vec<DialogueEvent>;
660+
661+
fn next(&mut self) -> Option<Self::Item> {
662+
self.vm.assert_can_continue().is_ok().then(||
663+
self.vm.continue_(#[cfg(feature = "bevy")] self.world)
664+
.unwrap_or_else(|e| panic!("Encountered error while running dialogue through its `Iterator` implementation: {e}")))
665+
}
666+
}

crates/yarnspinner/tests/language_tests.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,19 @@ fn test_end_of_notes_with_options_not_added() {
7979

8080
let mut dialogue = TestBase::default().with_compilation(result).dialogue;
8181
dialogue.set_node("Start").unwrap();
82-
let has_options = dialogue
83-
.flatten()
84-
.any(|event| matches!(event, DialogueEvent::Options(_)));
82+
83+
#[cfg(feature = "bevy")]
84+
let mut world = World::default();
85+
86+
let mut has_options = false;
87+
'outer: while let Some(events) = dialogue.next(#[cfg(feature = "bevy")] world) {
88+
for event in events {
89+
if matches!(event, DialogueEvent::Options(_)) {
90+
has_options = true;
91+
break 'outer;
92+
}
93+
}
94+
}
8595
assert!(!has_options);
8696
}
8797

crates/yarnspinner/tests/test_base/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::path::{Path, PathBuf};
1919
use std::result::Result;
2020
use std::sync::atomic::{AtomicBool, Ordering};
2121
use std::sync::Arc;
22+
#[cfg(feature = "bevy")]
23+
use bevy::prelude::World;
2224
use yarnspinner::compiler::*;
2325
use yarnspinner::core::*;
2426
use yarnspinner::runtime::*;
@@ -147,7 +149,10 @@ impl TestBase {
147149
pub fn run_standard_testcase(&mut self) -> &mut Self {
148150
self.dialogue.set_node("Start").unwrap();
149151

150-
while let Some(events) = self.dialogue.next() {
152+
#[cfg(feature = "bevy")]
153+
let mut world = World::default();
154+
155+
while let Some(events) = self.dialogue.next(#[cfg(feature = "bevy")] world) {
151156
for event in events {
152157
match event {
153158
DialogueEvent::Line(line) => {

0 commit comments

Comments
 (0)