Pārlūkot izejas kodu

Test more cases

David Peter 1 gadu atpakaļ
vecāks
revīzija
75516ebbba
3 mainītis faili ar 26 papildinājumiem un 15 dzēšanām
  1. 12 12
      numbat-cli/tests/integration.rs
  2. 1 1
      numbat/src/typed_ast.rs
  3. 13 2
      numbat/tests/interpreter.rs

+ 12 - 12
numbat-cli/tests/integration.rs

@@ -125,18 +125,18 @@ fn without_prelude() {
         .stderr(predicates::str::contains("unknown identifier"));
 }
 
-// #[test]
-// fn pretty_printing() {
-//     numbat()
-//         .arg("--pretty-print=always")
-//         .arg("--expression")
-//         .arg("let v=30km/h")
-//         .assert()
-//         .success()
-//         .stdout(predicates::str::contains(
-//             "let v: Velocity = 30 kilometre / hour",
-//         ));
-// }
+#[test]
+fn pretty_printing() {
+    numbat()
+        .arg("--pretty-print=always")
+        .arg("--expression")
+        .arg("let v=30km/h")
+        .assert()
+        .success()
+        .stdout(predicates::str::contains(
+            "let v: Velocity = 30 kilometre / hour",
+        ));
+}
 
 #[test]
 fn help_text() {

+ 1 - 1
numbat/src/typed_ast.rs

@@ -637,7 +637,7 @@ impl Statement {
                 *readable_type = Self::create_readable_type(registry, type_, type_annotation);
             }
             Statement::DefineFunction(_, _, _, parameters, _, fn_type, readable_return_type) => {
-                let (fn_type, type_parameters) = fn_type.instantiate_for_printing();
+                let (fn_type, _) = fn_type.instantiate_for_printing();
 
                 let Type::Fn(parameter_types, return_type) = fn_type.inner else {
                     unreachable!("Expected a function type")

+ 13 - 2
numbat/tests/interpreter.rs

@@ -758,6 +758,7 @@ fn test_recovery_after_runtime_error() {
 
 #[test]
 fn test_statement_pretty_printing() {
+    // Let definitions
     expect_pretty_print("let v = 10 m/s", "let v: Velocity = 10 metre / second");
     expect_pretty_print(
         "let v: Length * Frequency = 10 m/s",
@@ -766,6 +767,9 @@ fn test_statement_pretty_printing() {
 
     expect_pretty_print("let x = 0 + 1 m", "let x: Length = 0 + 1 metre");
 
+    expect_pretty_print("let x = 0", "let x: forall A: Dim. A = 0"); // TODO: This is not ideal. 'forall' is not valid Numbat syntax.
+
+    // Derived unit definitions
     expect_pretty_print(
         "unit my_length_base_unit: Length",
         "unit my_length_base_unit: Length",
@@ -774,14 +778,21 @@ fn test_statement_pretty_printing() {
         "unit my_custom_base_unit",
         "unit my_custom_base_unit: MyCustomBaseUnit",
     );
-
     expect_pretty_print(
         "unit my_speed_unit = 10 m/s",
         "unit my_speed_unit: Velocity = 10 metre / second",
     );
 
+    // Function definitions
     expect_pretty_print(
         "fn f(v)=(v+1 knot)/1s",
         "fn f(v: Velocity) -> Acceleration = (v + 1 knot) / 1 second",
-    )
+    );
+
+    expect_pretty_print("fn f(x) = x", "fn f<A>(x: A) -> A = x");
+
+    expect_pretty_print("fn f(x, y) = y", "fn f<A, B>(x: A, y: B) -> B = y");
+    expect_pretty_print("fn f(x, y) = x", "fn f<A, B>(x: B, y: A) -> B = x"); // TODO: This is correct, but it would be nice to associate 'x' to 'A', not 'B'.
+
+    expect_pretty_print("fn f(x) = 2 x", "fn f<A: Dim>(x: A) -> A = 2 x");
 }