TODO: Re-work dep checking with new arg format

pull/677/head
Rick Carlino 2018-02-22 16:33:17 -06:00
parent cfaef89bef
commit 97ebc01864
11 changed files with 42 additions and 34 deletions

View File

@ -8,4 +8,4 @@ module CeleryScript
@parent, @value, @kind = parent, value, kind
end
end
end
end

View File

@ -10,14 +10,14 @@ module CeleryScript
def initialize(parent = nil, args:, body: nil, comment: "", kind:)
@comment, @kind, @parent = comment, kind, parent
@args = HashWithIndifferentAccess.new(args.map do |key, value|
@args = args.map do |key, value|
[key, maybe_initialize(self, value, key)]
end.to_h) if args
end.to_h if args
@body = HashWithIndifferentAccess.new(body.map do |e|
@body = body.map do |e|
raise TypeCheckError, BODY_HAS_NON_NODES unless is_node?(e)
maybe_initialize(self, e)
end) if body
end if body
end
def maybe_initialize(parent, leaf_or_node, key = "__NEVER__")

View File

@ -4,7 +4,7 @@ module CeleryScript
class Corpus
BAD_NODE_NAME = "Can't find validation rules for node "
NO_ARG_SPEC = "CANT FIND ARG SPEC"
NO_NODE_SPEC =
NO_NODE_SPEC = "NO_NODE_SPEC"
def initialize
@arg_def_list = HashWithIndifferentAccess.new

View File

@ -51,6 +51,7 @@ module CeleryScriptSettingsBag
BAD_AXIS = '"%s" is not a valid axis. Allowed values: %s'
BAD_POINTER_ID = "Bad point ID: %s"
BAD_PIN_ID = "Can't find %s with id of %s"
NO_PIN_ID = "You must select a %s before using it."
BAD_POINTER_TYPE = '"%s" is not a type of point. Allowed values: %s'
BAD_PIN_TYPE = '"%s" is not a type of pin. Allowed values: %s'
BAD_SPEED = "Speed must be a percentage between 1-100"
@ -160,6 +161,7 @@ module CeleryScriptSettingsBag
.node(:named_pin, [:pin_type, :pin_id]) do |node|
klass = PIN_TYPE_MAP[node.args[:pin_type].value] or raise "NEVER"
id = node.args[:pin_id].value
node.invalidate!(NO_PIN_ID % [klass]) if (id == 0)
bad_node = !klass.exists?(id)
node.invalidate!(BAD_PIN_ID % [klass, id]) if bad_node
end

View File

@ -8,13 +8,8 @@ class EdgeNode < ApplicationRecord
validates_presence_of :sequence
belongs_to :primary_node
serialize :value, JSON
# BAD_KIND = "must be a valid CeleryScript node name"
# validates :kind, inclusion: { in: CeleryScriptSettingsBag::ANY_NODE_NAME,
# message: BAD_KIND,
# allow_nil: false }
def broadcast?
false
end
end
end

View File

@ -6,7 +6,7 @@ class PrimaryNode < ApplicationRecord
belongs_to :sequence
validates_presence_of :sequence
has_many :edge_nodes
BAD_KIND = "must be a valid CeleryScript argument name"
BAD_KIND = "`kind` must be one of: " + CeleryScriptSettingsBag::ANY_NODE_NAME.join(", ")
validates :kind, inclusion: { in: CeleryScriptSettingsBag::ANY_NODE_NAME,
message: BAD_KIND,
allow_nil: false }
@ -36,4 +36,4 @@ class PrimaryNode < ApplicationRecord
def broadcast?
false
end
end
end

View File

@ -14,9 +14,10 @@ module Peripherals
private
def sequences_using_it
@sequences_using_it ||= EdgeNode
.where(kind: "peripheral_id", value: peripheral.id)
.pluck(:sequence_id)
raise "FIXME"
# @sequences_using_it ||= EdgeNode
# .where(kind: "peripheral_id", value: peripheral.id)
# .pluck(:sequence_id)
end
def not_in_use?

View File

@ -32,9 +32,17 @@ describe Api::PeripheralsController do
FactoryBot.create(:sequence, device: user.device,
body: [
{
kind: "read_peripheral",
kind: "read_pin",
args: {
peripheral_id: peripheral.id
pin_number: {
kind: "named_pin",
args: {
pin_type: "Peripheral",
pin_id: peripheral.id
}
},
mode: 0,
label: "FOO"
}
}
])

View File

@ -19,8 +19,6 @@ describe CeleryScript::AstNode do
end
it "invalidates a node" do
expect { node.invalidate!("Boo!")}.to raise_error("Boo!")
expect { node.invalidate!("Boo!") }.to raise_error("Boo!")
end
end
end

View File

@ -84,12 +84,25 @@ describe CeleryScript::Checker do
it "validates peripheral presence" do
hash[:body] = [
{ kind: "read_peripheral", args: { peripheral_id: 0, pin_mode: 0 } }
{
kind: "read_pin",
args: {
pin_number: {
kind: "named_pin",
args: {
pin_type: "Peripheral",
pin_id: 0
}
},
pin_mode: 0,
label: "FOO"
}
}
]
chk = CeleryScript::Checker.new(tree, corpus)
expect(chk.valid?).to be false
expect(chk.error.message)
.to eq("You must select a peripheral before writing to it.")
.to eq("You must select a Peripheral before using it.")
end
it "Catches bad `pin_type`s in `read_pin`" do

View File

@ -145,13 +145,4 @@ describe CeleryScript::Corpus do
checker = CeleryScript::Checker.new(tree, CeleryScriptSettingsBag::Corpus)
expect(checker.error.message).to include("not a valid channel_name")
end
it "Handles peripheral_id validations" do
tree = CeleryScript::AstNode.new({
"kind": "read_peripheral",
"args": { "peripheral_id": -1, "pin_mode": 0 }
})
checker = CeleryScript::Checker.new(tree, CeleryScriptSettingsBag::Corpus)
expect(checker.error.message).to include("Peripheral #-1 does not exist.")
end
end