Skip to content

Instantly share code, notes, and snippets.

@ahirschberg
ahirschberg / ch_pos_test.js
Last active August 29, 2015 14:20
Excerpt from the BookPlayerPageGenerator tests
it('updates chapter position property to match audio player position', function () {
bppg.generatePage({book: BOOK_OBJECT, chapter: chapterObjInstance});
chapterObjInstance.position = -1 // set the position to a nonzero value
var dom_ele = $('#audioSource').trigger('timeupdate');
expect(chapterObjInstance.position).equal(0);
});
@ahirschberg
ahirschberg / sinon_expect_fail.js
Last active August 29, 2015 14:20
Sinon mocking strings and integers
// FAILING TEST
describe('#test', function () {
it('tests args', function () {
storageMock.expects('test').once().withExactArgs(1234);
storageManager.otherFunctionThatCallsTest(chapter_obj.id); // should call #test with args: 1234
storageMock.verify();
});
});
/* Console Output:
* Unexpected call: test(1234)
describe('#generatePage()', function () {
it('generates download book button that sends #downloadBook to downloadManager object when clicked', function () {
// ...
});
});
LDX #$00 ; load #00 into x
LDY #$01
loop:
INX ; increment X
STX $0200 ; draw a pixel of color x to screen at top left corner
BCS done
BNE loop ; any way to loop without checking anything?
done:
BRK
@ahirschberg
ahirschberg / bookpagegenerator.js
Last active August 29, 2015 14:19
Abbreviated version of book player page generation tests
describe('BookPlayerPageGenerator()', function () {
var bppg, dlManager, dlManagerMock;
before(function () {
// ... (omitted) set up audio and button elements and append them to body ...
// Set up a dummy object with a stubbed BookDownloadManager interface
dlManager = { downloadBook: function (book_id, chapter_obj) {},
downloadChapter: function (book_obj) {} };
@ahirschberg
ahirschberg / function_scope_example.js
Last active August 29, 2015 14:18
Shows variable/function scope in JavaScript
function Foo(args) {
var private_variable = args.private_var;
this.public_variable = args.public_var;
this.public_method = function () { };
function private_method() { }
}
%w[a b c d].permutation.first(5).each {|i| p i} #=> same output as permutations_lazy1.rb
def permutations_lazy(list)
# return [list] if list.size == 1
en = Enumerator.new do |y|
if list.size == 1
# yield a new array with the list as its only element (instead of returning)
y.yield [list]
end
list.each do |locked|
puts "locking #{locked}"
@ahirschberg
ahirschberg / permutations_lazy2.rb
Last active August 29, 2015 14:18
Non-working lazy version of permutations.rb
def permutations_lazy(list)
# return [list] if list.size == 1
en = Enumerator.new do |y|
if list.size == 1
# yield instead of returning in the base case of recursion
y.yield list
end
list.each do |locked|
puts "locking #{locked}"
@ahirschberg
ahirschberg / permutations_lazy1.rb
Last active August 29, 2015 14:18
Working lazy version of permutations using yield statements
def permutations_lazy(list)
# recursion's base case: return, not yield (outside of enumerator)
return [list] if list.size == 1
en = Enumerator.new do |y|
list.each do |locked|
puts "locking #{locked}"
list_copy = list.dup
list_copy.delete locked
permutations_lazy(list_copy).each do |permutation|
puts "permutation #{permutation}, locked #{locked}"