1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
910//! Handlers for testdriven test commands.
1112use mz_repr::explain::ExplainConfig;
1314use crate::{Def, TestCatalog, try_parse_def, try_parse_mir};
1516pub fn handle_define(catalog: &mut TestCatalog, input: &str) -> String {
17// Parse the relation, returning early on parse error.
18let result = match try_parse_def(catalog, input) {
19Ok(def) => match def {
20 Def::Source { name, cols, typ } => match catalog.insert(&name, cols, typ, true) {
21Ok(id) => format!("Source defined as {id}"),
22Err(err) => err,
23 },
24 },
25Err(err) => err,
26 };
27 result + "\n"
28}
2930pub fn handle_roundtrip(catalog: &TestCatalog, input: &str) -> String {
31let output = match try_parse_mir(catalog, input) {
32Ok(expr) => expr.explain(&ExplainConfig::default(), Some(catalog)),
33Err(err) => return err,
34 };
3536if strip_comments(input).trim() == output.trim() {
37"roundtrip OK\n".to_string()
38 } else {
39format!(
40"roundtrip produced a different output:\n~~~ expected:\n{}\n~~~ actual:\n{}\n~~~\n",
41 strip_comments(input),
42 output
43 )
44 }
45}
4647fn strip_comments(s: &str) -> String {
48let mut r = "".to_string();
49for line in s.lines() {
50let len = line.find("// {").unwrap_or(line.len());
51 r.push_str(line[0..len].trim_end());
52 r.push_str("\n");
53 }
54 r
55}