<commit_before>//
//  GRMustacheTests.swift
//  GRMustacheTests
//
//  Created by <NAME> on 25/10/2014.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import XCTest

class GRMustacheTests: XCTestCase {
    
    //    override func setUp() {
    //        super.setUp()
    //        // Put setup code here. This method is called before the invocation of each test method in the class.
    //    }
    //
    //    override func tearDown() {
    //        // Put teardown code here. This method is called after the invocation of each test method in the class.
    //        super.tearDown()
    //    }
    //
    //    func testExample() {
    //        // This is an example of a functional test case.
    //        XCTAssert(true, "Pass")
    //    }
    //
    //    func testPerformanceExample() {
    //        // This is an example of a performance test case.
    //        self.measureBlock() {
    //            // Put the code you want to measure the time of here.
    //        }
    //    }
    
    func testSwiftVariableInterpolation() {
        var error: NSError?
        let repository = MustacheTemplateRepository()
        if let template = repository.template(string: "<{{name}}>", error: &error) {
            let data = MustacheValue(["name": "Arthur"])
            if let rendering = template.render(data, error: &error) {
                XCTAssertEqual(rendering, "<Arthur>", "")
            } else {
                XCTFail("\(error!)")
            }
        } else {
            XCTFail("\(error!)")
        }
    }
    
    func testObjCVariableInterpolation() {
        var error: NSError?
        let repository = MustacheTemplateRepository()
        if let template = repository.template(string: "<{{name}}>", error: &error) {
            let data = MustacheValue(["name": "Arthur"])
            if let rendering = template.render(data, error: &error) {
                XCTAssertEqual(rendering, "<Arthur>", "")
            } else {
                XCTFail("\(error!)")
            }
        } else {
            XCTFail("\(error!)")
        }
    }
    
    func testPartial() {
        var error: NSError?
        let repository = MustacheTemplateRepository(templates: ["partial": "{{name}}"])
        if let template = repository.template(string: "<{{>partial}}>", error: &error) {
            let data = MustacheValue(["name": "Arthur"])
            if let rendering = template.render(data, error: &error) {
                XCTAssertEqual(rendering, "<Arthur>", "")
            } else {
                XCTFail("\(error!)")
            }
        } else {
            XCTFail("\(error!)")
        }
    }
    
    func testReadmeExample1() {
        let testBundle = NSBundle(forClass: GRMustacheTests.self)
        let template = MustacheTemplate(named: "example1", bundle: testBundle)!
        let value = MustacheValue([
            "name": "Chris",
            "value": 10000.0,
            "taxed_value": 10000 - (10000 * 0.4),
            "in_ca": true
            ])
        let rendering = template.render(value)!
        XCTAssertEqual(rendering, "Hello Chris\nYou have just won 10000.0 dollars!\n\nWell, 6000.0 dollars, after taxes.\n")
    }
    
    func testReadmeExample2() {
        // Define the `pluralize` filter.
        //
        // {{# pluralize(count) }}...{{/ }} renders the plural form of the
        // section content if the `count` argument is greater than 1.
        
        let pluralizeFilter = { (count: Int?) -> (MustacheValue) in
            
            // This filter returns an object that performs custom rendering:
            
            return MustacheValue({ (tag: MustacheTag, renderingInfo: RenderingInfo, contentType: ContentTypePointer, error: NSErrorPointer) -> (String?) in
                
                // Fetch the section inner content...
                
                let string = tag.innerTemplateString
                
                // ... and pluralize it if needed.
                
                if count! > 1 {
                    return string + "s"  // naive
                } else {
                    return string
                }
            })
        }
        
        
        // Register the pluralize filter for all Mustache renderings:
        
        MustacheConfiguration.defaultConfiguration.extendBaseContextWithValue(MustacheValue(pluralizeFilter), forKey: "pluralize")
        
        
        // I have 3 cats.
        
        let testBundle = NSBundle(forClass: GRMustacheTests.self)
        let template = MustacheTemplate(named: "example2", bundle: testBundle)!
        let value = MustacheValue(["cats": ["Kitty", "Pussy", "Melba"]])
        let rendering = template.render(value)!
        XCTAssertEqual(rendering, "I have 3 cats.")
    }
    
    func testReadmeExample3() {
        let user = ReadmeExample3User(name: "Arthur")
        let rendering = MustacheTemplate.render(MustacheValue(user), fromString:"Hello {{name}}!")!
        XCTAssertEqual(rendering, "Hello Arthur!")
    }
}

struct ReadmeExample3User {
    let name: String
}

extension ReadmeExample3User: MustacheTraversable {
    func valueForMustacheIdentifier(identifier: String) -> MustacheValue? {
        switch identifier {
        case "name":
            return MustacheValue(name)
        default:
            return nil
        }
    }
}<commit_msg>Test that user can extract custom objects wrapped into MustacheValue<commit_after>//
//  GRMustacheTests.swift
//  GRMustacheTests
//
//  Created by <NAME> on 25/10/2014.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import XCTest

class GRMustacheTests: XCTestCase {
    
    //    override func setUp() {
    //        super.setUp()
    //        // Put setup code here. This method is called before the invocation of each test method in the class.
    //    }
    //
    //    override func tearDown() {
    //        // Put teardown code here. This method is called after the invocation of each test method in the class.
    //        super.tearDown()
    //    }
    //
    //    func testExample() {
    //        // This is an example of a functional test case.
    //        XCTAssert(true, "Pass")
    //    }
    //
    //    func testPerformanceExample() {
    //        // This is an example of a performance test case.
    //        self.measureBlock() {
    //            // Put the code you want to measure the time of here.
    //        }
    //    }
    
    func testSwiftVariableInterpolation() {
        var error: NSError?
        let repository = MustacheTemplateRepository()
        if let template = repository.template(string: "<{{name}}>", error: &error) {
            let data = MustacheValue(["name": "Arthur"])
            if let rendering = template.render(data, error: &error) {
                XCTAssertEqual(rendering, "<Arthur>", "")
            } else {
                XCTFail("\(error!)")
            }
        } else {
            XCTFail("\(error!)")
        }
    }
    
    func testObjCVariableInterpolation() {
        var error: NSError?
        let repository = MustacheTemplateRepository()
        if let template = repository.template(string: "<{{name}}>", error: &error) {
            let data = MustacheValue(["name": "Arthur"])
            if let rendering = template.render(data, error: &error) {
                XCTAssertEqual(rendering, "<Arthur>", "")
            } else {
                XCTFail("\(error!)")
            }
        } else {
            XCTFail("\(error!)")
        }
    }
    
    func testPartial() {
        var error: NSError?
        let repository = MustacheTemplateRepository(templates: ["partial": "{{name}}"])
        if let template = repository.template(string: "<{{>partial}}>", error: &error) {
            let data = MustacheValue(["name": "Arthur"])
            if let rendering = template.render(data, error: &error) {
                XCTAssertEqual(rendering, "<Arthur>", "")
            } else {
                XCTFail("\(error!)")
            }
        } else {
            XCTFail("\(error!)")
        }
    }
    
    func testReadmeExample1() {
        let testBundle = NSBundle(forClass: GRMustacheTests.self)
        let template = MustacheTemplate(named: "example1", bundle: testBundle)!
        let value = MustacheValue([
            "name": "Chris",
            "value": 10000.0,
            "taxed_value": 10000 - (10000 * 0.4),
            "in_ca": true
            ])
        let rendering = template.render(value)!
        XCTAssertEqual(rendering, "Hello Chris\nYou have just won 10000.0 dollars!\n\nWell, 6000.0 dollars, after taxes.\n")
    }
    
    func testReadmeExample2() {
        // Define the `pluralize` filter.
        //
        // {{# pluralize(count) }}...{{/ }} renders the plural form of the
        // section content if the `count` argument is greater than 1.
        
        let pluralizeFilter = { (count: Int?) -> (MustacheValue) in
            
            // This filter returns an object that performs custom rendering:
            
            return MustacheValue({ (tag: MustacheTag, renderingInfo: RenderingInfo, contentType: ContentTypePointer, error: NSErrorPointer) -> (String?) in
                
                // Fetch the section inner content...
                
                let string = tag.innerTemplateString
                
                // ... and pluralize it if needed.
                
                if count! > 1 {
                    return string + "s"  // naive
                } else {
                    return string
                }
            })
        }
        
        
        // Register the pluralize filter for all Mustache renderings:
        
        MustacheConfiguration.defaultConfiguration.extendBaseContextWithValue(MustacheValue(pluralizeFilter), forKey: "pluralize")
        
        
        // I have 3 cats.
        
        let testBundle = NSBundle(forClass: GRMustacheTests.self)
        let template = MustacheTemplate(named: "example2", bundle: testBundle)!
        let value = MustacheValue(["cats": ["Kitty", "Pussy", "Melba"]])
        let rendering = template.render(value)!
        XCTAssertEqual(rendering, "I have 3 cats.")
    }
    
    func testReadmeExample3() {
        let user = ReadmeExample3User(name: "Arthur")
        let rendering = MustacheTemplate.render(MustacheValue(user), fromString:"Hello {{name}}!")!
        XCTAssertEqual(rendering, "Hello Arthur!")
    }
    
    func testCustomValueExtraction() {
        // Test that one can define a value extraction method similar to
        // MustacheValue.intValue, MustacheValue.doubleValue, etc.
        //
        // Here we use MustacheValue.extractedCustomValue
        let value = MustacheValue([
            "string": MustacheValue("success"),
            "custom": MustacheValue(CustomValue()),
            "f": MustacheValue({ (value: MustacheValue, error: NSErrorPointer?) -> (MustacheValue?) in
                if let c = value.customValue() {
                    return MustacheValue("custom")
                } else {
                    return MustacheValue("other")
                }
            })
            ])
        let template = MustacheTemplate(string:"{{f(custom)}},{{f(string)}}")!
        let rendering = template.render(value)!
        XCTAssertEqual(rendering, "custom,other")
    }
}

struct ReadmeExample3User {
    let name: String
}

extension ReadmeExample3User: MustacheTraversable {
    func valueForMustacheIdentifier(identifier: String) -> MustacheValue? {
        switch identifier {
        case "name":
            return MustacheValue(name)
        default:
            return nil
        }
    }
}

struct CustomValue: MustacheTraversable {
    func valueForMustacheIdentifier(identifier: String) -> MustacheValue? {
        return MustacheValue()
    }
}

extension MustacheValue {
    func customValue() -> CustomValue? {
        return self.traversableValue() as? CustomValue
    }
}
{'fraction_non_alphanumeric': 0.11601198402130493, 'fraction_numerical': 0.008821571238348869, 'mean_word_length': 1.7663443830570902, 'pattern_counts': {'":': 23, '<': 19, '<?xml version=': 0, '>': 32, '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': '393765', 'n_tokens_mistral': 3340, 'n_tokens_neox': 3167, 'n_words': 847}