Skip to content

Commit

Permalink
renamed fid -> protopatch within files & made specs pass
Browse files Browse the repository at this point in the history
  • Loading branch information
benatkin committed Oct 27, 2010
1 parent e802f4d commit 369f6a8
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 52 deletions.
4 changes: 2 additions & 2 deletions js/example_runner.html
Expand Up @@ -8,11 +8,11 @@
<script type="text/javascript" src="vendor/jasmine-0.10.0.js"></script>
<script type="text/javascript" src="vendor/TrivialReporter.js"></script>
<script type="text/javascript" src="vendor/consolex.js"></script>
<script type="text/javascript" src="lib/fid.js"></script>
<script type="text/javascript" src="lib/protopatch.js"></script>
<link rel="stylesheet" type="text/css" href="vendor/jasmine.css">

<script type="text/javascript">
jasmine.include('spec/fid_suite.js', true);
jasmine.include('spec/protopatch_spec.js', true);
</script>

<body>
Expand Down
4 changes: 2 additions & 2 deletions js/lib/protopatch.coffee
Expand Up @@ -28,7 +28,7 @@ class Patcher
p = null if _.size(p) == 0
p

class Fid
class ProtoPatch
@Patcher: Patcher
@_default_patcher: null

Expand All @@ -39,4 +39,4 @@ class Fid

@diff: (args...) -> @default_patcher().diff(args...)

window.Fid = Fid
window.ProtoPatch = ProtoPatch
16 changes: 8 additions & 8 deletions js/lib/protopatch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions js/spec/protopatch_spec.coffee
Expand Up @@ -20,47 +20,47 @@ flat_patch = {
"pears" : {"-": 1}
}

describe 'Fid', () ->
describe 'ProtoPatch', () ->

describe 'patch', () ->

it 'returns the same when patching with null', () ->
expect(Fid.patch({}, null)).toEqual({})
expect(ProtoPatch.patch({}, null)).toEqual({})

it 'removes "-" item in patch', () ->
expect(Fid.patch({'apples': 1}, {'apples': {'-': 1}})).toEqual({})
expect(ProtoPatch.patch({'apples': 1}, {'apples': {'-': 1}})).toEqual({})

it 'adds "+" item in patch', () ->
expect(Fid.patch({}, {'bananas': {'+': 3}})).toEqual({'bananas': 3})
expect(ProtoPatch.patch({}, {'bananas': {'+': 3}})).toEqual({'bananas': 3})

it 'replaces "-" and "+" item in patch', () ->
expect(Fid.patch({'bananas': 5}, {'bananas': {'-': 5, '+': 3}}))
expect(ProtoPatch.patch({'bananas': 5}, {'bananas': {'-': 5, '+': 3}}))
.toEqual({'bananas': 3})

it 'leaves unchanged with nil patch', () ->
expect(Fid.patch({'bananas': 5}, null)).toEqual({'bananas': 5})
expect(ProtoPatch.patch({'bananas': 5}, null)).toEqual({'bananas': 5})

it 'correctly patches with diff from README', () ->
expect(Fid.patch(flat_doc1, flat_patch)).toEqual(flat_doc2)
expect(ProtoPatch.patch(flat_doc1, flat_patch)).toEqual(flat_doc2)

describe 'diff', () ->

it 'returns null for two empty objects', () ->
expect(Fid.diff({}, {})).toBeNull()
expect(ProtoPatch.diff({}, {})).toBeNull()

it 'returns "-" for item not in 2nd object', () ->
expect(Fid.diff({'apples': 1}, {})).toEqual({'apples': {'-': 1}})
expect(ProtoPatch.diff({'apples': 1}, {})).toEqual({'apples': {'-': 1}})

it 'returns "+" for item not in 1st object', () ->
expect(Fid.diff({}, {'bananas': 3})).toEqual({'bananas': {'+': 3}})
expect(ProtoPatch.diff({}, {'bananas': 3})).toEqual({'bananas': {'+': 3}})

it 'returns "-" and "+" for changed item', ->
expect(Fid.diff({'bananas': 5}, {'bananas': 3}))
expect(ProtoPatch.diff({'bananas': 5}, {'bananas': 3}))
.toEqual({'bananas': {'-': 5, '+': 3}})

it 'omits unchanged item', ->
expect(Fid.diff({'bananas': 5}, {'bananas': 5})).toEqual()
expect(ProtoPatch.diff({'bananas': 5}, {'bananas': 5})).toEqual()

it 'correctly returns diff from README', ->
expect(Fid.diff(flat_doc1, flat_doc2)).toEqual(flat_patch)
expect(ProtoPatch.diff(flat_doc1, flat_doc2)).toEqual(flat_patch)

133 changes: 133 additions & 0 deletions js/spec/protopatch_spec.js
@@ -0,0 +1,133 @@
(function() {
var flat_doc1, flat_doc2, flat_patch;
flat_doc1 = {
"pears": 1,
"apples": {
"red": 2,
"green": 1
},
"bananas": 5,
"mangos": 2
};
flat_doc2 = {
"apples": {
"golden": 1
},
"bananas": 3,
"oranges": 6,
"mangos": 2
};
flat_patch = {
"apples": {
"+": {
"golden": 1
},
"-": {
"red": 2,
"green": 1
}
},
"bananas": {
"+": 3,
"-": 5
},
"oranges": {
"+": 6
},
"pears": {
"-": 1
}
};
describe('ProtoPatch', function() {
describe('patch', function() {
it('returns the same when patching with null', function() {
return expect(ProtoPatch.patch({}, null)).toEqual({});
});
it('removes "-" item in patch', function() {
return expect(ProtoPatch.patch({
'apples': 1
}, {
'apples': {
'-': 1
}
})).toEqual({});
});
it('adds "+" item in patch', function() {
return expect(ProtoPatch.patch({}, {
'bananas': {
'+': 3
}
})).toEqual({
'bananas': 3
});
});
it('replaces "-" and "+" item in patch', function() {
return expect(ProtoPatch.patch({
'bananas': 5
}, {
'bananas': {
'-': 5,
'+': 3
}
})).toEqual({
'bananas': 3
});
});
it('leaves unchanged with nil patch', function() {
return expect(ProtoPatch.patch({
'bananas': 5
}, null)).toEqual({
'bananas': 5
});
});
return it('correctly patches with diff from README', function() {
return expect(ProtoPatch.patch(flat_doc1, flat_patch)).toEqual(flat_doc2);
});
});
return describe('diff', function() {
it('returns null for two empty objects', function() {
return expect(ProtoPatch.diff({}, {})).toBeNull();
});
it('returns "-" for item not in 2nd object', function() {
return expect(ProtoPatch.diff({
'apples': 1
}, {})).toEqual({
'apples': {
'-': 1
}
});
});
it('returns "+" for item not in 1st object', function() {
return expect(ProtoPatch.diff({}, {
'bananas': 3
})).toEqual({
'bananas': {
'+': 3
}
});
});
it('returns "-" and "+" for changed item', function() {
return expect(ProtoPatch.diff({
'bananas': 5
}, {
'bananas': 3
})).toEqual({
'bananas': {
'-': 5,
'+': 3
}
});
});
it('omits unchanged item', function() {
return expect(ProtoPatch.diff({
'bananas': 5
}, {
'bananas': 5
})).toEqual();
});
return it('correctly returns diff from README', function() {
return expect(ProtoPatch.diff(flat_doc1, flat_doc2)).toEqual(flat_patch);
});
});
});
}).call(this);
4 changes: 2 additions & 2 deletions lib/protopatch.rb
@@ -1,4 +1,4 @@
module Fid
module ProtoPatch
class << self
attr_writer :default_patcher

Expand All @@ -13,5 +13,5 @@ def self.default_patcher
end
end

require 'fid/patcher'
require 'protopatch/patcher'

2 changes: 1 addition & 1 deletion lib/protopatch/patcher.rb
@@ -1,4 +1,4 @@
module Fid
module ProtoPatch
class Patcher
def initialize(opts={})
@opts = opts
Expand Down
4 changes: 2 additions & 2 deletions lib/protopatch/version.rb
@@ -1,3 +1,3 @@
module Fid
module ProtoPatch
VERSION = "0.0.0"
end
end
10 changes: 5 additions & 5 deletions protopatch.gemspec
@@ -1,19 +1,19 @@
# -*- encoding: utf-8 -*-
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'fid/version'
require 'protopatch/version'

Gem::Specification.new do |s|
s.name = "fid"
s.version = Fid::VERSION
s.name = "protopatch"
s.version = ProtoPatch::VERSION
s.platform = Gem::Platform::RUBY
s.authors = []
s.email = []
s.homepage = "http://rubygems.org/gems/fid"
s.homepage = "http://rubygems.org/gems/protopatch"
s.summary = "TODO: Write a gem summary"
s.description = "TODO: Write a gem description"

s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "fid"
s.rubyforge_project = "protopatch"

s.add_development_dependency "bundler", ">= 1.0.0.rc.5"
s.add_development_dependency "rspec", ">= 1.3.0"
Expand Down
32 changes: 16 additions & 16 deletions spec/protopatch_spec.rb
Expand Up @@ -22,57 +22,57 @@
"pears" => {"-" => 1}
}

describe Fid, "#patch" do
describe ProtoPatch, "#patch" do
it 'returns the same when patching with nil' do
Fid.patch({}, nil).should == {}
ProtoPatch.patch({}, nil).should == {}
end

it 'removes "-" item in patch' do
Fid.patch({'apples' => 1}, {'-' => {'apples' => 1}, '+' => {}}).should == {}
ProtoPatch.patch({'apples' => 1}, {'-' => {'apples' => 1}, '+' => {}}).should == {}
end

it 'adds "+" item in patch' do
Fid.patch({}, {'-' => {}, '+' => {'bananas' => 3}}).should == {'bananas' => 3}
ProtoPatch.patch({}, {'-' => {}, '+' => {'bananas' => 3}}).should == {'bananas' => 3}
end

it 'replaces "-" and "+" item in patch' do
Fid.patch({'bananas' => 5}, {'bananas' => {'-' => 5, '+' => 3}})
.should == {'bananas' => 3}
ProtoPatch.patch({'bananas' => 5}, {'bananas' => {'-' => 5, '+' => 3}})
.should == {'bananas' => 3}
end

it 'leaves unchanged with nil patch' do
Fid.patch({'bananas' => 5}, nil).should == {'bananas' => 5}
ProtoPatch.patch({'bananas' => 5}, nil).should == {'bananas' => 5}
end

it 'correctly patches with diff from README' do
Fid.patch(flat_doc1, flat_patch).should == flat_doc2
ProtoPatch.patch(flat_doc1, flat_patch).should == flat_doc2
end
end

describe Fid, "#diff" do
describe ProtoPatch, "#diff" do
it 'returns nil for two empty objects' do
Fid.diff({}, {}).should be_nil
ProtoPatch.diff({}, {}).should be_nil
end

it 'returns "-" for item not in 2nd object' do
Fid.diff({'apples' => 1}, {}).should == {'-' => {'apples' => 1}, '+' => {}}
ProtoPatch.diff({'apples' => 1}, {}).should == {'-' => {'apples' => 1}, '+' => {}}
end

it 'returns "+" for item not in 1st object' do
Fid.diff({}, {'bananas' => 3}).should == {'-' => {}, '+' => {'bananas' => 3}}
ProtoPatch.diff({}, {'bananas' => 3}).should == {'-' => {}, '+' => {'bananas' => 3}}
end

it 'returns "-" and "+" for changed item' do
Fid.diff({'bananas' => 5}, {'bananas' => 3})
.should == {'bananas' => {'-' => 5, '+' => 3}}
ProtoPatch.diff({'bananas' => 5}, {'bananas' => 3})
.should == {'bananas' => {'-' => 5, '+' => 3}}
end

it 'omits unchanged item' do
Fid.diff({'bananas' => 5}, {'bananas' => 5}).should be_nil
ProtoPatch.diff({'bananas' => 5}, {'bananas' => 5}).should be_nil
end

it 'correctly returns diff from README' do
Fid.diff(flat_doc1, flat_doc2).should == flat_patch
ProtoPatch.diff(flat_doc1, flat_doc2).should == flat_patch
end
end

2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1 +1 @@
require 'fid'
require 'protopatch'

0 comments on commit 369f6a8

Please sign in to comment.