Merge pull request #1551 from cantino/jsagent_fix_memory

Fix `this.memory(key, falsy)` not working in JavaScriptAgent
This commit is contained in:
Akinori MUSHA 2016-06-20 12:48:44 +09:00 committed by GitHub
commit 371bfa34b7
2 changed files with 28 additions and 7 deletions

View file

@ -114,12 +114,9 @@ module Agents
context["getOptions"] = lambda { |a, x| interpolated.to_json }
context["doLog"] = lambda { |a, x| log x }
context["doError"] = lambda { |a, x| error x }
context["getMemory"] = lambda do |a, x, y|
if x && y
memory[x] = clean_nans(y)
else
memory.to_json
end
context["getMemory"] = lambda { |a| memory.to_json }
context["setMemory"] = lambda do |a, x, y|
memory[x] = clean_nans(y)
end
context["deleteKey"] = lambda { |a, x| memory.delete(x).to_json }
context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
@ -168,7 +165,7 @@ module Agents
Agent.memory = function(key, value) {
if (typeof(key) !== "undefined" && typeof(value) !== "undefined") {
getMemory(key, value);
setMemory(key, value);
} else if (typeof(key) !== "undefined") {
return JSON.parse(getMemory())[key];
} else {

View file

@ -186,6 +186,30 @@ describe Agents::JavaScriptAgent do
@agent.save!
expect { @agent.reload.memory }.not_to raise_error
end
it "it stores null" do
@agent.options['code'] = 'Agent.check = function() {
this.memory("foo", "test");
this.memory("foo", null);
};'
@agent.save!
@agent.check
expect(@agent.memory['foo']).to eq(nil)
@agent.save!
expect { @agent.reload.memory }.not_to raise_error
end
it "it stores false" do
@agent.options['code'] = 'Agent.check = function() {
this.memory("foo", "test");
this.memory("foo", false);
};'
@agent.save!
@agent.check
expect(@agent.memory['foo']).to eq(false)
@agent.save!
expect { @agent.reload.memory }.not_to raise_error
end
end
describe "deleteKey" do