fn to_tokens()

in yaml_test_runner/src/step/match.rs [55:159]


    fn to_tokens(&self, tokens: &mut Tokens) {
        let expr = self.expr.expression();

        match &self.value {
            Yaml::String(s) => {
                if s.starts_with('/') {
                    let s = clean_regex(s);
                    if self.expr.is_body() {
                        tokens.append(quote! {
                            assert_regex_match!(&text, #s, true);
                        });
                    } else {
                        let ident = syn::Ident::from(expr.as_str());
                        tokens.append(quote! {
                            assert_regex_match!(json#ident.as_str().unwrap(), #s, true);
                        });
                    }
                } else {
                    let ident = syn::Ident::from(expr.as_str());

                    // handle set values
                    if s.starts_with('$') {
                        let t = {
                            let s = s
                                .trim_start_matches('$')
                                .trim_start_matches('{')
                                .trim_end_matches('}');
                            syn::Ident::from(s)
                        };

                        tokens.append(quote! {
                            assert_match!(json#ident, json!(#t));
                        });
                    } else {
                        tokens.append(quote! {
                            assert_match!(json#ident, json!(#s));
                        })
                    };
                }
            }
            Yaml::Integer(i) => {
                if self.expr.is_body() {
                    panic!("match on $body with i64");
                } else {
                    let ident = syn::Ident::from(expr.as_str());
                    tokens.append(quote! {
                        assert_numeric_match!(json#ident, #i);
                    });
                }
            }
            Yaml::Real(r) => {
                let f = r.parse::<f64>().unwrap();
                if self.expr.is_body() {
                    panic!("match on $body with f64");
                } else {
                    let ident = syn::Ident::from(expr.as_str());
                    tokens.append(quote! {
                        assert_match!(json#ident, json!(#f));
                    });
                }
            }
            Yaml::Null => {
                if self.expr.is_body() {
                    tokens.append(quote! {
                        assert!(text.is_empty(), "expected response to be null (empty) but was {}", &text);
                    });
                } else {
                    let ident = syn::Ident::from(expr.as_str());
                    tokens.append(quote! {
                        assert_null!(json#ident);
                    });
                }
            }
            Yaml::Boolean(b) => {
                if self.expr.is_body() {
                    panic!("match on $body with bool");
                } else {
                    let ident = syn::Ident::from(expr.as_str());
                    tokens.append(quote! {
                        assert_match!(json#ident, json!(#b));
                    });
                }
            }
            yaml if yaml.is_array() || yaml.as_hash().is_some() => {
                let json = {
                    let s = json_string_from_yaml(yaml);
                    syn::Ident::from(s)
                };

                if self.expr.is_body() {
                    tokens.append(quote! {
                        assert_match!(json, json!(#json));
                    });
                } else {
                    let ident = syn::Ident::from(expr.as_str());
                    tokens.append(quote! {
                        assert_match!(json#ident, json!(#json));
                    });
                }
            }
            yaml => {
                panic!("Bad yaml value {:?}", &yaml);
            }
        }
    }