Skip to content

Commit

Permalink
add extract spec and additional check
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmelt committed Apr 25, 2017
1 parent 023ab28 commit d34e99d
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/cosmos/script/extract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ def add_cmd_parameter(keyword, value, cmd_params)

def extract_fields_from_cmd_text(text)
split_string = text.split(/\s+with\s+/i, 2)
raise "ERROR: 'with' must be followed by parameters : #{text}" if split_string.length == 1 and text =~ /\s*with\s*/i
raise "ERROR: text must not be empty" if split_string.length == 0
raise "ERROR: 'with' must be followed by parameters : #{text}" if (split_string.length == 1 and text =~ /\s*with\s*/i) or (split_string.length == 2 and split_string[1].empty?)

# Extract target_name and cmd_name
first_half = split_string[0].split
raise "ERROR: Both Target Name and Command Name must be given : #{text}" if first_half.length < 2
raise "ERROR: Only Target Name and Command Name must be given before 'with' : #{text}" if first_half.length > 2
target_name = first_half[0]
cmd_name = first_half[1]
cmd_params = {}
cmd_name = first_half[1]
cmd_params = {}

if split_string.length == 2
# Extract Command Parameters
second_half = split_string[1].scan(SCANNING_REGULAR_EXPRESSION)
keyword = nil
value = nil
comma = nil
keyword = nil
value = nil
comma = nil
second_half.each do |item|
unless keyword
keyword = item
Expand All @@ -61,8 +62,8 @@ def extract_fields_from_cmd_text(text)
end
add_cmd_parameter(keyword, value, cmd_params)
keyword = nil
value = nil
comma = nil
value = nil
comma = nil
end
if keyword
if value
Expand Down
144 changes: 144 additions & 0 deletions spec/script/extract_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# encoding: ascii-8bit

# Copyright 2014 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt

require 'spec_helper'
require 'cosmos/script'
require 'tempfile'

module Cosmos

describe Extract do

describe "add_cmd_parameter" do
it "should remove quotes and preserve quoted strings" do
cmd_params = {}
add_cmd_parameter('TEST', '"3"', cmd_params)
expect(cmd_params['TEST']).to eql('3')
end

it "should convert unquoted strings to the correct value type" do
cmd_params = {}
add_cmd_parameter('TEST', '3', cmd_params)
expect(cmd_params['TEST']).to eql(3)
add_cmd_parameter('TEST2', '3.0', cmd_params)
expect(cmd_params['TEST2']).to eql(3.0)
add_cmd_parameter('TEST3', '0xA', cmd_params)
expect(cmd_params['TEST3']).to eql(0xA)
add_cmd_parameter('TEST4', '3e3', cmd_params)
expect(cmd_params['TEST4']).to eql(3e3)
add_cmd_parameter('TEST5', 'Ryan', cmd_params)
expect(cmd_params['TEST5']).to eql('Ryan')
add_cmd_parameter('TEST6', '3 4', cmd_params)
expect(cmd_params['TEST6']).to eql('3 4')
end
end

describe "extract_fields_from_cmd_text" do
it "should complain about empty strings" do
expect { extract_fields_from_cmd_text("") }.to raise_error(/text must not be empty/)
end

it "should complain about strings that end in with but have no other text" do
expect { extract_fields_from_cmd_text("TEST COMMAND with") }.to raise_error(/must be followed by parameters/)
expect { extract_fields_from_cmd_text("TEST COMMAND with ") }.to raise_error(/must be followed by parameters/)
end

it "should complain if target name or packet name are missing" do
expect { extract_fields_from_cmd_text("TEST") }.to raise_error(/Both Target Name and Command Name must be given/)
end

it "should complain if there are too many words before with" do
expect { extract_fields_from_cmd_text("TEST TEST TEST") }.to raise_error(/Only Target Name and Command Name must be given/)
end

it "should complain if any key value pairs are misformed" do
expect { extract_fields_from_cmd_text("TEST TEST with KEY VALUE, KEY VALUE, VALUE") }.to raise_error(/Missing value for last command parameter/)
expect { extract_fields_from_cmd_text("TEST TEST with KEY VALUE KEY VALUE") }.to raise_error(/Missing comma in command parameters/)
expect { extract_fields_from_cmd_text("TEST TEST with KEY VALUE KEY, KEY VALUE") }.to raise_error(/Missing comma in command parameters/)
expect { extract_fields_from_cmd_text("TEST TEST with KEY VALUE, KEY") }.to raise_error(/Missing value for last command parameter/)
end

it "should parse commands correctly" do
expect(extract_fields_from_cmd_text("TARGET PACKET with KEY1 VALUE1, KEY2 2, KEY3 '3', KEY4 4.0")).to eql(
['TARGET', 'PACKET', {'KEY1' => 'VALUE1', 'KEY2' => 2, 'KEY3' => '3', 'KEY4' => 4.0}])
end

it "should handle multiple array parameters" do
expect(extract_fields_from_cmd_text("TARGET PACKET with KEY1 [1,2,3,4], KEY2 2, KEY3 '3', KEY4 [5, 6, 7, 8]")).to eql(
['TARGET', 'PACKET', {'KEY1' => [1,2,3,4], 'KEY2' => 2, 'KEY3' => '3', 'KEY4' => [5,6,7,8]}])
expect(extract_fields_from_cmd_text("TARGET PACKET with KEY1 [1,2,3,4], KEY2 2, KEY3 '3', KEY4 ['1', '2', '3', '4']")).to eql(
['TARGET', 'PACKET', {'KEY1' => [1,2,3,4], 'KEY2' => 2, 'KEY3' => '3', 'KEY4' => ['1', '2', '3', '4']}])
end
end

describe "extract_fields_from_tlm_text" do
it "should require exactly TARGET_NAME PACKET_NAME ITEM_NAME" do
expect { extract_fields_from_tlm_text("") }.to raise_error(/Telemetry Item must be specified as/)
expect { extract_fields_from_tlm_text("TARGET") }.to raise_error(/Telemetry Item must be specified as/)
expect { extract_fields_from_tlm_text("TARGET PACKET") }.to raise_error(/Telemetry Item must be specified as/)
expect { extract_fields_from_tlm_text("TARGET PACKET ") }.to raise_error(/Telemetry Item must be specified as/)
expect { extract_fields_from_tlm_text("TARGET PACKET ITEM OTHER") }.to raise_error(/Telemetry Item must be specified as/)
end

it "should parse telemetry names correctly" do
expect(extract_fields_from_tlm_text("TARGET PACKET ITEM")).to eql(['TARGET', 'PACKET', 'ITEM'])
expect(extract_fields_from_tlm_text(" TARGET PACKET ITEM ")).to eql(['TARGET', 'PACKET', 'ITEM'])
end
end

describe "extract_fields_from_set_tlm_text" do
it "should complain if formatted incorrectly" do
expect { extract_fields_from_set_tlm_text("") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET PACKET") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET PACKET ITEM") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET PACKET ITEM=") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET PACKET ITEM= ") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET PACKET ITEM =") }.to raise_error(/Set Telemetry Item must be specified as/)
expect { extract_fields_from_set_tlm_text("TARGET PACKET ITEM = ") }.to raise_error(/Set Telemetry Item must be specified as/)
end

it "should parse set_tlm text correctly" do
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM= 5")).to eql(['TARGET', 'PACKET', 'ITEM', 5])
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM = 5")).to eql(['TARGET', 'PACKET', 'ITEM', 5])
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM =5")).to eql(['TARGET', 'PACKET', 'ITEM', 5])
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM=5")).to eql(['TARGET', 'PACKET', 'ITEM', 5])
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM = 5.0")).to eql(['TARGET', 'PACKET', 'ITEM', 5.0])
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM = Ryan")).to eql(['TARGET', 'PACKET', 'ITEM', 'Ryan'])
expect(extract_fields_from_set_tlm_text("TARGET PACKET ITEM = [1,2,3]")).to eql(['TARGET', 'PACKET', 'ITEM', [1,2,3]])
end
end

describe "extract_fields_from_check_text" do
it "should complain if formatted incorrectly" do
expect { extract_fields_from_check_text("") }.to raise_error(/Check improperly specified/)
expect { extract_fields_from_check_text("TARGET") }.to raise_error(/Check improperly specified/)
expect { extract_fields_from_check_text("TARGET PACKET") }.to raise_error(/Check improperly specified/)
end

it "should support no comparison" do
expect(extract_fields_from_check_text("TARGET PACKET ITEM")).to eql(['TARGET', 'PACKET', 'ITEM', nil])
expect(extract_fields_from_check_text("TARGET PACKET ITEM ")).to eql(['TARGET', 'PACKET', 'ITEM', nil])
end

it "should support comparisons" do
expect(extract_fields_from_check_text("TARGET PACKET ITEM == 5")).to eql(['TARGET', 'PACKET', 'ITEM', '== 5'])
expect(extract_fields_from_check_text("TARGET PACKET ITEM > 5")).to eql(['TARGET', 'PACKET', 'ITEM', '> 5'])
expect(extract_fields_from_check_text("TARGET PACKET ITEM < 5")).to eql(['TARGET', 'PACKET', 'ITEM', '< 5'])
end

it "should complain about trying to do an = comparison" do
expect { extract_fields_from_check_text("TARGET PACKET ITEM = 5") }.to raise_error(/ERROR: Use/)
end
end

end
end

0 comments on commit d34e99d

Please sign in to comment.