text:
<?php

namespace OlajosCs\QueryBuilder\MySQL;

use OlajosCs\QueryBuilder\ConnectionFactory;
use OlajosCs\QueryBuilder\Contracts\Connection;
use OlajosCs\QueryBuilder\PDO;
use OlajosCs\QueryBuilder\TestConfig;

/**
 * Class MySQLDatabaseTestCase
 *
 *
 */
abstract class MySQLTestCase extends \PHPUnit_Extensions_Database_TestCase
{
    /**
     * @var Connection
     */
    protected $queryBuilderConnection;

    /**
     * @var \PHPUnit_Extensions_Database_DB_IDatabaseConnection
     */
    protected $connection;

    /**
     * @var array
     */
    protected $dataSet;

    /**
     * Returns the test database connection.
     *
     * @return \PHPUnit_Extensions_Database_DB_IDatabaseConnection
     */
    protected function getConnection()
    {
        if ($this->connection === null) {
            $configArray = require(__DIR__ . '/../../config/config.php');
            $config      = new TestConfig($configArray['mysql']);

            $connectionFactory = new ConnectionFactory();
            $pdo               = new PDO($config);
            $this->queryBuilderConnection  = $connectionFactory->create($pdo);
            $this->connection  = $this->createDefaultDBConnection($this->queryBuilderConnection->getPdo());
        }

        return $this->connection;
    }

    /**
     * Returns the test dataset.
     *
     * @return \PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    protected function getDataSet()
    {
        if ($this->dataSet === null) {
            $this->dataSet = include(__DIR__ . '/../arraydataset.php');
        }

        return $this->createArrayDataSet($this->dataSet);
    }

    /**
     * Return a connection based on the config file
     *
     * @return Connection
     */
    protected function getQueryBuilderConnection()
    {
        return $this->queryBuilderConnection;
    }

    /**
     * Create tables if not exist
     */
    protected function setUp()
    {
        $connection = $this->getConnection()->getConnection();

        $connection->exec(
            'CREATE TABLE IF NOT EXISTS querybuilder_test_languages (id INT PRIMARY KEY, code VARCHAR(2))'
        );

        $connection->exec(
            'CREATE TABLE IF NOT EXISTS querybuilder_tests (id INT PRIMARY KEY, languageId INT, field VARCHAR(6))'
        );

        parent::setUp();
    }

}

redpajama_set_name: RedPajamaGithub


text:
title: Required
meaning: Something not necessary
---


text:
package org.metaborg.spoofax.core.dynamicclassloading;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;

import org.apache.commons.vfs2.FileObject;
import org.metaborg.core.MetaborgException;
import org.metaborg.core.MetaborgRuntimeException;
import org.metaborg.core.language.ILanguageCache;
import org.metaborg.core.language.ILanguageComponent;
import org.metaborg.core.language.ILanguageImpl;
import org.metaborg.core.resource.IResourceService;
import org.metaborg.util.log.ILogger;
import org.metaborg.util.log.LoggerUtils;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Injector;

public class DynamicClassLoadingService implements IDynamicClassLoadingService, ILanguageCache {
    private static final ILogger logger = LoggerUtils.logger(DynamicClassLoadingService.class);

    private final IResourceService resourceService;
    private final Set<ClassLoader> additionalClassLoaders;

    private final Map<ILanguageComponent, ClassLoader> classLoaderCache = Maps.newHashMap();
    private final Map<ILanguageComponent, Map<Class<?>, ServiceLoader<?>>> serviceLoaderCache = Maps.newHashMap();
    private final Injector injector;

    @Inject public DynamicClassLoadingService(IResourceService resourceService,
        Set<ClassLoader> additionalClassLoaders, Injector injector) {
        this.resourceService = resourceService;
        this.additionalClassLoaders = additionalClassLoaders;
        this.injector = injector;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T loadClass(ILanguageComponent component, String className, Class<T> expectedType) {
        final ClassLoader classLoader = classLoader(component);
        Class<?> theClass;
        try {
            logger.trace("Loading outliner class");
            theClass = classLoader.loadClass(className);
        } catch (ClassNotFoundException e) {
            throw new MetaborgRuntimeException("Given class was not found: " + className, e);
        }
        T instance;
        try {
            logger.trace("Instantiating class " + className);
            instance = (T) theClass.newInstance();
            injector.injectMembers(instance);
        } catch (InstantiationException e) {
            throw new MetaborgRuntimeException("Given class was not instantiable: " + className, e);
        } catch (IllegalAccessException e) {
            throw new MetaborgRuntimeException("Given class was not accessible: " + className, e);
        } catch (ClassCastException e) {
            throw new MetaborgRuntimeException("Given class does not implement required interface: " + className, e);
        }
        logger.trace("Successfully loaded and instantiated class " + className);
        return instance;
    }

    @Override
    public <T> List<T> loadClasses(ILanguageComponent component, Class<T> type) throws MetaborgException {
        final Map<Class<?>, ServiceLoader<?>> serviceLoaderCacheLevel2;
        if(serviceLoaderCache.containsKey(component)) {
            serviceLoaderCacheLevel2 = serviceLoaderCache.get(component);
            if(serviceLoaderCacheLevel2.containsKey(type)) {
                @SuppressWarnings("unchecked")
                ServiceLoader<T> serviceLoader = (ServiceLoader<T>) serviceLoaderCacheLevel2.get(type);
                return initializeInjectionFields(serviceLoader.iterator());
            }
        } else {
            serviceLoaderCacheLevel2 = Maps.newHashMap();
            serviceLoaderCache.put(component, serviceLoaderCacheLevel2);
        }
        final ClassLoader classLoader = classLoader(component);
        final ServiceLoader<T> serviceLoader = ServiceLoader.load(type, classLoader);
        serviceLoaderCacheLevel2.put(type, serviceLoader);
        return initializeInjectionFields(serviceLoader.iterator());
    }

    private <T> List<T> initializeInjectionFields(Iterator<T> iterator) {
        List<T> result = new ArrayList<>();
        for(; iterator.hasNext();) {
            T instance = iterator.next();
            injector.injectMembers(instance);
            result.add(instance);
        }
        return result;
    }

    private ClassLoader classLoader(ILanguageComponent component) {
        if(classLoaderCache.containsKey(component)) {
            return classLoaderCache.get(component);
        }
        final Collection<FileObject> jarFiles = component.facet(DynamicClassLoadingFacet.class).jarFiles;
        final URL[] classpath = new URL[jarFiles.size()];
        try {
            int i = 0;
            for(FileObject jar : jarFiles) {
                final File localJar = resourceService.localFile(jar);
                classpath[i] = localJar.toURI().toURL();
                i++;
            }
        } catch (MalformedURLException e) {
            throw new MetaborgRuntimeException(e);
        }
        logger.trace("Loading jar files {}", (Object) classpath);
        URLClassLoader classLoader = new URLClassLoader(classpath, new DynamicClassLoader(additionalClassLoaders));
        classLoaderCache.put(component, classLoader);
        return classLoader;
    }

    @Override
    public void invalidateCache(ILanguageComponent component) {
        serviceLoaderCache.remove(component);
        classLoaderCache.remove(component);
    }

    @Override
    public void invalidateCache(ILanguageImpl impl) {
        for (ILanguageComponent component : impl.components()) {
            invalidateCache(component);
        }
    }
}


text:
<?php

namespace Microsoft\Dynamics\Http;

class HeaderOption extends Option
{

}


text:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Contactify.Entities.Models
{
    public class User
    {
        public User()
        {
            this.Messages = new List<Message>();
            this.SenderMessageHeader = new List<MessageHeader>();
            this.ReceiverMessageHeader = new List<MessageHeader>();
            this.Posts = new List<Post>();
            this.Reports = new List<Report>();
        }

        [Key]
        [Column(Order = 1)]
        public int Id { get; set; }

        [Required]
        [MaxLength(50)]
        [Column(Order = 2)]
        public string Username { get; set; }

        [Required]
        [Column(Order = 3)]
        [StringLength(100), EmailAddress]
        public string Email { get; set; }

        [MaxLength(50)]
        [Column(Order = 4)]
        public string Firstname { get; set; }

        [MaxLength(50)]
        [Column(Order = 5)]
        public string Lastname { get; set; }

        [MaxLength(100)]
        [Column(Order = 6)]
        public string FullName { get; set; }

        [MaxLength(10000)]
        [Column(Order = 7)]
        public string ProfilePicture { get; set; }

        [Required]
        [MaxLength(450)]
        public string ApplicationUserId { get; set; }

        public virtual ICollection<Post> Posts { get; set; }

        public virtual ApplicationUser ApplicationUser { get; set; }

        public virtual ICollection<MessageHeader> SenderMessageHeader { get; set; }

        public virtual ICollection<MessageHeader> ReceiverMessageHeader { get; set; }

        public virtual ICollection<Message> Messages { get; set; }

        public virtual ICollection<Report> Reports { get; set; }
    }
}


text:
const {
  assertSyntaxError
} = Assert;

function assertNoSyntaxError(source) {
  return Function(source);
}

// (non-strict, strict) function, yield without escape
assertNoSyntaxError(`function f() { async yield => {} }`);
assertSyntaxError(`"use strict"; function f() { async yield => {} }`);

// (non-strict, strict) generator, yield without escape
assertSyntaxError(`function* g() { async yield => {} }`);
assertSyntaxError(`"use strict"; function* g() { async yield => {} }`);

// (non-strict, strict) function, yield with escape
assertNoSyntaxError(String.raw `function f() { async yi\u0065ld => {} }`);
assertSyntaxError(String.raw `"use strict"; function f() { async yi\u0065ld => {} }`);

// (non-strict, strict) generator, yield with escape
assertSyntaxError(String.raw `function* g() { async yi\u0065ld => {} }`);
assertSyntaxError(String.raw `"use strict"; function* g() { async yi\u0065ld => {} }`);


text:
/**
 * Module dependencies
 */

var EventEmitter = require('events');
var sinon = require('sinon');

/**
 * Create new `widgetStub`
 *
 * Implements event system for binding only. Unbinding not supported.
 */

var widgetStub = new EventEmitter();

/**
 * Methods
 */

widgetStub.bind = widgetStub.addListener; // real
widgetStub.unbind = sinon.spy();
widgetStub.play = sinon.spy();
widgetStub.pause = sinon.spy();

/**
 * Expose `widgetStub`
 */

module.exports = widgetStub;


text:
mod car_example_generated_codec;
use car_example_generated_codec::*;

use std::fs::File;
use std::io::prelude::*;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Write};

pub fn main() {
    ::std::process::exit(match run_car_example() {
                             Ok(_) => 0,
                             Err(e) => {
                                 writeln!(::std::io::stderr(), "error: {:?}", e).unwrap();
                                 1
                             }
                         });
}

fn read_sbe_file_generated_from_java_example() -> ::std::io::Result<Vec<u8>> {
    // Generated by the generateCarExampleDataFile gradle task.
    let mut f = File::open("car_example_data.sbe")?;
    let mut buffer = Vec::new();
    f.read_to_end(&mut buffer)?;
    Ok(buffer)
}

fn run_car_example() -> ::std::io::Result<()> {
    let reference_example_bytes = read_sbe_file_generated_from_java_example()?;
    decode_car_and_assert_expected_content(&reference_example_bytes)?;
    let bytes_encoded_from_rust = encode_car_from_scratch()?;
    assert_eq!(reference_example_bytes, bytes_encoded_from_rust);
    decode_car_and_assert_expected_content(&bytes_encoded_from_rust)?;
    Ok(())
}

impl std::convert::From<CodecErr> for IoError {
    fn from(codec_err: CodecErr) -> Self {
        IoError::new(IoErrorKind::Other, match codec_err {
            CodecErr::NotEnoughBytes => "not enough bytes",
            CodecErr::SliceIsLongerThanAllowedBySchema => "slice is longer than allowed by schema",
        })
    }
}

fn decode_car_and_assert_expected_content(buffer: &[u8]) -> CodecResult<()> {
    let (h, dec_fields) = start_decoding_car(&buffer).header()?;
    assert_eq!(49u16, h.block_length);
    assert_eq!(h.block_length as usize, ::std::mem::size_of::<CarFields>());
    assert_eq!(1u16, h.template_id);
    assert_eq!(1u16, h.schema_id);
    assert_eq!(0u16, h.version);
    println!("Header read");

    assert_eq!(Model::C, CarFields::discounted_model());
    assert_eq!(9000u16, Engine::max_rpm());
    assert_eq!("Petrol", Engine::fuel());

    let mut found_fuel_figures = Vec::<FuelFigure>::with_capacity(EXPECTED_FUEL_FIGURES.len());

    let (fields, dec_fuel_figures_header) = dec_fields.car_fields()?;
    assert_eq!(1234, fields.serial_number);
    assert_eq!(2013, fields.model_year);
    assert_eq!(BooleanType::T, fields.available);
    assert_eq!([97_i8, 98, 99, 100, 101, 102], fields.vehicle_code); // abcdef
    assert_eq!([0_u32, 1, 2, 3, 4], fields.some_numbers);
    assert_eq!(6, fields.extras.0);
    assert!(fields.extras.get_cruise_control());
    assert!(fields.extras.get_sports_pack());
    assert!(!fields.extras.get_sun_roof());
    assert_eq!(2000, fields.engine.capacity);
    assert_eq!(4, fields.engine.num_cylinders);
    assert_eq!(BoostType::NITROUS, fields.engine.booster.boost_type);
    assert_eq!(200, fields.engine.booster.horse_power);
    println!("Static-length fields all match the expected values");

    let dec_perf_figures_header = match dec_fuel_figures_header.fuel_figures_individually()? {
        Either::Left(mut dec_ff_members) => {
            println!("Got some fuel figure members");
            loop {
                let (ff_fields, dec_usage_description) = dec_ff_members.next_fuel_figures_member()?;
                let (usage_description, next_step) = dec_usage_description.usage_description()?;
                let usage_str = std::str::from_utf8(usage_description).unwrap();
                println!("Fuel Figure: Speed: {0}, MPG: {1}, Usage: {2}",
                         ff_fields.speed,
                         ff_fields.mpg,
                         usage_str);
                found_fuel_figures.push(FuelFigure {
                                            speed: ff_fields.speed,
                                            mpg: ff_fields.mpg,
                                            usage_description: usage_str,
                                        });
                match next_step {
                    Either::Left(more_members) => dec_ff_members = more_members,
                    Either::Right(done_with_group) => {
                        break done_with_group;
                    }
                }
            }
        }
        Either::Right(next_decoder) => next_decoder,
    };
    assert!(EXPECTED_FUEL_FIGURES
                .iter()
                .zip(found_fuel_figures.iter())
                .all(|(exp, found)| exp == found),
            "fuel figures should match expected values");

    let dec_manufacturer = match dec_perf_figures_header.performance_figures_individually()? {
        Either::Left(mut dec_pf_members) => {
            println!("Got some performance figure members");
            loop {
                let (pf_fields, dec_acceleration_header) = dec_pf_members
                    .next_performance_figures_member()?;
                println!("Performance Figure Fixed Fields: Octane Rating: {0}",
                         pf_fields.octane_rating);
                let (accel_slice, next_step) = dec_acceleration_header.acceleration_as_slice()?;
                for accel_fields in accel_slice {
                    println!("Acceleration: MPH: {0}, Seconds: {1}",
                             accel_fields.mph,
                             accel_fields.seconds);
                }
                match next_step {
                    Either::Left(more_members) => dec_pf_members = more_members,
                    Either::Right(done_with_group) => {
                        break done_with_group;
                    }
                }
            }
        }
        Either::Right(next_decoder) => next_decoder,
    };
    let (manufacturer, dec_model) = dec_manufacturer.manufacturer()?;
    let manufacturer = std::str::from_utf8(manufacturer).unwrap();
    println!("Manufacturer: {}", manufacturer);
    assert_eq!("Honda", manufacturer);

    let (model, dec_activation_code) = dec_model.model()?;
    let model = std::str::from_utf8(model).unwrap();
    println!("Model: {}", model);
    assert_eq!("Civic VTi", model);

    let (activation_code, dec_done) = dec_activation_code.activation_code()?;
    let activation_code = std::str::from_utf8(activation_code).unwrap();
    println!("Activation Code: {}", activation_code);
    assert_eq!("abcdef", activation_code);

    let position = dec_done.unwrap();
    println!("Finished decoding. Made it to position {0} out of {1}",
             position,
             buffer.len());
    Ok(())
}

fn encode_car_from_scratch() -> CodecResult<Vec<u8>> {
    let mut buffer = vec![0u8; 256];
    let used_pos = {
        let enc_header = start_encoding_car(&mut buffer);
        let enc_fields = enc_header
            .header_copy(&CarMessageHeader::default().message_header)?;
        println!("encoded header");
        let (fields, enc_fuel_figures_header) = enc_fields.car_fields()?;
        fields.serial_number = 1234;
        fields.model_year = 2013;
        fields.available = BooleanType::T;
        fields.code = Model::A;
        fields.vehicle_code = [97_i8, 98, 99, 100, 101, 102]; // abcdef
        fields.some_numbers = [0_u32, 1, 2, 3, 4];
        fields.extras = OptionalExtras::new();
        fields.extras.set_cruise_control(true)
            .set_sports_pack(true)
            .set_sun_roof(false);
        fields.engine = Engine {
            capacity: 2000,
            num_cylinders: 4,
            manufacturer_code: [49, 50, 51], // 123
            efficiency: 35,
            booster_enabled: BooleanType::T,
            booster: Booster {
                boost_type: BoostType::NITROUS,
                horse_power: 200,
            },
        };
        println!("encoded top level fields");
        let mut enc_fuel_figures = enc_fuel_figures_header.fuel_figures_individually()?;
        let mut fuel_figure_scratch = CarFuelFiguresMember { speed: 0, mpg: 0.0 };
        for ff in EXPECTED_FUEL_FIGURES {
            fuel_figure_scratch.speed = ff.speed;
            fuel_figure_scratch.mpg = ff.mpg;
            //fuel_figure_scratch.usage_description = ff.mpg;
            let enc_usage = enc_fuel_figures
                .next_fuel_figures_member(&fuel_figure_scratch)?;
            enc_fuel_figures = enc_usage
                .usage_description(ff.usage_description.as_bytes())?;
        }
        let enc_perf_figures_header = enc_fuel_figures.done_with_fuel_figures()?;
        println!("encoded fuel figures");
        let mut perf_figure_member_scratch = CarPerformanceFiguresMember { octane_rating: 0 };
        let mut enc_perf_figures = enc_perf_figures_header.performance_figures_individually()?;
        for pf in EXPECTED_PERF_FIXTURES {
            perf_figure_member_scratch.octane_rating = pf.octane_rating;
            let enc_accel = enc_perf_figures
                .next_performance_figures_member(&perf_figure_member_scratch)?;
            enc_perf_figures = enc_accel.acceleration_from_slice(&pf.acceleration)?;
        }
        let enc_manufacturer = enc_perf_figures.done_with_performance_figures()?;
        println!("encoded perf figures");
        let enc_model = enc_manufacturer.manufacturer("Honda".as_bytes())?;
        let enc_activation_code = enc_model.model("Civic VTi".as_bytes())?;
        let done = enc_activation_code.activation_code("abcdef".as_bytes())?;
        let pos = done.unwrap();
        pos
    };
    println!("encoded up to position {}", used_pos);
    buffer.truncate(used_pos);

    Ok(buffer)
}

#[derive(Debug, PartialEq)]
struct FuelFigure<'d> {
    speed: u16,
    mpg: f32,
    usage_description: &'d str,
}

const EXPECTED_FUEL_FIGURES: &'static [FuelFigure] = &[FuelFigure {
     speed: 30,
     mpg: 35.9,
     usage_description: "Urban Cycle",
 },
 FuelFigure {
     speed: 55,
     mpg: 49.0,
     usage_description: "Combined Cycle",
 },
 FuelFigure {
     speed: 75,
     mpg: 40.0,
     usage_description: "Highway Cycle",
 }];

struct PerfFigure {
    octane_rating: u8,
    acceleration: [CarPerformanceFiguresAccelerationMember; 3],
}

const EXPECTED_PERF_FIXTURES: &'static [PerfFigure] = &[PerfFigure {
     octane_rating: 95,
     acceleration: [CarPerformanceFiguresAccelerationMember {
                        mph: 30,
                        seconds: 4.0,
                    },
                    CarPerformanceFiguresAccelerationMember {
                        mph: 60,
                        seconds: 7.5,
                    },
                    CarPerformanceFiguresAccelerationMember {
                        mph: 100,
                        seconds: 12.2,
                    }],
 },
 PerfFigure {
     octane_rating: 99,
     acceleration: [CarPerformanceFiguresAccelerationMember {
                        mph: 30,
                        seconds: 3.8,
                    },
                    CarPerformanceFiguresAccelerationMember {
                        mph: 60,
                        seconds: 7.1,
                    },
                    CarPerformanceFiguresAccelerationMember {
                        mph: 100,
                        seconds: 11.8,
                    }],
 }];


text:
"""Test module configuration script."""

__author__ = 'Mike Gainer (mgainer@google.com)'

import cStringIO
import logging
import os
import shutil
import sys
import tempfile
import traceback
import unittest

import appengine_config
from common import yaml_files
from scripts import modules as module_config

class TestWithTempDir(unittest.TestCase):

    def setUp(self):
        super(TestWithTempDir, self).setUp()
        self._tmpdir = tempfile.mkdtemp()

    def tearDown(self):
        super(TestWithTempDir, self).tearDown()
        shutil.rmtree(self._tmpdir, ignore_errors=True)

    def _dump_dir(self, _, dirname, names):
        for name in names:
            path = os.path.join(dirname, name)
            if not os.path.isdir(path):
                print '-------------------------', path
                with open(path) as fp:
                    print fp.read()

    def _dump_tree(self):
        os.path.walk(self._tmpdir, self._dump_dir, None)

    def _write_content(self, path, content):
        with open(path, 'w') as fp:
            for line in content:
                fp.write(line)

    def _assert_content_equals(self, path, expected_lines):
        with open(path) as fp:
            actual_lines = fp.readlines()
        self.assertEquals(expected_lines, actual_lines)

class ManipulateAppYamlFileTest(TestWithTempDir):

    def setUp(self):
        super(ManipulateAppYamlFileTest, self).setUp()
        self._yaml_path = os.path.join(self._tmpdir, 'app.yaml')
        self._minimal_content = [
            '\n',
            '\n',
            'libraries:\n',
            '- name: jinja2\n',
            '  version: "2.6"\n',
            '\n',
            'env_variables:\n',
            '  FOO: bar\n',
            '  BAR: bleep\n',
            ]

    def test_read_write_unchanged(self):
        parsed_content = (
            '\n'
            '\n'
            'libraries:\n'
            '- name: jinja2\n'
            '  version: "2.6"\n'
            '\n'
            'env_variables:\n'
            '  FOO: bar\n'
            'scalar_str: "string"\n'
            'scalar_int: 123\n'
            'scalar_bool: true\n'
            'dict:\n'
            '  foo1: bar\n'
            '  foo2: 123\n'
            '  foo3: true\n'
            'list:\n'
            '- name: blah\n'
            '  value: 123\n'
            '- name: blahblah\n'
            '- value: 999\n'
            )
        with open(self._yaml_path, 'w') as fp:
            fp.write(parsed_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        app_yaml.write()
        with open(self._yaml_path) as fp:
            written_content = fp.read()
        self.assertEquals(parsed_content, written_content)

    def test_get_env_var(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        self.assertEquals('bar', app_yaml.get_env('FOO'))
        self.assertEquals('bleep', app_yaml.get_env('BAR'))

    def test_add_env_var(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        app_yaml.set_env('BAZ', 'bar')
        self.assertEquals('bar', app_yaml.get_env('BAZ'))
        app_yaml.write()

        expected = self._minimal_content + ['  BAZ: bar\n']
        self._assert_content_equals(self._yaml_path, expected)

    def test_overwrite_env_var(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        app_yaml.set_env('FOO', 'foo')
        self.assertEquals('foo', app_yaml.get_env('FOO'))
        app_yaml.write()

        expected = (
            self._minimal_content[:7] +
            ['  FOO: foo\n'] +
            self._minimal_content[8:])
        self._assert_content_equals(self._yaml_path, expected)

    def test_clear_env_var(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        app_yaml.set_env('BAR', '')
        self.assertIsNone(app_yaml.get_env('BAR'))
        app_yaml.write()

        expected = self._minimal_content[:-1]
        self._assert_content_equals(self._yaml_path, expected)

    def test_require_existing_library(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        app_yaml.require_library('jinja2', '2.6')
        app_yaml.write()

        expected = self._minimal_content
        self._assert_content_equals(self._yaml_path, expected)

    def test_require_new_library(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        app_yaml.require_library('frammis', '1.2')
        app_yaml.write()

        expected = (
            self._minimal_content[:3] +
            ['- name: frammis\n',
             '  version: "1.2"\n'] +
            self._minimal_content[3:])
        self._assert_content_equals(self._yaml_path, expected)

    def test_require_different_version_of_library(self):
        self._write_content(self._yaml_path, self._minimal_content)
        app_yaml = yaml_files.AppYamlFile(self._yaml_path)
        with self.assertRaises(ValueError):
            app_yaml.require_library('jinja2', '2.1')

class ModuleManifestTest(TestWithTempDir):

    def setUp(self):
        super(ModuleManifestTest, self).setUp()
        self._manifest_path = os.path.join(self._tmpdir,
                                           module_config._MANIFEST_NAME)

    def test_manifest_must_contain_module(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write('foo: bar\n')
        with self.assertRaises(KeyError):
            # pylint: disable=expression-not-assigned
            yaml_files.ModuleManifest(self._manifest_path).module_name

    def test_module_name_must_name_full_python_module(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write('module_name: bar\n')
        with self.assertRaises(ValueError):
            # pylint: disable=expression-not-assigned
            yaml_files.ModuleManifest(self._manifest_path).module_name

    def test_module_name_must_start_with_modules(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write('module_name: bar.baz\n')
        with self.assertRaises(ValueError):
            # pylint: disable=expression-not-assigned
            yaml_files.ModuleManifest(self._manifest_path).module_name

    def test_manifest_must_have_container_version(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write('module_name: modules.bar.bar_module\n')
        with self.assertRaises(KeyError):
            # pylint: disable=expression-not-assigned
            yaml_files.ModuleManifest(self._manifest_path).module_name

    def test_manifest_must_have_tests(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write(
                'module_name: modules.bar.bar_module\n'
                'container_version: 1.3\n'
                )
        with self.assertRaises(KeyError):
            # pylint: disable=expression-not-assigned
            yaml_files.ModuleManifest(self._manifest_path).module_name

    def test_minimal_manifest(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write(
                'module_name: modules.foo.foo_module\n'
                'container_version: 1.2.3\n'
                'tests:\n'
                '  this: 1\n'
                '  that: 2\n')
        manifest = yaml_files.ModuleManifest(self._manifest_path)
        self.assertEquals(manifest.module_name, 'foo')
        self.assertEquals(manifest.main_module, 'modules.foo.foo_module')
        self.assertEquals(manifest.third_party_libraries, {})
        self.assertEquals(manifest.appengine_libraries, {})
        self.assertEquals(manifest.tests, {'this': 1, 'that': 2})

    def test_version_compatibility(self):
        with open(self._manifest_path, 'w') as fp:
            fp.write(
                'module_name: modules.foo.foo_module\n'
                'container_version: 1.2.3\n'
                'tests:\n'
                '  this: 1\n'
                '  that: 2\n')
        manifest = yaml_files.ModuleManifest(self._manifest_path)
        manifest.assert_version_compatibility('1.2.3')
        manifest.assert_version_compatibility('1.2.4')
        manifest.assert_version_compatibility('1.3.0')
        manifest.assert_version_compatibility('2.0.0')

        with self.assertRaises(ValueError):
            manifest.assert_version_compatibility('1.2.2')
        with self.assertRaises(ValueError):
            manifest.assert_version_compatibility('1.1.9')
        with self.assertRaises(ValueError):
            manifest.assert_version_compatibility('0.9.9')

class ModuleIncorporationTest(TestWithTempDir):

    def _make_module(self, module_dir, module_name):
        with open(os.path.join(module_dir, '__init__.py'), 'w'):
            pass
        with open(os.path.join(module_dir, module_name), 'w') as fp:
            fp.write(
                'from models import custom_modules\n'
                'def register_module():\n'
                '  return custom_modules.Module("x", "x", [], [])'
                )

    def setUp(self):
        super(ModuleIncorporationTest, self).setUp()

        self.foo_dir = os.path.join(self._tmpdir, 'foo')
        self.foo_src_dir = os.path.join(self.foo_dir, 'src')
        self.foo_scripts_dir = os.path.join(self.foo_dir, 'scripts')
        self.bar_dir = os.path.join(self._tmpdir, 'bar')
        self.bar_src_dir = os.path.join(self.bar_dir, 'src')
        self.bar_scripts_dir = os.path.join(self.bar_dir, 'scripts')
        self.cb_dir = os.path.join(self._tmpdir, 'coursebuilder')
        self.cb_modules_dir = os.path.join(self.cb_dir, 'modules')
        self.scripts_dir = os.path.join(self.cb_dir, 'scripts')
        self.lib_dir = os.path.join(self.cb_dir, 'lib')
        self.modules_dir = os.path.join(self._tmpdir,
                                        'coursebuilder_resources', 'modules')
        for dirname in (self.foo_dir, self.foo_src_dir, self.foo_scripts_dir,
                        self.bar_dir, self.bar_src_dir, self.bar_scripts_dir,
                        self.cb_dir, self.scripts_dir, self.lib_dir,
                        self.cb_modules_dir, self.modules_dir):
            os.makedirs(dirname)

        foo_manifest_path = os.path.join(self.foo_dir,
                                         module_config._MANIFEST_NAME)
        with open(foo_manifest_path, 'w') as fp:
            fp.write(
                'module_name: modules.foo.foo_module\n'
                'container_version: 1.6.0\n'
                'tests:\n'
                '  tests.ext.foo.foo_tests.FooTest: 1\n'
                'third_party_libraries:\n'
                '- name: foo_stuff.zip\n')
        self._make_module(self.foo_src_dir, 'foo_module.py')
        foo_installer_path = os.path.join(self.foo_dir, 'scripts', 'setup.sh')
        with open(foo_installer_path, 'w') as fp:
            fp.write(
                '#!/bin/bash\n'
                'ln -s $(pwd)/src $2/modules/foo\n'
                'touch $2/lib/foo_stuff.zip\n'
                )

        bar_manifest_path = os.path.join(self.bar_dir,
                                         module_config._MANIFEST_NAME)
        with open(bar_manifest_path, 'w') as fp:
            fp.write(
                'module_name: modules.bar.bar_module\n'
                'container_version: 1.6.0\n'
                'tests:\n'
                '  tests.ext.bar.bar_tests.BarTest: 1\n'
                'appengine_libraries:\n'
                '- name: endpoints\n'
                '  version: "1.0"\n')
        self._make_module(self.bar_src_dir, 'bar_module.py')
        bar_installer_path = os.path.join(self.bar_dir, 'scripts', 'setup.sh')
        with open(bar_installer_path, 'w') as fp:
            fp.write(
                '#!/bin/bash\n'
                'ln -s $(pwd)/src $2/modules/bar\n'
                )

        self.initial_app_yaml = [
            'application: mycourse\n',
            'runtime: python27\n',
            'api_version: 1\n',
            'threadsafe: false\n',
            '\n',
            'env_variables:\n',
            '  GCB_PRODUCT_VERSION: "1.6.0"\n',
            '\n',
            'libraries:\n',
            '- name: jinja2\n',
            '  version: "2.6"\n',
            ]
        self.app_yaml_path = os.path.join(self.cb_dir, 'app.yaml')
        self._write_content(self.app_yaml_path, self.initial_app_yaml)
        self.third_party_tests_path = os.path.join(
            self.scripts_dir, 'third_party_tests.yaml')
        with open(os.path.join(self.cb_modules_dir, '__init__.py'), 'w'):
            pass

        self.log_stream = cStringIO.StringIO()
        self.old_log_handlers = list(module_config._LOG.handlers)
        module_config._LOG.handlers = [logging.StreamHandler(self.log_stream)]

        self.save_bundle_root = appengine_config.BUNDLE_ROOT
        appengine_config.BUNDLE_ROOT = self.cb_dir
        self.save_sys_path = sys.path
        sys.path.insert(0, self.cb_dir)
        self.save_modules = sys.modules.pop('modules')

    def tearDown(self):
        module_config._LOG.handlers = self.old_log_handlers
        appengine_config.BUNDLE_ROOT = self.save_bundle_root
        sys.path = self.save_sys_path
        sys.modules['modules'] = self.save_modules
        super(ModuleIncorporationTest, self).tearDown()

    def _install(self, modules_arg):
        if modules_arg:
            args = module_config.PARSER.parse_args([modules_arg])
        else:
            args = module_config.PARSER.parse_args([])
        try:
            module_config.main(args, self.cb_dir, self.modules_dir)
        except Exception, ex:
            self._dump_tree()
            traceback.print_exc()
            raise ex

    def _get_log(self):
        self.log_stream.flush()
        ret = self.log_stream.getvalue()
        self.log_stream.reset()
        return ret

    def _expect_logs(self, expected_lines):
        actual_lines = self._get_log().split('\n')
        for expected, actual in zip(expected_lines, actual_lines):
            self.assertIn(expected, actual)

    def test_install_foo(self):
        self._install('--targets=foo@%s' % self.foo_dir)
        expected = (
            self.initial_app_yaml[:7] +
            ['  GCB_THIRD_PARTY_LIBRARIES: foo_stuff.zip\n',
             '  GCB_THIRD_PARTY_MODULES: modules.foo.foo_module\n'] +
            self.initial_app_yaml[7:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)

        expected = [
            'tests:\n',
            '  tests.ext.foo.foo_tests.FooTest: 1\n',
            ]
        self._assert_content_equals(self.third_party_tests_path, expected)

        expected = [
            'Downloading module foo',
            'Installing module foo',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_with_file_localhost_url(self):
        self._install('--targets=foo@file://localhost%s' %
                      os.path.abspath(self.foo_dir))
        expected = [
            'Downloading module foo',
            'Installing module foo',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_with_file_nohost_url(self):
        self._install('--targets=foo@file://%s' %
                      os.path.abspath(self.foo_dir))
        expected = [
            'Downloading module foo',
            'Installing module foo',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_with_relative_path_url(self):
        self._install('--targets=foo@file://%s' %
                      os.path.relpath(self.foo_dir))
        expected = [
            'Downloading module foo',
            'Installing module foo',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_with_relative_path(self):
        self._install('--targets=foo@%s' % os.path.relpath(self.foo_dir))
        expected = [
            'Downloading module foo',
            'Installing module foo',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_both(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        expected = (
            self.initial_app_yaml[:7] +
            ['  GCB_THIRD_PARTY_LIBRARIES: foo_stuff.zip\n',
             '  GCB_THIRD_PARTY_MODULES:\n',
             '    modules.foo.foo_module\n',
             '    modules.bar.bar_module\n'] +
            self.initial_app_yaml[7:9] +
            ['- name: endpoints\n',
             '  version: "1.0"\n',] +
            self.initial_app_yaml[9:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)

        expected = [
            'tests:\n',
            '  tests.ext.bar.bar_tests.BarTest: 1\n',
            '  tests.ext.foo.foo_tests.FooTest: 1\n',
            ]
        self._assert_content_equals(self.third_party_tests_path, expected)

        expected = [
            'Downloading module foo',
            'Installing module foo',
            'Downloading module bar',
            'Installing module bar',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_reinstall_both(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        self._get_log()
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        expected = (
            self.initial_app_yaml[:7] +
            ['  GCB_THIRD_PARTY_LIBRARIES: foo_stuff.zip\n',
             '  GCB_THIRD_PARTY_MODULES:\n',
             '    modules.foo.foo_module\n',
             '    modules.bar.bar_module\n'] +
            self.initial_app_yaml[7:9] +
            ['- name: endpoints\n',
             '  version: "1.0"\n',] +
            self.initial_app_yaml[9:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)

        expected = [
            'tests:\n',
            '  tests.ext.bar.bar_tests.BarTest: 1\n',
            '  tests.ext.foo.foo_tests.FooTest: 1\n',
            ]
        self._assert_content_equals(self.third_party_tests_path, expected)

        expected = [
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_reinstall_both_after_manual_removal(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        self._get_log()
        os.unlink(os.path.join(self.cb_dir, 'modules', 'foo'))
        os.unlink(os.path.join(self.cb_dir, 'modules', 'bar'))
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        expected = (
            self.initial_app_yaml[:7] +
            ['  GCB_THIRD_PARTY_LIBRARIES: foo_stuff.zip\n',
             '  GCB_THIRD_PARTY_MODULES:\n',
             '    modules.foo.foo_module\n',
             '    modules.bar.bar_module\n'] +
            self.initial_app_yaml[7:9] +
            ['- name: endpoints\n',
             '  version: "1.0"\n',] +
            self.initial_app_yaml[9:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)

        expected = [
            'tests:\n',
            '  tests.ext.bar.bar_tests.BarTest: 1\n',
            '  tests.ext.foo.foo_tests.FooTest: 1\n',
            ]
        self._assert_content_equals(self.third_party_tests_path, expected)

        expected = [
            'Installing module foo',
            'Installing module bar',
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_both_then_reinstall_foo(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        self._get_log()
        self._install('--targets=foo@%s' % self.foo_dir)
        expected = (
            self.initial_app_yaml[:7] +
            ['  GCB_THIRD_PARTY_LIBRARIES: foo_stuff.zip\n',
             '  GCB_THIRD_PARTY_MODULES: modules.foo.foo_module\n'] +
            self.initial_app_yaml[7:9] +
            ['- name: endpoints\n',  # Note that AE lib requirement stays.
             '  version: "1.0"\n',] +
            self.initial_app_yaml[9:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)

        expected = [
            'tests:\n',
            '  tests.ext.foo.foo_tests.FooTest: 1\n',
            ]
        self._assert_content_equals(self.third_party_tests_path, expected)

        expected = [
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_both_then_reinstall_bar(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        self._get_log()
        self._install('--targets=bar@%s' % self.bar_dir)
        expected = (
            self.initial_app_yaml[:7] +
            ['  GCB_THIRD_PARTY_MODULES: modules.bar.bar_module\n'] +
            self.initial_app_yaml[7:9] +
            ['- name: endpoints\n',
             '  version: "1.0"\n',] +
            self.initial_app_yaml[9:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)

        expected = [
            'tests:\n',
            '  tests.ext.bar.bar_tests.BarTest: 1\n',
            ]
        self._assert_content_equals(self.third_party_tests_path, expected)

        expected = [
            'Updating scripts/third_party_tests.yaml',
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_install_both_then_reinstall_none(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        self._get_log()
        self._install(None)
        expected = (
            self.initial_app_yaml[:9] +
            ['- name: endpoints\n',  # Note that AE lib requirement stays.
             '  version: "1.0"\n',] +
            self.initial_app_yaml[9:]
            )
        self._assert_content_equals(self.app_yaml_path, expected)
        self.assertFalse(os.path.exists(self.third_party_tests_path))
        expected = [
            'Updating app.yaml',
            'You should change this from its default',
            ]
        self._expect_logs(expected)

    def test_appengine_config(self):
        self._install('--targets=foo@%s,bar@%s' % (self.foo_dir, self.bar_dir))
        yaml = yaml_files.AppYamlFile(self.app_yaml_path)
        os.environ.update(yaml.get_all_env())

        # Touch into place the hard-coded expected set of libs so we don't
        # get spurious errors.
        for lib in appengine_config.ALL_LIBS:
            with open(lib.file_path, 'w'):
                pass

        # Just looking for no crash.
        appengine_config._import_and_enable_modules('GCB_THIRD_PARTY_MODULES',
                                                    reraise=True)
        appengine_config.gcb_init_third_party()


text:
package io.seldon.general;

import io.seldon.api.resource.ResourceBean;

public class LinkType extends ResourceBean {

	/**
	 * is there a reason this shouldn't act like a bean?
	 */
	private Integer typeId;
	private String name;
	private Double defValue;

	public LinkType() {}

	public LinkType(Integer typeId, String name,Double defValue) {
		super();
		this.typeId = typeId;
		this.name = name;
		this.defValue = defValue;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getDefValue() {
		return defValue;
	}

	public void setDefValue(Double defValue) {
		this.defValue = defValue;
	}

	@Override
	public String toKey() {
		return typeId.toString();
	}

}


text:
<!doctype html>
<html>
<head>
<title>Test'em</title>
<link rel="stylesheet" href="/testem/buster-test.css">
<script src="/testem/buster-test.js"></script>
<script src="/testem.js"></script>
{{#scripts}}<script src="{{src}}"{{#attrs}} {{&.}} {{/attrs}}></script>{{/scripts}}
</head>
<body></body>
</html>


text:
<?php
namespace ParseApp;

use Parse\ParseClient;
use Parse\ParseQuery;
use Parse\ParseObject;
use Parse\ParseUser;
use Parse\ParseException;
use Parse\ParseFile;
use Cocur\Slugify\Slugify;

class Albums {

	function __construct(){
		ParseClient::initialize(
		      PARSE_APP_ID,
		      PARSE_REST_KEY,
		      PARSE_MASTER_KEY
		    );
	}

	public function createUser($params){
		$user = new ParseUser();
		$user->setUsername($params['login']);
		$user->setPassword($params['passwd']);
		try {
		  	$user->signUp();
		} catch (ParseException $ex) {
			die("Exception: " . $ex->getMessage());
		}
		return $user;
	}

	public function loginUser($params){
		try {
			$user = ParseUser::Login($params['login'], $params['passwd']);
			return $user;
		} catch (ParseException $ex) {
			die("Exception: " . $ex->getMessage());
		}
	}

	public function logoutUser($params){
		ParseUser::logOut();
	}

	public function createAlbum($name){
		if ($this->albumExists($name)){
			throw new \Exception("An album with this name already exists. Please choose another");
		}

		$user = ParseUser::getCurrentUser();
		// Make a new album
		$album = new ParseObject("Albums");
		$album->set("name", $name);
		$album->set("user", $user);
		$album->save();
		unset($_SESSION['albums']);
		return $album;
	}
	public function getAlbums(){

		 // This is really **bad** (bad practice example)
		// I am nuking the whole albums object and getting a new one
		// Better would be just clear the session/cache for that particular album
		if (!$_REQUEST['refresh'] == 1) {
			if (!empty($_SESSION['albums'])){
				return $_SESSION['albums'];
			}
		}
		$user = ParseUser::getCurrentUser();

		// Find all albums by the current user
		$query = new ParseQuery("Albums");
		$query->equalTo("user", $user);
		$albums = $query->find();
		$_SESSION['albums'] = array();
		foreach($albums as $album){
			$_SESSION['albums'][$album->getObjectId()] = $album;
		}
		return $albums;

	}

	function addPicture($album_id, $path){
		print "Writting to $album_id<br>";
		$query = new ParseQuery("Albums");
	  	$album = $query->get($album_id);
		$filedata = $this->addFile($path);

		// This is a hack
		// 1. Get all pictures of the album (json_encoded string)
		// 2. json_decode it to get an array
		// 3. Add a new picture to the array
		// 4. json_encode it to a string
		// 5. set is as a string to the field called pictures

		$pictures = $album->get("pictures");
		if (!empty($pictures)){
			$aPictures = json_decode($pictures);
		}
		$aPictures[] = $filedata;
		$pictures = json_encode($aPictures);

		$album->set("pictures", $pictures);

		$album->save();
	}

	public function addFile($path){
		// Get the filename and slugify it (remove all characters that may cause problems)
		$pathinfo = pathinfo($path);
		$slugger = new Slugify();
		$filename = $slugger->slugify($pathinfo['filename']) . '.' . strtolower($pathinfo['extension']);
		$file = ParseFile::createFromFile($path, $filename);
		$file->save();

		// Put the file in the bucket and return just some metaData
		$bucket = new ParseObject("Buckets");
		$bucket->set("name", base64_encode($file->getName()));
		$bucket->set("filename", $filename);
		$bucket->set("url", $file->getURL());
		$bucket->set("file", $file);
		$bucket->save();

		$metaData = []; //
		$metaData['bucket_id'] = $bucket->getObjectId();
		$metaData['name'] = $bucket->get("filename");
		$metaData['url'] = $bucket->get("url");

		return $metaData;

	}

	private function albumExists($name){
		$user = ParseUser::getCurrentUser();

		$query = new ParseQuery("Albums");
		$query->equalTo("user", $user);
		$query->equalTo("name", $name);

		$object = $query->first();
		if (is_object($object)){
			return true;
		}
		return false;

	}

}


text:
// Copyright (c) Alex Ghiondea. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Xunit;

namespace SudokuSolverLib.Tests
{
    public class SudokuPuzzles
    {
        [Fact]
        public void CreatePuzzle_NoHints()
        {
            var grid = SudokuPuzzle.Create(2, 2, 0);

            Assert.Equal(true, grid.SolveGrid());
            CheckHintsCount(grid, 0);
        }

        [Fact]
        public void CreatePuzzle_1x1_1hint()
        {
            var grid = SudokuPuzzle.Create(1, 1, 1);

            Assert.Equal(true, grid.SolveGrid());
            CheckHintsCount(grid, 1);
        }

        [Fact]
        public void CreatePuzzle_1x1_0hint()
        {
            var grid = SudokuPuzzle.Create(1, 1, 0);

            Assert.Equal(true, grid.SolveGrid());
            CheckHintsCount(grid, 0);
        }

        [Fact]
        public void CreatePuzzle_SomeHints()
        {
            var grid = SudokuPuzzle.Create(2, 2, 5);

            Assert.Equal(true, grid.SolveGrid());
            CheckHintsCount(grid, 5);
        }

        [Fact]
        public void CreatePuzzle_NonSquare()
        {
            var grid = SudokuPuzzle.Create(1, 3, 0);

            Assert.Equal(true, grid.SolveGrid());
            CheckHintsCount(grid, 0);
        }

        private void CheckHintsCount(SudokuPuzzle grid, int requestedCount)
        {
            int count = 0;
            foreach (var node in grid.GetNodes())
            {
                if (node.PartOfPuzzle)
                {
                    count++;
                }
            }
            Assert.Equal(count, requestedCount);
        }
    }
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using UnityEngine.Assertions;
namespace GGM.Utils
{
    public static class ReflectionHelper
    {
        /// <summary>
        /// 대상 Field가 SubComponent인지 판별한다.
        /// </summary>
        /// <param name="info">검사될 대상 FieldInfo.</param>
        /// <param name="checkSerialized">Serialized 판별 여부, false경우인지 Serialized판별을 하지 않음.</param>
        /// <returns>SubComponent인가 판별, 만약 checkSerialized가 True인 경우 Serialized인지도 판별.</returns>
        public static bool CheckSubComponent(this FieldInfo info, bool checkSerialized)
        {
            bool isSubComponent = info.ReflectedType.IsSubclassOf(typeof(GGM.Core.SubComponent));
            if (isSubComponent && (checkSerialized == false))
                return true;

            bool isPublic = info.IsPublic;
            var serializedField = info.GetCustomAttributes(true).FirstOrDefault(attr => attr is UnityEngine.SerializeField);
            if (isPublic || serializedField != null)
                return true;

            return false;

        }
    }
}


text:
<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Spencer Dixon</title>
  <link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon">
  <link rel="stylesheet" href="/assets/css/style.css" type="text/css">
  <link rel="stylesheet" href="/assets/css/markdown_styles.css" type="text/css">
  <link rel="stylesheet" href="/assets/css/syntax.css" type="text/css">

Spencer Dixon

Contract Ruby Developer

    Home

    Archive

    Github

    LinkedIn

    Twitter

Why accuracy isn't accurate

05 Dec 2017

When it comes to measuring how well our machine learning models do, there's one metric we tend to reach for first; accuracy.

Accuracy can be thought of as the percentage of correct guesses out of our total number of things we're guessing…

total_things = 100
correct_guesses = 70

accuracy_percentage = (correct_guesses / total_guesses) * 100
# Our accuracy is 70%

But there's a huge blind spot with accuracy as a single metric. Accuracy alone just looks at our correct guesses, but what if those were just chance? What if we had a classifier that just randomly guessed and as a result, it guessed 70 of our 100 examples correctly.

Precision vs Recall

We need to be skeptical about accuracy, and our correct guesses on their own. If we were looking at images of cats and dogs and classifying them, how many of the images we guessed were cats, turned out to actually be cats? Did we miss any images that could've been classified as cats but weren't?

Although these two questions sound similar, take a minute to think them through and understand the difference…

How many classification attempts were actually correct? (Precision)

How much of the dataset did we classify correctly? (Recall)

These metrics are known as Precision and Recall and give us a better look at the performance of our model than just accuracy alone.

To understand these better we need to understand the four possible states our binary guess can be in…

True Positives (TP): the number of positive examples, labeled correctly as positive.

False Positives (FP): the number of negative examples, labeled incorrecly as positive.

True Negatives (TN): the number of negative examples, labeled correctly as negative.

False Negatives (FN): the number of positive examples, labeled incorrectly as negative.

F1 Score

Now our model has three metrics; accuracy, precision and recall. Which one do we optimise for? Do we sacrifice precision, if we can improve recall? Guessing just a single cat picture correctly would give us a high precision (because we can demonstrate that out of all the guesses we make, we're very precise in classifying correctly), but we would have a terrible recall (because we've only classified one image out of our dataset).

Luckily we can combine precision and recall into a single score to find the best of both worlds. We'll take the harmonic mean of the two scores (We use the harmonic mean as that's best for rates and ratios). The harmonic mean calculates the average of the two scores but it also takes into account how similar the two values are. This is called the F1 score…

precision = 0.84
recall = 0.72

f1_score = 2 * (precision * recall) / (precision + recall)

# Our F1 Score is 0.775

Summary

Accuracy alone is a bad metric to measure our predictions by. It leaves out vital context like how many did we guess correctly by chance? How many were mislabelled and how much of the dataset did we actually predict correctly? This is where precision and recall can help us. As we may want to make a trade off between precision and recall to get a better and more balanced model, we can use the F1 score to tell us which model is better.

Made with Prosecco, a Jekyll theme I made while drinking Prosecco.


text:
package null

import (
	"database/sql/driver"
	"encoding"
	"encoding/json"
	"fmt"
	"testing"

	"github.com/corestoreio/pkg/util/assert"
)

var (
	boolJSON     = []byte(`true`)
	nullBoolJSON = []byte(`{"Bool":true,"Valid":true}`)
)

var (
	_ fmt.GoStringer             = (*Bool)(nil)
	_ fmt.Stringer               = (*Bool)(nil)
	_ json.Marshaler             = (*Bool)(nil)
	_ json.Unmarshaler           = (*Bool)(nil)
	_ encoding.BinaryMarshaler   = (*Bool)(nil)
	_ encoding.BinaryUnmarshaler = (*Bool)(nil)
	_ encoding.TextMarshaler     = (*Bool)(nil)
	_ encoding.TextUnmarshaler   = (*Bool)(nil)
	_ driver.Valuer              = (*Bool)(nil)
)

func TestMakeNullBool(t *testing.T) {
	b := MakeBool(true)
	assertBool(t, b, "MakeBool()")
	assert.Exactly(t, "true", b.String())

	zero := MakeBool(false)
	if !zero.Valid {
		t.Error("MakeBool(false)", "is invalid, but should be valid")
	}
	assert.Exactly(t, "false", zero.String())
	assert.Exactly(t, "null", Bool{}.String())
}

func TestNullBool_UnmarshalJSON(t *testing.T) {
	var b Bool
	err := json.Unmarshal(boolJSON, &b)
	maybePanic(err)
	assertBool(t, b, "bool json")

	var nb Bool
	err = json.Unmarshal(nullBoolJSON, &nb)
	maybePanic(err)
	assertBool(t, nb, "sq.Bool json")

	var null Bool
	err = json.Unmarshal(nullJSON, &null)
	maybePanic(err)
	assertNullBool(t, null, "null json")

	var badType Bool
	err = json.Unmarshal(intJSON, &badType)
	if err == nil {
		panic("err should not be nil")
	}
	assertNullBool(t, badType, "wrong type json")

	var invalid Bool
	err = invalid.UnmarshalJSON(invalidJSON)
	if _, ok := err.(*json.SyntaxError); !ok {
		t.Errorf("expected json.SyntaxError, not %T", err)
	}
}

func TestNullBool_UnmarshalText(t *testing.T) {
	var b Bool
	err := b.UnmarshalText([]byte("true"))
	maybePanic(err)
	assertBool(t, b, "UnmarshalText() bool")

	var zero Bool
	err = zero.UnmarshalText([]byte("false"))
	maybePanic(err)
	assertFalseBool(t, zero, "UnmarshalText() false")

	var blank Bool
	err = blank.UnmarshalText([]byte(""))
	maybePanic(err)
	assertNullBool(t, blank, "UnmarshalText() empty bool")

	var null Bool
	err = null.UnmarshalText([]byte(sqlStrNullLC))
	maybePanic(err)
	assertNullBool(t, null, `UnmarshalText() "null"`)

	var invalid Bool
	err = invalid.UnmarshalText([]byte(":D"))
	if err == nil {
		panic("err should not be nil")
	}
	assertNullBool(t, invalid, "invalid json")
}

func TestNullBool_JsonMarshal(t *testing.T) {
	b := MakeBool(true)
	data, err := json.Marshal(b)
	maybePanic(err)
	assertJSONEquals(t, data, "true", "non-empty json marshal")

	zero := MakeBool(false)
	data, err = json.Marshal(zero)
	maybePanic(err)
	assertJSONEquals(t, data, "false", "zero json marshal")

	// invalid values should be encoded as null
	null := Bool{}
	data, err = json.Marshal(null)
	maybePanic(err)
	assertJSONEquals(t, data, sqlStrNullLC, "null json marshal")
}

func TestNullBool_MarshalText(t *testing.T) {
	b := MakeBool(true)
	data, err := b.MarshalText()
	maybePanic(err)
	assertJSONEquals(t, data, "true", "non-empty text marshal")

	zero := MakeBool(false)
	data, err = zero.MarshalText()
	maybePanic(err)
	assertJSONEquals(t, data, "false", "zero text marshal")

	// invalid values should be encoded as null
	var null Bool
	data, err = null.MarshalText()
	maybePanic(err)
	assertJSONEquals(t, data, "", "null text marshal")
}

func TestNullBool_BinaryEncoding(t *testing.T) {
	runner := func(b Bool, want []byte) func(*testing.T) {
		return func(t *testing.T) {
			data, err := b.MarshalBinary()
			assert.NoError(t, err)
			assert.Exactly(t, want, data, "MarshalBinary")
			data, err = b.Marshal()
			assert.NoError(t, err)
			assert.Exactly(t, want, data, "Marshal")

			var decoded Bool
			assert.NoError(t, decoded.UnmarshalBinary(data), "UnmarshalBinary")
			assert.Exactly(t, b, decoded)
		}
	}
	t.Run("true", runner(MakeBool(true), []byte("\b\x01\x10\x01")))
	t.Run("false", runner(MakeBool(false), []byte("\x10\x01")))
	t.Run("null", runner(Bool{}, []byte("")))
}

func TestNullBool_BinaryDecoding(t *testing.T) {
	runner := func(data []byte, want Bool) func(*testing.T) {
		return func(t *testing.T) {
			var have Bool
			assert.NoError(t, have.UnmarshalBinary(data), "UnmarshalBinary")
			assert.Exactly(t, want, have, "UnmarshalBinary")
			assert.NoError(t, have.Unmarshal(data), "Unmarshal")
			assert.Exactly(t, want, have, "Unmarshal")
		}
	}
	t.Run("true", runner([]byte("\b\x01\x10\x01"), MakeBool(true)))
	t.Run("false", runner([]byte("\x10\x01"), MakeBool(false)))
	t.Run("null", runner([]byte(""), Bool{}))
	t.Run("junk", func(t *testing.T) {
		var have Bool
		err := have.UnmarshalBinary([]byte{2, 1, 3})
		assert.EqualError(t, err, "proto: Bool: illegal tag 0 (wire type 2)")
	})
}

func TestBoolPointer(t *testing.T) {
	b := MakeBool(true)
	ptr := b.Ptr()
	if !*ptr {
		t.Errorf("bad %s bool: %#v ≠ %v\n", "pointer", ptr, true)
	}

	var null Bool
	ptr = null.Ptr()
	if ptr != nil {
		t.Errorf("bad %s bool: %#v ≠ %s\n", "nil pointer", ptr, "nil")
	}
}

func TestBoolIsZero(t *testing.T) {
	b := MakeBool(true)
	if b.IsZero() {
		t.Errorf("IsZero() should be false")
	}

	var null Bool
	if !null.IsZero() {
		t.Errorf("IsZero() should be true")
	}

	zero := MakeBool(false)
	if zero.IsZero() {
		t.Errorf("IsZero() should be false")
	}
}

func TestBoolSetPtr(t *testing.T) {
	var change Bool
	v := true
	change.SetPtr(&v)
	assertBool(t, change, "SetPtr()")
}

func TestBoolSetValid(t *testing.T) {
	var change Bool
	assertNullBool(t, change, "SetValid()")
	change.SetValid(true)
	assertBool(t, change, "SetValid()")
}

func TestBoolScan(t *testing.T) {
	var b Bool
	err := b.Scan(true)
	maybePanic(err)
	assertBool(t, b, "scanned bool")

	var null Bool
	err = null.Scan(nil)
	maybePanic(err)
	assertNullBool(t, null, "scanned null")
}

func assertBool(t *testing.T, b Bool, from string) {
	if !b.Bool {
		t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, true)
	}
	if !b.Valid {
		t.Error(from, "is invalid, but should be valid")
	}
}

func assertFalseBool(t *testing.T, b Bool, from string) {
	if b.Bool {
		t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, false)
	}
	if !b.Valid {
		t.Error(from, "is invalid, but should be valid")
	}
}

func assertNullBool(t *testing.T, b Bool, from string) {
	if b.Valid {
		t.Error(from, "is valid, but should be invalid")
	}
}

func TestNewNullBool(t *testing.T) {
	assert.Equal(t, true, MakeBool(true).Bool)
	assert.True(t, MakeBool(true).Valid)
	assert.True(t, MakeBool(false).Valid)
	v, err := MakeBool(true).Value()
	assert.NoError(t, err)
	assert.Equal(t, true, v)
}


text:
package boofcv.alg.geo.robust;

import boofcv.alg.geo.MultiViewOps;
import boofcv.alg.geo.PerspectiveOps;
import boofcv.struct.geo.AssociatedPair;
import georegression.geometry.RotationMatrixGenerator;
import georegression.struct.homography.Homography2D_F64;
import georegression.struct.homography.UtilHomography;
import georegression.struct.point.Point2D_F64;
import georegression.struct.point.Vector3D_F64;
import georegression.transform.homography.HomographyPointOps_F64;
import org.ddogleg.fitting.modelset.DistanceFromModel;
import org.ejml.data.DenseMatrix64F;

import java.util.Random;

/**
 * @author Peter Abeles
 */
public class TestDistanceHomographyPixelSq extends StandardDistanceTest<Homography2D_F64, AssociatedPair> {

	Random rand = new Random(234);

	DenseMatrix64F K = new DenseMatrix64F(3,3,true,200,1,120,0,250,150,0,0,1);

	DenseMatrix64F H,H_pixel;

	@Override
	public DistanceFromModel<Homography2D_F64, AssociatedPair> create() {
		DistanceHomographyPixelSq alg = new DistanceHomographyPixelSq();
		alg.setIntrinsic(K.get(0,0),K.get(1,1),K.get(0,1));
		return alg;
	}

	@Override
	public Homography2D_F64 createRandomModel() {

		double rotX = rand.nextGaussian();
		double rotY = rand.nextGaussian();
		double rotZ = rand.nextGaussian();

		DenseMatrix64F R = RotationMatrixGenerator.eulerXYZ(rotX,rotY,rotZ, null);
		Vector3D_F64 T = new Vector3D_F64(0.2,-0.5,3);
		Vector3D_F64 N = new Vector3D_F64(-0.5,1,3);

		// compute the Homography in normalized image coordinates and pixel coordinates
		H = MultiViewOps.createHomography(R, T, 1.0, N);
		H_pixel = MultiViewOps.createHomography(R,T,1.0,N,K);

		Homography2D_F64 h = new Homography2D_F64();
		UtilHomography.convert(H, h);

		return h;
	}

	@Override
	public AssociatedPair createRandomData() {
		Point2D_F64 p1 = new Point2D_F64(rand.nextGaussian(),rand.nextGaussian());
		Point2D_F64 p2 = new Point2D_F64(rand.nextGaussian(),rand.nextGaussian());

		return new AssociatedPair(p1,p2,false);
	}

	@Override
	public double distance(Homography2D_F64 h, AssociatedPair associatedPair) {

		Point2D_F64 result = new Point2D_F64();

		Point2D_F64 pixel1 = new Point2D_F64();
		Point2D_F64 pixel2 = new Point2D_F64();

		// convert points into pixel coordinates
		PerspectiveOps.convertNormToPixel(K,associatedPair.p1,pixel1);
		PerspectiveOps.convertNormToPixel(K,associatedPair.p2,pixel2);

		// compute error in pixels, which is what it should be in
		Homography2D_F64 h_pixel = new Homography2D_F64();
		UtilHomography.convert(H_pixel,h_pixel);

		HomographyPointOps_F64.transform(h_pixel, pixel1, result);
		return result.distance2(pixel2);
	}
}