<commit_before>extern crate handlebars;
#[macro_use]
extern crate serde_json;

use handlebars::{Handlebars, RenderError, Helper, RenderContext};
use serde_json::Value;

fn is_truthy(v: &Value) -> bool {
    match *v {
        Value::Bool(ref i) => *i,
        Value::Number(ref n) => n.as_f64().map(|f| f.is_normal()).unwrap_or(false),
        Value::Null => false,
        Value::String(ref i) => i.len() > 0,
        Value::Array(ref i) => i.len() > 0,
        Value::Object(ref i) => i.len() > 0,
    }
}

#[test]
fn test_subexpression() {
    let mut hbs = Handlebars::new();

    hbs.register_helper(
        "gt",
        Box::new(|h: &Helper,
         _: &Handlebars,
         rc: &mut RenderContext|
         -> Result<(), RenderError> {
            let p1 = try!(h.param(0).and_then(|v| v.value().as_i64()).ok_or(
                RenderError::new("Param 0 with i64 type is required for gt helper."),
            ));
            let p2 = try!(h.param(1).and_then(|v| v.value().as_i64()).ok_or(
                RenderError::new("Param 1 with i64 type is required for gt helper."),
            ));

            if p1 > p2 {
                rc.writer.write("true".as_bytes())?;
            }

            Ok(())
        }),
    );

    hbs.register_helper(
        "not",
        Box::new(|h: &Helper,
         _: &Handlebars,
         rc: &mut RenderContext|
         -> Result<(), RenderError> {
            let p1 = h.param(0).map(|v| is_truthy(v.value())).unwrap_or(false);
            if !p1 {
                rc.writer.write("true".as_bytes())?;
            }
            Ok(())
        }),
    );

    let data = json!({"a": 1, "b": 0, "c": 2});

    assert_eq!(
        hbs.template_render(
            "{{#if (gt a b)}}Success{{else}}Failed{{/if}}",
            &data,
        ).unwrap(),
        "Success"
    );

    assert_eq!(
        hbs.template_render(
            "{{#if (gt a c)}}Success{{else}}Failed{{/if}}",
            &data,
        ).unwrap(),
        "Failed"
    );
<commit_msg>(test) update test for typed subexpression<commit_after>extern crate handlebars;
#[macro_use]
extern crate serde_json;

use handlebars::{Handlebars, RenderError, Helper, RenderContext, HelperDef};
use serde_json::Value;

struct GtHelper;

impl HelperDef for GtHelper {
    fn call_inner(
        &self,
        h: &Helper,
        _: &Handlebars,
        _: &mut RenderContext,
    ) -> Result<Option<Value>, RenderError> {
        let p1 = try!(h.param(0).and_then(|v| v.value().as_i64()).ok_or(
            RenderError::new("Param 0 with i64 type is required for gt helper."),
        ));
        let p2 = try!(h.param(1).and_then(|v| v.value().as_i64()).ok_or(
            RenderError::new("Param 1 with i64 type is required for gt helper."),
        ));

        Ok(Some(Value::Bool(p1 > p2)))
    }
}

struct NotHelper;

impl HelperDef for NotHelper {
    fn call_inner(
        &self,
        h: &Helper,
        _: &Handlebars,
        _: &mut RenderContext,
    ) -> Result<Option<Value>, RenderError> {
        let p1 = try!(h.param(0).and_then(|v| v.value().as_bool()).ok_or(
            RenderError::new("Param 0 with bool type is required for not helper."),
        ));

        Ok(Some(Value::Bool(!p1)))
    }
}

#[test]
fn test_subexpression() {
    let mut hbs = Handlebars::new();

    hbs.register_helper("gt", Box::new(GtHelper));
    hbs.register_helper("not", Box::new(NotHelper));

    let data = json!({"a": 1, "b": 0, "c": 2});

    assert_eq!(
        hbs.template_render(
            "{{#if (gt a b)}}Success{{else}}Failed{{/if}}",
            &data,
        ).unwrap(),
        "Success"
    );

    assert_eq!(
        hbs.template_render(
            "{{#if (gt a c)}}Success{{else}}Failed{{/if}}",
            &data,
        ).unwrap(),
        "Failed"
    );

{'fraction_non_alphanumeric': 0.17657894736842106, 'fraction_numerical': 0.013157894736842105, 'mean_word_length': 2.0827250608272507, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7184345', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1289, 'n_words': 269}