AmberVM 0.0.19
Sign up to get free protection for your applications and to get access to all the features.
- data/README +38 -0
- data/bin/ambervm +278 -0
- data/lib/amber/acts_as_rvm_type.rb +157 -0
- data/lib/amber/classes/association.rb +36 -0
- data/lib/amber/classes/block.rb +52 -0
- data/lib/amber/classes/boolean.rb +40 -0
- data/lib/amber/classes/class.rb +50 -0
- data/lib/amber/classes/error.rb +22 -0
- data/lib/amber/classes/list.rb +96 -0
- data/lib/amber/classes/null.rb +35 -0
- data/lib/amber/classes/number.rb +95 -0
- data/lib/amber/classes/object.rb +56 -0
- data/lib/amber/classes/string.rb +79 -0
- data/lib/amber/classes.rb +113 -0
- data/lib/amber/environment.rb +251 -0
- data/lib/amber/fukubukuro/ecma_core.rb +409 -0
- data/lib/amber/fukubukuro.rb +866 -0
- data/lib/amber/functions/all.rb +3 -0
- data/lib/amber/functions/array/append.rb +50 -0
- data/lib/amber/functions/array/at.rb +50 -0
- data/lib/amber/functions/array/set_at.rb +50 -0
- data/lib/amber/functions/array.rb +30 -0
- data/lib/amber/functions/association/assoc_get.rb +55 -0
- data/lib/amber/functions/association/assoc_set.rb +56 -0
- data/lib/amber/functions/bitwise/bitwise_and.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_not.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_or.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_xor.rb +41 -0
- data/lib/amber/functions/bitwise.rb +3 -0
- data/lib/amber/functions/collection/get.rb +66 -0
- data/lib/amber/functions/collection/set.rb +67 -0
- data/lib/amber/functions/collection/size.rb +54 -0
- data/lib/amber/functions/general/cmp.rb +43 -0
- data/lib/amber/functions/general/eq.rb +45 -0
- data/lib/amber/functions/general/gt.rb +45 -0
- data/lib/amber/functions/general/gte.rb +45 -0
- data/lib/amber/functions/general/lt.rb +45 -0
- data/lib/amber/functions/general/lte.rb +45 -0
- data/lib/amber/functions/general/neq.rb +45 -0
- data/lib/amber/functions/general/type.rb +43 -0
- data/lib/amber/functions/general.rb +3 -0
- data/lib/amber/functions/io/print.rb +45 -0
- data/lib/amber/functions/io.rb +3 -0
- data/lib/amber/functions/list/align.rb +73 -0
- data/lib/amber/functions/list/join.rb +45 -0
- data/lib/amber/functions/list/map.rb +58 -0
- data/lib/amber/functions/list/split.rb +55 -0
- data/lib/amber/functions/list.rb +3 -0
- data/lib/amber/functions/logic/and.rb +55 -0
- data/lib/amber/functions/logic/not.rb +40 -0
- data/lib/amber/functions/logic/or.rb +50 -0
- data/lib/amber/functions/logic.rb +3 -0
- data/lib/amber/functions/math/abs.rb +39 -0
- data/lib/amber/functions/math/acos.rb +39 -0
- data/lib/amber/functions/math/add.rb +40 -0
- data/lib/amber/functions/math/asin.rb +39 -0
- data/lib/amber/functions/math/atan.rb +39 -0
- data/lib/amber/functions/math/ceil.rb +39 -0
- data/lib/amber/functions/math/cos.rb +39 -0
- data/lib/amber/functions/math/dec.rb +39 -0
- data/lib/amber/functions/math/div.rb +44 -0
- data/lib/amber/functions/math/exp.rb +39 -0
- data/lib/amber/functions/math/floor.rb +39 -0
- data/lib/amber/functions/math/inc.rb +39 -0
- data/lib/amber/functions/math/log.rb +39 -0
- data/lib/amber/functions/math/mod.rb +41 -0
- data/lib/amber/functions/math/mul.rb +43 -0
- data/lib/amber/functions/math/neg.rb +43 -0
- data/lib/amber/functions/math/power.rb +43 -0
- data/lib/amber/functions/math/rand.rb +36 -0
- data/lib/amber/functions/math/round.rb +39 -0
- data/lib/amber/functions/math/shl.rb +41 -0
- data/lib/amber/functions/math/shr.rb +41 -0
- data/lib/amber/functions/math/sin.rb +39 -0
- data/lib/amber/functions/math/sub.rb +43 -0
- data/lib/amber/functions/math/tan.rb +39 -0
- data/lib/amber/functions/math.rb +3 -0
- data/lib/amber/functions/objects/send.rb +22 -0
- data/lib/amber/functions/rails/print.rb +44 -0
- data/lib/amber/functions/rails.rb +3 -0
- data/lib/amber/functions/string/ansi.rb +24 -0
- data/lib/amber/functions/string/capstr.rb +23 -0
- data/lib/amber/functions/string/center.rb +25 -0
- data/lib/amber/functions/string/chr.rb +16 -0
- data/lib/amber/functions/string/ljust.rb +26 -0
- data/lib/amber/functions/string/regmatch.rb +34 -0
- data/lib/amber/functions/string/rjust.rb +26 -0
- data/lib/amber/functions/string.rb +3 -0
- data/lib/amber/functions.rb +103 -0
- data/lib/amber/interpreter.rb +1380 -0
- data/lib/amber/languages/brainfuck.rb +153 -0
- data/lib/amber/languages/ecma/compiler.rb +1661 -0
- data/lib/amber/languages/ecma/core-math.js +67 -0
- data/lib/amber/languages/ecma/core-objects.js +57 -0
- data/lib/amber/languages/ecma.rb +9 -0
- data/lib/amber/languages/ecma_fuku/compiler.rb +1622 -0
- data/lib/amber/languages/ecma_fuku/core-math.js +67 -0
- data/lib/amber/languages/ecma_fuku/core-objects.js +56 -0
- data/lib/amber/languages/ecma_fuku.rb +13 -0
- data/lib/amber/languages/math/compiler.rb +70 -0
- data/lib/amber/languages/math/tokenizer.rb +69 -0
- data/lib/amber/languages/math/tree.rb +110 -0
- data/lib/amber/languages/math.rb +26 -0
- data/lib/amber/languages.rb +99 -0
- data/lib/amber/library.rb +79 -0
- data/lib/amber/optimisation.rb +299 -0
- data/lib/amber/plugin.rb +337 -0
- data/lib/amber/rails.rb +90 -0
- data/lib/amber.rb +106 -0
- data/spec/amber/class_spec.rb +27 -0
- data/spec/amber/enviroment_spec.rb +61 -0
- data/spec/amber/function_spec.rb +25 -0
- data/spec/amber/functions/association/assoc_get_spec.rb +41 -0
- data/spec/amber/functions/association/assoc_set_spec.rb +43 -0
- data/spec/amber/functions/collection/get_spec.rb +12 -0
- data/spec/amber/functions/collection/set_spec.rb +10 -0
- data/spec/amber/functions/collection/size_spec.rb +10 -0
- data/spec/amber/functions/list/split_spec.rb +47 -0
- data/spec/amber/functions/string/ansi_spec.rb +44 -0
- data/spec/amber/functions/string/capstr_spec.rb +42 -0
- data/spec/amber/functions/string/center_spec.rb +49 -0
- data/spec/amber/functions/string/ljust_spec.rb +49 -0
- data/spec/amber/functions/string/regmatch_spec.rb +52 -0
- data/spec/amber/functions/string/rjust_spec.rb +49 -0
- data/spec/amber/interpreter/assignment_spec.rb +22 -0
- data/spec/amber/interpreter/condition_spec.rb +103 -0
- data/spec/amber/interpreter/constant_spec.rb +31 -0
- data/spec/amber/interpreter/core_call_spec.rb +72 -0
- data/spec/amber/interpreter/interpreter_spec.rb +11 -0
- data/spec/amber/interpreter/parameter_spec.rb +24 -0
- data/spec/amber/interpreter/sequence_spec.rb +47 -0
- data/spec/amber/interpreter/variable_spec.rb +24 -0
- data/spec/amber/plugin_spec.rb +10 -0
- data/spec/classes/atom/association_spec.rb +39 -0
- data/spec/classes/atom/block_spec.rb +25 -0
- data/spec/classes/atom/boolean_spec.rb +67 -0
- data/spec/classes/atom/error_spec.rb +43 -0
- data/spec/classes/atom/list_spec.rb +68 -0
- data/spec/classes/atom/number_spec.rb +132 -0
- data/spec/classes/atom/string_spec.rb +175 -0
- data/spec/languages/ecma/ecma_array_spec.rb +79 -0
- data/spec/languages/ecma/ecma_closure_spec.rb +38 -0
- data/spec/languages/ecma/ecma_literals_spec.rb +71 -0
- data/spec/languages/ecma/ecma_objects_spec.rb +165 -0
- data/spec/languages/ecma/ecma_old_spec.rb +540 -0
- data/spec/languages/ecma/ecma_spec.rb +64 -0
- data/spec/languages/ecma_fuku/ecma_array_spec.rb +61 -0
- data/spec/languages/ecma_fuku/ecma_closure_spec.rb +33 -0
- data/spec/languages/ecma_fuku/ecma_function_spec.rb +84 -0
- data/spec/languages/ecma_fuku/ecma_literals_spec.rb +55 -0
- data/spec/languages/ecma_fuku/ecma_objects_spec.rb +133 -0
- data/spec/languages/ecma_fuku/ecma_old_spec.rb +415 -0
- data/spec/languages/ecma_fuku/ecma_operator_spec.rb +33 -0
- data/spec/languages/ecma_fuku/ecma_spec.rb +52 -0
- data/spec/languages/math/compiler_spec.rb +49 -0
- data/spec/languages/math/tokenizer_spec.rb +73 -0
- data/spec/languages/math/tree_spec.rb +153 -0
- metadata +225 -0
data/lib/amber/rails.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License
|
3
|
+
|
4
|
+
Copyright (c) 2008 Heinz N. 'Licenser' Gies
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Ruby Mush"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
require 'amber/acts_as_amber_type.rb'
|
26
|
+
|
27
|
+
# Inlcude the AmberVM::ACtsAsAmberVMType into active record so functions like
|
28
|
+
# +acts_as_amber_type+ and comparable
|
29
|
+
ActiveRecord::Base.class_eval do
|
30
|
+
include AmberVM::ActsAsAmberVMType
|
31
|
+
end
|
32
|
+
|
33
|
+
module ActionController #:nodoc:
|
34
|
+
class Base
|
35
|
+
# This function allows to render code that gets compiled by amber. The first
|
36
|
+
# parameter passed is the code to compile, a string most likely, the second
|
37
|
+
# is a hash that is passed to the render function, see the Rails API doc for
|
38
|
+
# details on what to put there.
|
39
|
+
#
|
40
|
+
# In addition to those rails specifc parameters amber_render accepts 4 additioal
|
41
|
+
# parameters.They are the same as used in +amber_execute+.
|
42
|
+
def amber_reder code, options = {}
|
43
|
+
render options.merge({:text => amber_execute(code, options)})
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# The amber_execute executes a string of code with a certein behavior and
|
48
|
+
# returns not the result but what was written in the special :render variable
|
49
|
+
# of the environment.
|
50
|
+
#
|
51
|
+
# The folowing options can be passed to it to influence it's behaviro.
|
52
|
+
#
|
53
|
+
# :language:: The language that should be used to compile the code, be adwised
|
54
|
+
# that it is nessessary to load this prior to executing it.
|
55
|
+
#
|
56
|
+
# :environment:: This allows to pass a own environment, for example to publish
|
57
|
+
# variables or to maintain state between executions.
|
58
|
+
#
|
59
|
+
# :object:: If this is set the code will be executed in the context of the
|
60
|
+
# object passed, this using it's local variable storage and it's
|
61
|
+
# functions. Be aware that when using :object and :environment
|
62
|
+
# togehter, variables set in the code will NOT go in the environment
|
63
|
+
# as they are local in the object.
|
64
|
+
#
|
65
|
+
# :timeout:: This is used to limit the execution time of the code, it can be
|
66
|
+
# used to prevent runaway processes beeing executed. Very helpfull
|
67
|
+
# when the code executed isn't 100% trunstworty.
|
68
|
+
def amber_execute code, options = {}
|
69
|
+
options = {
|
70
|
+
:language => :ecma,
|
71
|
+
:environment => AmberVM::Interpreter.env
|
72
|
+
}.merge(options)
|
73
|
+
compiler = AmberVM::Languages[options[:language]].new
|
74
|
+
code = compiler.compile(code)
|
75
|
+
code = AmberVM::Interpreter::ObjectContext.new(AmberVM::Interpreter::Constant.new(options[:object]), code) if options[:object]
|
76
|
+
if options[:environment][:render].is_a? AmberVM::Classes::Error or options[:environment][:render].nil?
|
77
|
+
options[:environment][:render] = ""
|
78
|
+
end
|
79
|
+
puts options[:environment][:render].inspect
|
80
|
+
if options[:timeout]
|
81
|
+
s = AmberVM::Safety.new :timeout => options[:timeout]
|
82
|
+
s.execute(code, options[:environment])
|
83
|
+
else
|
84
|
+
code.execute(options[:environment])
|
85
|
+
end
|
86
|
+
options[:environment][:render].val
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
require 'amber/functions/rails.rb'
|
data/lib/amber.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'amber/interpreter'
|
2
|
+
require 'amber/classes'
|
3
|
+
require 'amber/functions'
|
4
|
+
require 'amber/languages'
|
5
|
+
require 'timeout'
|
6
|
+
|
7
|
+
# This is the rVM library. Including it loads the very basic functionalites rVM offers.
|
8
|
+
#
|
9
|
+
# It however does not yet load any functions or languages for security propose.
|
10
|
+
# This will have to be done by hand for the reason that it will force the user to concider
|
11
|
+
# which functions he wants to offer and which not.
|
12
|
+
#
|
13
|
+
# You may very well load your own functions and languages with rVM in the same way you can
|
14
|
+
# load shipped languages.
|
15
|
+
#
|
16
|
+
# It also contains some usefull functions that wil allow to make the usuage easyer.
|
17
|
+
module AmberVM
|
18
|
+
VERSION = "0.0.19" unless defined?(AmberVM::VERSION)
|
19
|
+
|
20
|
+
# This class is designed to allow execting code more safely
|
21
|
+
#
|
22
|
+
# It allows to limit executin in multiple ways to prevent all sorty of runaway code.
|
23
|
+
class Safety
|
24
|
+
|
25
|
+
attr_reader :timeout
|
26
|
+
def initialize options = {}
|
27
|
+
@timeout = options[:timeout]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Sets the timeout for the executin, in seconds (you may use 0.5 and such)
|
31
|
+
#
|
32
|
+
# The execution is aboarded and a exeption thrown if the
|
33
|
+
def timeout=t
|
34
|
+
raise ArgumentError, "The timeout must be a number!" if not t.is_a? Numeric
|
35
|
+
@timeout = t
|
36
|
+
end
|
37
|
+
|
38
|
+
# Executes the code with the former set security measures.
|
39
|
+
#
|
40
|
+
# Exceptions are thrown according to the problem
|
41
|
+
def execute code, env
|
42
|
+
res = nil
|
43
|
+
if @timeout
|
44
|
+
Timeout::timeout(@timeout) do
|
45
|
+
res = code.execute(env)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
res = code.execute(env)
|
49
|
+
end
|
50
|
+
res
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
@@strict = false
|
56
|
+
|
57
|
+
# This getter returns if rVM handles types strict.
|
58
|
+
#
|
59
|
+
# Calling a method with wrong types will raise a error if this is true.
|
60
|
+
# If false rVM will try to typecast accordingly.
|
61
|
+
def strict
|
62
|
+
@@strict
|
63
|
+
end
|
64
|
+
|
65
|
+
# Sets if variables are strictly typed or casted for function calls.
|
66
|
+
# This allows to make a language either more secure or more flexible.
|
67
|
+
def strict= v
|
68
|
+
@@strict = v
|
69
|
+
end
|
70
|
+
|
71
|
+
# Compils a code the given language.
|
72
|
+
# A new compiler is created for that and discarded afterwards.
|
73
|
+
#
|
74
|
+
# Using +compiler_for+ and calling compile for it yourself
|
75
|
+
# is more performant if you plan on doing thise more then once.
|
76
|
+
def compile language, code
|
77
|
+
compiler_for(language).compile(code)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Creates you a compiler object for the given language.
|
81
|
+
# If no language is given a error is raised.
|
82
|
+
def compiler_for language
|
83
|
+
if (l = AmberVM::Languages[language])
|
84
|
+
l.new
|
85
|
+
else
|
86
|
+
raise "AmberVM Error: Unknown Language #{language}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# A utility function to give debug output based on weather $DEBUG is set or not
|
91
|
+
def debug text
|
92
|
+
if $HTML_DEBUG
|
93
|
+
puts "<p><pre>" + (">>> #{text}".gsub("<","<").gsub('>','>').gsub("\n","<br/>\n"))+ "</pre></p>"
|
94
|
+
else
|
95
|
+
puts ">>> #{text}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
class String
|
103
|
+
def to_html
|
104
|
+
self.gsub('<','<').gsub('>', '>').gsub("\n", '<br/>').gsub(' ', ' ')
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'amber/classes'
|
2
|
+
require 'spec/amber/helpers/plugin_helper'
|
3
|
+
require File.dirname(__FILE__) + '/helpers/class_helper'
|
4
|
+
|
5
|
+
describe AmberVM::Classes do
|
6
|
+
it_should_behave_like "a plugin host"
|
7
|
+
end
|
8
|
+
|
9
|
+
describe AmberVM::Classes::Class do
|
10
|
+
describe "(plugin)" do
|
11
|
+
before do
|
12
|
+
@object = AmberVM::Classes::Class
|
13
|
+
end
|
14
|
+
it_should_behave_like "a plugin"
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "(class)" do
|
19
|
+
before do
|
20
|
+
@object = AmberVM::Classes::Class.new
|
21
|
+
end
|
22
|
+
|
23
|
+
it_should_behave_like "a class"
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'amber/interpreter'
|
2
|
+
|
3
|
+
shared_examples_for "a environment" do
|
4
|
+
it "should get the locals" do
|
5
|
+
@env['1'].val.should == 'one'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return nil for unset locals" do
|
9
|
+
@env['2'].should == nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should allow to change locals" do
|
13
|
+
@env['2'] = 'two'
|
14
|
+
@env['2'].val.should == 'two'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe AmberVM::Interpreter::Environment do
|
19
|
+
before :each do
|
20
|
+
@env = AmberVM::Interpreter::Environment.new({:params => [1,2], :locals => {'1' => 'one'}})
|
21
|
+
end
|
22
|
+
|
23
|
+
it_should_behave_like "a environment"
|
24
|
+
|
25
|
+
it "should autocast locals to variable storages on creation" do
|
26
|
+
env = AmberVM::Interpreter::Environment.new({:params => [1,2], :locals => {'1' => 'one'}})
|
27
|
+
env['1'].should be_kind_of(AmberVM::Interpreter::VariableStorage)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should get the params" do
|
31
|
+
@env.param(0).should be(1)
|
32
|
+
@env.param(1).should be(2)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have a path" do
|
36
|
+
@env.path.should be_kind_of(Array)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
describe "(parented)" do
|
41
|
+
before do
|
42
|
+
@old_env = @env
|
43
|
+
@env = AmberVM::Interpreter::Environment.new({}, @old_env)
|
44
|
+
end
|
45
|
+
|
46
|
+
it_should_behave_like "a environment"
|
47
|
+
|
48
|
+
=begin
|
49
|
+
it "should have the same path as it's parrent" do
|
50
|
+
@env.path.should be(@old_env.path)
|
51
|
+
end
|
52
|
+
=end
|
53
|
+
it "should apply changes to already set variables to the parent" do
|
54
|
+
@env['1'].val.should == 'one'
|
55
|
+
@env['1'] = 'ONE!'
|
56
|
+
@env['1'].val.should == 'ONE!'
|
57
|
+
@old_env['1'].val.should == 'ONE!'
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'amber/functions'
|
2
|
+
require 'spec/amber/helpers/plugin_helper'
|
3
|
+
require File.dirname(__FILE__) + '/helpers/function_helper'
|
4
|
+
|
5
|
+
describe AmberVM::Functions do
|
6
|
+
it_should_behave_like "a plugin host"
|
7
|
+
end
|
8
|
+
|
9
|
+
describe AmberVM::Functions::Function do
|
10
|
+
describe "(plugin)" do
|
11
|
+
before do
|
12
|
+
@object = AmberVM::Functions::Function
|
13
|
+
end
|
14
|
+
it_should_behave_like "a plugin"
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "(function)" do
|
19
|
+
before do
|
20
|
+
@object = AmberVM::Functions::Function
|
21
|
+
end
|
22
|
+
it_should_behave_like "a function"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'amber/functions'
|
2
|
+
require 'amber/functions/association/assoc_get'
|
3
|
+
|
4
|
+
describe AmberVM::Functions::AssocGet do
|
5
|
+
before(:each) do
|
6
|
+
@env = mock('env mock')
|
7
|
+
@function = AmberVM::Functions[:assoc_get]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should accept a usual function call with 2 parameters and get the value" do
|
11
|
+
params = [
|
12
|
+
p1 = mock('param 1 mock'),
|
13
|
+
p2 = mock('param 2 mock')
|
14
|
+
]
|
15
|
+
res = p1.should_receive(:[]).with(p2).and_return('result')
|
16
|
+
@function.execute(params, @env).should == 'result'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should accept a object call wiht 1 parameter and a object in env and get the value" do
|
20
|
+
params = [
|
21
|
+
p1 = mock('param 1 mock')
|
22
|
+
]
|
23
|
+
obj = mock('association mock')
|
24
|
+
@env.should_receive(:read_var_val).once.with(:self).and_return(obj)
|
25
|
+
obj.should_receive(:is_a?).once.with(AmberVM::Classes::Association).and_return(true)
|
26
|
+
res = obj.should_receive(:[]).with(p1).and_return('result')
|
27
|
+
@function.execute(params, @env).should == 'result'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return an error with more then 2 arguments" do
|
31
|
+
params = [1,2,3]
|
32
|
+
@function.execute(params, @env).should be_an_instance_of(AmberVM::Classes::Error)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
it "should return an error with no arguments" do
|
37
|
+
params = []
|
38
|
+
@function.execute(params, @env).should be_an_instance_of(AmberVM::Classes::Error)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'amber/functions'
|
2
|
+
require 'amber/functions/association/assoc_set'
|
3
|
+
|
4
|
+
describe AmberVM::Functions::AssocSet do
|
5
|
+
before(:each) do
|
6
|
+
@env = mock('env mock')
|
7
|
+
@function = AmberVM::Functions[:assoc_set]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should accept a usual function call with 3 parameters and return the value it sets" do
|
11
|
+
params = [
|
12
|
+
p1 = mock('param 1 mock'),
|
13
|
+
p2 = mock('param 2 mock'),
|
14
|
+
p3 = mock('param 3 mock')
|
15
|
+
]
|
16
|
+
p1.should_receive(:[]=).with(p2, p3).and_return(p3)
|
17
|
+
@function.execute(params, @env).should == p3
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should accept a object call wiht 2 parameter and a object in env and return the value it sets" do
|
21
|
+
params = [
|
22
|
+
p1 = mock('param 1 mock'),
|
23
|
+
p2 = mock('param 2 mock')
|
24
|
+
]
|
25
|
+
obj = mock('association mock')
|
26
|
+
@env.should_receive(:read_var_val).once.with(:self).and_return(obj)
|
27
|
+
obj.should_receive(:is_a?).once.with(AmberVM::Classes::Association).and_return(true)
|
28
|
+
res = obj.should_receive(:[]=).with(p1,p2).and_return(p2)
|
29
|
+
@function.execute(params, @env).should == p2
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return an error with more then 4 arguments" do
|
33
|
+
params = [1, 2, 3, 4]
|
34
|
+
@function.execute(params, @env).should be_an_instance_of(AmberVM::Classes::Error)
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
it "should return an error with less then 2 arguments" do
|
39
|
+
params = [1]
|
40
|
+
@function.execute(params, @env).should be_an_instance_of(AmberVM::Classes::Error)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'amber/functions'
|
2
|
+
require 'amber/functions/association/assoc_get'
|
3
|
+
require 'amber/functions/array/at'
|
4
|
+
require 'amber/functions/collection/get'
|
5
|
+
|
6
|
+
describe AmberVM::Functions::Get do
|
7
|
+
before(:each) do
|
8
|
+
@env = mock('env mock')
|
9
|
+
@function = AmberVM::Functions[:get]
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec/amber/helpers/plugin_helper'
|
2
|
+
require 'spec/amber/helpers/function_helper'
|
3
|
+
require 'amber/functions/list/split'
|
4
|
+
|
5
|
+
describe AmberVM::Functions::Split do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@object = AmberVM::Functions::Split
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "(plugin)" do
|
12
|
+
it_should_behave_like "a plugin"
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "(function)" do
|
16
|
+
it_should_behave_like "a function"
|
17
|
+
|
18
|
+
it "should have a signature of string, string" do
|
19
|
+
@object.signature.should == [AmberVM::Classes::String, AmberVM::Classes::String]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have a type of :list" do
|
23
|
+
@object.data_type.should == :list
|
24
|
+
end
|
25
|
+
end
|
26
|
+
describe "(functionality)" do
|
27
|
+
before do
|
28
|
+
@env = mock('env mock')
|
29
|
+
@old_db = $DB
|
30
|
+
$DB = mock('db mock')
|
31
|
+
end
|
32
|
+
|
33
|
+
after do
|
34
|
+
$DB = @old_db
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have tests implemented" do
|
38
|
+
pending "This needs to be implemented, locate functions neeeeeeeds specs."
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not work with more then 3 and less the 2 params" do
|
42
|
+
@object.execute([1], nil).should be_instance_of(AmberVM::Classes::Error)
|
43
|
+
@object.execute([1,2,3,1], nil).should be_instance_of(AmberVM::Classes::Error)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec/amber/helpers/plugin_helper'
|
2
|
+
require 'spec/amber/helpers/function_helper'
|
3
|
+
require 'amber/functions/string/ansi'
|
4
|
+
|
5
|
+
|
6
|
+
describe AmberVM::Functions::Ansi do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@object = AmberVM::Functions::Ansi
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "(plugin)" do
|
13
|
+
it_should_behave_like "a plugin"
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "(function)" do
|
18
|
+
it_should_behave_like "a function"
|
19
|
+
|
20
|
+
it "should have a signature of string, string" do
|
21
|
+
@object.signature.should == [AmberVM::Classes::String, AmberVM::Classes::String]
|
22
|
+
end
|
23
|
+
it "should have a type of boolean" do
|
24
|
+
@object.data_type.should == :string
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "(functionality)" do
|
29
|
+
|
30
|
+
it "should not work with less then 2 params" do
|
31
|
+
@object.execute([], nil).should be_instance_of(AmberVM::Classes::Error)
|
32
|
+
@object.execute([1], nil).should be_instance_of(AmberVM::Classes::Error)
|
33
|
+
@object.execute([1,2,3], nil).should be_instance_of(AmberVM::Classes::Error)
|
34
|
+
@object.execute([1,2,3,4], nil).should be_instance_of(AmberVM::Classes::Error)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should call the ansi function for the seclond with the first argument as parameter" do
|
38
|
+
arg1 = mock('arg mock')
|
39
|
+
arg1.should_receive(:ansi).once.with(1).and_return 'one'
|
40
|
+
@object.execute([1,arg1], nil).should == 'one'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec/amber/helpers/plugin_helper'
|
2
|
+
require 'spec/amber/helpers/function_helper'
|
3
|
+
require 'amber/functions/string/capstr'
|
4
|
+
|
5
|
+
|
6
|
+
describe AmberVM::Functions::Capstr do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@object = AmberVM::Functions::Capstr
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
describe "(plugin)" do
|
14
|
+
it_should_behave_like "a plugin"
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "(function)" do
|
19
|
+
it_should_behave_like "a function"
|
20
|
+
|
21
|
+
it "should have a signature of string" do
|
22
|
+
@object.signature.should == [AmberVM::Classes::String]
|
23
|
+
end
|
24
|
+
it "should have a type of boolean" do
|
25
|
+
@object.data_type.should == :string
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "(functionality)" do
|
30
|
+
|
31
|
+
it "should not work with more or less then 1 param" do
|
32
|
+
@object.execute([], nil).should be_instance_of(AmberVM::Classes::Error)
|
33
|
+
@object.execute([1,2], nil).should be_instance_of(AmberVM::Classes::Error)
|
34
|
+
@object.execute([1,2,3], nil).should be_instance_of(AmberVM::Classes::Error)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should capitalize the first letter" do
|
38
|
+
@object.execute(["abc"], nil).should == 'Abc'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec/amber/helpers/plugin_helper'
|
2
|
+
require 'spec/amber/helpers/function_helper'
|
3
|
+
require 'amber/functions/string/center'
|
4
|
+
|
5
|
+
|
6
|
+
describe AmberVM::Functions::Center do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@object = AmberVM::Functions::Center
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "(plugin)" do
|
13
|
+
it_should_behave_like "a plugin"
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "(function)" do
|
18
|
+
it_should_behave_like "a function"
|
19
|
+
|
20
|
+
it "should have a signature of string, string" do
|
21
|
+
@object.signature.should == [AmberVM::Classes::String, AmberVM::Classes::Number, AmberVM::Classes::String]
|
22
|
+
end
|
23
|
+
it "should have a type of boolean" do
|
24
|
+
@object.data_type.should == :string
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "(functionality)" do
|
29
|
+
|
30
|
+
it "should not work with less then 2 params or more then 3" do
|
31
|
+
@object.execute([], nil).should be_instance_of(AmberVM::Classes::Error)
|
32
|
+
@object.execute([1], nil).should be_instance_of(AmberVM::Classes::Error)
|
33
|
+
@object.execute([1,2,3,4], nil).should be_instance_of(AmberVM::Classes::Error)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should call the center function with one param if it is given" do
|
37
|
+
arg1 = mock('arg mock')
|
38
|
+
arg1.should_receive(:center).once.with(1).and_return 'center'
|
39
|
+
@object.execute([arg1,1], nil).should == 'center'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should call the center function with one two param if they are given" do
|
43
|
+
arg1 = mock('arg mock')
|
44
|
+
arg1.should_receive(:center).once.with(1,2).and_return 'center2'
|
45
|
+
@object.execute([arg1,1,2], nil).should == 'center2'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec/amber/helpers/plugin_helper'
|
2
|
+
require 'spec/amber/helpers/function_helper'
|
3
|
+
require 'amber/functions/string/ljust'
|
4
|
+
|
5
|
+
|
6
|
+
describe AmberVM::Functions::Ljust do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@object = AmberVM::Functions::Ljust
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "(plugin)" do
|
13
|
+
it_should_behave_like "a plugin"
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "(function)" do
|
18
|
+
it_should_behave_like "a function"
|
19
|
+
|
20
|
+
it "should have a signature of string, string" do
|
21
|
+
@object.signature.should == [AmberVM::Classes::String, AmberVM::Classes::Number, AmberVM::Classes::String]
|
22
|
+
end
|
23
|
+
it "should have a type of boolean" do
|
24
|
+
@object.data_type.should == :string
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "(functionality)" do
|
29
|
+
|
30
|
+
it "should not work with less then 2 params or more then 3" do
|
31
|
+
@object.execute([], nil).should be_instance_of(AmberVM::Classes::Error)
|
32
|
+
@object.execute([1], nil).should be_instance_of(AmberVM::Classes::Error)
|
33
|
+
@object.execute([1,2,3,4], nil).should be_instance_of(AmberVM::Classes::Error)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should call the ljust function with one param if it is given" do
|
37
|
+
arg1 = mock('arg mock')
|
38
|
+
arg1.should_receive(:ljust).once.with(1).and_return 'ljust'
|
39
|
+
@object.execute([arg1,1], nil).should == 'ljust'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should call the ljust function with one two param if they are given" do
|
43
|
+
arg1 = mock('arg mock')
|
44
|
+
arg1.should_receive(:ljust).once.with(1,2).and_return 'ljust'
|
45
|
+
@object.execute([arg1,1,2], nil).should == 'ljust'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|