Skip to content

Commit

Permalink
Add Feature test for matching on multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Burrows committed Jun 30, 2010
1 parent 6f3378c commit 66ccb91
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
17 changes: 17 additions & 0 deletions features/match_fields.feature
Expand Up @@ -16,5 +16,22 @@ Feature: jse matches specific fields within the json
"""

Scenario: Exact match on multiple fields
Given I have a log file containing:
"""
{"level":"INFO","message":"line one"}
{"level":"DEBUG","message":"line two"}
{"level":"ERROR","message":"line three"}
{"level":"INFO","message":"line four"}
"""
When I run "jse level:INFO message:'line one'" on my log file
Then I should see:
"""
{"level":"INFO","message":"line one"}
"""
But I should not see:
"""
{"level":"INFO","message":"line four"}
"""

Scenario: Regexp match on one field
Scenario: Regexp match on multiple fields
42 changes: 41 additions & 1 deletion features/step_definitions/match_fields_steps.rb
Expand Up @@ -7,7 +7,8 @@
end

When /^I run "jse([^\"]*)" on my log file$/ do |opts|
JSE::CLI.execute(output, opts.split(' ') + [LOG_FILE_NAME])
options = parse_options(opts)
JSE::CLI.execute(output, options + [LOG_FILE_NAME])
end

Then /^I should see:$/ do |text|
Expand All @@ -16,6 +17,12 @@
end
end

Then /^I should not see:$/ do |text|
text.split("\n").each do |line|
output.should_not include(line)
end
end

class Output
def messages
@messages ||= []
Expand All @@ -37,3 +44,36 @@ def executable_path
def output
@output ||= Output.new
end

# The shell automagically does this part
# Leave out some stuff like escaping quotes
# Just don't nest string or quotes in tests!
def parse_options(opt_string)
options = []
word = ""
in_string = false
string_delimiter = ""

opt_string.each_char do |c|
if !in_string && (c == '"' || c == '\'')
in_string = true
string_delimiter = c
elsif in_string && c == string_delimiter
in_string = false
elsif c =~ /\s/ && !in_string
options << word unless word.chomp == ''
word = ""
else
word << c
end
end

# Add the last argument
if in_string
raise "Unterminated string!"
else
options << word
end

options
end
1 change: 1 addition & 0 deletions spec/spec.opts
@@ -0,0 +1 @@
--color

0 comments on commit 66ccb91

Please sign in to comment.