lib/atp/ast/builder.rb in atp-0.7.0 vs lib/atp/ast/builder.rb in atp-0.8.0

- old
+ new

@@ -12,10 +12,28 @@ def flow(str) n(:flow, name(str)) end + # Ensures the given flow ast has a volatile node, then adds the + # given flags to it + def add_volatile_flags(flow, flags) + name, *nodes = *flow + if nodes[0] && nodes[0].type == :volatile + v = nodes.shift + else + v = n0(:volatile) + end + existing = v.children.map { |f| f.type == :flag ? f.value : nil }.compact + new = [] + flags.each do |flag| + new << n(:flag, flag) unless existing.include?(flag) + end + v = v.updated(nil, v.children + new) + flow.updated(nil, [name, v] + nodes) + end + def name(str) n(:name, str.to_s) end def log(str, options = {}) @@ -218,19 +236,100 @@ end children << number(n) if n children << id(options[:id].to_s.downcase.to_sym) if options[:id] + if levels = options[:level] || options[:levels] + levels = [levels] unless levels.is_a?(Array) + levels.each do |l| + children << level(l[:name], l[:value], l[:unit] || l[:units]) + end + end + + if lims = options[:limit] || options[:limits] + lims = [lims] unless lims.is_a?(Array) + lims.each do |l| + children << limit(l[:value], l[:rule], l[:unit] || l[:units]) + end + end + + if pins = options[:pin] || options[:pins] + pins = [pins] unless pins.is_a?(Array) + pins.each do |p| + if p.is_a?(Hash) + children << pin(p[:name]) + else + children << pin(p) + end + end + end + + if pats = options[:pattern] || options[:patterns] + pats = [pats] unless pats.is_a?(Array) + pats.each do |p| + if p.is_a?(Hash) + children << pattern(p[:name], p[:path]) + else + children << pattern(p) + end + end + end + + if options[:meta] + attrs = [] + options[:meta].each { |k, v| attrs << attribute(k, v) } + children << n(:meta, *attrs) + end + + if subs = options[:sub_test] || options[:sub_tests] + subs = [subs] unless subs.is_a?(Array) + subs.each do |s| + children << s.updated(:sub_test, nil) + end + end + children << on_fail(options[:on_fail]) if options[:on_fail] children << on_pass(options[:on_pass]) if options[:on_pass] test = n(:test, *children) if options[:conditions] apply_conditions(test, options[:conditions]) else test end + end + + def pattern(name, path = nil) + if path + n(:pattern, name, path) + else + n(:pattern, name) + end + end + + def attribute(name, value) + n(:attribute, name, value) + end + + def level(name, value, units = nil) + if units + n(:level, name, value, units) + else + n(:level, name, value) + end + end + + def limit(value, rule, units = nil) + if units + n(:limit, value, rule, units) + else + n(:limit, value, rule) + end + end + + def pin(name) + n(:pin, name) end def on_fail(options = {}) children = [] if options[:bin] || options[:softbin]