| •1 |
#![cfg(feature = "from-parser")] |
| 2 |
|
| 3 |
use anyhow::Result; |
| 4 |
use pretty_assertions::assert_eq; |
| 5 |
use wit_encoder::{Package, packages_from_parsed}; |
| 6 |
use wit_parser::Resolve; |
| 7 |
|
| 8 |
const MAIN_PACKAGE_SOURCE: &str = indoc::indoc! {" |
| 9 |
package foo:main; |
| 10 |
|
| 11 |
interface main-interface-a { |
| 12 |
use foo:dep-a/dep-a-interface-b.{ ra }; |
| 13 |
} |
| 14 |
|
| 15 |
interface main-interface-b { |
| 16 |
record mb { |
| 17 |
x: f32, |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
interface main-interface-c {} |
| 22 |
|
| 23 |
interface main-interface-d {} |
| 24 |
|
| 25 |
interface main-interface-e {} |
| 26 |
|
| 27 |
world main-world-a { |
| 28 |
use foo:dep-c/dep-c-interface-b.{ cb, cb as cbcb }; |
| 29 |
import foo:dep-c/dep-c-interface-a; |
| 30 |
export foo:dep-c/dep-c-interface-b; |
| 31 |
include foo:dep-c/dep-c-world-a; |
| 32 |
|
| 33 |
use foo:dep-b/[email protected].{ bb, bb as bbbb }; |
| 34 |
import foo:dep-b/[email protected]; |
| 35 |
export foo:dep-b/[email protected]; |
| 36 |
include foo:dep-b/[email protected]; |
| 37 |
|
| 38 |
use main-interface-b.{ mb, mb as mbmb }; |
| 39 |
import main-interface-b; |
| 40 |
export main-interface-a; |
| 41 |
include main-world-b; |
| 42 |
} |
| 43 |
|
| 44 |
world main-world-b { |
| 45 |
export main-interface-d; |
| 46 |
import main-interface-d; |
| 47 |
} |
| 48 |
"}; |
| 49 |
|
| 50 |
const MAIN_PACKAGE_RESOLVED_ENCODED: &str = indoc::indoc! {" |
| 51 |
package foo:main; |
| 52 |
|
| 53 |
interface main-interface-a { |
| 54 |
use foo:dep-a/dep-a-interface-b.{ ra }; |
| 55 |
} |
| 56 |
|
| 57 |
interface main-interface-b { |
| 58 |
record mb { |
| 59 |
x: f32, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
interface main-interface-c {} |
| 64 |
|
| 65 |
interface main-interface-d {} |
| 66 |
|
| 67 |
interface main-interface-e {} |
| 68 |
|
| 69 |
world main-world-b { |
| 70 |
import main-interface-d; |
| 71 |
export main-interface-d; |
| 72 |
} |
| 73 |
|
| 74 |
world main-world-a { |
| 75 |
use foo:dep-b/[email protected].{ bb, bb as bbbb }; |
| 76 |
use foo:dep-c/dep-c-interface-b.{ cb, cb as cbcb }; |
| 77 |
use main-interface-b.{ mb, mb as mbmb }; |
| 78 |
import foo:dep-a/dep-a-interface-b; |
| 79 |
import foo:dep-b/[email protected]; |
| 80 |
import foo:dep-b/[email protected]; |
| 81 |
import foo:dep-c/dep-c-interface-a; |
| 82 |
import foo:dep-c/dep-c-interface-b; |
| 83 |
import foo:dep-c/dep-c-interface-c; |
| 84 |
import main-interface-b; |
| 85 |
import main-interface-d; |
| 86 |
export foo:dep-b/[email protected]; |
| 87 |
export foo:dep-c/dep-c-interface-b; |
| 88 |
export foo:dep-c/dep-c-interface-d; |
| 89 |
export main-interface-a; |
| 90 |
export main-interface-d; |
| 91 |
} |
| 92 |
"}; |
| 93 |
|
| 94 |
const DEP_PACKAGE_A: &str = indoc::indoc! {" |
| 95 |
package foo:dep-a; |
| 96 |
|
| 97 |
interface dep-a-interface-b { |
| 98 |
use foo:dep-b/[email protected].{a}; |
| 99 |
type ra = a; |
| 100 |
} |
| 101 |
|
| 102 |
world dep-a-world {} |
| 103 |
"}; |
| 104 |
|
| 105 |
const DEP_PACKAGE_B: &str = indoc::indoc! {" |
| 106 |
package foo:[email protected]; |
| 107 |
|
| 108 |
interface dep-b-interface-a { |
| 109 |
record a { |
| 110 |
x: f32, |
| 111 |
y: f32, |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
interface dep-b-interface-b { |
| 116 |
use foo:dep-c/dep-c-interface-a.{a}; |
| 117 |
|
| 118 |
record bb { |
| 119 |
x: f32, |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
world dep-b-world-a { |
| 124 |
export dep-b-interface-a; |
| 125 |
} |
| 126 |
|
| 127 |
world dep-b-world-b {} |
| 128 |
"}; |
| 129 |
|
| 130 |
const DEP_PACKAGE_C: &str = indoc::indoc! {" |
| 131 |
package foo:dep-c; |
| 132 |
|
| 133 |
interface dep-c-interface-a { |
| 134 |
record a { |
| 135 |
x: f32, |
| 136 |
y: f32, |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
interface dep-c-interface-b { |
| 141 |
record cb { |
| 142 |
x: f32, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
interface dep-c-interface-c {} |
| 147 |
|
| 148 |
interface dep-c-interface-d {} |
| 149 |
|
| 150 |
world dep-c-world-a { |
| 151 |
import dep-c-interface-c; |
| 152 |
export dep-c-interface-d; |
| 153 |
} |
| 154 |
"}; |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn resolve_encode_resolve_round_trip_with_use_and_include() { |
| 158 |
let packages = packages_from_parsed( |
| 159 |
&resolve_packages(&[ |
| 160 |
("dep_c.wit", DEP_PACKAGE_C), |
| 161 |
("dep_b.wit", DEP_PACKAGE_B), |
| 162 |
("dep_a.wit", DEP_PACKAGE_A), |
| 163 |
("main_foo.wit", MAIN_PACKAGE_SOURCE), |
| 164 |
]) |
| 165 |
.unwrap(), |
| 166 |
); |
| 167 |
|
| 168 |
assert_eq!(packages.len(), 4); |
| 169 |
|
| 170 |
assert_package_names( |
| 171 |
&packages, |
| 172 |
&["foo:dep-c", "foo:[email protected]", "foo:dep-a", "foo:main"], |
| 173 |
); |
| 174 |
|
| 175 |
// Resolve should still work after rendering the encoded packages |
| 176 |
let packages = packages_from_parsed( |
| 177 |
&resolve_packages(&[ |
| 178 |
("dep_c.wit", &packages[0].to_string()), |
| 179 |
("dep_b.wit", &packages[1].to_string()), |
| 180 |
("dep_a.wit", &packages[2].to_string()), |
| 181 |
("main_foo.wit", &packages[3].to_string()), |
| 182 |
]) |
| 183 |
.unwrap(), |
| 184 |
); |
| 185 |
|
| 186 |
// Check that no package is lost |
| 187 |
assert_package_names( |
| 188 |
&packages, |
| 189 |
&["foo:dep-c", "foo:[email protected]", "foo:dep-a", "foo:main"], |
| 190 |
); |
| 191 |
|
| 192 |
// Check for the encoded main wit |
| 193 |
assert_eq!(MAIN_PACKAGE_RESOLVED_ENCODED, &packages[3].to_string()); |
| 194 |
} |
| 195 |
|
| 196 |
fn resolve_packages(packages: &[(&str, &str)]) -> Result<Resolve> { |
| 197 |
let mut resolve = Resolve::new(); |
| 198 |
for (name, wit) in packages { |
| 199 |
resolve.push_str(name, wit)?; |
| 200 |
} |
| 201 |
Ok(resolve) |
| 202 |
} |
| 203 |
|
| 204 |
// The order of converted packages currently follows the order of pushes to the parser's package arena. |
| 205 |
// If this changes, then explicit sorting or search should be added to these tests |
| 206 |
fn assert_package_names(packages: &[Package], package_names: &[&str]) { |
| 207 |
assert_eq!( |
| 208 |
packages |
| 209 |
.iter() |
| 210 |
.map(|p| p.name().to_string()) |
| 211 |
.collect::<Vec<_>>(), |
| 212 |
package_names |
| 213 |
); |
| 214 |
} |