Skip to content

Instantly share code, notes, and snippets.

@barahilia
barahilia / json-pattern-matcher
Last active Aug 29, 2015
JSON pattern matching
View json-pattern-matcher
Allows match JSON objects by pattern. Intended for APIs comparison and verification.
E.g.:
compare(
{ a: 1, b: ['x', 'y'] }, // pattern
{ a: 42, b: ['first', 'second', 'any', 'last'] } // object for check
);
@barahilia
barahilia / tdd-trees-comp-c++
Last active Aug 29, 2015
TDD for trees comparison in C++
View tdd-trees-comp-c++
Build with: `g++ test.cpp && ./a.out`
For editing two files in `vim` run command `:vsplit tree.h`.
http://www.cs.swarthmore.edu/help/vim/windows.html
@barahilia
barahilia / highlight.md
Created Oct 12, 2014
Kramdown yellow highlight
View highlight.md
Fill details here...
@barahilia
barahilia / points-compression.md
Last active Aug 29, 2015
Points compression algo
View points-compression.md
View yield-profiling.md

Demonstration of phenomenon, when caller cumulative time can be less than the callee, given it's the only caller. This may happen if callee is generator and caller returns callee object as is. Then profiler sees a lot of time spent in callee and almost no time at caller.

View fractal-hedge-maze-solution.py
"""Find exit from the fractal maze.
See http://puzzling.stackexchange.com/questions/37675/alice-and-the-fractal-hedge-maze
"""
from unittest import TestCase
from Queue import PriorityQueue
from collections import defaultdict
from itertools import count, product
@barahilia
barahilia / tamper.py
Last active Jul 28, 2016
Tampering class method in python (temporarily replacing/mocking/faking for unit test)
View tamper.py
import functools
def tamper(target, attr_name, mock_obj):
def decor(func):
@functools.wraps(func)
def worker(*args, **kwargs):
backup = getattr(target, attr_name)
try:
setattr(target, attr_name, mock_obj)
View discover-unit-tests.py
from unittest import TestLoader, TextTestRunner
def discover_and_run(verbose=False, names=None):
# names is None or a list [name, ...]
# name is test_suite[.class[.name]]
verbosity = 2 if verbose else 1
loader = TestLoader()
if names:
View list-unit-tests.py
from unittest import TestSuite
def print_tests(tests):
if isinstance(tests, TestSuite):
for child in tests:
print_tests(child)
else:
print tests.id() # package.module.TestClass.test_name
@barahilia
barahilia / run-jasmine.js
Last active Mar 2, 2017 — forked from dlidstrom/run-jasmine.js
Runs Jasmine tests using PhantomJS. Adapted for use within TeamCity..Compatible with Jasmine 2.0.
View run-jasmine.js
var system = require('system'),
env = system.env;
/**
* Wait until the test condition is true or a timeout occurs. Useful for waiting
* on a server response or for a ui change (fadeIn, etc.) to occur.
*
* @param testFx javascript condition that evaluates to a boolean,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.