Skip to content

Instantly share code, notes, and snippets.

@samleb
Created February 3, 2009 13:14
Show Gist options
  • Save samleb/57523 to your computer and use it in GitHub Desktop.
Save samleb/57523 to your computer and use it in GitHub Desktop.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe JavascriptObfuscator do
def obfuscate(js)
JavascriptObfuscator.obfuscate(js)
end
it "should rename properties starting with an underscore in accessing notation" do
obfuscate("object._aPrivateProperty").should == "object._a"
obfuscate("object .
_aPrivateProperty").should == "object .
_a"
end
it "should rename properties starting with an underscore in litteral notation" do
obfuscate("{ _aPrivateProperty: true }").should == "{ _a: true }"
obfuscate("{_aPrivateProperty:true}").should == "{_a:true}"
obfuscate("{
aPublicProperty: false,
_aPrivateProperty: true
}").should == "{
aPublicProperty: false,
_a: true
}"
end
it "should not rename variables starting with an underscore (as tools like YUI compressor already deal with it)" do
obfuscate("_aPrivateVariable").should == "_aPrivateVariable"
obfuscate("var _aPrivateVariable").should == "var _aPrivateVariable"
end
it "should not rename propeties starting with an underscore in bracket accessing notation" do
obfuscate('object["_aPrivateProperty"]').should == 'object["_aPrivateProperty"]'
end
it "should use different identifiers for different obfuscated properties" do
obfuscate("{ _first: 1, _second: 2, _third: 3 }").should == "{ _a: 1, _b: 2, _c: 3 }"
end
it "should use the same identifier for the same obfuscated property" do
obfuscate("var a = { _aPrivateProperty: true }; a._aPrivateProperty = false;").should ==
"var a = { _a: true }; a._a = false;"
end
it "should always use valid identifiers (i.e. containing valid characters)" do
# "o._a;o._aa;o._aaa;[...]"
js = (1..100).collect { |i| "o._#{'a' * i}" }.join(";")
obfuscate(js).should =~ /(o\._(\w+);){99}/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment