mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
Javascript Agent Hash and Array memory (#1524)
* Javascript Agent Hash and Array memory * add specs
This commit is contained in:
parent
90f5de5fe3
commit
4b011a1370
2 changed files with 47 additions and 2 deletions
|
@ -220,9 +220,9 @@ module Agents
|
|||
end
|
||||
|
||||
def clean_nans(input)
|
||||
if input.is_a?(Array)
|
||||
if input.is_a?(V8::Array)
|
||||
input.map {|v| clean_nans(v) }
|
||||
elsif input.is_a?(Hash)
|
||||
elsif input.is_a?(V8::Object)
|
||||
input.inject({}) { |m, (k, v)| m[k] = clean_nans(v); m }
|
||||
elsif input.is_a?(Float) && input.nan?
|
||||
'NaN'
|
||||
|
|
|
@ -132,7 +132,9 @@ describe Agents::JavaScriptAgent do
|
|||
expect(AgentLog.last.message).to match(/oh no/)
|
||||
expect(AgentLog.last.level).to eq(4)
|
||||
end
|
||||
end
|
||||
|
||||
describe "getMemory" do
|
||||
it "won't store NaNs" do
|
||||
@agent.options['code'] = 'Agent.check = function() { this.memory("foo", NaN); };'
|
||||
@agent.save!
|
||||
|
@ -141,6 +143,49 @@ describe Agents::JavaScriptAgent do
|
|||
@agent.save!
|
||||
expect { @agent.reload.memory }.not_to raise_error
|
||||
end
|
||||
|
||||
it "it stores an Array" do
|
||||
@agent.options['code'] = 'Agent.check = function() {
|
||||
var arr = [1,2];
|
||||
this.memory("foo", arr);
|
||||
};'
|
||||
@agent.save!
|
||||
@agent.check
|
||||
expect(@agent.memory['foo']).to eq([1,2])
|
||||
@agent.save!
|
||||
expect { @agent.reload.memory }.not_to raise_error
|
||||
end
|
||||
|
||||
it "it stores a Hash" do
|
||||
@agent.options['code'] = 'Agent.check = function() {
|
||||
var obj = {};
|
||||
obj["one"] = 1;
|
||||
obj["two"] = [1,2];
|
||||
this.memory("foo", obj);
|
||||
};'
|
||||
@agent.save!
|
||||
@agent.check
|
||||
expect(@agent.memory['foo']).to eq({"one"=>1, "two"=> [1,2]})
|
||||
@agent.save!
|
||||
expect { @agent.reload.memory }.not_to raise_error
|
||||
end
|
||||
|
||||
it "it stores a nested Hash" do
|
||||
@agent.options['code'] = 'Agent.check = function() {
|
||||
var u = {};
|
||||
u["one"] = 1;
|
||||
u["two"] = 2;
|
||||
var obj = {};
|
||||
obj["three"] = 3;
|
||||
obj["four"] = u;
|
||||
this.memory("foo", obj);
|
||||
};'
|
||||
@agent.save!
|
||||
@agent.check
|
||||
expect(@agent.memory['foo']).to eq({"three"=>3, "four"=>{"one"=>1, "two"=>2}})
|
||||
@agent.save!
|
||||
expect { @agent.reload.memory }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe "creating events" do
|
||||
|
|
Loading…
Add table
Reference in a new issue